Spaces:
Sleeping
Sleeping
EMMA commited on
Commit ·
a7dd220
1
Parent(s): 0d969e5
update code
Browse files
app.py
CHANGED
|
@@ -517,7 +517,11 @@ def _build_session_choices(user_data: dict):
|
|
| 517 |
turn_count = len(session.get("conversation", []))
|
| 518 |
if turn_count == 0:
|
| 519 |
continue # skip empty sessions (e.g. the current one, just started)
|
| 520 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 521 |
choices.append((label, idx))
|
| 522 |
choices.reverse() # newest first
|
| 523 |
return choices
|
|
@@ -716,6 +720,13 @@ body:has(.progress-level)::before {
|
|
| 716 |
variant="primary",
|
| 717 |
elem_classes=["action-btn"],
|
| 718 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 719 |
|
| 720 |
# -------------------------------------------------------
|
| 721 |
# Internal Functions
|
|
@@ -1029,9 +1040,10 @@ body:has(.progress-level)::before {
|
|
| 1029 |
print(f"Error syncing memory after resuming session: {e}")
|
| 1030 |
|
| 1031 |
resumed_date = resumed_session.get("date", "an earlier session")
|
|
|
|
| 1032 |
new_header = (
|
| 1033 |
"<h2 style='text-align: center; color: #333;'>"
|
| 1034 |
-
f"🧠 EMMA: Session for {user_name} (resumed
|
| 1035 |
)
|
| 1036 |
|
| 1037 |
return (
|
|
@@ -1041,7 +1053,43 @@ body:has(.progress-level)::before {
|
|
| 1041 |
gr.update(open=False),
|
| 1042 |
gr.update(choices=_build_session_choices(user_data), value=None),
|
| 1043 |
gr.update(value=[]),
|
| 1044 |
-
gr.update(value=f"✅ Resumed session
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1045 |
)
|
| 1046 |
|
| 1047 |
def handle_new_session(state):
|
|
@@ -1152,6 +1200,12 @@ body:has(.progress-level)::before {
|
|
| 1152 |
outputs=[chatbot, state, active_header, history_sidebar, session_radio, history_viewer, system_msg],
|
| 1153 |
)
|
| 1154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1155 |
return demo
|
| 1156 |
|
| 1157 |
|
|
|
|
| 517 |
turn_count = len(session.get("conversation", []))
|
| 518 |
if turn_count == 0:
|
| 519 |
continue # skip empty sessions (e.g. the current one, just started)
|
| 520 |
+
title = (session.get("title") or "").strip()
|
| 521 |
+
if title:
|
| 522 |
+
label = f"{title} ({session.get('date', 'Unknown date')}, {turn_count} messages)"
|
| 523 |
+
else:
|
| 524 |
+
label = f"{session.get('date', 'Unknown date')} ({turn_count} messages)"
|
| 525 |
choices.append((label, idx))
|
| 526 |
choices.reverse() # newest first
|
| 527 |
return choices
|
|
|
|
| 720 |
variant="primary",
|
| 721 |
elem_classes=["action-btn"],
|
| 722 |
)
|
| 723 |
+
with gr.Row():
|
| 724 |
+
rename_input = gr.Textbox(
|
| 725 |
+
placeholder="New name for selected session...",
|
| 726 |
+
show_label=False,
|
| 727 |
+
scale=3,
|
| 728 |
+
)
|
| 729 |
+
rename_btn = gr.Button("✏️ Rename", scale=1)
|
| 730 |
|
| 731 |
# -------------------------------------------------------
|
| 732 |
# Internal Functions
|
|
|
|
| 1040 |
print(f"Error syncing memory after resuming session: {e}")
|
| 1041 |
|
| 1042 |
resumed_date = resumed_session.get("date", "an earlier session")
|
| 1043 |
+
resumed_label = (resumed_session.get("title") or "").strip() or resumed_date
|
| 1044 |
new_header = (
|
| 1045 |
"<h2 style='text-align: center; color: #333;'>"
|
| 1046 |
+
f"🧠 EMMA: Session for {user_name} (resumed: {resumed_label})</h2>"
|
| 1047 |
)
|
| 1048 |
|
| 1049 |
return (
|
|
|
|
| 1053 |
gr.update(open=False),
|
| 1054 |
gr.update(choices=_build_session_choices(user_data), value=None),
|
| 1055 |
gr.update(value=[]),
|
| 1056 |
+
gr.update(value=f"✅ Resumed session: {resumed_label}.", visible=True),
|
| 1057 |
+
)
|
| 1058 |
+
|
| 1059 |
+
def handle_rename_session(session_id, new_name, state):
|
| 1060 |
+
user_name = state.get("user_name")
|
| 1061 |
+
if not user_name or "memory" not in state or user_name not in state["memory"]:
|
| 1062 |
+
return gr.update(), gr.update(), gr.update(value="⚠️ You must be logged in to rename a session.", visible=True)
|
| 1063 |
+
|
| 1064 |
+
if session_id is None:
|
| 1065 |
+
return gr.update(), gr.update(), gr.update(value="⚠️ Please select a session first.", visible=True)
|
| 1066 |
+
|
| 1067 |
+
new_name = (new_name or "").strip()
|
| 1068 |
+
if not new_name:
|
| 1069 |
+
return gr.update(), gr.update(), gr.update(value="⚠️ Enter a name before renaming.", visible=True)
|
| 1070 |
+
|
| 1071 |
+
user_data = state["memory"][user_name]
|
| 1072 |
+
sessions = user_data.get("sessions", [])
|
| 1073 |
+
|
| 1074 |
+
try:
|
| 1075 |
+
idx = int(session_id)
|
| 1076 |
+
except (TypeError, ValueError):
|
| 1077 |
+
return gr.update(), gr.update(), gr.update(value="⚠️ Invalid session selected.", visible=True)
|
| 1078 |
+
|
| 1079 |
+
if idx < 0 or idx >= len(sessions):
|
| 1080 |
+
return gr.update(), gr.update(), gr.update(value="⚠️ That session could not be found.", visible=True)
|
| 1081 |
+
|
| 1082 |
+
sessions[idx]["title"] = new_name
|
| 1083 |
+
|
| 1084 |
+
try:
|
| 1085 |
+
sync_memory_to_hf()
|
| 1086 |
+
except Exception as e:
|
| 1087 |
+
print(f"Error syncing memory after renaming session: {e}")
|
| 1088 |
+
|
| 1089 |
+
return (
|
| 1090 |
+
gr.update(choices=_build_session_choices(user_data), value=idx),
|
| 1091 |
+
gr.update(value=""),
|
| 1092 |
+
gr.update(value=f"✅ Renamed to \"{new_name}\".", visible=True),
|
| 1093 |
)
|
| 1094 |
|
| 1095 |
def handle_new_session(state):
|
|
|
|
| 1200 |
outputs=[chatbot, state, active_header, history_sidebar, session_radio, history_viewer, system_msg],
|
| 1201 |
)
|
| 1202 |
|
| 1203 |
+
rename_btn.click(
|
| 1204 |
+
handle_rename_session,
|
| 1205 |
+
inputs=[session_radio, rename_input, state],
|
| 1206 |
+
outputs=[session_radio, rename_input, system_msg],
|
| 1207 |
+
)
|
| 1208 |
+
|
| 1209 |
return demo
|
| 1210 |
|
| 1211 |
|