Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -153,19 +153,43 @@ def load_dataset_by_name(name: str):
|
|
| 153 |
def refresh_sessions_cb():
|
| 154 |
labels, _ = list_sessions()
|
| 155 |
selected = labels[0] if labels else None
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
def new_chat_cb():
|
| 159 |
sid = create_session("New chat")
|
| 160 |
labels, _ = list_sessions()
|
| 161 |
selected = next((lbl for lbl in labels if lbl.startswith(f"{sid} ")), None)
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
def load_session_cb(selected_label):
|
| 165 |
sid = label_to_id(selected_label)
|
| 166 |
if not sid:
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
def delete_chat_cb(selected_label):
|
| 171 |
sid = label_to_id(selected_label)
|
|
@@ -173,7 +197,15 @@ def delete_chat_cb(selected_label):
|
|
| 173 |
delete_session(sid)
|
| 174 |
labels, _ = list_sessions()
|
| 175 |
selected = labels[0] if labels else None
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
FRONTEND_KEYWORDS = [
|
| 179 |
"react", "vue", "angular", "html", "css", "javascript", "tailwind", "recharts", "typescript"
|
|
@@ -379,14 +411,10 @@ with gr.Blocks(title="Backend-Focused LLaMA/Mistral CRUD Assistant", theme=gr.th
|
|
| 379 |
# -----------------------
|
| 380 |
# Editable title input
|
| 381 |
# -----------------------
|
| 382 |
-
#
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
edit_title_box = gr.Textbox(label="✏️ Rename Chat", placeholder="Edit selected chat title...")
|
| 386 |
-
save_title_btn = gr.Button("💾", elem_id="save-title-btn")
|
| 387 |
-
|
| 388 |
|
| 389 |
-
|
| 390 |
def rename_session_cb(new_title, selected_label):
|
| 391 |
sid = label_to_id(selected_label)
|
| 392 |
if sid and new_title.strip():
|
|
@@ -404,13 +432,11 @@ with gr.Blocks(title="Backend-Focused LLaMA/Mistral CRUD Assistant", theme=gr.th
|
|
| 404 |
# Connect button to callback
|
| 405 |
save_title_btn.click(rename_session_cb, inputs=[edit_title_box, session_list], outputs=session_list)
|
| 406 |
|
| 407 |
-
|
| 408 |
with gr.Row(elem_id="session-action-row"):
|
| 409 |
new_btn = gr.Button("➕", elem_id="new-chat-btn")
|
| 410 |
-
del_btn = gr.Button("🗑️", elem_id="delete-chat-btn")
|
| 411 |
-
refresh_btn = gr.Button("🔄", elem_id="refresh-btn")
|
| 412 |
-
|
| 413 |
-
|
| 414 |
|
| 415 |
gr.Markdown("### 🤖 Model Selection")
|
| 416 |
model_choice = gr.Dropdown(
|
|
@@ -449,10 +475,25 @@ with gr.Blocks(title="Backend-Focused LLaMA/Mistral CRUD Assistant", theme=gr.th
|
|
| 449 |
send_btn = gr.Button("Send ▶️", variant="primary")
|
| 450 |
regen_btn = gr.Button("Regenerate 🔁", variant="secondary")
|
| 451 |
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 456 |
|
| 457 |
send_btn.click(
|
| 458 |
send_cb,
|
|
@@ -473,4 +514,4 @@ with gr.Blocks(title="Backend-Focused LLaMA/Mistral CRUD Assistant", theme=gr.th
|
|
| 473 |
)
|
| 474 |
|
| 475 |
if __name__ == "__main__":
|
| 476 |
-
demo.launch()
|
|
|
|
| 153 |
def refresh_sessions_cb():
|
| 154 |
labels, _ = list_sessions()
|
| 155 |
selected = labels[0] if labels else None
|
| 156 |
+
visible = True if selected else False
|
| 157 |
+
return (
|
| 158 |
+
gr.update(choices=labels, value=selected),
|
| 159 |
+
gr.update(visible=visible),
|
| 160 |
+
gr.update(visible=visible),
|
| 161 |
+
gr.update(visible=visible),
|
| 162 |
+
gr.update(visible=visible),
|
| 163 |
+
)
|
| 164 |
|
| 165 |
def new_chat_cb():
|
| 166 |
sid = create_session("New chat")
|
| 167 |
labels, _ = list_sessions()
|
| 168 |
selected = next((lbl for lbl in labels if lbl.startswith(f"{sid} ")), None)
|
| 169 |
+
# Return: session_list, chatbot (empty), user_box cleared, then make edit/save/delete/refresh visible
|
| 170 |
+
return (
|
| 171 |
+
gr.update(choices=labels, value=selected),
|
| 172 |
+
[], # chatbot messages
|
| 173 |
+
"", # user_box cleared
|
| 174 |
+
gr.update(visible=True), # edit_title_box visible
|
| 175 |
+
gr.update(visible=True), # save_title_btn visible
|
| 176 |
+
gr.update(visible=True), # del_btn visible
|
| 177 |
+
gr.update(visible=True), # refresh_btn visible
|
| 178 |
+
)
|
| 179 |
|
| 180 |
def load_session_cb(selected_label):
|
| 181 |
sid = label_to_id(selected_label)
|
| 182 |
if not sid:
|
| 183 |
+
# no valid selection -> hide controls
|
| 184 |
+
return [], gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
| 185 |
+
msgs = get_messages(sid)
|
| 186 |
+
return (
|
| 187 |
+
msgs,
|
| 188 |
+
gr.update(visible=True),
|
| 189 |
+
gr.update(visible=True),
|
| 190 |
+
gr.update(visible=True),
|
| 191 |
+
gr.update(visible=True),
|
| 192 |
+
)
|
| 193 |
|
| 194 |
def delete_chat_cb(selected_label):
|
| 195 |
sid = label_to_id(selected_label)
|
|
|
|
| 197 |
delete_session(sid)
|
| 198 |
labels, _ = list_sessions()
|
| 199 |
selected = labels[0] if labels else None
|
| 200 |
+
visible = True if selected else False
|
| 201 |
+
return (
|
| 202 |
+
gr.update(choices=labels, value=selected),
|
| 203 |
+
[], # chatbot cleared
|
| 204 |
+
gr.update(visible=visible),
|
| 205 |
+
gr.update(visible=visible),
|
| 206 |
+
gr.update(visible=visible),
|
| 207 |
+
gr.update(visible=visible),
|
| 208 |
+
)
|
| 209 |
|
| 210 |
FRONTEND_KEYWORDS = [
|
| 211 |
"react", "vue", "angular", "html", "css", "javascript", "tailwind", "recharts", "typescript"
|
|
|
|
| 411 |
# -----------------------
|
| 412 |
# Editable title input
|
| 413 |
# -----------------------
|
| 414 |
+
# Hidden initially — will be shown when a chat is created/selected
|
| 415 |
+
edit_title_box = gr.Textbox(label="✏️ Rename Chat", placeholder="Edit selected chat title...", visible=False)
|
| 416 |
+
save_title_btn = gr.Button("💾", elem_id="save-title-btn", visible=False)
|
|
|
|
|
|
|
|
|
|
| 417 |
|
|
|
|
| 418 |
def rename_session_cb(new_title, selected_label):
|
| 419 |
sid = label_to_id(selected_label)
|
| 420 |
if sid and new_title.strip():
|
|
|
|
| 432 |
# Connect button to callback
|
| 433 |
save_title_btn.click(rename_session_cb, inputs=[edit_title_box, session_list], outputs=session_list)
|
| 434 |
|
| 435 |
+
# Action buttons: New Chat always visible, others hidden initially
|
| 436 |
with gr.Row(elem_id="session-action-row"):
|
| 437 |
new_btn = gr.Button("➕", elem_id="new-chat-btn")
|
| 438 |
+
del_btn = gr.Button("🗑️", elem_id="delete-chat-btn", visible=False)
|
| 439 |
+
refresh_btn = gr.Button("🔄", elem_id="refresh-btn", visible=False)
|
|
|
|
|
|
|
| 440 |
|
| 441 |
gr.Markdown("### 🤖 Model Selection")
|
| 442 |
model_choice = gr.Dropdown(
|
|
|
|
| 475 |
send_btn = gr.Button("Send ▶️", variant="primary")
|
| 476 |
regen_btn = gr.Button("Regenerate 🔁", variant="secondary")
|
| 477 |
|
| 478 |
+
# Wire callbacks — updated outputs so visibility changes flow correctly
|
| 479 |
+
refresh_btn.click(
|
| 480 |
+
refresh_sessions_cb,
|
| 481 |
+
outputs=[session_list, edit_title_box, save_title_btn, del_btn, refresh_btn],
|
| 482 |
+
)
|
| 483 |
+
new_btn.click(
|
| 484 |
+
new_chat_cb,
|
| 485 |
+
outputs=[session_list, chatbot, user_box, edit_title_box, save_title_btn, del_btn, refresh_btn],
|
| 486 |
+
)
|
| 487 |
+
del_btn.click(
|
| 488 |
+
delete_chat_cb,
|
| 489 |
+
inputs=session_list,
|
| 490 |
+
outputs=[session_list, chatbot, edit_title_box, save_title_btn, del_btn, refresh_btn],
|
| 491 |
+
)
|
| 492 |
+
session_list.change(
|
| 493 |
+
load_session_cb,
|
| 494 |
+
inputs=session_list,
|
| 495 |
+
outputs=[chatbot, edit_title_box, save_title_btn, del_btn, refresh_btn],
|
| 496 |
+
)
|
| 497 |
|
| 498 |
send_btn.click(
|
| 499 |
send_cb,
|
|
|
|
| 514 |
)
|
| 515 |
|
| 516 |
if __name__ == "__main__":
|
| 517 |
+
demo.launch()
|