Spaces:
Sleeping
Sleeping
yuxj
commited on
Commit
•
06945e5
1
Parent(s):
2eb2cea
提交运行脚本
Browse files- app.py +47 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline, M2M100ForConditionalGeneration, M2M100Tokenizer, AutoTokenizer, AutoModelForSeq2SeqLM
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
translator_1 = pipeline("translation", model = "penpen/novel-zh-en", max_time = 7)
|
5 |
+
|
6 |
+
translator_2_model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_1.2B")
|
7 |
+
translator_2_tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_1.2B")
|
8 |
+
|
9 |
+
translator_3_model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-zh-en")
|
10 |
+
translator_3_tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-zh-en")
|
11 |
+
|
12 |
+
def model_1(text):
|
13 |
+
return translator_1(text)[0]["translation_text"]
|
14 |
+
|
15 |
+
def model_2(text):
|
16 |
+
translator_2_tokenizer.src_lang = "zh"
|
17 |
+
encoded_zh = translator_2_tokenizer(text, return_tensors = "pt", truncation = True, max_length = 512)
|
18 |
+
generated_tokens = translator_2_model.generate(**encoded_zh, forced_bos_token_id = translator_2_tokenizer.get_lang_id("en"))
|
19 |
+
return translator_2_tokenizer.batch_decode(generated_tokens, skip_special_tokens = True)[0]
|
20 |
+
|
21 |
+
def model_3(text):
|
22 |
+
batch = translator_3_tokenizer(text, return_tensors = "pt", truncation = True, max_length = 512)
|
23 |
+
generated_tokens = translator_3_model.generate(**batch)
|
24 |
+
return translator_3_tokenizer.batch_decode(generated_tokens, skip_special_tokens = True)[0]
|
25 |
+
|
26 |
+
def on_click(text):
|
27 |
+
print('input: ', chines_text)
|
28 |
+
res_1 = model_1(text)
|
29 |
+
print('model_1: ', res_1)
|
30 |
+
res_2 = model_2(text)
|
31 |
+
print('model_2: ', res_2)
|
32 |
+
res_3 = model_3(text)
|
33 |
+
print('model_3: ', res_3)
|
34 |
+
print('----------------------------')
|
35 |
+
return res_1, res_2, res_3
|
36 |
+
|
37 |
+
with gr.Blocks() as block:
|
38 |
+
gr.Markdown("<center><h1>中文翻译英文对比</h1></center>")
|
39 |
+
tb_input = gr.Textbox(label = "输入", placeholder = "输入中文句子", lines = 1)
|
40 |
+
btn = gr.Button("翻译", variant = 'primary')
|
41 |
+
tb_trans_1 = gr.Textbox(label = "模型1(penpen/novel-zh-en)")
|
42 |
+
tb_trans_2 = gr.Textbox(label = "模型2(facebook/m2m100_1.2B)")
|
43 |
+
tb_trans_3 = gr.Textbox(label = "模型3(Helsinki-NLP/opus-mt-zh-en)")
|
44 |
+
btn.click(fn = on_click, inputs = tb_input, outputs = [tb_trans_1, tb_trans_2, tb_trans_3])
|
45 |
+
gr.close_all()
|
46 |
+
block.queue(concurrency_count = 5)
|
47 |
+
block.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
accelerate
|
3 |
+
datasets
|
4 |
+
evaluate
|
5 |
+
sacremoses
|
6 |
+
SentencePiece
|