lamgamepro1 commited on
Commit
e5c2467
·
verified ·
1 Parent(s): 3541880

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -33
app.py CHANGED
@@ -6,10 +6,8 @@ MODEL = "mistralai/Mistral-7B-Instruct-v0.1"
6
  # In-memory chat history
7
  chat_history = {}
8
 
9
- def get_user_id(hf_token):
10
- return hash(hf_token.token) % 1000000 if hf_token and hf_token.token else None
11
-
12
  def respond(message, history, system_message, max_tokens, temperature, top_p, hf_token):
 
13
  if not hf_token or not hf_token.token:
14
  yield "❌ Vui lòng đăng nhập Hugging Face (nút Login sidebar)!"
15
  return
@@ -17,59 +15,71 @@ def respond(message, history, system_message, max_tokens, temperature, top_p, hf
17
  try:
18
  client = InferenceClient(token=hf_token.token, model=MODEL)
19
  messages = [{"role": "system", "content": system_message}]
20
- messages.extend(history)
 
 
 
 
 
 
21
  messages.append({"role": "user", "content": message})
22
 
23
  response = ""
24
- for message in client.chat_completion(
25
- messages, max_tokens=max_tokens, stream=True,
26
- temperature=temperature, top_p=top_p
 
 
 
27
  ):
28
- if message.choices and message.choices[0].delta.content:
29
- response += message.choices[0].delta.content
30
  yield response
31
 
32
  except Exception as e:
33
- yield f"❌ Lỗi: {str(e)}"
34
 
35
  def load_history(hf_token):
36
- user_id = get_user_id(hf_token)
37
- if user_id and user_id in chat_history:
38
- return [[h["user"], h["ai"]] for h in chat_history[user_id][-10:]]
 
 
 
 
39
  return []
40
 
41
- def save_history(hf_token, message, response):
42
- user_id = get_user_id(hf_token)
43
- if user_id and hf_token.token:
44
- if user_id not in chat_history:
45
- chat_history[user_id] = []
46
- chat_history[user_id].append({"user": message, "ai": response})
47
- if len(chat_history[user_id]) > 20:
48
- chat_history[user_id] = chat_history[user_id][-20:]
49
 
50
- with gr.Blocks(title="NodeAI") as demo: # ❌ NO theme ở đây
51
  gr.Markdown("# 🚀 NodeAI - HF Login + History")
52
 
53
  with gr.Row():
54
  # Sidebar
55
  with gr.Column(scale=1, min_width=350):
56
  gr.Markdown("## 🔐 Hugging Face")
57
- login_btn = gr.LoginButton() # ✅ CHỈ LoginButton
58
 
59
  gr.Markdown("## 📚 Lịch sử Chat")
60
  history_display = gr.Chatbot(
61
- height=300, label="Lịch sử",
 
62
  avatar_images=("👤", "🤖")
63
  )
64
 
65
  with gr.Row():
66
- load_btn = gr.Button("📥 Load", variant="secondary")
67
- clear_btn = gr.Button("🗑️ Clear", variant="stop")
68
 
69
  # Main Chat
70
  with gr.Column(scale=4):
71
  chatbot = gr.ChatInterface(
72
- respond,
73
  additional_inputs=[
74
  gr.Textbox(
75
  value="Bạn là NodeAI - trợ lý AI thông minh, trả lời tiếng Việt.",
@@ -80,17 +90,16 @@ with gr.Blocks(title="NodeAI") as demo: # ❌ NO theme ở đây
80
  gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p"),
81
  ],
82
  title="💬 NodeAI Chat",
83
- description="**Đăng nhập HF sidebar → Chat → Auto save history**"
84
  )
85
 
86
- # Events
87
- load_btn.click(load_history, gr.OAuthToken(), history_display)
88
- clear_btn.click(lambda: [], outputs=history_display)
89
 
90
  if __name__ == "__main__":
91
- # ✅ Theme ở launch() - Gradio 6.0 syntax
92
  demo.launch(
93
- theme=gr.themes.Soft(primary_hue="blue"),
94
  server_name="0.0.0.0",
95
  server_port=7860
96
  )
 
6
  # In-memory chat history
7
  chat_history = {}
8
 
 
 
 
9
  def respond(message, history, system_message, max_tokens, temperature, top_p, hf_token):
10
+ """Chat function - 7 args đúng chuẩn ChatInterface"""
11
  if not hf_token or not hf_token.token:
12
  yield "❌ Vui lòng đăng nhập Hugging Face (nút Login sidebar)!"
13
  return
 
15
  try:
16
  client = InferenceClient(token=hf_token.token, model=MODEL)
17
  messages = [{"role": "system", "content": system_message}]
18
+
19
+ # Convert history to messages format
20
+ for user_msg, assistant_msg in history:
21
+ messages.append({"role": "user", "content": str(user_msg)})
22
+ if assistant_msg:
23
+ messages.append({"role": "assistant", "content": str(assistant_msg)})
24
+
25
  messages.append({"role": "user", "content": message})
26
 
27
  response = ""
28
+ for chunk in client.chat_completion(
29
+ messages,
30
+ max_tokens=max_tokens,
31
+ stream=True,
32
+ temperature=temperature,
33
+ top_p=top_p
34
  ):
35
+ if chunk.choices[0].delta.content:
36
+ response += chunk.choices[0].delta.content
37
  yield response
38
 
39
  except Exception as e:
40
+ yield f"❌ Lỗi model: {str(e)}"
41
 
42
  def load_history(hf_token):
43
+ """Load history - Gradio tự inject hf_token"""
44
+ if not hf_token or not hf_token.token:
45
+ return []
46
+
47
+ user_id = hash(hf_token.token) % 1000000
48
+ if user_id in chat_history:
49
+ return chat_history[user_id][-10:]
50
  return []
51
 
52
+ def clear_history(hf_token):
53
+ """Clear history"""
54
+ if hf_token and hf_token.token:
55
+ user_id = hash(hf_token.token) % 1000000
56
+ chat_history[user_id] = []
57
+ return []
 
 
58
 
59
+ with gr.Blocks(title="NodeAI") as demo:
60
  gr.Markdown("# 🚀 NodeAI - HF Login + History")
61
 
62
  with gr.Row():
63
  # Sidebar
64
  with gr.Column(scale=1, min_width=350):
65
  gr.Markdown("## 🔐 Hugging Face")
66
+ gr.LoginButton() # ✅ Native HF Login
67
 
68
  gr.Markdown("## 📚 Lịch sử Chat")
69
  history_display = gr.Chatbot(
70
+ height=300,
71
+ label="Lịch sử tin nhắn",
72
  avatar_images=("👤", "🤖")
73
  )
74
 
75
  with gr.Row():
76
+ load_btn = gr.Button("📥 Load History", variant="secondary")
77
+ clear_btn = gr.Button("🗑️ Clear History", variant="stop")
78
 
79
  # Main Chat
80
  with gr.Column(scale=4):
81
  chatbot = gr.ChatInterface(
82
+ respond, # ✅ Auto nhận 7 args từ ChatInterface
83
  additional_inputs=[
84
  gr.Textbox(
85
  value="Bạn là NodeAI - trợ lý AI thông minh, trả lời tiếng Việt.",
 
90
  gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p"),
91
  ],
92
  title="💬 NodeAI Chat",
93
+ description="**Đăng nhập HF → Chat → Auto save history theo user**"
94
  )
95
 
96
+ # ✅ History events - Gradio tự inject OAuthToken
97
+ load_btn.click(load_history, outputs=history_display)
98
+ clear_btn.click(clear_history, outputs=history_display)
99
 
100
  if __name__ == "__main__":
 
101
  demo.launch(
102
+ theme=gr.themes.Soft(primary_hue="blue"), # ✅ Theme ở launch()
103
  server_name="0.0.0.0",
104
  server_port=7860
105
  )