Spaces:
Sleeping
Sleeping
sbkapelner
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
3 |
+
import langid
|
4 |
+
|
5 |
+
# Load models and tokenizers into dictionaries for easier access
|
6 |
+
models = {
|
7 |
+
"en": {
|
8 |
+
"fr":
|
9 |
+
AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-fr"),
|
10 |
+
"es":
|
11 |
+
AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-es"),
|
12 |
+
},
|
13 |
+
"fr": {
|
14 |
+
"en":
|
15 |
+
AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-fr-en"),
|
16 |
+
"es":
|
17 |
+
AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-fr-es"),
|
18 |
+
},
|
19 |
+
"es": {
|
20 |
+
"en":
|
21 |
+
AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-es-en"),
|
22 |
+
"fr":
|
23 |
+
AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-es-fr"),
|
24 |
+
},
|
25 |
+
}
|
26 |
+
|
27 |
+
tokenizers = {
|
28 |
+
"en": {
|
29 |
+
"fr":
|
30 |
+
AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-fr"),
|
31 |
+
"es":
|
32 |
+
AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-es"),
|
33 |
+
},
|
34 |
+
"fr": {
|
35 |
+
"en":
|
36 |
+
AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-fr-en"),
|
37 |
+
"es":
|
38 |
+
AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-fr-es"),
|
39 |
+
},
|
40 |
+
"es": {
|
41 |
+
"en":
|
42 |
+
AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-es-en"),
|
43 |
+
"fr":
|
44 |
+
AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-es-fr"),
|
45 |
+
},
|
46 |
+
}
|
47 |
+
|
48 |
+
def translate(input_text, source_lang, target_lang):
|
49 |
+
tokenizer = tokenizers[source_lang][target_lang]
|
50 |
+
model = models[source_lang][target_lang]
|
51 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
52 |
+
translated_tokens = model.generate(**inputs)
|
53 |
+
return tokenizer.batch_decode(translated_tokens,
|
54 |
+
skip_special_tokens=True)[0]
|
55 |
+
|
56 |
+
def translate_text(input_text):
|
57 |
+
detected_lang, _ = langid.classify(input_text)
|
58 |
+
translations = {"English": "", "French": "", "Spanish": ""}
|
59 |
+
|
60 |
+
if detected_lang == "en":
|
61 |
+
translations["French"] = translate(input_text, "en", "fr")
|
62 |
+
translations["Spanish"] = translate(input_text, "en", "es")
|
63 |
+
elif detected_lang == "fr":
|
64 |
+
translations["English"] = translate(input_text, "fr", "en")
|
65 |
+
translations["Spanish"] = translate(input_text, "fr", "es")
|
66 |
+
elif detected_lang == "es":
|
67 |
+
translations["English"] = translate(input_text, "es", "en")
|
68 |
+
translations["French"] = translate(input_text, "es", "fr")
|
69 |
+
else:
|
70 |
+
translations["Error"] = "Language not supported for translation."
|
71 |
+
|
72 |
+
return translations["English"], translations["French"], translations["Spanish"]
|
73 |
+
|
74 |
+
def clear_textboxes():
|
75 |
+
return "", ""
|
76 |
+
|
77 |
+
with gr.Blocks() as demo:
|
78 |
+
with gr.Row():
|
79 |
+
with gr.Column():
|
80 |
+
text_to_translate = gr.Textbox(label="Text to Translate")
|
81 |
+
translate_btn = gr.Button(value="Translate")
|
82 |
+
with gr.Column():
|
83 |
+
translation_en = gr.Textbox(label="Translation to English")
|
84 |
+
translation_fr = gr.Textbox(label="Translation to French")
|
85 |
+
translation_es = gr.Textbox(label="Translation to Spanish")
|
86 |
+
clear_btn = gr.Button(value="Clear")
|
87 |
+
translate_btn.click(
|
88 |
+
fn=translate_text,
|
89 |
+
inputs=[text_to_translate],
|
90 |
+
outputs=[translation_en, translation_fr, translation_es]
|
91 |
+
)
|
92 |
+
|
93 |
+
clear_btn.click(
|
94 |
+
fn=clear_textboxes,
|
95 |
+
inputs=None,
|
96 |
+
outputs=[text_to_translate, translation_en, translation_fr, translation_es]
|
97 |
+
)
|
98 |
+
|
99 |
+
demo.launch(share=True)
|