doubleggg commited on
Commit
c83be26
·
verified ·
1 Parent(s): e5c4865

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +63 -7
app.py CHANGED
@@ -7,11 +7,14 @@ CACHE_MODELOS = {}
7
 
8
 
9
  def cargar_modelo(origen, destino):
 
10
  if origen == destino: return None
11
 
 
12
  nombre_repo = f"traductor_{origen}_{destino}"
13
  repo_id = f"{USUARIO_HF}/{nombre_repo}"
14
 
 
15
  if repo_id in CACHE_MODELOS:
16
  return CACHE_MODELOS[repo_id]
17
 
@@ -23,11 +26,12 @@ def cargar_modelo(origen, destino):
23
  CACHE_MODELOS[repo_id] = pipe
24
  return pipe
25
  except Exception as e:
26
- print(f"Error cargando {repo_id}: {e}")
27
  return None
28
 
29
 
30
  def logica_traduccion(texto, origen_nombre, destino_nombre):
 
31
  codigos = {"Español": "es", "Francés": "fr", "Ruso": "ru", "Rumano": "ro"}
32
  origen = codigos[origen_nombre]
33
  destino = codigos[destino_nombre]
@@ -36,22 +40,74 @@ def logica_traduccion(texto, origen_nombre, destino_nombre):
36
  if origen == destino: return texto
37
 
38
  try:
39
- # Lógica de Puente (Español)
40
  if origen == "es" or destino == "es":
41
  pipe = cargar_modelo(origen, destino)
42
- if not pipe: return f"⚠️ Error: No encontré el modelo {origen}->{destino}."
43
  return pipe(texto)[0]['translation_text']
 
 
44
  else:
45
- # Puente: Origen -> Español -> Destino
46
  pipe1 = cargar_modelo(origen, "es")
 
47
  intermedio = pipe1(texto)[0]['translation_text']
 
 
48
  pipe2 = cargar_modelo("es", destino)
 
49
  final = pipe2(intermedio)[0]['translation_text']
 
50
  return final
51
 
52
  except Exception as e:
53
- return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- # --- INTERFAZ GRÁFICA (DISEÑO CLÁSICO) ---
 
56
 
57
- # Solo forzamos la fuente para que el ruso
 
 
7
 
8
 
9
  def cargar_modelo(origen, destino):
10
+ # Si origen y destino son iguales, no hacemos nada
11
  if origen == destino: return None
12
 
13
+ # Definimos el nombre del repositorio
14
  nombre_repo = f"traductor_{origen}_{destino}"
15
  repo_id = f"{USUARIO_HF}/{nombre_repo}"
16
 
17
+ # Revisamos si ya está cargado en memoria RAM
18
  if repo_id in CACHE_MODELOS:
19
  return CACHE_MODELOS[repo_id]
20
 
 
26
  CACHE_MODELOS[repo_id] = pipe
27
  return pipe
28
  except Exception as e:
29
+ print(f"❌ Error cargando {repo_id}: {e}")
30
  return None
31
 
32
 
33
  def logica_traduccion(texto, origen_nombre, destino_nombre):
34
+ # Mapeo de nombres a códigos
35
  codigos = {"Español": "es", "Francés": "fr", "Ruso": "ru", "Rumano": "ro"}
36
  origen = codigos[origen_nombre]
37
  destino = codigos[destino_nombre]
 
40
  if origen == destino: return texto
41
 
42
  try:
43
+ # CASO 1: Traducción directa (si involucra Español)
44
  if origen == "es" or destino == "es":
45
  pipe = cargar_modelo(origen, destino)
46
+ if not pipe: return f"⚠️ Error: No existe el modelo {origen}->{destino}."
47
  return pipe(texto)[0]['translation_text']
48
+
49
+ # CASO 2: Puente (Origen -> Español -> Destino)
50
  else:
51
+ # Paso A: Origen -> Español
52
  pipe1 = cargar_modelo(origen, "es")
53
+ if not pipe1: return f"⚠️ Fallo en el paso 1 ({origen}->es)"
54
  intermedio = pipe1(texto)[0]['translation_text']
55
+
56
+ # Paso B: Español -> Destino
57
  pipe2 = cargar_modelo("es", destino)
58
+ if not pipe2: return f"⚠️ Fallo en el paso 2 (es->{destino})"
59
  final = pipe2(intermedio)[0]['translation_text']
60
+
61
  return final
62
 
63
  except Exception as e:
64
+ return f"Error en la traducción: {str(e)}"
65
+
66
+
67
+ # --- INTERFAZ GRÁFICA (DISEÑO CLÁSICO + ARREGLO RUSO) ---
68
+
69
+ # Este CSS arregla la fuente del Ruso manteniendo el diseño clásico
70
+ css_fix = """
71
+ textarea {
72
+ font-family: 'Noto Sans', 'Arial', sans-serif !important;
73
+ }
74
+ """
75
+
76
+ with gr.Blocks(css=css_fix, title="Traductor ESCOM") as demo:
77
+ gr.Markdown("# 🌍 Traductor Universal ESCOM")
78
+ gr.Markdown("Proyecto Final NLP | Ingeniería en Sistemas Computacionales")
79
+
80
+ with gr.Row():
81
+ # Columna Izquierda (Entrada)
82
+ with gr.Column():
83
+ menu_origen = gr.Dropdown(
84
+ ["Español", "Francés", "Ruso", "Rumano"],
85
+ label="Idioma de Origen",
86
+ value="Español"
87
+ )
88
+ entrada = gr.Textbox(
89
+ label="Texto a traducir",
90
+ placeholder="Escribe aquí tu frase...",
91
+ lines=5
92
+ )
93
+
94
+ # Columna Derecha (Salida)
95
+ with gr.Column():
96
+ menu_destino = gr.Dropdown(
97
+ ["Español", "Francés", "Ruso", "Rumano"],
98
+ label="Idioma de Destino",
99
+ value="Ruso"
100
+ )
101
+ salida = gr.Textbox(
102
+ label="Traducción",
103
+ lines=5,
104
+ interactive=False
105
+ )
106
+
107
+ boton = gr.Button("Traducir", variant="primary")
108
 
109
+ # Conexión del botón con la lógica
110
+ boton.click(fn=logica_traduccion, inputs=[entrada, menu_origen, menu_destino], outputs=salida)
111
 
112
+ if __name__ == "__main__":
113
+ demo.launch()