Kleber commited on
Commit
7abb54c
1 Parent(s): cf0b6e4

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
3
+ import torch
4
+
5
+ LANGS = ["kin_Latn","eng_Latn"]
6
+ TASK = "translation"
7
+ # CKPT = "DigitalUmuganda/Finetuned-NLLB"
8
+ MODELS = ["facebook/nllb-200-distilled-600M","DigitalUmuganda/Finetuned-NLLB"]
9
+ # model = AutoModelForSeq2SeqLM.from_pretrained(CKPT)
10
+ # tokenizer = AutoTokenizer.from_pretrained(CKPT)
11
+
12
+ device = 0 if torch.cuda.is_available() else -1
13
+
14
+ du_model = AutoModelForSeq2SeqLM.from_pretrained("DigitalUmuganda/Finetuned-NLLB")
15
+ fb_model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M")
16
+ models = {"facebook/nllb-200-distilled-600M":fb_model,"DigitalUmuganda/Finetuned-NLLB":du_model}
17
+ tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M")
18
+ # def translate(text, src_lang, tgt_lang, max_length=400):
19
+ def translate(CKPT,text, src_lang, tgt_lang, max_length=400):
20
+
21
+ """
22
+ Translate the text from source lang to target lang
23
+ """
24
+
25
+
26
+ translation_pipeline = pipeline(TASK,
27
+ model=models[CKPT],
28
+ tokenizer=tokenizer,
29
+ src_lang=src_lang,
30
+ tgt_lang=tgt_lang,
31
+ max_length=max_length,
32
+ device=device)
33
+
34
+ result = translation_pipeline(text)
35
+ return result[0]['translation_text']
36
+
37
+
38
+ gr.Interface(
39
+ translate,
40
+ [
41
+ gr.components.Dropdown(label="choose a model",choices=MODELS)
42
+ gr.components.Textbox(label="Text"),
43
+ gr.components.Dropdown(label="Source Language", choices=LANGS),
44
+ gr.components.Dropdown(label="Target Language", choices=LANGS),
45
+ #gr.components.Slider(8, 512, value=400, step=8, label="Max Length")
46
+ ],
47
+ ["text"],
48
+ #examples=examples,
49
+ # article=article,
50
+ cache_examples=False,
51
+ title="Finetuned-NLLB-1",
52
+ #description=description
53
+ ).launch()
54
+