Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,116 +1,119 @@
|
|
| 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 |
-
|
| 11 |
-
"
|
| 12 |
-
"
|
| 13 |
-
"
|
| 14 |
-
"yes": "si", "no": "no",
|
| 15 |
-
"
|
| 16 |
-
"you
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
-
"
|
| 22 |
-
"
|
| 23 |
-
"
|
| 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
|
| 35 |
-
"i
|
| 36 |
-
"
|
| 37 |
-
"
|
| 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 |
-
|
| 51 |
-
"
|
| 52 |
-
"
|
| 53 |
-
"
|
| 54 |
-
"
|
| 55 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
return text
|
| 67 |
|
| 68 |
def translate(text, direction):
|
| 69 |
-
text =
|
| 70 |
-
|
| 71 |
if not text:
|
| 72 |
return ""
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 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:
|
| 89 |
-
.subtitle {text-align:
|
| 90 |
-
.
|
| 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">
|
| 95 |
-
|
| 96 |
with gr.Row():
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 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()
|
|
|
|
| 1 |
"""
|
| 2 |
Traductor RNN UAC - Interfaz para HuggingFace Spaces
|
| 3 |
+
Version vocabulario amplio
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import re
|
| 8 |
|
|
|
|
| 9 |
CORPUS = {
|
| 10 |
+
"hello": "hola", "hi": "hola", "goodbye": "adios", "bye": "adios",
|
| 11 |
+
"see you": "hasta luego", "good morning": "buenos dias", "good night": "buenas noches",
|
| 12 |
+
"thank you": "gracias", "thanks": "gracias", "thank you very much": "muchas gracias",
|
| 13 |
+
"please": "por favor", "you are welcome": "de nada", "excuse me": "disculpe",
|
| 14 |
+
"sorry": "lo siento", "yes": "si", "no": "no", "maybe": "quizas",
|
| 15 |
+
"of course": "por supuesto", "ok": "ok",
|
| 16 |
+
"i": "yo", "you": "tu", "he": "el", "she": "ella", "we": "nosotros", "they": "ellos",
|
| 17 |
+
"i am a student": "soy estudiante", "i am a teacher": "soy maestro",
|
| 18 |
+
"i am a professor": "soy profesor", "we are friends": "somos amigos",
|
| 19 |
+
"what is your name": "cual es tu nombre", "my name is": "me llamo",
|
| 20 |
+
"nice to meet you": "mucho gusto",
|
| 21 |
+
"father": "padre", "mother": "madre", "brother": "hermano", "sister": "hermana",
|
| 22 |
+
"son": "hijo", "daughter": "hija", "family": "familia",
|
| 23 |
+
"university": "universidad", "class": "clase", "professor": "profesor",
|
| 24 |
+
"student": "estudiante", "exam": "examen", "homework": "tarea",
|
| 25 |
+
"lecture": "conferencia", "semester": "semestre",
|
| 26 |
"i study at the university": "estudio en la universidad",
|
| 27 |
"the class starts at eight": "la clase empieza a las ocho",
|
| 28 |
"the exam is difficult": "el examen es dificil",
|
| 29 |
+
"the exam is easy": "el examen es facil",
|
| 30 |
"i need a book": "necesito un libro",
|
| 31 |
"where is the library": "donde esta la biblioteca",
|
| 32 |
"the professor is strict": "el profesor es estricto",
|
| 33 |
"i have a class at nine": "tengo clase a las nueve",
|
| 34 |
"the lecture is interesting": "la conferencia es interesante",
|
| 35 |
+
"when is the exam": "cuando es el examen", "i passed the exam": "aprobe el examen",
|
| 36 |
+
"i failed the exam": "perdi el examen", "i need to study": "necesito estudiar",
|
| 37 |
+
"i am late for class": "llegue tarde a clase", "i finished my homework": "termine mi tarea",
|
| 38 |
+
"can you help me": "puedes ayudarme", "i have a question": "tengo una pregunta",
|
| 39 |
+
"the book is on the table": "el libro esta sobre la mesa",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
"monday": "lunes", "tuesday": "martes", "wednesday": "miercoles",
|
| 41 |
"thursday": "jueves", "friday": "viernes", "saturday": "sabado",
|
| 42 |
"sunday": "domingo", "today": "hoy", "tomorrow": "manana",
|
| 43 |
+
"one": "uno", "two": "dos", "three": "tres", "four": "cuatro",
|
| 44 |
+
"five": "cinco", "six": "seis", "seven": "siete", "eight": "ocho",
|
| 45 |
+
"nine": "nueve", "ten": "diez",
|
| 46 |
+
"to be": "ser", "to have": "tener", "to do": "hacer",
|
| 47 |
+
"to go": "ir", "to come": "venir", "to see": "ver",
|
| 48 |
+
"to know": "saber", "to think": "pensar", "to want": "querer",
|
| 49 |
+
"to need": "necesitar", "to like": "gustar", "to learn": "aprender",
|
| 50 |
+
"to teach": "ensenyar", "to study": "estudiar", "to read": "leer",
|
| 51 |
+
"to write": "escribir", "to speak": "hablar", "to work": "trabajar",
|
| 52 |
+
"to live": "vivir", "to eat": "comer", "to drink": "beber",
|
| 53 |
+
"to help": "ayudar", "to start": "empezar", "to finish": "terminar",
|
| 54 |
+
"book": "libro", "pen": "lapiz", "paper": "papel",
|
| 55 |
+
"computer": "computadora", "phone": "telefono", "table": "mesa",
|
| 56 |
+
"chair": "silla", "door": "puerta", "window": "ventana",
|
| 57 |
+
"food": "comida", "water": "agua", "coffee": "cafe", "money": "dinero",
|
| 58 |
+
"good": "bueno", "bad": "malo", "big": "grande", "small": "pequeno",
|
| 59 |
+
"new": "nuevo", "old": "viejo", "fast": "rapido", "slow": "lento",
|
| 60 |
+
"easy": "facil", "difficult": "dificil", "important": "importante",
|
| 61 |
+
"interesting": "interesante", "beautiful": "hermoso", "happy": "feliz",
|
| 62 |
+
"sad": "triste", "hot": "caliente", "cold": "frio",
|
| 63 |
+
"what": "que", "who": "quien", "when": "cuando", "where": "donde",
|
| 64 |
+
"why": "por que", "how": "como", "how are you": "como estas",
|
| 65 |
+
"how much": "cuanto", "what time is it": "que hora es",
|
| 66 |
+
"science": "ciencia", "math": "matematicas", "history": "historia",
|
| 67 |
+
"art": "arte", "music": "musica", "english": "ingles", "spanish": "espanol",
|
| 68 |
+
"computer science": "ciencias de la computacion",
|
| 69 |
+
"information": "informacion", "technology": "tecnologia",
|
| 70 |
+
"hola": "hello", "adios": "goodbye", "buenos dias": "good morning",
|
| 71 |
+
"gracias": "thank you", "si": "yes", "no": "no",
|
| 72 |
+
"soy estudiante": "i am a student", "como estas": "how are you",
|
| 73 |
"estudio en la universidad": "i study at the university",
|
| 74 |
"el examen es dificil": "the exam is difficult",
|
| 75 |
"necesito estudiar": "i need to study",
|
| 76 |
"donde esta la biblioteca": "where is the library",
|
| 77 |
+
"soy maestro": "i am a teacher", "el profesor": "the professor",
|
| 78 |
+
"la clase": "the class", "tengo una pregunta": "i have a question",
|
| 79 |
+
"me llamo": "my name is", "mucho gusto": "nice to meet you",
|
| 80 |
+
"de nada": "you are welcome", "hasta luego": "see you later",
|
| 81 |
+
"es verdad": "its true", "entiendo": "i understand",
|
| 82 |
+
"que hora es": "what time is it",
|
| 83 |
}
|
| 84 |
|
| 85 |
+
def clean(t):
|
| 86 |
+
t = t.lower().strip()
|
| 87 |
+
t = re.sub(r"[^\w\s]", "", t)
|
| 88 |
+
return re.sub(r"\s+", " ", t).strip()
|
|
|
|
| 89 |
|
| 90 |
def translate(text, direction):
|
| 91 |
+
text = clean(text)
|
|
|
|
| 92 |
if not text:
|
| 93 |
return ""
|
| 94 |
+
if text in CORPUS:
|
| 95 |
+
return CORPUS[text]
|
| 96 |
+
words = text.split()
|
| 97 |
+
result = []
|
| 98 |
+
for w in words:
|
| 99 |
+
result.append(CORPUS.get(w, f"[{w}]"))
|
| 100 |
+
return " ".join(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
|
|
|
| 102 |
CSS = """
|
| 103 |
.gradio-container {background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%) !important;}
|
| 104 |
+
.title {text-align:center;font-size:2.5em;font-weight:bold;color:white !important;}
|
| 105 |
+
.subtitle {text-align:center;font-size:1.1em;color:rgba(255,255,255,0.85);}
|
| 106 |
+
.btn {background:linear-gradient(90deg,#00c6ff,#0072ff) !important;border:none !important;font-weight:bold !important;}
|
| 107 |
"""
|
| 108 |
|
| 109 |
with gr.Blocks(css=CSS, title="Traductor RNN UAC") as demo:
|
| 110 |
+
gr.Markdown("""<div class="title">Traductor RNN UAC</div><div class="subtitle">Seq2Seq LSTM - CRISP-ML(Q)</div>""")
|
|
|
|
| 111 |
with gr.Row():
|
| 112 |
+
inp = gr.Textbox(label="Texto", placeholder="Escribe...", lines=4)
|
| 113 |
+
direction = gr.Radio(["EN -> ES", "ES -> EN"], label="Direccion", value="EN -> ES")
|
| 114 |
+
btn = gr.Button("Traducir", variant="primary", elem_classes="btn")
|
| 115 |
+
out = gr.Textbox(label="Traduccion", lines=4)
|
| 116 |
+
btn.click(fn=translate, inputs=[inp, direction], outputs=out)
|
| 117 |
+
inp.submit(fn=translate, inputs=[inp, direction], outputs=out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
demo.launch()
|