File size: 2,460 Bytes
78ba245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bcd01d
 
 
 
 
 
 
1e2a87b
78ba245
6bcd01d
78ba245
ddb72fb
 
 
 
 
 
78ba245
 
06cb1ab
78ba245
ddb72fb
 
78ba245
 
 
 
 
 
6bcd01d
 
3ff2ab1
6bcd01d
78ba245
 
 
a32c94c
78ba245
6bcd01d
 
78ba245
6bcd01d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
import torch

LANGS = ["kin_Latn","eng_Latn"]
TASK = "translation"
# CKPT = "DigitalUmuganda/Finetuned-NLLB"
# MODELS = ["facebook/nllb-200-distilled-600M","DigitalUmuganda/Finetuned-NLLB"]
# model = AutoModelForSeq2SeqLM.from_pretrained(CKPT)
# tokenizer = AutoTokenizer.from_pretrained(CKPT)

device = 0 if torch.cuda.is_available() else -1

general_model = AutoModelForSeq2SeqLM.from_pretrained("mbazaNLP/Nllb_finetuned_general_en_kin")
#education_model = AutoModelForSeq2SeqLM.from_pretrained("mbazaNLP/Nllb_finetuned_education_en_kin")
#tourism_model = AutoModelForSeq2SeqLM.from_pretrained("mbazaNLP/Nllb_finetuned_tourism_en_kin")
#MODELS = {"General model":general_model_model,"Education model":education_model,"Tourism model":tourism_model}
#MODELS = {"Education model":education_model,"Tourism model":tourism_model}


tokenizer = AutoTokenizer.from_pretrained("mbazaNLP/Nllb_finetuned_general_en_kin")
# def translate(text, src_lang, tgt_lang, max_length=400):

TASK = "translation"


device = 0 if torch.cuda.is_available() else -1



def translate(text, source_lang, target_lang, max_length=450):
    """
    Translate text from source language to target language
    """
    # src_lang = choose_language(source_lang)
    # tgt_lang= choose_language(target_lang)
    # if src_lang==None:
    #     return "Error: the source langage is incorrect"
    # elif tgt_lang==None:
    #     return "Error: the target language is incorrect"

    translation_pipeline = pipeline(TASK,
                                    model=general_model,
                                    tokenizer=tokenizer,
                                    src_lang=source_lang,
                                    tgt_lang=target_lang,
                                    max_length=max_length,
                                    device=device)
    result = translation_pipeline(text)
    return result[0]['translation_text']


gradio_ui= gr.Interface(
    fn=translate,
    title="NLLB-General EN-KIN Translation Demo",
    inputs= [
        gr.components.Textbox(label="Text"),
        gr.components.Dropdown(label="Source Language", choices=LANGS),
        gr.components.Dropdown(label="Target Language", choices=LANGS),
      #  gr.components.Slider(8, 400, value=400, step=8, label="Max Length")
    ],
    outputs=gr.outputs.Textbox(label="Translated text")
)

gradio_ui.launch()