Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,6 +26,7 @@ def get_db_history(profile: gr.OAuthProfile | None):
|
|
| 26 |
return []
|
| 27 |
|
| 28 |
def load_selected_chat(evt: gr.SelectData, db_data):
|
|
|
|
| 29 |
selected_item = db_data[evt.index]
|
| 30 |
return selected_item['history'], selected_item['session_id']
|
| 31 |
|
|
@@ -61,7 +62,7 @@ def chat_engine(message, history, session_id, profile: gr.OAuthProfile | None):
|
|
| 61 |
except Exception as e:
|
| 62 |
yield gr.update(), new_history + [{"role": "assistant", "content": f"Lỗi: {str(e)}"}], session_id
|
| 63 |
|
| 64 |
-
# 4. UI
|
| 65 |
with gr.Blocks() as demo:
|
| 66 |
session_id = gr.State(str(uuid.uuid4()))
|
| 67 |
db_state = gr.State([])
|
|
@@ -73,6 +74,7 @@ with gr.Blocks() as demo:
|
|
| 73 |
new_chat_btn = gr.Button("+ NEW WORKSPACE", variant="primary")
|
| 74 |
|
| 75 |
gr.Markdown("### 🗄️ Database Memory")
|
|
|
|
| 76 |
history_list = gr.Dataset(
|
| 77 |
components=[gr.Textbox(visible=False)],
|
| 78 |
label="Lịch sử chat",
|
|
@@ -81,20 +83,23 @@ with gr.Blocks() as demo:
|
|
| 81 |
)
|
| 82 |
|
| 83 |
with gr.Column(scale=4):
|
| 84 |
-
# FIX: Bỏ type="messages"
|
| 85 |
chatbot = gr.Chatbot(show_label=False, height="85vh")
|
| 86 |
msg_input = gr.MultimodalTextbox(show_label=False, placeholder="Hỏi NodeAI...", container=False)
|
| 87 |
|
|
|
|
| 88 |
def refresh_data(profile: gr.OAuthProfile | None):
|
| 89 |
-
if not profile:
|
|
|
|
| 90 |
data = get_db_history(profile)
|
| 91 |
samples = [[item['title']] for item in data]
|
| 92 |
-
return
|
|
|
|
| 93 |
|
| 94 |
demo.load(refresh_data, None, [db_state, history_list])
|
| 95 |
|
| 96 |
history_list.select(fn=load_selected_chat, inputs=[db_state], outputs=[chatbot, session_id])
|
| 97 |
|
|
|
|
| 98 |
msg_input.submit(chat_engine, [msg_input, chatbot, session_id], [msg_input, chatbot, session_id]).then(
|
| 99 |
refresh_data, None, [db_state, history_list]
|
| 100 |
)
|
|
@@ -102,5 +107,4 @@ with gr.Blocks() as demo:
|
|
| 102 |
new_chat_btn.click(lambda: ([], str(uuid.uuid4())), None, [chatbot, session_id])
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
| 105 |
-
# FIX: Chuyển css xuống launch()
|
| 106 |
demo.launch(css=custom_css)
|
|
|
|
| 26 |
return []
|
| 27 |
|
| 28 |
def load_selected_chat(evt: gr.SelectData, db_data):
|
| 29 |
+
# Lấy dữ liệu từ item được chọn
|
| 30 |
selected_item = db_data[evt.index]
|
| 31 |
return selected_item['history'], selected_item['session_id']
|
| 32 |
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
yield gr.update(), new_history + [{"role": "assistant", "content": f"Lỗi: {str(e)}"}], session_id
|
| 64 |
|
| 65 |
+
# 4. UI
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
session_id = gr.State(str(uuid.uuid4()))
|
| 68 |
db_state = gr.State([])
|
|
|
|
| 74 |
new_chat_btn = gr.Button("+ NEW WORKSPACE", variant="primary")
|
| 75 |
|
| 76 |
gr.Markdown("### 🗄️ Database Memory")
|
| 77 |
+
# Dataset ban đầu
|
| 78 |
history_list = gr.Dataset(
|
| 79 |
components=[gr.Textbox(visible=False)],
|
| 80 |
label="Lịch sử chat",
|
|
|
|
| 83 |
)
|
| 84 |
|
| 85 |
with gr.Column(scale=4):
|
|
|
|
| 86 |
chatbot = gr.Chatbot(show_label=False, height="85vh")
|
| 87 |
msg_input = gr.MultimodalTextbox(show_label=False, placeholder="Hỏi NodeAI...", container=False)
|
| 88 |
|
| 89 |
+
# FIX: Hàm refresh trả về đối tượng gr.Dataset mới
|
| 90 |
def refresh_data(profile: gr.OAuthProfile | None):
|
| 91 |
+
if not profile:
|
| 92 |
+
return [], gr.Dataset(samples=[])
|
| 93 |
data = get_db_history(profile)
|
| 94 |
samples = [[item['title']] for item in data]
|
| 95 |
+
# QUAN TRỌNG: Phải return gr.Dataset(...) thay vì chỉ return samples
|
| 96 |
+
return data, gr.Dataset(samples=samples)
|
| 97 |
|
| 98 |
demo.load(refresh_data, None, [db_state, history_list])
|
| 99 |
|
| 100 |
history_list.select(fn=load_selected_chat, inputs=[db_state], outputs=[chatbot, session_id])
|
| 101 |
|
| 102 |
+
# Sau khi chat xong, gọi refresh để cập nhật Sidebar
|
| 103 |
msg_input.submit(chat_engine, [msg_input, chatbot, session_id], [msg_input, chatbot, session_id]).then(
|
| 104 |
refresh_data, None, [db_state, history_list]
|
| 105 |
)
|
|
|
|
| 107 |
new_chat_btn.click(lambda: ([], str(uuid.uuid4())), None, [chatbot, session_id])
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
|
|
|
| 110 |
demo.launch(css=custom_css)
|