Spaces:
Runtime error
Runtime error
Timing0311
commited on
Commit
•
81b7cc0
1
Parent(s):
e055bb3
Add transjob v1.0
Browse files
app.py
CHANGED
@@ -1,17 +1,41 @@
|
|
1 |
from transformers import MT5ForConditionalGeneration, AutoTokenizer
|
2 |
-
import gradio as
|
3 |
|
4 |
-
|
5 |
-
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
return response
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
|
|
|
1 |
from transformers import MT5ForConditionalGeneration, AutoTokenizer
|
2 |
+
import gradio as gr
|
3 |
|
4 |
+
trans_mdl = MT5ForConditionalGeneration.from_pretrained("K024/mt5-zh-ja-en-trimmed")
|
5 |
+
trans_tokenizer = AutoTokenizer.from_pretrained("K024/mt5-zh-ja-en-trimmed")
|
6 |
|
7 |
+
|
8 |
+
def translation_job(data):
|
9 |
+
job = data[0]
|
10 |
+
text = data[1]
|
11 |
+
|
12 |
+
|
13 |
+
# 设置翻译任务和提示语的映射
|
14 |
+
job_key = ["中译日", "中译英", "日译中", "英译中", "日译英", "英译日"]
|
15 |
+
job_value = ["zh2ja:", "zh2en:", "ja2zh:", "en2zh:", "ja2en:", "en2ja:"]
|
16 |
+
job_map = dict(zip(job_key, job_value))
|
17 |
+
|
18 |
+
input = job_map[job] + text
|
19 |
+
enc = trans_tokenizer(input, return_tensor="pt")
|
20 |
+
tokens = trans_mdl.generate(**enc)
|
21 |
+
response = trans_tokenizer.batch_decode(tokens)
|
22 |
return response
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
with gr.Blocks() as app:
|
27 |
+
# 中英日三语翻译任务
|
28 |
+
with gr.Tab("中英日三语翻译"):
|
29 |
+
job_name = gr.Dropdown(
|
30 |
+
["中译日", "中译英", "日译中", "英译中", "日译英", "英译日"],
|
31 |
+
label = "翻译任务选择",
|
32 |
+
info = "单选"
|
33 |
+
)
|
34 |
+
source_text = gr.Textbox(lines=1, label="翻译文本", placeholder="请输入要翻译的文本")
|
35 |
+
trans_result = gr.Textbox(lines=1, label="翻译结果", placeholder="翻译结果")
|
36 |
+
trans_btn = gr.Button("翻译")
|
37 |
+
|
38 |
+
trans_btn.click(translation_job, inputs=[job_name, source_text], outputs=trans_result)
|
39 |
+
app.launch()
|
40 |
|
41 |
|