Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -499,18 +499,60 @@ Chat with the assistant to brainstorm your course idea. Use the panel on the lef
|
|
| 499 |
|
| 500 |
# Helper to refresh the sidebar session list. Returns an update for session_picker.
|
| 501 |
def refresh_sessions():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 502 |
chats = list_chats()
|
| 503 |
-
choices = [
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 508 |
|
| 509 |
# Load the selected chat into memory and return display pairs/history
|
| 510 |
-
def load_session(
|
| 511 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 512 |
return [], [], [], [], "", {}
|
| 513 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 514 |
history = []
|
| 515 |
pairs = []
|
| 516 |
buffer = []
|
|
|
|
| 499 |
|
| 500 |
# Helper to refresh the sidebar session list. Returns an update for session_picker.
|
| 501 |
def refresh_sessions():
|
| 502 |
+
"""Refresh the list of chat sessions for the sidebar.
|
| 503 |
+
|
| 504 |
+
Each chat will be displayed by its title (or "Untitled" if none) followed by a short
|
| 505 |
+
portion of its key in parentheses. The actual radio choice value will be this
|
| 506 |
+
formatted string so that we can extract the chat key later on when the user
|
| 507 |
+
selects a session. The currently active chat will be pre-selected.
|
| 508 |
+
|
| 509 |
+
Returns:
|
| 510 |
+
A gradio update for the ``session_picker`` radio component.
|
| 511 |
+
"""
|
| 512 |
chats = list_chats()
|
| 513 |
+
choices = []
|
| 514 |
+
current_label = None
|
| 515 |
+
current_key = state_chat_key.value
|
| 516 |
+
for c in chats:
|
| 517 |
+
title = c.get("title") or "Untitled"
|
| 518 |
+
key = c.get("key")
|
| 519 |
+
# Build a display label that includes a short portion of the key so it
|
| 520 |
+
# remains unique even if multiple chats share the same title.
|
| 521 |
+
label = f"{title} ({key[:8]})"
|
| 522 |
+
choices.append(label)
|
| 523 |
+
if key == current_key:
|
| 524 |
+
current_label = label
|
| 525 |
+
# If the current chat isn't found (e.g. after deletion), select the first chat
|
| 526 |
+
if not current_label and choices:
|
| 527 |
+
current_label = choices[0]
|
| 528 |
+
return gr.update(choices=choices, value=current_label)
|
| 529 |
|
| 530 |
# Load the selected chat into memory and return display pairs/history
|
| 531 |
+
def load_session(selected_value):
|
| 532 |
+
"""Load a chat session when selected in the sidebar.
|
| 533 |
+
|
| 534 |
+
The session_picker returns a formatted label that includes the chat key in
|
| 535 |
+
parentheses. Extract the chat key from this label and then load the
|
| 536 |
+
conversation messages from the database. The chat history and pair lists
|
| 537 |
+
are reconstructed for display in the Chatbot, while sources, plan and
|
| 538 |
+
resource_cache are reset.
|
| 539 |
+
|
| 540 |
+
Args:
|
| 541 |
+
selected_value: The radio selection value from session_picker, which is
|
| 542 |
+
a string like ``"Title (abcdef12)"``. If None, returns empty state.
|
| 543 |
+
|
| 544 |
+
Returns:
|
| 545 |
+
Tuple of (chatbot_pairs, chat_history, chat_pairs, sources, plan, resource_cache).
|
| 546 |
+
"""
|
| 547 |
+
if not selected_value:
|
| 548 |
return [], [], [], [], "", {}
|
| 549 |
+
# Extract the chat key from the label. It is enclosed in parentheses at the end.
|
| 550 |
+
key = selected_value
|
| 551 |
+
if "(" in selected_value and selected_value.endswith(")"):
|
| 552 |
+
key = selected_value[selected_value.rfind("(") + 1 : -1]
|
| 553 |
+
# Update the global state for the current chat key
|
| 554 |
+
state_chat_key.value = key
|
| 555 |
+
msgs = load_chat(key)
|
| 556 |
history = []
|
| 557 |
pairs = []
|
| 558 |
buffer = []
|