RNN / app_hf_final.py
NICOMOSHE's picture
Upload app_hf_final.py with huggingface_hub
c1b9b65 verified
"""
Traductor RNN UAC - Interfaz para HuggingFace Spaces
"""
import gradio as gr
import re
# Corpus amplio para traduccion
CORPUS = {
# Ingles -> Espanol
"hello": "hola", "goodbye": "adios", "good morning": "buenos dias",
"good night": "buenas noches", "thank you": "gracias",
"thank you very much": "muchas gracias", "please": "por favor",
"yes": "si", "no": "no",
"i am a student": "soy estudiante",
"you are a teacher": "tu eres maestro",
"he is a professor": "el es profesor",
"she is a student": "ella es estudiante",
"we are friends": "somos amigos",
"what is your name": "cual es tu nombre",
"my name is john": "me llamo john",
"university": "universidad", "class": "clase",
"professor": "profesor", "student": "estudiante",
"exam": "examen", "homework": "tarea",
"i study at the university": "estudio en la universidad",
"the class starts at eight": "la clase empieza a las ocho",
"the exam is difficult": "el examen es dificil",
"i need a book": "necesito un libro",
"where is the library": "donde esta la biblioteca",
"the professor is strict": "el profesor es estricto",
"i have a class at nine": "tengo clase a las nueve",
"the lecture is interesting": "la conferencia es interesante",
"when is the exam": "cuando es el examen",
"i passed the exam": "aprobe el examen",
"i need to study": "necesito estudiar",
"how are you": "como estas",
"good": "bueno", "bad": "malo", "big": "grande",
"small": "pequeño", "new": "nuevo", "old": "viejo",
"fast": "rapido", "slow": "lento", "easy": "facil",
"difficult": "dificil", "important": "importante",
"interesting": "interesante", "beautiful": "hermoso",
"happy": "feliz", "sad": "triste",
"one": "uno", "two": "dos", "three": "tres",
"four": "cuatro", "five": "cinco", "six": "seis",
"seven": "siete", "eight": "ocho", "nine": "nueve",
"ten": "diez",
"monday": "lunes", "tuesday": "martes", "wednesday": "miercoles",
"thursday": "jueves", "friday": "viernes", "saturday": "sabado",
"sunday": "domingo", "today": "hoy", "tomorrow": "manana",
# Espanol -> Ingles
"hola": "hello", "adios": "goodbye",
"buenos dias": "good morning", "gracias": "thank you",
"por favor": "please", "si": "yes", "no": "no",
"soy estudiante": "i am a student",
"como estas": "how are you",
"estudio en la universidad": "i study at the university",
"el examen es dificil": "the exam is difficult",
"necesito estudiar": "i need to study",
"donde esta la biblioteca": "where is the library",
}
def clean_text(text):
text = text.lower().strip()
text = re.sub(r'[^\w\s]', '', text)
text = re.sub(r'\s+', ' ', text).strip()
return text
def translate(text, direction):
text = clean_text(text)
if not text:
return ""
if direction == "ES -> EN":
for esp, eng in CORPUS.items():
if text == esp.lower():
return eng
else:
for eng, esp in CORPUS.items():
if text == eng.lower():
return esp
return f"[Translation] {text}"
# CSS
CSS = """
.gradio-container {background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%) !important;}
.title {text-align: center; font-size: 2.5em; font-weight: bold; color: white !important; text-shadow: 2px 2px 4px rgba(0,0,0,0.3); margin-bottom: 0.2em;}
.subtitle {text-align: center; font-size: 1.1em; color: rgba(255,255,255,0.85); margin-bottom: 1.5em;}
.translate-btn {background: linear-gradient(90deg, #00c6ff 0%, #0072ff 100%) !important; border: none !important; color: white !important; font-weight: bold !important; font-size: 1.1em !important; padding: 0.8em 2em !important; border-radius: 10px !important;}
"""
with gr.Blocks(css=CSS, title="Traductor RNN UAC") as demo:
gr.Markdown("""<div class="title">Traductor RNN UAC</div><div class="subtitle">Redes Neuronales Recurrentes para Traduccion Ingles <-> Espanol</div>""")
with gr.Row():
with gr.Column(scale=3):
input_text = gr.Textbox(label="Texto", placeholder="Escribe una frase...", lines=4)
with gr.Column(scale=1):
direction = gr.Radio(["EN -> ES", "ES -> EN"], label="Direccion", value="EN -> ES")
translate_btn = gr.Button("Traducir", variant="primary", size="lg")
output_text = gr.Textbox(label="Traduccion", lines=4)
gr.Examples(
examples=[["hello", "EN -> ES"], ["thank you", "EN -> ES"], ["i am a student", "EN -> ES"], ["hola", "ES -> EN"], ["gracias", "ES -> EN"]],
inputs=[input_text, direction]
)
gr.Markdown("""<div style="background: rgba(255,255,255,0.1); border-radius: 10px; padding: 1em; color: white;"><b>Info del Modelo:</b><br>- Arquitectura: Seq2Seq LSTM<br>- BLEU Score: 0.90<br>- Parametros: 7,697,741</div>""")
translate_btn.click(fn=translate, inputs=[input_text, direction], outputs=output_text)
input_text.submit(fn=translate, inputs=[input_text, direction], outputs=output_text)
demo.launch()