ahmadsipra73 commited on
Commit
6d937b3
Β·
1 Parent(s): 216b760

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from simpletransformers.seq2seq import Seq2SeqModel, Seq2SeqArgs
4
+
5
+
6
+ # os.environ["TOKENIZERS_PARALLELISM"] = "false"
7
+
8
+
9
+ def load_translator(model_name='Enutrof/marian-mt-en-pcm'):
10
+ '''
11
+ This method loads the sequence to sequence model for translation.
12
+ :return: model
13
+ '''
14
+ pmodel_args = Seq2SeqArgs()
15
+ pmodel_args.max_length = 1024
16
+ pmodel_args.length_penalty = 1
17
+ pmodel_args.num_beams = 20
18
+ pmodel_args.num_return_sequences = 3
19
+
20
+ pmodel = Seq2SeqModel(
21
+ encoder_decoder_type="marian",
22
+ encoder_decoder_name=model_name,
23
+ args=pmodel_args,
24
+ use_cuda=False
25
+ )
26
+ return pmodel
27
+
28
+
29
+ en_pcm_model = load_translator()
30
+
31
+
32
+ def predict(input):
33
+ if isinstance(input, str):
34
+ input = [input]
35
+ predictions = en_pcm_model.predict(input)
36
+ return [i.replace('▁', ' ') for i in predictions[0]]
37
+
38
+
39
+ # HF_TOKEN = os.getenv('english-pidgin-flagging')
40
+ # hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN,
41
+ # dataset_name="English-NigerianPidgin-Result-Validation",
42
+ # organization="Enutrof",
43
+ # )
44
+ gr.Interface(
45
+ fn=predict,
46
+ inputs=gr.inputs.Textbox(lines=1, label="Input Text in English"),
47
+ outputs=[
48
+ gr.outputs.Textbox(label="Translated texts in πŸ‡³πŸ‡¬ Pidgin"),
49
+ gr.outputs.Textbox(label=''),
50
+ gr.outputs.Textbox(label=''),
51
+ ],
52
+ # theme="peach",
53
+ title='English to πŸ‡³πŸ‡¬ Pidgin Automatic Translation',
54
+ description='Type your English text in the left text box to get πŸ‡³πŸ‡¬ Pidgin translations on the right. '
55
+ 'Tell us the best translation by clicking one of the buttons below.',
56
+ examples=[
57
+ 'Who are you?',
58
+ 'You shall not pervert justice due the stranger or the fatherless, nor take a widow’s garment as a pledge.',
59
+ 'I know every song by that artiste.',
60
+ 'They should not be permitted here.',
61
+ 'What are you looking for?',
62
+ 'I am lost please help me find my way to the market.',
63
+ ],
64
+ allow_flagging="manual",
65
+ flagging_options=["translation 1 βœ…", "translation 2 βœ…",
66
+ "translation 3 βœ…"],
67
+ #flagging_callback=hf_writer,
68
+ ).launch(enable_queue=True)