Prof-Reza commited on
Commit
a9d92ec
·
verified ·
1 Parent(s): 67e0a60

Refactor session picker to radio with label and value pairs; fix rename and delete reliability.

Browse files

Switch the session picker back to a Radio component using (label, value) pairs. Each entry shows the chat title and part of its key, but the radio's value is the full key. refresh_sessions builds these pairs and selects the active key. load_session now directly receives the selected key. This ensures that renaming a chat updates the sidebar correctly and deleting a chat switches to the next one without issues.

Files changed (1) hide show
  1. app.py +35 -30
app.py CHANGED
@@ -478,12 +478,21 @@ Chat with the assistant to brainstorm your course idea. Use the panel on the lef
478
 
479
  # Define layout with a sidebar for sessions and a main panel for chat
480
  with gr.Row():
 
 
 
 
481
  with gr.Column(scale=1, min_width=220):
482
- session_picker = gr.Radio(label="Your Chats", choices=[], interactive=True)
 
 
 
 
483
  new_chat_btn = gr.Button("New Chat")
484
  rename_input = gr.Textbox(label="Rename chat", placeholder="New title", lines=1)
485
  rename_btn = gr.Button("Rename")
486
  delete_btn = gr.Button("Delete Chat")
 
487
  with gr.Column(scale=4):
488
  chatbot = gr.Chatbot(label="Conversation", height=400)
489
  msg_input = gr.Textbox(
@@ -502,57 +511,53 @@ Chat with the assistant to brainstorm your course idea. Use the panel on the lef
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 = []
 
478
 
479
  # Define layout with a sidebar for sessions and a main panel for chat
480
  with gr.Row():
481
+ # Sidebar: manage chat sessions. We use a Dropdown instead of a Radio to allow custom
482
+ # labels separate from the underlying chat keys. Each option will display the
483
+ # chat title with a short portion of its key, but the actual value will be the
484
+ # chat key itself. This makes renaming and deleting sessions more reliable.
485
  with gr.Column(scale=1, min_width=220):
486
+ # Use a Radio component for session selection. Each choice will be a tuple
487
+ # (label, value) where label is the human-readable title and value is
488
+ # the underlying chat key. The value returned when a choice is
489
+ # selected will be the chat key, which we can use to load the session.
490
+ session_picker = gr.Radio(label="Your Chats", choices=[], value=None, interactive=True)
491
  new_chat_btn = gr.Button("New Chat")
492
  rename_input = gr.Textbox(label="Rename chat", placeholder="New title", lines=1)
493
  rename_btn = gr.Button("Rename")
494
  delete_btn = gr.Button("Delete Chat")
495
+ # Main panel: chat, outline and generation actions
496
  with gr.Column(scale=4):
497
  chatbot = gr.Chatbot(label="Conversation", height=400)
498
  msg_input = gr.Textbox(
 
511
  """Refresh the list of chat sessions for the sidebar.
512
 
513
  Each chat will be displayed by its title (or "Untitled" if none) followed by a short
514
+ portion of its key in parentheses. The actual value of each dropdown choice will be
515
+ the full chat key, while the displayed label will be this formatted string. The
516
+ currently active chat key will be selected when possible.
517
 
518
  Returns:
519
+ A gradio update for the ``session_picker`` dropdown component.
520
  """
521
  chats = list_chats()
522
+ # Build a list of (label, value) tuples for the Radio. Each label shows
523
+ # the chat title and a short portion of the key, while the value is the
524
+ # full chat key. When a user selects an option, the value (chat key)
525
+ # will be returned.
526
+ options: list[tuple[str, str]] = []
527
  current_key = state_chat_key.value
528
+ selected_key = None
529
  for c in chats:
530
  title = c.get("title") or "Untitled"
531
  key = c.get("key")
 
 
532
  label = f"{title} ({key[:8]})"
533
+ options.append((label, key))
534
  if key == current_key:
535
+ selected_key = key
536
+ # If current chat not found (e.g. after deletion), pick the first one
537
+ if not selected_key and options:
538
+ selected_key = options[0][1]
539
+ return gr.update(choices=options, value=selected_key)
540
 
541
  # Load the selected chat into memory and return display pairs/history
542
+ def load_session(selected_key):
543
  """Load a chat session when selected in the sidebar.
544
 
545
+ The session_picker dropdown returns the chat key directly. Load the
546
+ conversation messages from the database and reconstruct the history and
547
+ pairs for display. Sources, plan and resource_cache are reset.
 
 
548
 
549
  Args:
550
+ selected_key: The chat key selected from the dropdown. If None,
551
+ returns empty state.
552
 
553
  Returns:
554
  Tuple of (chatbot_pairs, chat_history, chat_pairs, sources, plan, resource_cache).
555
  """
556
+ if not selected_key:
557
  return [], [], [], [], "", {}
 
 
 
 
558
  # Update the global state for the current chat key
559
+ state_chat_key.value = selected_key
560
+ msgs = load_chat(selected_key)
561
  history = []
562
  pairs = []
563
  buffer = []