Kleber commited on
Commit
e5d3229
1 Parent(s): ed9b79a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -20,39 +20,46 @@ education_model = AutoModelForSeq2SeqLM.from_pretrained("mbazaNLP/Nllb_finetuned
20
 
21
  tokenizer = AutoTokenizer.from_pretrained("mbazaNLP/Nllb_finetuned_general_en_kin")
22
  # def translate(text, src_lang, tgt_lang, max_length=400):
23
- def translate(CKPT,text, src_lang, tgt_lang, max_length=400):
24
 
 
 
 
 
 
 
 
 
25
  """
26
- Translate the text from source lang to target lang
27
  """
28
-
 
 
 
 
 
29
 
30
  translation_pipeline = pipeline(TASK,
31
  model=education_model,
32
  tokenizer=tokenizer,
33
- src_lang=src_lang,
34
- tgt_lang=tgt_lang,
35
  max_length=max_length,
36
  device=device)
37
-
38
  result = translation_pipeline(text)
39
  return result[0]['translation_text']
40
 
41
 
42
- gr.Interface(
43
- translate,
44
- [
45
- #gr.components.Dropdown(label="choose a model",choices=MODELS),
46
  gr.components.Textbox(label="Text"),
47
  gr.components.Dropdown(label="Source Language", choices=LANGS),
48
  gr.components.Dropdown(label="Target Language", choices=LANGS),
49
- #gr.components.Slider(8, 512, value=400, step=8, label="Max Length")
50
  ],
51
- ["text"],
52
- #examples=examples,
53
- # article=article,
54
- cache_examples=False,
55
- title="Finetuned-NLLB-EN-KIN",
56
- #description=description
57
- ).launch()
58
 
 
 
20
 
21
  tokenizer = AutoTokenizer.from_pretrained("mbazaNLP/Nllb_finetuned_general_en_kin")
22
  # def translate(text, src_lang, tgt_lang, max_length=400):
 
23
 
24
+ TASK = "translation"
25
+
26
+
27
+ device = 0 if torch.cuda.is_available() else -1
28
+
29
+
30
+
31
+ def translate(text, source_lang, target_lang, max_length=400):
32
  """
33
+ Translate text from source language to target language
34
  """
35
+ # src_lang = choose_language(source_lang)
36
+ # tgt_lang= choose_language(target_lang)
37
+ # if src_lang==None:
38
+ # return "Error: the source langage is incorrect"
39
+ # elif tgt_lang==None:
40
+ # return "Error: the target language is incorrect"
41
 
42
  translation_pipeline = pipeline(TASK,
43
  model=education_model,
44
  tokenizer=tokenizer,
45
+ src_lang=source_lang,
46
+ tgt_lang=target_lang,
47
  max_length=max_length,
48
  device=device)
 
49
  result = translation_pipeline(text)
50
  return result[0]['translation_text']
51
 
52
 
53
+ gradio_ui= gr.Interface(
54
+ fn=translate,
55
+ title="NLLB-General EN-KIN Translation Demo",
56
+ inputs= [
57
  gr.components.Textbox(label="Text"),
58
  gr.components.Dropdown(label="Source Language", choices=LANGS),
59
  gr.components.Dropdown(label="Target Language", choices=LANGS),
60
+ # gr.components.Slider(8, 400, value=400, step=8, label="Max Length")
61
  ],
62
+ outputs=gr.outputs.Textbox(label="Translated text")
63
+ )
 
 
 
 
 
64
 
65
+ gradio_ui.launch()