File size: 2,370 Bytes
3d6100c
92c7715
3d6100c
92c7715
3d6100c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92c7715
 
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
import spaces
import gradio as gr
from transformers import MT5ForConditionalGeneration, MT5Tokenizer,T5ForConditionalGeneration, T5Tokenizer

models = {"finetuned mt5-base":"alakxender/mt5-base-dv-en", "madlad400-3b":"google/madlad400-3b-mt"}

def tranlate(text:str,model_name:str):
    if (len(text)>2000):
        raise gr.Error(f"Try smaller text, yours is {len(text)}. try to fit to 2000 chars.")

    if (model_name is None):
        raise gr.Error("huh! not sure what to do without a model. select a model.")

    if model_name =='finetuned mt5-base':
        return mt5_translate(text,model_name)
    else:
        return t5_tranlaste(text,model_name)
    
@spaces.GPU(duration=120)
def t5_tranlaste(text:str,model_name:str):

    model = T5ForConditionalGeneration.from_pretrained(models[model_name], device_map="auto")
    tokenizer = T5Tokenizer.from_pretrained(models[model_name])

    text = f"<2en> {text}"
    input_ids = tokenizer(text, return_tensors="pt").input_ids.to(model.device)
    outputs = model.generate(input_ids=input_ids, max_new_tokens=1024*2)

    translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)

    return translated_text

def mt5_translate(text:str, model_name:str):
    
    model = MT5ForConditionalGeneration.from_pretrained(models[model_name])
    tokenizer = MT5Tokenizer.from_pretrained(models[model_name])
    inputs = tokenizer(text, return_tensors="pt")
    result = model.generate(input_ids=inputs['input_ids'], attention_mask=inputs['attention_mask'], max_new_tokens=1024*2)
    translated_text = tokenizer.decode(result[0],  skip_special_tokens=True)
    return translated_text

css = """
.textbox1 textarea {
    font-size: 18px !important;
    font-family: 'MV_Faseyha', 'Faruma', 'A_Faruma' !important;
    line-height: 1.8 !important;
}
"""

demo = gr.Interface(
    fn=tranlate,
    inputs= [
        gr.Textbox(lines=5, label="Enter Dhivehi Text", rtl=True, elem_classes="textbox1"),
        gr.Dropdown(choices=list(models.keys()), label="Select a model", value="finetuned mt5-base"),
    ],
    css=css,
    outputs=gr.Textbox(label="English Translation"),
    title="Dhivehi to English Translation",
    description="Translate Dhivehi text to English",
    examples=[["މާލޭގައި ފެންބޮޑުވާ މަގުތައް މަރާމާތު ކުރަން ފަށައިފި"]]
)

demo.launch()