lamgamepro1 commited on
Commit
1a9a75b
·
verified ·
1 Parent(s): cc300c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -69
app.py CHANGED
@@ -3,57 +3,32 @@ from huggingface_hub import InferenceClient
3
  import os
4
  import uuid
5
 
6
- # 1. Khởi tạo Client
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
- text-align: left !important;
22
- border: none !important;
23
- background: #333 !important;
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-item:hover { background: #2d2e30; }
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
- messages = [{"role": "system", "content": "Bạn là NodeAI Pro, có giao diện phong cách Gemini."}]
 
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
- yield "", history + [
66
  {"role": "user", "content": [{"type": "text", "text": user_text}]},
67
  {"role": "assistant", "content": [{"type": "text", "text": response_text}]}
68
- ], session_id, chat_store
 
 
 
 
69
  except Exception as e:
70
- yield "", history + [{"role": "assistant", "content": [{"type": "text", "text": f"❌ Lỗi: {str(e)}"}]}], session_id, chat_store
71
 
72
- def start_new_chat():
73
- return [], str(uuid.uuid4()), gr.update()
 
 
74
 
75
- def update_sidebar_list(store):
76
- if not store:
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
- new_chat_btn = gr.Button("+ Cuộc trò chuyện mới", elem_classes="nav-btn")
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
- # Thay thế Spacer bằng Markdown rỗng có chiều cao
95
- gr.Markdown("<div style='height: 40vh;'></div>")
 
 
 
 
 
 
 
 
 
 
96
  gr.LoginButton(size="sm")
97
 
98
- # MAIN CHAT
99
  with gr.Column(scale=4):
100
- chatbot = gr.Chatbot(label=None, height=650)
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ý sự kiện
114
- ev = msg_input.submit(chat_fn, [msg_input, chatbot, session_id, chat_store], [msg_input, chatbot, session_id, chat_store])
115
- ev.then(update_sidebar_list, chat_store, history_list)
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)