bentosmau commited on
Commit
1b711ea
·
1 Parent(s): 7e37a85

Update chat to use the latest Gradio message format

Browse files

Modify `app.py` and `matematicas.py` to conform to Gradio 6.x message format, updating historical data structures and message handling logic.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e3ff2484-bbd8-4aba-bea0-1940769b874a
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: 4d298101-0a40-4135-a51a-1ae9661f9c06
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1739408b-93a5-479b-a658-30f2493b0467/e3ff2484-bbd8-4aba-bea0-1940769b874a/eHvZpi8
Replit-Helium-Checkpoint-Created: true

attached_assets/Captura_de_pantalla_2026-04-14_181337_1776201235750.png ADDED
chat-app/app.py CHANGED
@@ -116,13 +116,22 @@ def detectar_roblox(mensaje):
116
  def modo_calculadora_activo(historial):
117
  if not historial:
118
  return False
119
- ultima_respuesta = (historial[-1][1] or "").lower()
120
- return "calculadora neo-1" in ultima_respuesta or "aquí tienes nuestra calculadora" in ultima_respuesta
 
 
 
 
 
 
 
 
 
121
 
122
  def stream_texto(texto, historial):
123
  CHUNK = 8
124
  for i in range(0, len(texto), CHUNK):
125
- historial[-1][1] += texto[i:i+CHUNK]
126
  time.sleep(0.02)
127
  yield historial
128
 
@@ -140,13 +149,13 @@ def responder(mensaje, historial):
140
  "- `2 ** 8` (potencia)\n- `144 ** 0.5` (raíz cuadrada)\n\n"
141
  "_Escribe tu operación o di 'salir de calculadora' para volver._"
142
  )
143
- historial = historial + [[mensaje, ""]]
144
  for h in stream_texto(saludo, historial):
145
  yield h, ""
146
  return
147
 
148
  if texto in ("salir de calculadora", "salir calculadora", "cerrar calculadora", "volver al chat"):
149
- historial = historial + [[mensaje, "De acuerdo, volviendo al chat normal. ¡Pregúntame lo que quieras! 😊"]]
150
  yield historial, ""
151
  return
152
 
@@ -154,7 +163,7 @@ def responder(mensaje, historial):
154
  resultado = resolver_operacion(mensaje)
155
  if resultado is not None:
156
  respuesta = formatear_resultado(mensaje, resultado)
157
- historial = historial + [[mensaje, ""]]
158
  for h in stream_texto(respuesta, historial):
159
  yield h, ""
160
  return
@@ -162,16 +171,16 @@ def responder(mensaje, historial):
162
  tipo_roblox, nombre_roblox = detectar_roblox(mensaje)
163
 
164
  if tipo_roblox == "jugador":
165
- historial = historial + [[mensaje, "🔍 Buscando jugador en Roblox..."]]
166
  yield historial, ""
167
  datos = buscar_jugador(nombre_roblox)
168
  resultado = formatear_jugador(datos)
169
- historial[-1][1] = resultado
170
  yield historial, ""
171
  return
172
 
173
  if tipo_roblox == "juego":
174
- historial = historial + [[mensaje, "🔍 Buscando juego en Roblox..."]]
175
  yield historial, ""
176
  datos = buscar_juego(nombre_roblox)
177
  resultado = formatear_juego(datos)
@@ -180,18 +189,18 @@ def responder(mensaje, historial):
180
  explicacion = generar_explicacion_juego(datos)
181
  resultado = resultado + "\n\n💡 **¿De qué trata el juego?**\n" + explicacion
182
 
183
- historial[-1][1] = resultado
184
  yield historial, ""
185
  return
186
 
187
  respuesta_personalizada = buscar_respuesta_personalizada(mensaje)
188
  if respuesta_personalizada:
189
- historial = historial + [[mensaje, ""]]
190
  for h in stream_texto(respuesta_personalizada, historial):
191
  yield h, ""
192
  return
193
 
194
- historial = historial + [[mensaje, "🤖 NEO-1 aún no tiene una respuesta para eso. Puedes enseñarle más temas agregando respuestas al modelo."]]
195
  yield historial, ""
196
 
197
  with gr.Blocks(title="mdfjbots-neo-1") as demo:
 
116
  def modo_calculadora_activo(historial):
117
  if not historial:
118
  return False
119
+ for msg in reversed(historial):
120
+ if msg.get("role") == "assistant":
121
+ texto = (msg.get("content") or "").lower()
122
+ return "calculadora neo-1" in texto or "aquí tienes nuestra calculadora" in texto
123
+ return False
124
+
125
+ def añadir_turno(historial, user_msg, bot_msg=""):
126
+ return historial + [
127
+ {"role": "user", "content": user_msg},
128
+ {"role": "assistant", "content": bot_msg},
129
+ ]
130
 
131
  def stream_texto(texto, historial):
132
  CHUNK = 8
133
  for i in range(0, len(texto), CHUNK):
134
+ historial[-1]["content"] += texto[i:i+CHUNK]
135
  time.sleep(0.02)
136
  yield historial
137
 
 
149
  "- `2 ** 8` (potencia)\n- `144 ** 0.5` (raíz cuadrada)\n\n"
150
  "_Escribe tu operación o di 'salir de calculadora' para volver._"
151
  )
152
+ historial = añadir_turno(historial, mensaje)
153
  for h in stream_texto(saludo, historial):
154
  yield h, ""
155
  return
156
 
157
  if texto in ("salir de calculadora", "salir calculadora", "cerrar calculadora", "volver al chat"):
158
+ historial = añadir_turno(historial, mensaje, "De acuerdo, volviendo al chat normal. ¡Pregúntame lo que quieras! 😊")
159
  yield historial, ""
160
  return
161
 
 
163
  resultado = resolver_operacion(mensaje)
164
  if resultado is not None:
165
  respuesta = formatear_resultado(mensaje, resultado)
166
+ historial = añadir_turno(historial, mensaje)
167
  for h in stream_texto(respuesta, historial):
168
  yield h, ""
169
  return
 
171
  tipo_roblox, nombre_roblox = detectar_roblox(mensaje)
172
 
173
  if tipo_roblox == "jugador":
174
+ historial = añadir_turno(historial, mensaje, "🔍 Buscando jugador en Roblox...")
175
  yield historial, ""
176
  datos = buscar_jugador(nombre_roblox)
177
  resultado = formatear_jugador(datos)
178
+ historial[-1]["content"] = resultado
179
  yield historial, ""
180
  return
181
 
182
  if tipo_roblox == "juego":
183
+ historial = añadir_turno(historial, mensaje, "🔍 Buscando juego en Roblox...")
184
  yield historial, ""
185
  datos = buscar_juego(nombre_roblox)
186
  resultado = formatear_juego(datos)
 
189
  explicacion = generar_explicacion_juego(datos)
190
  resultado = resultado + "\n\n💡 **¿De qué trata el juego?**\n" + explicacion
191
 
192
+ historial[-1]["content"] = resultado
193
  yield historial, ""
194
  return
195
 
196
  respuesta_personalizada = buscar_respuesta_personalizada(mensaje)
197
  if respuesta_personalizada:
198
+ historial = añadir_turno(historial, mensaje)
199
  for h in stream_texto(respuesta_personalizada, historial):
200
  yield h, ""
201
  return
202
 
203
+ historial = añadir_turno(historial, mensaje, "🤖 NEO-1 aún no tiene una respuesta para eso. Puedes enseñarle más temas agregando respuestas al modelo.")
204
  yield historial, ""
205
 
206
  with gr.Blocks(title="mdfjbots-neo-1") as demo:
chat-app/matematicas.py CHANGED
@@ -79,8 +79,13 @@ def extraer_nombre_usuario(historial):
79
  r"llámame\s+([A-Za-záéíóúÁÉÍÓÚñÑ]+)",
80
  r"llamame\s+([A-Za-záéíóúÁÉÍÓÚñÑ]+)",
81
  ]
82
- for turno in historial:
83
- mensaje_usuario = (turno[0] or "").lower()
 
 
 
 
 
84
  for patron in patrones_nombre:
85
  m = re.search(patron, mensaje_usuario)
86
  if m:
 
79
  r"llámame\s+([A-Za-záéíóúÁÉÍÓÚñÑ]+)",
80
  r"llamame\s+([A-Za-záéíóúÁÉÍÓÚñÑ]+)",
81
  ]
82
+ for msg in historial:
83
+ if isinstance(msg, dict):
84
+ if msg.get("role") != "user":
85
+ continue
86
+ mensaje_usuario = (msg.get("content") or "").lower()
87
+ else:
88
+ mensaje_usuario = (msg[0] or "").lower()
89
  for patron in patrones_nombre:
90
  m = re.search(patron, mensaje_usuario)
91
  if m: