Salavat commited on
Commit
084cf95
1 Parent(s): 315ec2a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
2
+ import gradio as gr
3
+
4
+
5
+ DEBUG_UI = False
6
+ LANGS = {
7
+ 'English': 'eng_Latn',
8
+ 'Interslavic': 'isv_Latn',
9
+ # 'Интерславик': 'isv_Cyrl',
10
+ 'Russian': 'rus_Cyrl',
11
+ 'Belarusian': 'bel_Cyrl',
12
+ 'Ukrainian': 'ukr_Cyrl',
13
+ 'Bulgarian': 'bul_Cyrl',
14
+ 'Macedonian': 'mkd_Cyrl',
15
+ 'Serbian': 'srp_Cyrl',
16
+ 'Croatian': 'hrv_Latn',
17
+ 'Bosnian': 'bos_Latn',
18
+ 'Slovenian': 'slv_Latn',
19
+ 'Slovak': 'slk_Latn',
20
+ 'Czech': 'ces_Latn',
21
+ 'Polish': 'pol_Latn',
22
+ 'Silesian': 'szl_Latn',
23
+ 'Esperanto': 'epo_Latn',
24
+ 'German': 'deu_Latn',
25
+ 'French': 'fra_Latn',
26
+ 'Spanish': 'spa_Latn',
27
+ }
28
+
29
+
30
+ if DEBUG_UI:
31
+ def translate(text, src_lang, tgt_lang):
32
+ return text
33
+
34
+ else:
35
+ # model_name = 'Salavat/nllb-200-distilled-600M-finetuned-isv'
36
+ model_name = 'Salavat/nllb-200-distilled-600M-finetuned-isv_v2'
37
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
38
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
39
+
40
+ def get_lang(lang):
41
+ if lang in LANGS:
42
+ return LANGS[lang]
43
+ return lang
44
+
45
+ def translate(text, from_, to_):
46
+ """
47
+ Translate the text from source lang to target lang
48
+ """
49
+ translation_pipeline = pipeline(
50
+ "translation", model=model, tokenizer=tokenizer, max_length=400,
51
+ src_lang=LANGS.get(from_, from_), tgt_lang=LANGS.get(to_, to_)
52
+ )
53
+ result = translation_pipeline(text.split('\n'))
54
+ output = '\n'.join(line['translation_text'] for line in result)
55
+ return output
56
+
57
+
58
+ demo = gr.Blocks()
59
+
60
+ with demo:
61
+ gr.Markdown("This is a modified version of the original [NLLB-Translator](https://huggingface.co/spaces/Narrativaai/NLLB-Translator) space")
62
+ with gr.Row():
63
+ lang_input = gr.components.Dropdown(label="From", choices=list(LANGS.keys()), value='English')
64
+ lang_output = gr.components.Dropdown(label="To", choices=list(LANGS.keys()), value='Interslavic')
65
+ with gr.Row().style(equal_height=True):
66
+ text_input = gr.components.Textbox(label="Text", lines=7, placeholder="Your text")
67
+ text_output = gr.components.Textbox(label="Result", lines=7, placeholder="Translation...")
68
+ button = gr.Button("Translate")
69
+
70
+ button.click(translate, inputs=[text_input, lang_input, lang_output], outputs=text_output)
71
+
72
+ demo.launch()