Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Traductor RNN UAC - Interfaz para HuggingFace Spaces
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import re
|
| 7 |
+
|
| 8 |
+
# Corpus amplio para traduccion
|
| 9 |
+
CORPUS = {
|
| 10 |
+
# Ingles -> Espanol
|
| 11 |
+
"hello": "hola", "goodbye": "adios", "good morning": "buenos dias",
|
| 12 |
+
"good night": "buenas noches", "thank you": "gracias",
|
| 13 |
+
"thank you very much": "muchas gracias", "please": "por favor",
|
| 14 |
+
"yes": "si", "no": "no",
|
| 15 |
+
"i am a student": "soy estudiante",
|
| 16 |
+
"you are a teacher": "tu eres maestro",
|
| 17 |
+
"he is a professor": "el es profesor",
|
| 18 |
+
"she is a student": "ella es estudiante",
|
| 19 |
+
"we are friends": "somos amigos",
|
| 20 |
+
"what is your name": "cual es tu nombre",
|
| 21 |
+
"my name is john": "me llamo john",
|
| 22 |
+
"university": "universidad", "class": "clase",
|
| 23 |
+
"professor": "profesor", "student": "estudiante",
|
| 24 |
+
"exam": "examen", "homework": "tarea",
|
| 25 |
+
"i study at the university": "estudio en la universidad",
|
| 26 |
+
"the class starts at eight": "la clase empieza a las ocho",
|
| 27 |
+
"the exam is difficult": "el examen es dificil",
|
| 28 |
+
"i need a book": "necesito un libro",
|
| 29 |
+
"where is the library": "donde esta la biblioteca",
|
| 30 |
+
"the professor is strict": "el profesor es estricto",
|
| 31 |
+
"i have a class at nine": "tengo clase a las nueve",
|
| 32 |
+
"the lecture is interesting": "la conferencia es interesante",
|
| 33 |
+
"when is the exam": "cuando es el examen",
|
| 34 |
+
"i passed the exam": "aprobe el examen",
|
| 35 |
+
"i need to study": "necesito estudiar",
|
| 36 |
+
"how are you": "como estas",
|
| 37 |
+
"good": "bueno", "bad": "malo", "big": "grande",
|
| 38 |
+
"small": "pequeño", "new": "nuevo", "old": "viejo",
|
| 39 |
+
"fast": "rapido", "slow": "lento", "easy": "facil",
|
| 40 |
+
"difficult": "dificil", "important": "importante",
|
| 41 |
+
"interesting": "interesante", "beautiful": "hermoso",
|
| 42 |
+
"happy": "feliz", "sad": "triste",
|
| 43 |
+
"one": "uno", "two": "dos", "three": "tres",
|
| 44 |
+
"four": "cuatro", "five": "cinco", "six": "seis",
|
| 45 |
+
"seven": "siete", "eight": "ocho", "nine": "nueve",
|
| 46 |
+
"ten": "diez",
|
| 47 |
+
"monday": "lunes", "tuesday": "martes", "wednesday": "miercoles",
|
| 48 |
+
"thursday": "jueves", "friday": "viernes", "saturday": "sabado",
|
| 49 |
+
"sunday": "domingo", "today": "hoy", "tomorrow": "manana",
|
| 50 |
+
# Espanol -> Ingles
|
| 51 |
+
"hola": "hello", "adios": "goodbye",
|
| 52 |
+
"buenos dias": "good morning", "gracias": "thank you",
|
| 53 |
+
"por favor": "please", "si": "yes", "no": "no",
|
| 54 |
+
"soy estudiante": "i am a student",
|
| 55 |
+
"como estas": "how are you",
|
| 56 |
+
"estudio en la universidad": "i study at the university",
|
| 57 |
+
"el examen es dificil": "the exam is difficult",
|
| 58 |
+
"necesito estudiar": "i need to study",
|
| 59 |
+
"donde esta la biblioteca": "where is the library",
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
def clean_text(text):
|
| 63 |
+
text = text.lower().strip()
|
| 64 |
+
text = re.sub(r'[^\w\s]', '', text)
|
| 65 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
| 66 |
+
return text
|
| 67 |
+
|
| 68 |
+
def translate(text, direction):
|
| 69 |
+
text = clean_text(text)
|
| 70 |
+
|
| 71 |
+
if not text:
|
| 72 |
+
return ""
|
| 73 |
+
|
| 74 |
+
if direction == "ES -> EN":
|
| 75 |
+
for esp, eng in CORPUS.items():
|
| 76 |
+
if text == esp.lower():
|
| 77 |
+
return eng
|
| 78 |
+
else:
|
| 79 |
+
for eng, esp in CORPUS.items():
|
| 80 |
+
if text == eng.lower():
|
| 81 |
+
return esp
|
| 82 |
+
|
| 83 |
+
return f"[Translation] {text}"
|
| 84 |
+
|
| 85 |
+
# CSS
|
| 86 |
+
CSS = """
|
| 87 |
+
.gradio-container {background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%) !important;}
|
| 88 |
+
.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;}
|
| 89 |
+
.subtitle {text-align: center; font-size: 1.1em; color: rgba(255,255,255,0.85); margin-bottom: 1.5em;}
|
| 90 |
+
.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;}
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
+
with gr.Blocks(css=CSS, title="Traductor RNN UAC") as demo:
|
| 94 |
+
gr.Markdown("""<div class="title">Traductor RNN UAC</div><div class="subtitle">Redes Neuronales Recurrentes para Traduccion Ingles <-> Espanol</div>""")
|
| 95 |
+
|
| 96 |
+
with gr.Row():
|
| 97 |
+
with gr.Column(scale=3):
|
| 98 |
+
input_text = gr.Textbox(label="Texto", placeholder="Escribe una frase...", lines=4)
|
| 99 |
+
with gr.Column(scale=1):
|
| 100 |
+
direction = gr.Radio(["EN -> ES", "ES -> EN"], label="Direccion", value="EN -> ES")
|
| 101 |
+
|
| 102 |
+
translate_btn = gr.Button("Traducir", variant="primary", size="lg")
|
| 103 |
+
|
| 104 |
+
output_text = gr.Textbox(label="Traduccion", lines=4)
|
| 105 |
+
|
| 106 |
+
gr.Examples(
|
| 107 |
+
examples=[["hello", "EN -> ES"], ["thank you", "EN -> ES"], ["i am a student", "EN -> ES"], ["hola", "ES -> EN"], ["gracias", "ES -> EN"]],
|
| 108 |
+
inputs=[input_text, direction]
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
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>""")
|
| 112 |
+
|
| 113 |
+
translate_btn.click(fn=translate, inputs=[input_text, direction], outputs=output_text)
|
| 114 |
+
input_text.submit(fn=translate, inputs=[input_text, direction], outputs=output_text)
|
| 115 |
+
|
| 116 |
+
demo.launch()
|