Spaces:
Running
on
Zero
Running
on
Zero
Yurii Paniv
commited on
Commit
·
15d500a
1
Parent(s):
6abca35
Add description
Browse files
app.py
CHANGED
|
@@ -12,6 +12,8 @@ from time import sleep
|
|
| 12 |
from threading import Thread
|
| 13 |
from torch import float16
|
| 14 |
import spaces
|
|
|
|
|
|
|
| 15 |
|
| 16 |
config = PeftConfig.from_pretrained("lang-uk/dragoman")
|
| 17 |
quant_config = BitsAndBytesConfig(
|
|
@@ -41,40 +43,64 @@ def translate(input_text):
|
|
| 41 |
input_text = f"[INST] {input_text} [/INST]"
|
| 42 |
inputs = tokenizer([input_text], return_tensors="pt").to(model.device)
|
| 43 |
|
| 44 |
-
generation_kwargs = dict(
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
# streaming support
|
| 48 |
-
#streamer = TextIteratorStreamer(
|
| 49 |
# tokenizer, skip_prompt=True, skip_special_tokens=True
|
| 50 |
-
#)
|
| 51 |
-
|
| 52 |
-
#thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 53 |
|
| 54 |
-
#thread.
|
| 55 |
|
|
|
|
| 56 |
|
| 57 |
-
#for new_text in streamer:
|
| 58 |
# generated_text += new_text
|
| 59 |
# yield generated_text
|
| 60 |
|
| 61 |
-
#generated_text += "\n"
|
| 62 |
-
#yield generated_text
|
| 63 |
|
| 64 |
output = model.generate(**generation_kwargs)
|
| 65 |
-
output =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
return output
|
| 67 |
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
iface = gr.Interface(
|
| 70 |
fn=translate,
|
| 71 |
inputs=gr.Textbox(
|
| 72 |
-
value="",
|
| 73 |
label="Source sentence",
|
| 74 |
),
|
| 75 |
outputs=gr.Textbox(label="Translated sentence"),
|
| 76 |
-
examples=[
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
)
|
| 80 |
iface.launch()
|
|
|
|
| 12 |
from threading import Thread
|
| 13 |
from torch import float16
|
| 14 |
import spaces
|
| 15 |
+
import huggingface_hub
|
| 16 |
+
|
| 17 |
|
| 18 |
config = PeftConfig.from_pretrained("lang-uk/dragoman")
|
| 19 |
quant_config = BitsAndBytesConfig(
|
|
|
|
| 43 |
input_text = f"[INST] {input_text} [/INST]"
|
| 44 |
inputs = tokenizer([input_text], return_tensors="pt").to(model.device)
|
| 45 |
|
| 46 |
+
generation_kwargs = dict(
|
| 47 |
+
inputs, max_new_tokens=200, num_beams=10, temperature=1
|
| 48 |
+
) # streamer=streamer,
|
| 49 |
+
|
| 50 |
# streaming support
|
| 51 |
+
# streamer = TextIteratorStreamer(
|
| 52 |
# tokenizer, skip_prompt=True, skip_special_tokens=True
|
| 53 |
+
# )
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 56 |
|
| 57 |
+
# thread.start()
|
| 58 |
|
| 59 |
+
# for new_text in streamer:
|
| 60 |
# generated_text += new_text
|
| 61 |
# yield generated_text
|
| 62 |
|
| 63 |
+
# generated_text += "\n"
|
| 64 |
+
# yield generated_text
|
| 65 |
|
| 66 |
output = model.generate(**generation_kwargs)
|
| 67 |
+
output = (
|
| 68 |
+
tokenizer.decode(output[0], skip_special_tokens=True)
|
| 69 |
+
.split("[/INST] ")[-1]
|
| 70 |
+
.strip()
|
| 71 |
+
)
|
| 72 |
return output
|
| 73 |
|
| 74 |
|
| 75 |
+
# download description of the model
|
| 76 |
+
desc_file = huggingface_hub.hf_hub_download("lang-uk/dragoman", "README.md")
|
| 77 |
+
|
| 78 |
+
with open(desc_file, "r") as f:
|
| 79 |
+
model_description = f.read()
|
| 80 |
+
model_description = model_description[model_description.find("---", 1) + 5 :]
|
| 81 |
+
|
| 82 |
+
|
| 83 |
iface = gr.Interface(
|
| 84 |
fn=translate,
|
| 85 |
inputs=gr.Textbox(
|
| 86 |
+
value='This demo contains a model from paper "Setting up the Data Printer with Improved English to Ukrainian Machine Translation", accepted to UNLP 2024 workshop at the LREC-COLING 2024 conference.',
|
| 87 |
label="Source sentence",
|
| 88 |
),
|
| 89 |
outputs=gr.Textbox(label="Translated sentence"),
|
| 90 |
+
examples=[
|
| 91 |
+
[
|
| 92 |
+
"ChatGPT (Chat Generative Pre-trained Transformer) is a chatbot developed by OpenAI and launched on November 30, 2022. Based on a large language model, it enables users to refine and steer a conversation towards a desired length, format, style, level of detail, and language. Successive prompts and replies, known as prompt engineering, are considered at each conversation stage as a context.[2] ",
|
| 93 |
+
"who holds this neighborhood?",
|
| 94 |
+
]
|
| 95 |
+
],
|
| 96 |
+
title="Dragoman: SOTA English-Ukrainian translation model",
|
| 97 |
+
description='This demo contains a model from paper "Setting up the Data Printer with Improved English to Ukrainian Machine Translation", accepted to UNLP 2024 workshop at the LREC-COLING 2024 conference.',
|
| 98 |
+
article=model_description,
|
| 99 |
+
# thumbnail: str | None = None,
|
| 100 |
+
# css: str | None = None,
|
| 101 |
+
# batch: bool = False,
|
| 102 |
+
# max_batch_size: int = 4,
|
| 103 |
+
# api_name: str | Literal[False] | None = "predict",
|
| 104 |
+
submit_btn="Translate",
|
| 105 |
)
|
| 106 |
iface.launch()
|