Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from easynmt import EasyNMT
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
trans_model = EasyNMT("m2m_100_1.2B")
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
example_sample = [
|
| 9 |
+
["What is the US currency?","en","zh"],
|
| 10 |
+
["What is the US currency?","en","ja"],
|
| 11 |
+
["美国的通货是什么?", "zh", "en"]
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
def demo_func(src_question, src_lang, tgt_lang):
|
| 15 |
+
assert type(src_question) == type("")
|
| 16 |
+
assert type(src_lang) == type("")
|
| 17 |
+
assert type(tgt_lang) == type("")
|
| 18 |
+
if "[SEP]" in src_question:
|
| 19 |
+
src_question = list(filter(lambda xx: xx ,map(lambda x: x.strip() ,
|
| 20 |
+
src_question.split("[SEP]"))))
|
| 21 |
+
else:
|
| 22 |
+
src_question = [src_question]
|
| 23 |
+
tgt_question = trans_model.translate(
|
| 24 |
+
src_question,
|
| 25 |
+
source_lang=src_lang, target_lang = tgt_lang
|
| 26 |
+
)
|
| 27 |
+
assert type(tgt_question) == type([])
|
| 28 |
+
tgt_question = "[SEP]".join(tgt_question)
|
| 29 |
+
return {
|
| 30 |
+
"Target Question": tgt_question
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
demo = gr.Interface(
|
| 35 |
+
fn=demo_func,
|
| 36 |
+
inputs=[gr.Text(label = "Source Question"),
|
| 37 |
+
gr.Text(label = "Source Language", value = "en"),
|
| 38 |
+
gr.Text(label = "Target Language", value = "zh")
|
| 39 |
+
],
|
| 40 |
+
outputs="json",
|
| 41 |
+
title=f"Translate 🐱 demonstration",
|
| 42 |
+
examples=example_sample if example_sample else None,
|
| 43 |
+
cache_examples = False
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
demo.launch(server_name=None, server_port=None)
|