Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -146,40 +146,64 @@ def chat(user_input, chat_history, current_step, user_context):
|
|
146 |
if current_step < len(preguntas):
|
147 |
siguiente_pregunta = preguntar_al_usuario(preguntas[current_step][1])
|
148 |
chat_history.append(("assistant", siguiente_pregunta))
|
|
|
149 |
else:
|
150 |
resultado = predict_disponibilidad(user_context)
|
151 |
if "error" in resultado:
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
return chat_history, current_step, user_context
|
166 |
-
|
167 |
else:
|
168 |
clima = resultado["candidatas"][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
msg = (
|
170 |
-
f"
|
171 |
f"🌡️ Temperatura aprox.: {clima['temperature']}°C\n"
|
172 |
f"☔ Precipitación aprox.: {clima['precip']} mm\n\n"
|
173 |
-
f"
|
|
|
174 |
)
|
175 |
for r in resultado["candidatas"]:
|
176 |
emoji = "✅" if r["pred_pct"] >= resultado["target_pct"] else "⚠️"
|
177 |
msg += (
|
178 |
-
f"{emoji}
|
179 |
f"{round(r['pred_pct']*100)}% disponibilidad\n"
|
180 |
)
|
181 |
chat_history.append(("assistant", msg.strip()))
|
182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
return chat_history, current_step, user_context
|
184 |
|
185 |
# Interfaz Gradio
|
|
|
146 |
if current_step < len(preguntas):
|
147 |
siguiente_pregunta = preguntar_al_usuario(preguntas[current_step][1])
|
148 |
chat_history.append(("assistant", siguiente_pregunta))
|
149 |
+
else:
|
150 |
else:
|
151 |
resultado = predict_disponibilidad(user_context)
|
152 |
if "error" in resultado:
|
153 |
+
chat_history.append(("assistant", resultado["error"] + " Reiniciando conversación..."))
|
154 |
+
user_context = {
|
155 |
+
"ubicacion": None,
|
156 |
+
"month": None,
|
157 |
+
"day": None,
|
158 |
+
"hour": None,
|
159 |
+
"target_pct": None,
|
160 |
+
"temperature": None,
|
161 |
+
"lluvia": None
|
162 |
+
}
|
163 |
+
current_step = 0
|
164 |
+
chat_history.append(("assistant", preguntar_al_usuario(preguntas[0][1])))
|
165 |
+
return chat_history, current_step, user_context
|
|
|
|
|
166 |
else:
|
167 |
clima = resultado["candidatas"][0]
|
168 |
+
|
169 |
+
# Resumen del contexto
|
170 |
+
resumen_contexto = (
|
171 |
+
f"🗓️ Día: {user_context['day']:02d}/{user_context['month']:02d}/2025\n"
|
172 |
+
f"🕒 Hora: {user_context['hour']:02d}:00h\n"
|
173 |
+
f"📍 Ubicación: {user_context['ubicacion']}\n"
|
174 |
+
f"🎯 Porcentaje mínimo deseado de bicis: {int(user_context['target_pct'] * 100)}%"
|
175 |
+
)
|
176 |
+
|
177 |
+
# Mensaje principal
|
178 |
msg = (
|
179 |
+
f"📈 Predicción meteorológica:\n"
|
180 |
f"🌡️ Temperatura aprox.: {clima['temperature']}°C\n"
|
181 |
f"☔ Precipitación aprox.: {clima['precip']} mm\n\n"
|
182 |
+
f"{resumen_contexto}\n\n"
|
183 |
+
f"🚲 Estaciones ordenadas por disponibilidad:\n"
|
184 |
)
|
185 |
for r in resultado["candidatas"]:
|
186 |
emoji = "✅" if r["pred_pct"] >= resultado["target_pct"] else "⚠️"
|
187 |
msg += (
|
188 |
+
f"{emoji} '{r['address']}' (ID {r['station_id']}): "
|
189 |
f"{round(r['pred_pct']*100)}% disponibilidad\n"
|
190 |
)
|
191 |
chat_history.append(("assistant", msg.strip()))
|
192 |
|
193 |
+
# Generar resumen final con el LLM
|
194 |
+
resumen_llm = client.chat.completions.create(
|
195 |
+
messages=[
|
196 |
+
{"role": "system", "content": "Eres un asistente experto en movilidad urbana. Resume de forma clara y amigable si el usuario podrá encontrar bicis disponibles, y en qué estaciones, según los datos que se te dan."},
|
197 |
+
{"role": "user", "content": f"Aquí tienes el resultado del sistema:\n{msg.strip()}"}
|
198 |
+
],
|
199 |
+
model="llama-3.3-70b-versatile",
|
200 |
+
temperature=0.5,
|
201 |
+
max_completion_tokens=256
|
202 |
+
).choices[0].message.content.strip()
|
203 |
+
|
204 |
+
chat_history.append(("assistant", resumen_llm))
|
205 |
+
|
206 |
+
|
207 |
return chat_history, current_step, user_context
|
208 |
|
209 |
# Interfaz Gradio
|