ahmadsipra73 commited on
Commit
8ebe69b
1 Parent(s): cdebc02

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
3
+ import torch
4
+
5
+ model = AutoModelForSeq2SeqLM.from_pretrained("Jayyydyyy/m2m100_418m_tokipona")
6
+ tokenizer = AutoTokenizer.from_pretrained("facebook/m2m100_418M")
7
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
8
+ LANG_CODES = {
9
+ "English":"en",
10
+ "toki pona":"tl"
11
+ }
12
+
13
+ def translate(text, src_lang, tgt_lang, candidates:int):
14
+ """
15
+ Translate the text from source lang to target lang
16
+ """
17
+
18
+ src = LANG_CODES.get(src_lang)
19
+ tgt = LANG_CODES.get(tgt_lang)
20
+
21
+ tokenizer.src_lang = src
22
+ tokenizer.tgt_lang = tgt
23
+
24
+ ins = tokenizer(text, return_tensors='pt').to(device)
25
+
26
+ gen_args = {
27
+ 'return_dict_in_generate': True,
28
+ 'output_scores': True,
29
+ 'output_hidden_states': True,
30
+ 'length_penalty': 0.0, # don't encourage longer or shorter output,
31
+ 'num_return_sequences': candidates,
32
+ 'num_beams':candidates,
33
+ 'forced_bos_token_id': tokenizer.lang_code_to_id[tgt]
34
+ }
35
+
36
+
37
+ outs = model.generate(**{**ins, **gen_args})
38
+ output = tokenizer.batch_decode(outs.sequences, skip_special_tokens=True)
39
+
40
+ return '\n'.join(output)
41
+
42
+ with gr.Blocks() as app:
43
+ markdown="""
44
+ # An English / toki pona Neural Machine Translation App!
45
+
46
+ ### toki a! 💬
47
+ This is an english to toki pona / toki pona to english neural machine translation app.
48
+ Input your text to translate, a source language and target language, and desired number of return sequences!
49
+ ### Grammar Regularization
50
+ An interesting quirk of training a many-to-many translation model is that pseudo-grammar correction
51
+ can be achieved by translating *from* **language A** *to* **language A**
52
+
53
+ Remember, this can ***approximate*** grammaticality, but it isn't always the best.
54
+
55
+ For example, "mi li toki e toki pona" (Source Language: toki pona & Target Language: toki pona) will result in:
56
+ - ['mi toki e toki pona.', 'mi toki pona.', 'mi toki e toki pona']
57
+ - (Thus, the ungrammatical "li" is dropped)
58
+ ### Model and Data
59
+ This app utilizes a fine-tuned version of Facebook/Meta AI's M2M100 418M param model.
60
+
61
+ By leveraging the pretrained weights of the massively multilingual M2M100 model,
62
+ we can jumpstart our transfer learning to accomplish machine translation for toki pona!
63
+
64
+ The model was fine-tuned on the English/toki pona bitexts found at [https://tatoeba.org/](https://tatoeba.org/)
65
+
66
+ ### This app is a work in progress and obviously not all translations will be perfect.
67
+ In addition to parameter quantity and the hyper-parameters used while training,
68
+ the *quality of data* found on Tatoeba directly influences the perfomance of projects like this!
69
+ If you wish to contribute, please add high quality and diverse translations to Tatoeba!
70
+ """
71
+
72
+ with gr.Row():
73
+ gr.Markdown(markdown)
74
+ with gr.Column():
75
+ input_text = gr.components.Textbox(label="Input Text", value="Raccoons are fascinating creatures, but I prefer opossums.")
76
+ source_lang = gr.components.Dropdown(label="Source Language", value="English", choices=list(LANG_CODES.keys()))
77
+ target_lang = gr.components.Dropdown(label="Target Language", value="toki pona", choices=list(LANG_CODES.keys()))
78
+ return_seqs = gr.Slider(label="Number of return sequences", value=3, minimum=1, maximum=12, step=1)
79
+
80
+ inputs=[input_text, source_lang, target_lang, return_seqs]
81
+ outputs = gr.Textbox()
82
+
83
+ translate_btn = gr.Button("Translate! | o ante toki!")
84
+ translate_btn.click(translate, inputs=inputs, outputs=outputs)
85
+
86
+ gr.Examples(
87
+ [
88
+ ["Hello! How are you?", "English", "toki pona", 3],
89
+ ["toki a! ilo pi ante toki ni li pona!", "toki pona", "English", 3],
90
+ ["mi li toki e toki pona", "toki pona", "toki pona", 3],
91
+ ],
92
+ inputs=inputs
93
+ )
94
+
95
+ app.launch()