bentosmau commited on
Commit ·
7e37a85
1
Parent(s): 1adfa7f
Improve chat response streaming and enable queuing for better interactivity
Browse filesIntroduce a streaming helper function for chunked text output, refactor existing response generation to use this new function, and enable Gradio's queueing mechanism to ensure proper streaming of responses.
Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e3ff2484-bbd8-4aba-bea0-1940769b874a
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: 0c4ee636-825b-41a5-8412-23686f658b7d
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
- chat-app/app.py +26 -13
chat-app/app.py
CHANGED
|
@@ -119,17 +119,30 @@ def modo_calculadora_activo(historial):
|
|
| 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 responder(mensaje, historial):
|
| 123 |
texto = mensaje.strip().lower()
|
| 124 |
|
| 125 |
if es_solicitud_calculadora(mensaje):
|
| 126 |
nombre = extraer_nombre_usuario(historial)
|
| 127 |
-
saludo =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
historial = historial + [[mensaje, ""]]
|
| 129 |
-
for
|
| 130 |
-
|
| 131 |
-
time.sleep(0.005)
|
| 132 |
-
yield historial, ""
|
| 133 |
return
|
| 134 |
|
| 135 |
if texto in ("salir de calculadora", "salir calculadora", "cerrar calculadora", "volver al chat"):
|
|
@@ -142,10 +155,8 @@ def responder(mensaje, historial):
|
|
| 142 |
if resultado is not None:
|
| 143 |
respuesta = formatear_resultado(mensaje, resultado)
|
| 144 |
historial = historial + [[mensaje, ""]]
|
| 145 |
-
for
|
| 146 |
-
|
| 147 |
-
time.sleep(0.005)
|
| 148 |
-
yield historial, ""
|
| 149 |
return
|
| 150 |
|
| 151 |
tipo_roblox, nombre_roblox = detectar_roblox(mensaje)
|
|
@@ -176,10 +187,8 @@ def responder(mensaje, historial):
|
|
| 176 |
respuesta_personalizada = buscar_respuesta_personalizada(mensaje)
|
| 177 |
if respuesta_personalizada:
|
| 178 |
historial = historial + [[mensaje, ""]]
|
| 179 |
-
for
|
| 180 |
-
|
| 181 |
-
time.sleep(0.008)
|
| 182 |
-
yield historial, ""
|
| 183 |
return
|
| 184 |
|
| 185 |
historial = historial + [[mensaje, "🤖 NEO-1 aún no tiene una respuesta para eso. Puedes enseñarle más temas agregando respuestas al modelo."]]
|
|
@@ -218,6 +227,9 @@ with gr.Blocks(title="mdfjbots-neo-1") as demo:
|
|
| 218 |
"buscar jugador Builderman",
|
| 219 |
"buscar juego Adopt Me",
|
| 220 |
"definición de historia",
|
|
|
|
|
|
|
|
|
|
| 221 |
],
|
| 222 |
inputs=entrada,
|
| 223 |
label="Ejemplos de preguntas",
|
|
@@ -242,6 +254,7 @@ if __name__ == "__main__":
|
|
| 242 |
port = int(os.environ.get("PORT", 5000))
|
| 243 |
dev_domain = os.environ.get("REPLIT_DEV_DOMAIN", "")
|
| 244 |
root_path = f"https://{dev_domain}/__neo1" if dev_domain else ""
|
|
|
|
| 245 |
demo.launch(
|
| 246 |
server_name="0.0.0.0",
|
| 247 |
server_port=port,
|
|
|
|
| 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 |
+
|
| 129 |
def responder(mensaje, historial):
|
| 130 |
texto = mensaje.strip().lower()
|
| 131 |
|
| 132 |
if es_solicitud_calculadora(mensaje):
|
| 133 |
nombre = extraer_nombre_usuario(historial)
|
| 134 |
+
saludo = (
|
| 135 |
+
f"Claro 😀, {nombre} aquí tienes nuestra calculadora:\n\n"
|
| 136 |
+
"🧮 **Calculadora Virtual NEO-1**\n"
|
| 137 |
+
"Escribe cualquier operación matemática y la resolveré al instante.\n\n"
|
| 138 |
+
"**Ejemplos:**\n"
|
| 139 |
+
"- `5 + 3`\n- `12 * 7`\n- `100 / 4`\n"
|
| 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"):
|
|
|
|
| 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
|
| 161 |
|
| 162 |
tipo_roblox, nombre_roblox = detectar_roblox(mensaje)
|
|
|
|
| 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."]]
|
|
|
|
| 227 |
"buscar jugador Builderman",
|
| 228 |
"buscar juego Adopt Me",
|
| 229 |
"definición de historia",
|
| 230 |
+
"calculadora",
|
| 231 |
+
"que es linux",
|
| 232 |
+
"guerra de corea",
|
| 233 |
],
|
| 234 |
inputs=entrada,
|
| 235 |
label="Ejemplos de preguntas",
|
|
|
|
| 254 |
port = int(os.environ.get("PORT", 5000))
|
| 255 |
dev_domain = os.environ.get("REPLIT_DEV_DOMAIN", "")
|
| 256 |
root_path = f"https://{dev_domain}/__neo1" if dev_domain else ""
|
| 257 |
+
demo.queue()
|
| 258 |
demo.launch(
|
| 259 |
server_name="0.0.0.0",
|
| 260 |
server_port=port,
|