|
|
import gradio as gr |
|
|
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer |
|
|
import torch |
|
|
|
|
|
|
|
|
model_name = "projecte-aina/aina-translator-es-ast" |
|
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
|
model = AutoModelForSeq2SeqLM.from_pretrained(model_name) |
|
|
|
|
|
def translate_text(text): |
|
|
""" |
|
|
Traduce texto del espa帽ol al asturiano |
|
|
""" |
|
|
try: |
|
|
if not text: |
|
|
return "" |
|
|
|
|
|
|
|
|
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512) |
|
|
|
|
|
|
|
|
outputs = model.generate(**inputs, max_length=512) |
|
|
|
|
|
|
|
|
translation = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
print(f"Texto original: {text}") |
|
|
print(f"Traducci贸n: {translation}") |
|
|
|
|
|
return translation |
|
|
|
|
|
except Exception as e: |
|
|
print(f"Error durante la traducci贸n: {str(e)}") |
|
|
return f"Error en la traducci贸n: {str(e)}" |
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
|
fn=translate_text, |
|
|
inputs=gr.Textbox(label="Texto en espa帽ol", placeholder="Escribe aqu铆 el texto a traducir..."), |
|
|
outputs=gr.Textbox(label="Traducci贸n al asturiano"), |
|
|
title="Traductor Espa帽ol-Asturiano", |
|
|
description="Traductor basado en el modelo AINA para traducir del espa帽ol al asturiano.", |
|
|
examples=[ |
|
|
["Hola, 驴c贸mo jjjest谩s?"], |
|
|
["Me gusta mucho Asturias y su cultura"], |
|
|
["El cielo est谩 muy azul hoy"] |
|
|
] |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch(debug=True) |