Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -291,11 +291,11 @@ def _parse_ids(text, valid_ids):
|
|
| 291 |
def handle_start_journal(user_id):
|
| 292 |
blank = ([], [], False, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), [], "marking", "")
|
| 293 |
if not user_id:
|
| 294 |
-
return ([
|
| 295 |
tasks = get_tasks(user_id, only_today=True)
|
| 296 |
if not tasks:
|
| 297 |
msg = "You don't have any tasks logged for today. Add some tasks first, then come back to reflect!"
|
| 298 |
-
return ([
|
| 299 |
|
| 300 |
tasks_state = [{
|
| 301 |
"task_id": t["id"], "title": t["title"], "life_area": t.get("life_area",""),
|
|
@@ -320,7 +320,7 @@ def handle_start_journal(user_id):
|
|
| 320 |
f"You've done **{completed}/{total}** so far. Still open: {inc_str}. "
|
| 321 |
f"Tell me if you finished any more, or say **skip** to reflect.")
|
| 322 |
|
| 323 |
-
first_msg =
|
| 324 |
hist = [{"role": "assistant", "content": opener, "focus": "task_review"}]
|
| 325 |
return (
|
| 326 |
[first_msg], hist, True,
|
|
@@ -340,7 +340,7 @@ def handle_journal_message(user_id, user_text, audio_path, chat, hist, tasks_sta
|
|
| 340 |
user_text = transcribed
|
| 341 |
voice_used = True
|
| 342 |
except Exception as e:
|
| 343 |
-
err =
|
| 344 |
return list(chat) + [err], hist, "", None, tasks_state, active, phase, ""
|
| 345 |
|
| 346 |
if not active or not user_text.strip():
|
|
@@ -348,7 +348,7 @@ def handle_journal_message(user_id, user_text, audio_path, chat, hist, tasks_sta
|
|
| 348 |
|
| 349 |
# Show voice transcription as label if voice was used
|
| 350 |
display_text = f"π *{user_text}*" if voice_used else user_text
|
| 351 |
-
user_msg =
|
| 352 |
chat = list(chat) + [user_msg]
|
| 353 |
hist = list(hist) + [{"role": "user", "content": user_text}]
|
| 354 |
|
|
@@ -374,14 +374,14 @@ def handle_journal_message(user_id, user_text, audio_path, chat, hist, tasks_sta
|
|
| 374 |
ctx = _ensure_context(user_id)
|
| 375 |
opening = build_opening_question(ctx, tasks_state)
|
| 376 |
bridge = confirm + opening["question"]
|
| 377 |
-
ai_msg =
|
| 378 |
hist.append({"role": "assistant", "content": bridge, "focus": opening["question_focus"]})
|
| 379 |
return chat + [ai_msg], hist, "", None, tasks_state, active, "reflecting", ""
|
| 380 |
else:
|
| 381 |
remaining = [t for t in tasks_state if not t["completed"]]
|
| 382 |
rem_str = " | ".join(f"#{t['task_id']} {t['title']}" for t in remaining[:5])
|
| 383 |
follow = confirm + f"Still pending: {rem_str}\n\nAnything else you finished? Or say **skip** to start reflecting."
|
| 384 |
-
ai_msg =
|
| 385 |
hist.append({"role": "assistant", "content": follow, "focus": "task_marking"})
|
| 386 |
return chat + [ai_msg], hist, "", None, tasks_state, active, "marking", ""
|
| 387 |
|
|
@@ -391,34 +391,34 @@ def handle_journal_message(user_id, user_text, audio_path, chat, hist, tasks_sta
|
|
| 391 |
ctx = _ensure_context(user_id)
|
| 392 |
result = get_next_journal_question(ctx, tasks_state, hist)
|
| 393 |
except Exception as e:
|
| 394 |
-
err_msg =
|
| 395 |
return chat + [err_msg], hist, "", None, tasks_state, active, phase, ""
|
| 396 |
|
| 397 |
if result.get("session_complete"):
|
| 398 |
closing = "That's a solid reflection β you've given me plenty to work with. π§ Hit **Finish & Save** to lock in your insights!"
|
| 399 |
-
ai_msg =
|
| 400 |
hist.append({"role": "assistant", "content": closing, "focus": "complete"})
|
| 401 |
return chat + [ai_msg], hist, "", None, tasks_state, active, "done", _ok("Session complete β click Finish & Save")
|
| 402 |
|
| 403 |
next_q = result.get("question") or "Anything else you'd like to add about today?"
|
| 404 |
-
ai_msg =
|
| 405 |
hist.append({"role": "assistant", "content": next_q, "focus": result.get("question_focus","")})
|
| 406 |
return chat + [ai_msg], hist, "", None, tasks_state, active, "reflecting", ""
|
| 407 |
|
| 408 |
# ββ Phase: done βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 409 |
-
nudge =
|
| 410 |
return chat + [nudge], hist, "", None, tasks_state, active, "done", ""
|
| 411 |
|
| 412 |
|
| 413 |
def handle_finish_journal(user_id, chat, hist, tasks_state):
|
| 414 |
if not user_id or not hist:
|
| 415 |
-
return list(chat) + [
|
| 416 |
try:
|
| 417 |
ctx = _ensure_context(user_id)
|
| 418 |
updated = synthesize_journal(ctx, tasks_state, hist)
|
| 419 |
save_user_context(user_id, updated)
|
| 420 |
except Exception as e:
|
| 421 |
-
return list(chat) + [
|
| 422 |
|
| 423 |
total = len(tasks_state)
|
| 424 |
done = sum(1 for t in tasks_state if t.get("completed"))
|
|
@@ -430,7 +430,7 @@ def handle_finish_journal(user_id, chat, hist, tasks_state):
|
|
| 430 |
f"**What I learned:**\n{notes_md}\n\n"
|
| 431 |
f"I'll use this to make tomorrow's schedule smarter. Great work today! π"
|
| 432 |
)
|
| 433 |
-
return list(chat) + [
|
| 434 |
|
| 435 |
|
| 436 |
def handle_restart_journal():
|
|
@@ -683,10 +683,7 @@ with gr.Blocks(title="π§ Second Brain") as demo:
|
|
| 683 |
# ββ TAB 3: JOURNAL βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 684 |
with gr.Tab("π Journal"):
|
| 685 |
gr.HTML('<p style="color:#475569;font-size:13px;margin-bottom:12px">End-of-day reflection. Chat naturally β the AI marks tasks, asks questions, and updates your profile.</p>')
|
| 686 |
-
journal_chatbot = gr.Chatbot(
|
| 687 |
-
label="", height=440, type="messages",
|
| 688 |
-
placeholder="<p style='color:#334155;text-align:center;padding-top:80px'>Click <b>βΆ Start Session</b> to begin</p>",
|
| 689 |
-
)
|
| 690 |
with gr.Row():
|
| 691 |
j_answer = gr.Textbox(label="", placeholder="Type or use the micβ¦", lines=1,
|
| 692 |
scale=5, interactive=False, show_label=False)
|
|
|
|
| 291 |
def handle_start_journal(user_id):
|
| 292 |
blank = ([], [], False, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False), [], "marking", "")
|
| 293 |
if not user_id:
|
| 294 |
+
return ([(None, "β Please sign in first.")],) + blank[1:]
|
| 295 |
tasks = get_tasks(user_id, only_today=True)
|
| 296 |
if not tasks:
|
| 297 |
msg = "You don't have any tasks logged for today. Add some tasks first, then come back to reflect!"
|
| 298 |
+
return ([(None, msg)],) + blank[1:]
|
| 299 |
|
| 300 |
tasks_state = [{
|
| 301 |
"task_id": t["id"], "title": t["title"], "life_area": t.get("life_area",""),
|
|
|
|
| 320 |
f"You've done **{completed}/{total}** so far. Still open: {inc_str}. "
|
| 321 |
f"Tell me if you finished any more, or say **skip** to reflect.")
|
| 322 |
|
| 323 |
+
first_msg = (None, opener)
|
| 324 |
hist = [{"role": "assistant", "content": opener, "focus": "task_review"}]
|
| 325 |
return (
|
| 326 |
[first_msg], hist, True,
|
|
|
|
| 340 |
user_text = transcribed
|
| 341 |
voice_used = True
|
| 342 |
except Exception as e:
|
| 343 |
+
err = (None, f"β Couldn't transcribe audio: {e}. Please type instead.")
|
| 344 |
return list(chat) + [err], hist, "", None, tasks_state, active, phase, ""
|
| 345 |
|
| 346 |
if not active or not user_text.strip():
|
|
|
|
| 348 |
|
| 349 |
# Show voice transcription as label if voice was used
|
| 350 |
display_text = f"π *{user_text}*" if voice_used else user_text
|
| 351 |
+
user_msg = (display_text, None)
|
| 352 |
chat = list(chat) + [user_msg]
|
| 353 |
hist = list(hist) + [{"role": "user", "content": user_text}]
|
| 354 |
|
|
|
|
| 374 |
ctx = _ensure_context(user_id)
|
| 375 |
opening = build_opening_question(ctx, tasks_state)
|
| 376 |
bridge = confirm + opening["question"]
|
| 377 |
+
ai_msg = (None, bridge)
|
| 378 |
hist.append({"role": "assistant", "content": bridge, "focus": opening["question_focus"]})
|
| 379 |
return chat + [ai_msg], hist, "", None, tasks_state, active, "reflecting", ""
|
| 380 |
else:
|
| 381 |
remaining = [t for t in tasks_state if not t["completed"]]
|
| 382 |
rem_str = " | ".join(f"#{t['task_id']} {t['title']}" for t in remaining[:5])
|
| 383 |
follow = confirm + f"Still pending: {rem_str}\n\nAnything else you finished? Or say **skip** to start reflecting."
|
| 384 |
+
ai_msg = (None, follow)
|
| 385 |
hist.append({"role": "assistant", "content": follow, "focus": "task_marking"})
|
| 386 |
return chat + [ai_msg], hist, "", None, tasks_state, active, "marking", ""
|
| 387 |
|
|
|
|
| 391 |
ctx = _ensure_context(user_id)
|
| 392 |
result = get_next_journal_question(ctx, tasks_state, hist)
|
| 393 |
except Exception as e:
|
| 394 |
+
err_msg = (None, f"β AI error: {e}. Try sending again.")
|
| 395 |
return chat + [err_msg], hist, "", None, tasks_state, active, phase, ""
|
| 396 |
|
| 397 |
if result.get("session_complete"):
|
| 398 |
closing = "That's a solid reflection β you've given me plenty to work with. π§ Hit **Finish & Save** to lock in your insights!"
|
| 399 |
+
ai_msg = (None, closing)
|
| 400 |
hist.append({"role": "assistant", "content": closing, "focus": "complete"})
|
| 401 |
return chat + [ai_msg], hist, "", None, tasks_state, active, "done", _ok("Session complete β click Finish & Save")
|
| 402 |
|
| 403 |
next_q = result.get("question") or "Anything else you'd like to add about today?"
|
| 404 |
+
ai_msg = (None, next_q)
|
| 405 |
hist.append({"role": "assistant", "content": next_q, "focus": result.get("question_focus","")})
|
| 406 |
return chat + [ai_msg], hist, "", None, tasks_state, active, "reflecting", ""
|
| 407 |
|
| 408 |
# ββ Phase: done βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 409 |
+
nudge = (None, "Session is complete! Click **Finish & Save** to save your insights.")
|
| 410 |
return chat + [nudge], hist, "", None, tasks_state, active, "done", ""
|
| 411 |
|
| 412 |
|
| 413 |
def handle_finish_journal(user_id, chat, hist, tasks_state):
|
| 414 |
if not user_id or not hist:
|
| 415 |
+
return list(chat) + [(None, "β Nothing to save yet.")], ""
|
| 416 |
try:
|
| 417 |
ctx = _ensure_context(user_id)
|
| 418 |
updated = synthesize_journal(ctx, tasks_state, hist)
|
| 419 |
save_user_context(user_id, updated)
|
| 420 |
except Exception as e:
|
| 421 |
+
return list(chat) + [(None, f"β Save failed: {e}")], _err("Save failed")
|
| 422 |
|
| 423 |
total = len(tasks_state)
|
| 424 |
done = sum(1 for t in tasks_state if t.get("completed"))
|
|
|
|
| 430 |
f"**What I learned:**\n{notes_md}\n\n"
|
| 431 |
f"I'll use this to make tomorrow's schedule smarter. Great work today! π"
|
| 432 |
)
|
| 433 |
+
return list(chat) + [(None, summary)], _ok(f"Saved! {done}/{total} tasks ({rate}%)")
|
| 434 |
|
| 435 |
|
| 436 |
def handle_restart_journal():
|
|
|
|
| 683 |
# ββ TAB 3: JOURNAL βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 684 |
with gr.Tab("π Journal"):
|
| 685 |
gr.HTML('<p style="color:#475569;font-size:13px;margin-bottom:12px">End-of-day reflection. Chat naturally β the AI marks tasks, asks questions, and updates your profile.</p>')
|
| 686 |
+
journal_chatbot = gr.Chatbot(label="", height=440)
|
|
|
|
|
|
|
|
|
|
| 687 |
with gr.Row():
|
| 688 |
j_answer = gr.Textbox(label="", placeholder="Type or use the micβ¦", lines=1,
|
| 689 |
scale=5, interactive=False, show_label=False)
|