jisukim8873
commited on
Commit
·
ac6d958
1
Parent(s):
b13324b
trans options
Browse files
app.py
CHANGED
@@ -1,15 +1,58 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
4 |
|
5 |
+
en2ko = "KoJLabs/nllb-finetuned-en2ko"
|
6 |
+
ko2en = "KoJLabs/nllb-finetuned-ko2en"
|
7 |
+
style = "KoJLabs/bart-speech-style-converter"
|
8 |
|
9 |
+
en2ko_model = AutoModelForSeq2SeqLM.from_pretrained(en2ko)
|
10 |
+
ko2en_model = AutoModelForSeq2SeqLM.from_pretrained(ko2en)
|
11 |
+
style_model = AutoModelForSeq2SeqLM.from_pretrained(style)
|
12 |
|
13 |
+
en2ko_tokenizer = AutoTokenizer.from_pretrained(en2ko)
|
14 |
+
ko2en_tokenizer = AutoTokenizer.from_pretrained(ko2en)
|
15 |
+
style_tokenizer = AutoTokenizer.from_pretrained(style)
|
16 |
+
|
17 |
+
def translate(source, target, text):
|
18 |
+
formats = {"English":"eng_Latn", "Korean":"kor_Hang"}
|
19 |
+
src = formats[source]
|
20 |
+
tgt = formats[target]
|
21 |
+
|
22 |
+
if src == "eng_Latn":
|
23 |
+
translator = pipeline(
|
24 |
+
'translation',
|
25 |
+
model=en2ko_model,
|
26 |
+
tokenizer=ko2en_tokenizer,
|
27 |
+
src_lang=src,
|
28 |
+
tgt_lang=tgt,
|
29 |
+
)
|
30 |
+
|
31 |
+
if src == "kor_Hang":
|
32 |
+
translator = pipeline(
|
33 |
+
'translation',
|
34 |
+
model=ko2en_model,
|
35 |
+
tokenizer=en2ko_tokenizer,
|
36 |
+
src_lang=src,
|
37 |
+
tgt_lang=tgt
|
38 |
+
)
|
39 |
+
|
40 |
+
output = translator(text)
|
41 |
+
translated_text = output[0]['translation_text']
|
42 |
+
|
43 |
+
return translated_text
|
44 |
+
|
45 |
+
title = 'KoTAN Translator & Speech-style converter'
|
46 |
+
lang = ['English','Korean']
|
47 |
+
|
48 |
+
translator_app = gr.Interface(
|
49 |
+
fn=translate,
|
50 |
+
inputs=[gr.inputs.Dropdown(choices=lang, label='Source Language'), gr.inputs.Dropdown(choices=lang, label='Target Language'), gr.inputs.Textbox(lines=5, label='Text to Translate')],
|
51 |
+
outputs=[gr.outputs.Textbox(label='Translated Text')],
|
52 |
+
title=title,
|
53 |
+
description = 'KoTAN: Korean Translation and Augmentation with fine-tuned NLLB. If you want to download as pip package, please visit our github. (https://github.com/KoJLabs/KoTAN)',
|
54 |
+
article='Jisu, Kim. Juhwan, Lee',
|
55 |
+
enable_queue=True,
|
56 |
+
)
|
57 |
+
|
58 |
+
translator_app.launch()
|