File size: 1,650 Bytes
8a59ccc
7ec241c
 
8a59ccc
7ec241c
8a59ccc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4409c4
8a59ccc
 
a4409c4
 
8a59ccc
 
a4409c4
8a59ccc
 
 
 
a4409c4
 
 
8a59ccc
 
 
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
from transformers import AutoTokenizer, MT5ForConditionalGeneration
import gradio as gr

model_name = "alfsnd/mt5-base-spanish-yoremnokki"

tokenizer = AutoTokenizer.from_pretrained(model_name)

model = MT5ForConditionalGeneration.from_pretrained(model_name)

def translate(input_text, src_lang, to_lang):
    prompt = f"translate {src_lang} to {to_lang}: {input_text}"
    input_ids = tokenizer(prompt, return_tensors="pt").input_ids
    outputs = model.generate(input_ids, max_new_tokens=1000)
    model_translation = tokenizer.decode(outputs[0])
    final_translation = model_translation[5:-4]
    return final_translation

languages = [
    'Spanish', 'Yoremnokki'
]

desc = "<p>Traductor de Español a Yoremnokki utilizando un modelo de inteligecia artifical (alfsnd/mt5-base-spanish-yoremnokki)</p>"

translator = gr.Interface(fn=translate,
                          inputs=[gr.Textbox(label="Traducir", placeholder="Introduzca su texto"),
                                  gr.Dropdown(label="De",
                                              choices=languages,
                                              value="Spanish",),
                                  gr.Dropdown(label="A",
                                              choices=languages,
                                              value="Yoremnokki")],
                          outputs=gr.Textbox(label="Traducción"),
                          title="Traductor de Español a Yoremnokki",
                          description=desc,
                          submit_btn="Traducir",
                          clear_btn="Limpiar",
                          )

translator.launch()