Enutrof commited on
Commit
4b8e36c
β€’
1 Parent(s): 59608ee

Updated app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -8
app.py CHANGED
@@ -1,13 +1,34 @@
1
  import gradio as gr
2
- from simpletransformers.seq2seq import Seq2SeqModel
3
 
4
  # Define the models' paths
5
  BM_MODEL_PATH = "Enutrof/marian-mt-en-pcm"
6
  BBGM_MODEL_PATH = "NITHUB-AI/marian-mt-bbc-en-pcm"
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  #Load models
9
- bm_model = Seq2SeqModel(encoder_decoder_type="marian", encoder_decoder_name=BM_MODEL_PATH, use_cuda=False)
10
- bbgm_model = Seq2SeqModel(encoder_decoder_type="marian", encoder_decoder_name=BBGM_MODEL_PATH, use_cuda=False)
 
 
11
 
12
  # Dictionary to easily select model
13
  models = {
@@ -16,9 +37,11 @@ models = {
16
  }
17
 
18
  def translate(model_name, source_sentence, num_beams):
19
- selected_model = models[model_name]
20
- predictions = selected_model.predict([source_sentence] * 3, num_beams=int(num_beams), num_return_sequences=3)
21
- return tuple(predictions)
 
 
22
 
23
  # Gradio interface
24
  interface = gr.Interface(
@@ -33,7 +56,17 @@ interface = gr.Interface(
33
  gr.Textbox(label="Prediction 2"),
34
  gr.Textbox(label="Prediction 3"),
35
  ],
36
- live=True
 
 
 
 
 
 
 
 
 
 
37
  )
38
 
39
- interface.launch()
 
1
  import gradio as gr
2
+ from simpletransformers.seq2seq import Seq2SeqModel, Seq2SeqArgs
3
 
4
  # Define the models' paths
5
  BM_MODEL_PATH = "Enutrof/marian-mt-en-pcm"
6
  BBGM_MODEL_PATH = "NITHUB-AI/marian-mt-bbc-en-pcm"
7
 
8
+ def load_translator(model_name='Enutrof/marian-mt-en-pcm'):
9
+ '''
10
+ This method loads the sequence to sequence model for translation.
11
+ :return: model
12
+ '''
13
+ pmodel_args = Seq2SeqArgs()
14
+ pmodel_args.max_length = 1024
15
+ pmodel_args.length_penalty = 1
16
+ pmodel_args.num_beams = 20
17
+ pmodel_args.num_return_sequences = 3
18
+
19
+ pmodel = Seq2SeqModel(
20
+ encoder_decoder_type="marian",
21
+ encoder_decoder_name=model_name,
22
+ args=pmodel_args,
23
+ use_cuda=False
24
+ )
25
+ return pmodel
26
+
27
  #Load models
28
+
29
+ bm_model = load_translator(BM_MODEL_PATH)
30
+ bbgm_model = load_translator(BBGM_MODEL_PATH)
31
+
32
 
33
  # Dictionary to easily select model
34
  models = {
 
37
  }
38
 
39
  def translate(model_name, source_sentence, num_beams):
40
+ if isinstance(source_sentence, str):
41
+ source_sentence = [source_sentence]
42
+ model = models[model_name]
43
+ predictions = model.predict(input, num_beams=int(num_beams))
44
+ return [i.replace('▁', ' ') for i in predictions[0]]
45
 
46
  # Gradio interface
47
  interface = gr.Interface(
 
56
  gr.Textbox(label="Prediction 2"),
57
  gr.Textbox(label="Prediction 3"),
58
  ],
59
+ title='English to πŸ‡³πŸ‡¬ Pidgin Automatic Translation',
60
+ description='Type your English text in the left text box to get πŸ‡³πŸ‡¬ Pidgin translations on the right. '
61
+ 'Tell us the best translation by clicking one of the buttons below.',
62
+ examples=[
63
+ 'Who are you?',
64
+ 'Is a personal philosophy of moral relativism, the only way to survive in this ethically complex world, or is it just an excuse to justify doing bad things?',
65
+ 'I know every song by that artiste.',
66
+ 'They should not be permitted here.',
67
+ 'What are you looking for?',
68
+ 'I am lost, please help me find my way to the market.',
69
+ ]
70
  )
71
 
72
+ interface.launch(enable_queue=True)