Commit
·
edcb7d2
1
Parent(s):
3c0b1d8
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,28 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, T5Tokenizer, MT5ForConditionalGeneration
|
3 |
|
4 |
tokenizer = T5Tokenizer.from_pretrained("engmatic-earth/mt5-zh-ja-en-trimmed-fine-tuned-v1")
|
5 |
model = AutoModelForSeq2SeqLM.from_pretrained("engmatic-earth/mt5-zh-ja-en-trimmed-fine-tuned-v1")
|
6 |
|
7 |
-
def output(
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
return translated_phrase
|
14 |
|
15 |
interface = gr.Interface(fn=output, inputs=gr.inputs.Textbox(lines=3, placeholder="Write what you want to say in Japanese.")
|
|
|
1 |
+
import re
|
2 |
import gradio as gr
|
3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, T5Tokenizer, MT5ForConditionalGeneration
|
4 |
|
5 |
tokenizer = T5Tokenizer.from_pretrained("engmatic-earth/mt5-zh-ja-en-trimmed-fine-tuned-v1")
|
6 |
model = AutoModelForSeq2SeqLM.from_pretrained("engmatic-earth/mt5-zh-ja-en-trimmed-fine-tuned-v1")
|
7 |
|
8 |
+
def output(input_text):
|
9 |
+
input_text = re.sub('(!|!)', '.', input_text)
|
10 |
+
target_sentence_list = []
|
11 |
+
for part in input_text.split("."):
|
12 |
+
sentence = part.strip()
|
13 |
+
if sentence != '':
|
14 |
+
target_sentence_list.append(sentence)
|
15 |
+
|
16 |
+
translation_subject_list = []
|
17 |
+
for i in target_sentence_list:
|
18 |
+
target_sentence = ["en2ja: " + str(i)]
|
19 |
+
translated = model.generate(**tokenizer(target_sentence, return_tensors="pt"), max_length=1000)
|
20 |
+
tgt_text = [tokenizer.decode(t, skip_special_tokens=True) for t in translated]
|
21 |
+
translated_phrase = tgt_text[0] + "。" ## "\n"も入れると改行も
|
22 |
+
translated_phrase = translated_phrase.replace(" ", "")
|
23 |
+
translation_subject_list.append(translated_phrase)
|
24 |
+
translated_phrase = "\n".join(translation_subject_list)
|
25 |
+
|
26 |
return translated_phrase
|
27 |
|
28 |
interface = gr.Interface(fn=output, inputs=gr.inputs.Textbox(lines=3, placeholder="Write what you want to say in Japanese.")
|