Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -148,79 +148,78 @@ def chat(user_input, chat_history, current_step, user_context):
|
|
| 148 |
chat_history.append(("assistant", siguiente_pregunta))
|
| 149 |
else:
|
| 150 |
resultado = predict_disponibilidad(user_context)
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
)
|
| 177 |
-
|
| 178 |
-
# 📈 Predicción meteorológica
|
| 179 |
-
resumen_meteo = (
|
| 180 |
-
f"🌡️ *Temperatura esperada*: {clima['temperature']}°C\n"
|
| 181 |
-
f"☔ *Precipitación esperada*: {clima['precip']} mm"
|
| 182 |
-
)
|
| 183 |
-
|
| 184 |
-
# 🚲 Disponibilidad de estaciones
|
| 185 |
-
msg_estaciones = "🚲 *Estaciones más cercanas ordenadas por disponibilidad:*\n"
|
| 186 |
-
for r in resultado["candidatas"]:
|
| 187 |
-
emoji = "✅" if r["pred_pct"] >= resultado["target_pct"] else "⚠️"
|
| 188 |
-
msg_estaciones += (
|
| 189 |
-
f"{emoji} '{r['address']}' (ID {r['station_id']}): "
|
| 190 |
-
f"{round(r['pred_pct']*100)}% disponibilidad\n"
|
| 191 |
)
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
# 🧠 Resumen generado por LLM
|
| 199 |
-
resumen_llm = client.chat.completions.create(
|
| 200 |
-
messages=[
|
| 201 |
-
{
|
| 202 |
-
"role": "system",
|
| 203 |
-
"content": (
|
| 204 |
-
"Eres un asistente experto en movilidad urbana. Resume al usuario de forma clara y amigable "
|
| 205 |
-
"si podrá encontrar bicis disponibles, indicando las mejores estaciones según los datos meteorológicos "
|
| 206 |
-
"y el porcentaje mínimo requerido."
|
| 207 |
-
)
|
| 208 |
-
},
|
| 209 |
-
{"role": "user", "content": mensaje_final.strip()}
|
| 210 |
-
],
|
| 211 |
-
model="llama-3.3-70b-versatile",
|
| 212 |
-
temperature=0.5,
|
| 213 |
-
max_completion_tokens=256
|
| 214 |
-
).choices[0].message.content.strip()
|
| 215 |
-
|
| 216 |
-
# Añadir texto fijo al final del resumen
|
| 217 |
-
resumen_llm += "\n\nSi quieres hacer otra consulta, dime una nueva ubicación o escribe 'reiniciar'."
|
| 218 |
-
|
| 219 |
-
chat_history.append(("assistant", resumen_llm))
|
| 220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
return chat_history, current_step, user_context
|
| 223 |
|
|
|
|
| 224 |
# Interfaz Gradio
|
| 225 |
with gr.Blocks() as demo:
|
| 226 |
chatbot = gr.Chatbot()
|
|
|
|
| 148 |
chat_history.append(("assistant", siguiente_pregunta))
|
| 149 |
else:
|
| 150 |
resultado = predict_disponibilidad(user_context)
|
| 151 |
+
if "error" in resultado:
|
| 152 |
+
chat_history.append(("assistant", resultado["error"] + " Reiniciando conversación..."))
|
| 153 |
+
user_context = {
|
| 154 |
+
"ubicacion": None,
|
| 155 |
+
"month": None,
|
| 156 |
+
"day": None,
|
| 157 |
+
"hour": None,
|
| 158 |
+
"target_pct": None,
|
| 159 |
+
"temperature": None,
|
| 160 |
+
"lluvia": None
|
| 161 |
+
}
|
| 162 |
+
current_step = 0
|
| 163 |
+
chat_history.append(("assistant", preguntar_al_usuario(preguntas[0][1])))
|
| 164 |
+
return chat_history, current_step, user_context
|
| 165 |
+
else:
|
| 166 |
+
clima = resultado["candidatas"][0]
|
| 167 |
+
|
| 168 |
+
# 🧾 Resumen del contexto
|
| 169 |
+
fecha_str = f"{user_context['day']:02d}/{user_context['month']:02d}/2025"
|
| 170 |
+
hora_str = f"{user_context['hour']:02d}:00h"
|
| 171 |
+
resumen_contexto = (
|
| 172 |
+
f"📍 *Ubicación*: {user_context['ubicacion']}\n"
|
| 173 |
+
f"🗓️ *Día*: {fecha_str}\n"
|
| 174 |
+
f"🕒 *Hora*: {hora_str}\n"
|
| 175 |
+
f"🎯 *Porcentaje mínimo deseado de bicis*: {int(user_context['target_pct'] * 100)}%"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
)
|
| 177 |
|
| 178 |
+
# 📈 Predicción meteorológica
|
| 179 |
+
resumen_meteo = (
|
| 180 |
+
f"🌡️ *Temperatura esperada*: {clima['temperature']}°C\n"
|
| 181 |
+
f"☔ *Precipitación esperada*: {clima['precip']} mm"
|
| 182 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
+
# 🚲 Disponibilidad de estaciones
|
| 185 |
+
msg_estaciones = "🚲 *Estaciones más cercanas ordenadas por disponibilidad:*\n"
|
| 186 |
+
for r in resultado["candidatas"]:
|
| 187 |
+
emoji = "✅" if r["pred_pct"] >= resultado["target_pct"] else "⚠️"
|
| 188 |
+
msg_estaciones += (
|
| 189 |
+
f"{emoji} '{r['address']}' (ID {r['station_id']}): "
|
| 190 |
+
f"{round(r['pred_pct']*100)}% disponibilidad\n"
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
# Construir mensaje completo
|
| 194 |
+
mensaje_final = f"{resumen_contexto}\n\n{resumen_meteo}\n\n{msg_estaciones}"
|
| 195 |
+
chat_history.append(("assistant", mensaje_final.strip()))
|
| 196 |
+
|
| 197 |
+
# 🧠 Resumen generado por LLM
|
| 198 |
+
resumen_llm = client.chat.completions.create(
|
| 199 |
+
messages=[
|
| 200 |
+
{
|
| 201 |
+
"role": "system",
|
| 202 |
+
"content": (
|
| 203 |
+
"Eres un asistente experto en movilidad urbana. Resume al usuario de forma clara y amigable "
|
| 204 |
+
"si podrá encontrar bicis disponibles, indicando las mejores estaciones según los datos meteorológicos "
|
| 205 |
+
"y el porcentaje mínimo requerido."
|
| 206 |
+
)
|
| 207 |
+
},
|
| 208 |
+
{"role": "user", "content": mensaje_final.strip()}
|
| 209 |
+
],
|
| 210 |
+
model="llama-3.3-70b-versatile",
|
| 211 |
+
temperature=0.5,
|
| 212 |
+
max_completion_tokens=256
|
| 213 |
+
).choices[0].message.content.strip()
|
| 214 |
+
|
| 215 |
+
# Añadir texto fijo al final del resumen
|
| 216 |
+
resumen_llm += "\n\nSi quieres hacer otra consulta, dime una nueva ubicación o escribe 'reiniciar'."
|
| 217 |
+
|
| 218 |
+
chat_history.append(("assistant", resumen_llm))
|
| 219 |
|
| 220 |
return chat_history, current_step, user_context
|
| 221 |
|
| 222 |
+
|
| 223 |
# Interfaz Gradio
|
| 224 |
with gr.Blocks() as demo:
|
| 225 |
chatbot = gr.Chatbot()
|