Spaces:
Running
Running
fix: Repair departure Logic
Browse files- app.py +12 -7
- services/planner_service.py +38 -20
app.py
CHANGED
|
@@ -83,12 +83,17 @@ class LifeFlowAI:
|
|
| 83 |
|
| 84 |
elif evt_type == "complete":
|
| 85 |
tasks_html = self.service.generate_task_list_html(current_session)
|
| 86 |
-
date_str = datetime.now().strftime("%Y-%m-%d")
|
| 87 |
-
loc_str = "Selected Location"
|
| 88 |
-
if current_session.lat and current_session.lng:
|
| 89 |
-
loc_str = f"{current_session.lat:.2f}, {current_session.lng:.2f}"
|
| 90 |
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
chat_history = self._get_gradio_chat_history(current_session)
|
| 93 |
if not chat_history:
|
| 94 |
chat_history = [
|
|
@@ -305,7 +310,7 @@ class LifeFlowAI:
|
|
| 305 |
close_doc_btn.click(fn=lambda: gr.update(visible=False), outputs=[doc_modal])
|
| 306 |
theme_btn.click(fn=None, js="() => { document.querySelector('.gradio-container').classList.toggle('theme-dark'); }")
|
| 307 |
|
| 308 |
-
|
| 309 |
fn=self.analyze_wrapper,
|
| 310 |
inputs=[user_input, auto_loc, lat_in, lon_in, session_state],
|
| 311 |
outputs=[step1_container, step2_container, step3_container, s1_stream_output, task_list_box, task_summary_box, chatbot, session_state]
|
|
@@ -375,7 +380,7 @@ class LifeFlowAI:
|
|
| 375 |
def main():
|
| 376 |
app = LifeFlowAI()
|
| 377 |
demo = app.build_interface()
|
| 378 |
-
demo.launch(server_name="0.0.0.0", server_port=
|
| 379 |
#7860
|
| 380 |
if __name__ == "__main__":
|
| 381 |
main()
|
|
|
|
| 83 |
|
| 84 |
elif evt_type == "complete":
|
| 85 |
tasks_html = self.service.generate_task_list_html(current_session)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
date_str = event.get("start_time", "N/A")
|
| 88 |
+
high_priority = event.get("high_priority", "N/A")
|
| 89 |
+
total_time = event.get("total_time", "N/A")
|
| 90 |
+
loc_str = event.get("start_location", "N/A")
|
| 91 |
+
|
| 92 |
+
summary_html = create_summary_card(len(current_session.task_list),
|
| 93 |
+
high_priority,
|
| 94 |
+
total_time,
|
| 95 |
+
location=loc_str, date=date_str)
|
| 96 |
+
|
| 97 |
chat_history = self._get_gradio_chat_history(current_session)
|
| 98 |
if not chat_history:
|
| 99 |
chat_history = [
|
|
|
|
| 310 |
close_doc_btn.click(fn=lambda: gr.update(visible=False), outputs=[doc_modal])
|
| 311 |
theme_btn.click(fn=None, js="() => { document.querySelector('.gradio-container').classList.toggle('theme-dark'); }")
|
| 312 |
|
| 313 |
+
analyze_btn.click(
|
| 314 |
fn=self.analyze_wrapper,
|
| 315 |
inputs=[user_input, auto_loc, lat_in, lon_in, session_state],
|
| 316 |
outputs=[step1_container, step2_container, step3_container, s1_stream_output, task_list_box, task_summary_box, chatbot, session_state]
|
|
|
|
| 380 |
def main():
|
| 381 |
app = LifeFlowAI()
|
| 382 |
demo = app.build_interface()
|
| 383 |
+
demo.launch(server_name="0.0.0.0", server_port=8080, share=True, show_error=True)
|
| 384 |
#7860
|
| 385 |
if __name__ == "__main__":
|
| 386 |
main()
|
services/planner_service.py
CHANGED
|
@@ -313,21 +313,24 @@ class PlannerService:
|
|
| 313 |
# 解析 JSON 結果
|
| 314 |
json_data = "{" + accumulated_response.split("{", maxsplit=1)[-1]
|
| 315 |
json_data = json_data.replace("`", "").replace("@", "").replace("\\", " ").replace("\n", " ")
|
| 316 |
-
|
| 317 |
-
session.planner_agent.update_session_state(
|
| 318 |
-
session_id=session.session_id,
|
| 319 |
-
session_state_updates={"task_list": json_data}
|
| 320 |
-
)
|
| 321 |
-
|
| 322 |
try:
|
| 323 |
task_list_data = json.loads(json_data)
|
| 324 |
if task_list_data["global_info"]["start_location"].lower() == "user location":
|
| 325 |
-
|
| 326 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
|
| 328 |
session.task_list = self._convert_task_list_to_ui_format(task_list_data)
|
| 329 |
except Exception as e:
|
| 330 |
logger.error(f"Failed to parse task_list: {e}")
|
|
|
|
| 331 |
session.task_list = []
|
| 332 |
|
| 333 |
# 🛡️ 檢查 2: Planner 是否回傳空列表
|
|
@@ -343,14 +346,17 @@ class PlannerService:
|
|
| 343 |
}
|
| 344 |
return
|
| 345 |
|
| 346 |
-
|
|
|
|
| 347 |
yield {
|
| 348 |
"type": "complete",
|
| 349 |
"stream_text": "Analysis complete!",
|
| 350 |
-
"
|
| 351 |
-
"
|
|
|
|
|
|
|
| 352 |
"session": session,
|
| 353 |
-
"block_next_step": False
|
| 354 |
}
|
| 355 |
|
| 356 |
except Exception as e:
|
|
@@ -411,19 +417,31 @@ class PlannerService:
|
|
| 411 |
json_data = "{" + accumulated_response.split("{", maxsplit=1)[-1]
|
| 412 |
json_data = json_data.replace("`", "").replace("@", "").replace("\\", " ").replace("\n", " ")
|
| 413 |
|
| 414 |
-
session.planner_agent.update_session_state(
|
| 415 |
-
session_id=session.session_id,
|
| 416 |
-
session_state_updates={"task_list": json_data}
|
| 417 |
-
)
|
| 418 |
|
| 419 |
-
|
| 420 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
|
| 422 |
# 🔥 更新 Summary (回應 "Does summary still exist?" -> Yes!)
|
| 423 |
high_priority = sum(1 for t in session.task_list if t.get("priority") == "HIGH")
|
| 424 |
total_time = sum(int(t.get("duration", "0").split()[0]) for t in session.task_list if t.get("duration"))
|
| 425 |
-
|
| 426 |
-
|
|
|
|
|
|
|
| 427 |
|
| 428 |
done_message = session.chat_history[-1]["message"].replace(
|
| 429 |
"🤔 AI is thinking...",
|
|
|
|
| 313 |
# 解析 JSON 結果
|
| 314 |
json_data = "{" + accumulated_response.split("{", maxsplit=1)[-1]
|
| 315 |
json_data = json_data.replace("`", "").replace("@", "").replace("\\", " ").replace("\n", " ")
|
| 316 |
+
task_list_data = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
try:
|
| 318 |
task_list_data = json.loads(json_data)
|
| 319 |
if task_list_data["global_info"]["start_location"].lower() == "user location":
|
| 320 |
+
task_list_data["global_info"]["start_location"] = {
|
| 321 |
+
"lat": lat,
|
| 322 |
+
"lon": lon
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
session.planner_agent.update_session_state(
|
| 326 |
+
session_id=session.session_id,
|
| 327 |
+
session_state_updates={"task_list": json_data}
|
| 328 |
+
)
|
| 329 |
|
| 330 |
session.task_list = self._convert_task_list_to_ui_format(task_list_data)
|
| 331 |
except Exception as e:
|
| 332 |
logger.error(f"Failed to parse task_list: {e}")
|
| 333 |
+
print(json_data)
|
| 334 |
session.task_list = []
|
| 335 |
|
| 336 |
# 🛡️ 檢查 2: Planner 是否回傳空列表
|
|
|
|
| 346 |
}
|
| 347 |
return
|
| 348 |
|
| 349 |
+
high_priority = sum(1 for t in session.task_list if t.get("priority") == "HIGH")
|
| 350 |
+
total_time = sum(int(t.get("duration", "0").split()[0]) for t in session.task_list if t.get("duration"))
|
| 351 |
yield {
|
| 352 |
"type": "complete",
|
| 353 |
"stream_text": "Analysis complete!",
|
| 354 |
+
"start_location": task_list_data["global_info"].get("start_location", "N/A"),
|
| 355 |
+
"high_priority": high_priority,
|
| 356 |
+
"total_time": total_time,
|
| 357 |
+
"start_time": task_list_data["global_info"].get("departure_time", "N/A"),
|
| 358 |
"session": session,
|
| 359 |
+
"block_next_step": False
|
| 360 |
}
|
| 361 |
|
| 362 |
except Exception as e:
|
|
|
|
| 417 |
json_data = "{" + accumulated_response.split("{", maxsplit=1)[-1]
|
| 418 |
json_data = json_data.replace("`", "").replace("@", "").replace("\\", " ").replace("\n", " ")
|
| 419 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
|
| 421 |
+
try:
|
| 422 |
+
task_list_data = json.loads(json_data)
|
| 423 |
+
if task_list_data["global_info"]["start_location"].lower() == "user location":
|
| 424 |
+
task_list_data["global_info"]["start_location"] = {
|
| 425 |
+
"lat": session.lat,
|
| 426 |
+
"lon": session.lng
|
| 427 |
+
}
|
| 428 |
+
session.planner_agent.update_session_state(
|
| 429 |
+
session_id=session.session_id,
|
| 430 |
+
session_state_updates={"task_list": json_data}
|
| 431 |
+
)
|
| 432 |
+
session.task_list = self._convert_task_list_to_ui_format(task_list_data)
|
| 433 |
+
except Exception as e:
|
| 434 |
+
logger.error(f"Failed to parse modified task_list: {e}")
|
| 435 |
+
print(json_data)
|
| 436 |
+
raise e
|
| 437 |
|
| 438 |
# 🔥 更新 Summary (回應 "Does summary still exist?" -> Yes!)
|
| 439 |
high_priority = sum(1 for t in session.task_list if t.get("priority") == "HIGH")
|
| 440 |
total_time = sum(int(t.get("duration", "0").split()[0]) for t in session.task_list if t.get("duration"))
|
| 441 |
+
start_location = task_list_data["global_info"].get("start_location", "N/A")
|
| 442 |
+
date = task_list_data["global_info"].get("departure_time", "N/A"),
|
| 443 |
+
|
| 444 |
+
summary_html = create_summary_card(len(session.task_list), high_priority, total_time, start_location, date)
|
| 445 |
|
| 446 |
done_message = session.chat_history[-1]["message"].replace(
|
| 447 |
"🤔 AI is thinking...",
|