Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,57 +3,32 @@ from huggingface_hub import InferenceClient
|
|
| 3 |
import os
|
| 4 |
import uuid
|
| 5 |
|
| 6 |
-
# 1.
|
| 7 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 8 |
client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
|
| 9 |
|
| 10 |
-
# 2. CSS Gemini Dark Mode
|
| 11 |
custom_css = """
|
| 12 |
body, .gradio-container { background-color: #131314 !important; color: #e3e3e3 !important; }
|
| 13 |
-
.sidebar {
|
| 14 |
-
background-color: #1e1f20 !important;
|
| 15 |
-
border-right: 1px solid #333 !important;
|
| 16 |
-
height: 100vh;
|
| 17 |
-
padding: 15px !important;
|
| 18 |
-
}
|
| 19 |
.gemini-title { color: #8ab4f8; font-size: 24px; font-weight: bold; margin-bottom: 20px; }
|
| 20 |
-
.nav-btn {
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
color: #e3e3e3 !important;
|
| 25 |
-
margin-bottom: 10px;
|
| 26 |
-
border-radius: 20px !important;
|
| 27 |
-
}
|
| 28 |
-
.history-item {
|
| 29 |
-
padding: 8px 12px;
|
| 30 |
-
margin: 4px 0;
|
| 31 |
-
border-radius: 8px;
|
| 32 |
-
font-size: 0.9em;
|
| 33 |
-
cursor: pointer;
|
| 34 |
-
white-space: nowrap;
|
| 35 |
-
overflow: hidden;
|
| 36 |
-
text-overflow: ellipsis;
|
| 37 |
}
|
| 38 |
-
.history-
|
| 39 |
.footer-tag { font-size: 0.75rem; color: #9aa0a6; text-align: center; margin-top: 15px; }
|
| 40 |
"""
|
| 41 |
|
| 42 |
def chat_fn(message, history, session_id, chat_store):
|
| 43 |
-
if not message:
|
| 44 |
-
return "", history, session_id, chat_store
|
| 45 |
-
|
| 46 |
user_text = message if isinstance(message, str) else message.get("text", "")
|
| 47 |
-
|
| 48 |
-
if not history:
|
| 49 |
-
chat_store[session_id] = user_text[:25] + "..."
|
| 50 |
|
| 51 |
-
|
|
|
|
| 52 |
for turn in history:
|
| 53 |
-
role = turn["role"]
|
| 54 |
content = turn["content"][0]["text"] if isinstance(turn["content"], list) else turn["content"]
|
| 55 |
-
messages.append({"role": role, "content": content})
|
| 56 |
-
|
| 57 |
messages.append({"role": "user", "content": user_text})
|
| 58 |
|
| 59 |
try:
|
|
@@ -62,21 +37,24 @@ def chat_fn(message, history, session_id, chat_store):
|
|
| 62 |
delta = token.choices[0].delta.content
|
| 63 |
if delta:
|
| 64 |
response_text += delta
|
| 65 |
-
|
| 66 |
{"role": "user", "content": [{"type": "text", "text": user_text}]},
|
| 67 |
{"role": "assistant", "content": [{"type": "text", "text": response_text}]}
|
| 68 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
-
yield "", history + [{"role": "assistant", "content": [{"type": "text", "text":
|
| 71 |
|
| 72 |
-
def
|
| 73 |
-
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
def
|
| 76 |
-
|
| 77 |
-
return "<div style='color:#555'><i>Trống</i></div>"
|
| 78 |
-
items = [f"<div class='history-item'>🕒 {title}</div>" for title in store.values()]
|
| 79 |
-
return "".join(items)
|
| 80 |
|
| 81 |
with gr.Blocks(title="NodeAI Gemini") as demo:
|
| 82 |
chat_store = gr.State({})
|
|
@@ -86,38 +64,36 @@ with gr.Blocks(title="NodeAI Gemini") as demo:
|
|
| 86 |
# SIDEBAR
|
| 87 |
with gr.Column(scale=1, elem_classes="sidebar"):
|
| 88 |
gr.Markdown("Gemini", elem_classes="gemini-title")
|
| 89 |
-
|
| 90 |
gr.Markdown("---")
|
| 91 |
gr.Markdown("### Gần đây")
|
| 92 |
-
history_list = gr.HTML("<div style='color:#555'><i>Trống</i></div>")
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
gr.LoginButton(size="sm")
|
| 97 |
|
| 98 |
-
# MAIN
|
| 99 |
with gr.Column(scale=4):
|
| 100 |
-
chatbot = gr.Chatbot(label=None, height=
|
| 101 |
-
|
| 102 |
with gr.Row():
|
| 103 |
-
msg_input = gr.Textbox(
|
| 104 |
-
placeholder="Nhập câu hỏi tại đây...",
|
| 105 |
-
show_label=False,
|
| 106 |
-
container=False,
|
| 107 |
-
scale=9
|
| 108 |
-
)
|
| 109 |
submit_btn = gr.Button("🚀", scale=1, variant="primary")
|
| 110 |
-
|
| 111 |
-
gr.Markdown("NodeAI Pro v4.1 | Kết nối an toàn", elem_classes="footer-tag")
|
| 112 |
|
| 113 |
-
# Xử lý
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
btn_ev = submit_btn.click(chat_fn, [msg_input, chatbot, session_id, chat_store], [msg_input, chatbot, session_id, chat_store])
|
| 118 |
-
btn_ev.then(update_sidebar_list, chat_store, history_list)
|
| 119 |
-
|
| 120 |
-
new_chat_btn.click(start_new_chat, None, [chatbot, session_id, history_list])
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
| 123 |
demo.launch(css=custom_css)
|
|
|
|
| 3 |
import os
|
| 4 |
import uuid
|
| 5 |
|
| 6 |
+
# 1. Cấu hình Model
|
| 7 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 8 |
client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
|
| 9 |
|
|
|
|
| 10 |
custom_css = """
|
| 11 |
body, .gradio-container { background-color: #131314 !important; color: #e3e3e3 !important; }
|
| 12 |
+
.sidebar { background-color: #1e1f20 !important; border-right: 1px solid #333 !important; height: 100vh; padding: 15px !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
.gemini-title { color: #8ab4f8; font-size: 24px; font-weight: bold; margin-bottom: 20px; }
|
| 14 |
+
.nav-btn { text-align: left !important; background: #333 !important; color: white !important; margin-bottom: 10px; border-radius: 20px !important; }
|
| 15 |
+
.history-btn {
|
| 16 |
+
width: 100%; text-align: left !important; background: transparent !important;
|
| 17 |
+
border: none !important; color: #ccc !important; cursor: pointer; padding: 5px 10px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
+
.history-btn:hover { background: #2d2e30 !important; color: white !important; }
|
| 20 |
.footer-tag { font-size: 0.75rem; color: #9aa0a6; text-align: center; margin-top: 15px; }
|
| 21 |
"""
|
| 22 |
|
| 23 |
def chat_fn(message, history, session_id, chat_store):
|
| 24 |
+
if not message: return "", history, session_id, chat_store
|
|
|
|
|
|
|
| 25 |
user_text = message if isinstance(message, str) else message.get("text", "")
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Gọi API
|
| 28 |
+
messages = [{"role": "system", "content": "Bạn là NodeAI Pro phong cách Gemini."}]
|
| 29 |
for turn in history:
|
|
|
|
| 30 |
content = turn["content"][0]["text"] if isinstance(turn["content"], list) else turn["content"]
|
| 31 |
+
messages.append({"role": turn["role"], "content": content})
|
|
|
|
| 32 |
messages.append({"role": "user", "content": user_text})
|
| 33 |
|
| 34 |
try:
|
|
|
|
| 37 |
delta = token.choices[0].delta.content
|
| 38 |
if delta:
|
| 39 |
response_text += delta
|
| 40 |
+
current_history = history + [
|
| 41 |
{"role": "user", "content": [{"type": "text", "text": user_text}]},
|
| 42 |
{"role": "assistant", "content": [{"type": "text", "text": response_text}]}
|
| 43 |
+
]
|
| 44 |
+
yield "", current_history, session_id, chat_store
|
| 45 |
+
|
| 46 |
+
# LƯU TOÀN BỘ VÀO STORE
|
| 47 |
+
chat_store[session_id] = {"title": user_text[:20] + "...", "data": current_history}
|
| 48 |
except Exception as e:
|
| 49 |
+
yield "", history + [{"role": "assistant", "content": [{"type": "text", "text": str(e)}]}], session_id, chat_store
|
| 50 |
|
| 51 |
+
def load_old_chat(session_id_to_load, chat_store):
|
| 52 |
+
# Lấy dữ liệu từ store dựa trên ID đã chọn
|
| 53 |
+
old_data = chat_store.get(session_id_to_load, {}).get("data", [])
|
| 54 |
+
return old_data, session_id_to_load
|
| 55 |
|
| 56 |
+
def start_new_chat():
|
| 57 |
+
return [], str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
with gr.Blocks(title="NodeAI Gemini") as demo:
|
| 60 |
chat_store = gr.State({})
|
|
|
|
| 64 |
# SIDEBAR
|
| 65 |
with gr.Column(scale=1, elem_classes="sidebar"):
|
| 66 |
gr.Markdown("Gemini", elem_classes="gemini-title")
|
| 67 |
+
new_btn = gr.Button("+ Cuộc trò chuyện mới", elem_classes="nav-btn")
|
| 68 |
gr.Markdown("---")
|
| 69 |
gr.Markdown("### Gần đây")
|
|
|
|
| 70 |
|
| 71 |
+
# Danh sách các nút lịch sử
|
| 72 |
+
history_container = gr.Column()
|
| 73 |
+
|
| 74 |
+
@gr.render(inputs=chat_store)
|
| 75 |
+
def render_history(store):
|
| 76 |
+
for sid, info in store.items():
|
| 77 |
+
# Tạo một nút cho mỗi cuộc hội thoại cũ
|
| 78 |
+
btn = gr.Button(f"🕒 {info['title']}", elem_classes="history-btn")
|
| 79 |
+
# Khi nhấn vào nút này, load lại chat đó
|
| 80 |
+
btn.click(load_old_chat, [gr.State(sid), chat_store], [chatbot, session_id])
|
| 81 |
+
|
| 82 |
+
gr.Markdown("<div style='height: 30vh;'></div>")
|
| 83 |
gr.LoginButton(size="sm")
|
| 84 |
|
| 85 |
+
# MAIN
|
| 86 |
with gr.Column(scale=4):
|
| 87 |
+
chatbot = gr.Chatbot(label=None, height=600)
|
|
|
|
| 88 |
with gr.Row():
|
| 89 |
+
msg_input = gr.Textbox(placeholder="Nhập yêu cầu...", show_label=False, container=False, scale=9)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
submit_btn = gr.Button("🚀", scale=1, variant="primary")
|
| 91 |
+
gr.Markdown("NodeAI Pro v4.5 | Powered by Qwen 2.5", elem_classes="footer-tag")
|
|
|
|
| 92 |
|
| 93 |
+
# Xử lý
|
| 94 |
+
msg_input.submit(chat_fn, [msg_input, chatbot, session_id, chat_store], [msg_input, chatbot, session_id, chat_store])
|
| 95 |
+
submit_btn.click(chat_fn, [msg_input, chatbot, session_id, chat_store], [msg_input, chatbot, session_id, chat_store])
|
| 96 |
+
new_btn.click(start_new_chat, None, [chatbot, session_id])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
demo.launch(css=custom_css)
|