Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,11 +26,10 @@ def get_db_history(profile: gr.OAuthProfile | None):
|
|
| 26 |
return []
|
| 27 |
|
| 28 |
def load_selected_chat(evt: gr.SelectData, db_data):
|
| 29 |
-
# Lấy dữ liệu từ item được chọn trong danh sách
|
| 30 |
selected_item = db_data[evt.index]
|
| 31 |
return selected_item['history'], selected_item['session_id']
|
| 32 |
|
| 33 |
-
# 3. GIAO DIỆN
|
| 34 |
custom_css = """
|
| 35 |
.sidebar-node { background: rgba(15, 23, 42, 0.8) !important; border-right: 1px solid #38bdf833; padding: 20px; }
|
| 36 |
.logo-area { font-family: 'Orbitron'; background: linear-gradient(90deg, #38bdf8, #818cf8); -webkit-background-clip: text; -webkit-text-fill-color: transparent; font-size: 22px; text-align: center; font-weight: bold; margin-bottom: 20px; }
|
|
@@ -62,20 +61,18 @@ def chat_engine(message, history, session_id, profile: gr.OAuthProfile | None):
|
|
| 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(
|
| 67 |
session_id = gr.State(str(uuid.uuid4()))
|
| 68 |
-
db_state = gr.State([])
|
| 69 |
|
| 70 |
with gr.Row():
|
| 71 |
with gr.Column(scale=1, elem_classes="sidebar-node"):
|
| 72 |
gr.HTML("<div class='logo-area'>NODEAI GENESIS</div>")
|
| 73 |
gr.LoginButton(value="Login with Hugging Face", size="sm")
|
| 74 |
-
|
| 75 |
new_chat_btn = gr.Button("+ NEW WORKSPACE", variant="primary")
|
| 76 |
|
| 77 |
gr.Markdown("### 🗄️ Database Memory")
|
| 78 |
-
# Dùng gr.Dataset thay cho việc render nút bấm động để tránh lỗi KeyError
|
| 79 |
history_list = gr.Dataset(
|
| 80 |
components=[gr.Textbox(visible=False)],
|
| 81 |
label="Lịch sử chat",
|
|
@@ -84,25 +81,19 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 84 |
)
|
| 85 |
|
| 86 |
with gr.Column(scale=4):
|
| 87 |
-
|
|
|
|
| 88 |
msg_input = gr.MultimodalTextbox(show_label=False, placeholder="Hỏi NodeAI...", container=False)
|
| 89 |
|
| 90 |
-
# LOGIC CẬP NHẬT
|
| 91 |
def refresh_data(profile: gr.OAuthProfile | None):
|
| 92 |
if not profile: return [], []
|
| 93 |
data = get_db_history(profile)
|
| 94 |
-
# Chuyển đổi data thành format của Dataset (danh sách các hàng)
|
| 95 |
samples = [[item['title']] for item in data]
|
| 96 |
return data, samples
|
| 97 |
|
| 98 |
demo.load(refresh_data, None, [db_state, history_list])
|
| 99 |
|
| 100 |
-
|
| 101 |
-
history_list.select(
|
| 102 |
-
fn=load_selected_chat,
|
| 103 |
-
inputs=[db_state],
|
| 104 |
-
outputs=[chatbot, session_id]
|
| 105 |
-
)
|
| 106 |
|
| 107 |
msg_input.submit(chat_engine, [msg_input, chatbot, session_id], [msg_input, chatbot, session_id]).then(
|
| 108 |
refresh_data, None, [db_state, history_list]
|
|
@@ -111,4 +102,5 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 111 |
new_chat_btn.click(lambda: ([], str(uuid.uuid4())), None, [chatbot, session_id])
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
-
|
|
|
|
|
|
| 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 |
|
| 32 |
+
# 3. GIAO DIỆN (CSS)
|
| 33 |
custom_css = """
|
| 34 |
.sidebar-node { background: rgba(15, 23, 42, 0.8) !important; border-right: 1px solid #38bdf833; padding: 20px; }
|
| 35 |
.logo-area { font-family: 'Orbitron'; background: linear-gradient(90deg, #38bdf8, #818cf8); -webkit-background-clip: text; -webkit-text-fill-color: transparent; font-size: 22px; text-align: center; font-weight: bold; margin-bottom: 20px; }
|
|
|
|
| 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 - BỎ THAM SỐ CSS KHỎI BLOCKS
|
| 65 |
+
with gr.Blocks() as demo:
|
| 66 |
session_id = gr.State(str(uuid.uuid4()))
|
| 67 |
+
db_state = gr.State([])
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
with gr.Column(scale=1, elem_classes="sidebar-node"):
|
| 71 |
gr.HTML("<div class='logo-area'>NODEAI GENESIS</div>")
|
| 72 |
gr.LoginButton(value="Login with Hugging Face", size="sm")
|
|
|
|
| 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 |
)
|
| 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: return [], []
|
| 90 |
data = get_db_history(profile)
|
|
|
|
| 91 |
samples = [[item['title']] for item in data]
|
| 92 |
return data, samples
|
| 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]
|
|
|
|
| 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)
|