Timing0311 commited on
Commit
81b7cc0
1 Parent(s): e055bb3

Add transjob v1.0

Browse files
Files changed (1) hide show
  1. app.py +35 -11
app.py CHANGED
@@ -1,17 +1,41 @@
1
  from transformers import MT5ForConditionalGeneration, AutoTokenizer
2
- import gradio as grad
3
 
4
- mdl = MT5ForConditionalGeneration.from_pretrained("google/mt5-small")
5
- tokenizer = AutoTokenizer.from_pretrained("google/mt5-small")
6
 
7
- def translation_CN2EN(text):
8
- inp = "translate Chinese to English: " + text
9
- enc = tokenizer(inp, return_tensors="pt").input_ids
10
- tokens = mdl(**enc)
11
- response = tokenizer.decode(tokens[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
12
  return response
13
- para=grad.Textbox(lines=1, label="Chinese Text", placeholder="Text in Chinese")
14
- out=grad.Textbox(lines=1, label="English Translation")
15
- grad.Interface(translation_CN2EN, inputs=para, outputs=out).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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