Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
add app.py
Browse files
app.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
4 |
+
|
5 |
+
model_path = "anzorq/m2m100_418M_ft_ru-kbd_44K"
|
6 |
+
src_lang="ru"
|
7 |
+
tgt_lang="zu"
|
8 |
+
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, src_lang=src_lang)
|
10 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
|
11 |
+
|
12 |
+
def translate(text):
|
13 |
+
inputs = tokenizer(text, return_tensors="pt")
|
14 |
+
|
15 |
+
translated_tokens = model.generate(
|
16 |
+
**inputs, forced_bos_token_id=tokenizer.lang_code_to_id[tgt_lang], num_beams=4, num_return_sequences=4
|
17 |
+
)
|
18 |
+
|
19 |
+
translations = []
|
20 |
+
for translation in tokenizer.batch_decode(translated_tokens, skip_special_tokens=True):
|
21 |
+
translations.append(translation)
|
22 |
+
|
23 |
+
return translations
|
24 |
+
|
25 |
+
iface = gr.Interface(fn=translate, inputs="text", outputs="text")
|
26 |
+
iface.launch()
|