Spaces:
Running
on
Zero
Running
on
Zero
Yurii Paniv
commited on
Commit
·
6a2e815
1
Parent(s):
2375bc3
Initial commit
Browse files- .gitignore +2 -0
- app.py +43 -0
- requirements.txt +5 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
env
|
| 2 |
+
__pycache__
|
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
from peft import PeftModel, PeftConfig
|
| 4 |
+
from transformers import MistralForCausalLM, TextIteratorStreamer, AutoTokenizer, BitsAndBytesConfig
|
| 5 |
+
from time import sleep
|
| 6 |
+
from threading import Thread
|
| 7 |
+
from torch import float16
|
| 8 |
+
|
| 9 |
+
config = PeftConfig.from_pretrained("lang-uk/dragoman")
|
| 10 |
+
quant_config = BitsAndBytesConfig(
|
| 11 |
+
load_in_4bit=True,
|
| 12 |
+
bnb_4bit_quant_type="nf4",
|
| 13 |
+
bnb_4bit_compute_dtype=float16,
|
| 14 |
+
bnb_4bit_use_double_quant=False,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
model = MistralForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1",
|
| 18 |
+
quantization_config=quant_config,
|
| 19 |
+
device_map="auto",)
|
| 20 |
+
model = PeftModel.from_pretrained(model, "lang-uk/dragoman")
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1", use_fast=False, add_bos_token=False)
|
| 22 |
+
|
| 23 |
+
def translate(input_text):
|
| 24 |
+
# iteratively generate
|
| 25 |
+
input_text = input_text.strip()
|
| 26 |
+
input_text = f"[INST] {input_text} [/INST]"
|
| 27 |
+
inputs = tokenizer([input_text], return_tensors="pt")
|
| 28 |
+
|
| 29 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 30 |
+
generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=200)
|
| 31 |
+
|
| 32 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 33 |
+
|
| 34 |
+
thread.start()
|
| 35 |
+
|
| 36 |
+
generated_text = ""
|
| 37 |
+
for new_text in streamer:
|
| 38 |
+
generated_text += new_text
|
| 39 |
+
yield generated_text
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
iface = gr.Interface(fn=translate, inputs="text", outputs="text", examples=[["who holds this neighborhood?"]])
|
| 43 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
peft
|
| 3 |
+
torch
|
| 4 |
+
sentencepiece
|
| 5 |
+
bitsandbytes
|