File size: 1,729 Bytes
f38f412 39d0228 60e80b2 f38f412 60e80b2 39d0228 f38f412 253e06f 60e80b2 253e06f 60e80b2 f38f412 253e06f 39d0228 253e06f f38f412 253e06f 60e80b2 |
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 |
import gradio as gr
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
import torch
# Cargar el modelo y tokenizador directamente en lugar de usar pipeline
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 ""
# Tokenizar el texto
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
# Generar traducci贸n
outputs = model.generate(**inputs, max_length=512)
# Decodificar la traducci贸n
translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"Texto original: {text}") # Debug
print(f"Traducci贸n: {translation}") # Debug
return translation
except Exception as e:
print(f"Error durante la traducci贸n: {str(e)}") # Debug
return f"Error en la traducci贸n: {str(e)}"
# Crear la interfaz Gradio
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"]
]
)
# Lanzar la aplicaci贸n
if __name__ == "__main__":
demo.launch(debug=True) |