Files changed (1) hide show
  1. app.py +20 -44
app.py CHANGED
@@ -1,54 +1,30 @@
1
  import gradio as gr
2
-
3
-
4
  from translation import Translator, LANGUAGES, MODEL_URL
5
- LANGUAGES_LIST = list(LANGUAGES.keys())
6
-
7
-
8
- def translate_wrapper(text, src, trg, by_sentence=True, preprocess=True, random=False, num_beams=4):
9
- src_lang = LANGUAGES.get(src)
10
- tgt_lang = LANGUAGES.get(trg)
11
- # if src == trg:
12
- # return 'Please choose two different languages'
13
- result = translator.translate(
14
- text=text,
15
- src_lang=src_lang,
16
- tgt_lang=tgt_lang,
17
- do_sample=random,
18
- num_beams=int(num_beams),
19
- by_sentence=by_sentence,
20
- preprocess=preprocess,
21
- )
22
- return result
23
-
24
 
25
- article = f"""
26
- This is the demo for a NLLB-200-600M model fine-tuned for a few (mostly new) languages.
27
-
28
- The model itself is available at https://huggingface.co/{MODEL_URL}
29
-
30
- If you want to host in on your own backend, consider running this dockerized app: https://github.com/slone-nlp/nllb-docker-demo.
31
- """
32
 
 
 
 
 
33
 
34
  interface = gr.Interface(
35
- translate_wrapper,
36
- [
37
- gr.Textbox(label="Text", lines=2, placeholder='text to translate '),
38
- gr.Dropdown(LANGUAGES_LIST, type="value", label='source language', value=LANGUAGES_LIST[0]),
39
- gr.Dropdown(LANGUAGES_LIST, type="value", label='target language', value=LANGUAGES_LIST[1]),
40
- gr.Checkbox(label="by sentence", value=True),
41
- gr.Checkbox(label="text preprocesing", value=True),
42
- gr.Checkbox(label="randomize", value=False),
43
- gr.Dropdown([1, 2, 3, 4, 5], label="number of beams", value=4),
44
  ],
45
- "text",
46
- title='Erzya-Russian translation',
47
- article=article,
48
  )
49
 
50
-
51
  if __name__ == '__main__':
52
- translator = Translator()
53
-
54
- interface.launch()
 
1
  import gradio as gr
2
+ import threading
 
3
  from translation import Translator, LANGUAGES, MODEL_URL
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ translator = Translator()
6
+ lang_list = list(LANGUAGES.keys())
 
 
 
 
 
7
 
8
+ @gr.cache
9
+ def translate(text, src, trg, **kwargs):
10
+ src_lang, tgt_lang = LANGUAGES.get(src), LANGUAGES.get(trg)
11
+ return translator.translate(text, src_lang, tgt_lang, **kwargs)
12
 
13
  interface = gr.Interface(
14
+ fn=translate,
15
+ inputs=[
16
+ gr.Textbox(label="Text", lines=2, description="Enter the text you want to translate."),
17
+ gr.Dropdown(lang_list, type="value", label='Source Language', value=lang_list[0], description="Select the language of the input text."),
18
+ gr.Dropdown(lang_list, type="value", label='Target Language', value=lang_list[1], description="Select the language you want to translate to."),
19
+ gr.Checkbox(label="By Sentence", value=True, description="Translate the text sentence by sentence."),
20
+ gr.Checkbox(label="Preprocess", value=True, description="Preprocess the text before translation."),
21
+ gr.Checkbox(label="Randomize", value=False, description="Randomize the translation output."),
22
+ gr.Slider(value=4, label="Number of Beams", minimum=1, maximum=5, step=1, description="Select the number of beams for the translation process.")
23
  ],
24
+ outputs="text",
25
+ title='Erzya-Russian Translation',
26
+ article=f"Model: https://huggingface.co/{MODEL_URL}"
27
  )
28
 
 
29
  if __name__ == '__main__':
30
+ threading.Thread(target=interface.launch, kwargs=dict(share=True, server_name="0.0.0.0", server_port=7860)).start()