lamgamepro1 commited on
Commit
f5d5355
·
verified ·
1 Parent(s): 0ecf63a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -6,7 +6,7 @@ import os
6
  HF_TOKEN = os.environ.get("HUGGINGFACE_HUB_TOKEN")
7
  client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
8
 
9
- # 2. GIAO DIỆN CSS (Sẽ dùng trong .launch)
10
  custom_css = """
11
  footer, .built-with, header, #header { display: none !important; }
12
  .gradio-container { max-width: 100% !important; padding: 0 !important; }
@@ -31,37 +31,37 @@ js_cleaner = """
31
  " style="display:none;">
32
  """
33
 
34
- # 3. CHAT ENGINE (Cấu trúc Memory dạng Tuple cho bản cũ)
35
  def chat_engine(message, history, profile: gr.OAuthProfile | None):
36
  text = message.get("text", "").strip()
37
  if not text:
38
- yield "", history
39
  return
40
 
41
  user_name = profile.name if profile else "Master"
42
- # Chuyển đổi history từ Tuple sang mảng hội thoại cho AI
43
- messages = [{"role": "system", "content": f"Bạn là NodeAI Genesis. Master: {user_name}."}]
44
 
45
- for user_msg, bot_msg in history:
46
- if user_msg: messages.append({"role": "user", "content": user_msg})
47
- if bot_msg: messages.append({"role": "assistant", "content": bot_msg})
48
 
 
 
49
  messages.append({"role": "user", "content": text})
50
 
51
- # Chuẩn bị history mới
52
- new_history = history + [[text, ""]]
53
  ans = ""
54
 
 
 
 
55
  try:
56
  for token in client.chat_completion(messages=messages, max_tokens=2048, stream=True):
57
  if token.choices[0].delta.content:
58
  ans += token.choices[0].delta.content
59
- # Cập nhật tin nhắn cuối cùng trong mảng history
60
- new_history[-1][1] = ans
61
- yield "", new_history
62
  except Exception as e:
63
- new_history[-1][1] = f"Lỗi: {str(e)}"
64
- yield "", new_history
65
 
66
  # 4. GIAO DIỆN UI
67
  with gr.Blocks() as demo:
@@ -73,23 +73,23 @@ with gr.Blocks() as demo:
73
  gr.LoginButton(value="Đăng nhập", size="sm")
74
  new_chat_btn = gr.Button("+ CLEAR MEMORY", variant="stop")
75
  gr.Markdown("---")
76
- gr.Markdown("💡 **Script Memory**: Đang chạy bản ổn định.")
77
 
78
  with gr.Column(scale=4):
79
- # Xóa tham số type="messages" để tránh lỗi
80
  chatbot = gr.Chatbot(show_label=False, height="78vh")
81
 
82
  gr.HTML("<div class='custom-warning'>NodeAI không thể chắc chắn 100%, hãy rà soát lại khi bạn có chuyện gì quan trọng!</div>")
83
 
84
  msg_input = gr.MultimodalTextbox(
85
  show_label=False,
86
- placeholder="Hỏi NodeAI...",
87
  container=False
88
  )
89
 
90
  # Xử lý sự kiện
91
  msg_input.submit(chat_engine, [msg_input, chatbot], [msg_input, chatbot])
92
- new_chat_btn.click(lambda: None, None, chatbot)
93
 
94
  # 5. LAUNCH
95
  if __name__ == "__main__":
 
6
  HF_TOKEN = os.environ.get("HUGGINGFACE_HUB_TOKEN")
7
  client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
8
 
9
+ # 2. GIAO DIỆN CSS
10
  custom_css = """
11
  footer, .built-with, header, #header { display: none !important; }
12
  .gradio-container { max-width: 100% !important; padding: 0 !important; }
 
31
  " style="display:none;">
32
  """
33
 
34
+ # 3. CHAT ENGINE (Sử dụng định dạng {'role': '...', 'content': '...'})
35
  def chat_engine(message, history, profile: gr.OAuthProfile | None):
36
  text = message.get("text", "").strip()
37
  if not text:
38
+ yield gr.update(value={"text": "", "files": []}), history
39
  return
40
 
41
  user_name = profile.name if profile else "Master"
 
 
42
 
43
+ # Tạo danh sách tin nhắn gửi đi cho AI (Bao gồm System Prompt)
44
+ messages = [{"role": "system", "content": f"Bạn là NodeAI Genesis. Master: {user_name}."}]
 
45
 
46
+ # history bây giờ là danh sách các dict, chỉ cần cộng vào là xong
47
+ messages.extend(history)
48
  messages.append({"role": "user", "content": text})
49
 
50
+ # Cập nhật history cho chatbot hiển thị
51
+ new_history = history + [{"role": "user", "content": text}]
52
  ans = ""
53
 
54
+ # Gửi trạng thái đang chờ
55
+ yield gr.update(value={"text": "", "files": []}), new_history + [{"role": "assistant", "content": "..."}]
56
+
57
  try:
58
  for token in client.chat_completion(messages=messages, max_tokens=2048, stream=True):
59
  if token.choices[0].delta.content:
60
  ans += token.choices[0].delta.content
61
+ # Gửi history kèm tin nhắn assistant đang được cập nhật
62
+ yield gr.update(), new_history + [{"role": "assistant", "content": ans}]
 
63
  except Exception as e:
64
+ yield gr.update(), new_history + [{"role": "assistant", "content": f"Lỗi: {str(e)}"}]
 
65
 
66
  # 4. GIAO DIỆN UI
67
  with gr.Blocks() as demo:
 
73
  gr.LoginButton(value="Đăng nhập", size="sm")
74
  new_chat_btn = gr.Button("+ CLEAR MEMORY", variant="stop")
75
  gr.Markdown("---")
76
+ gr.Markdown("💡 **Status**: Script Memory (Messages Format)")
77
 
78
  with gr.Column(scale=4):
79
+ # Không đặt type="messages" nữa vì bản này tự hiểu khi nhận list dict
80
  chatbot = gr.Chatbot(show_label=False, height="78vh")
81
 
82
  gr.HTML("<div class='custom-warning'>NodeAI không thể chắc chắn 100%, hãy rà soát lại khi bạn có chuyện gì quan trọng!</div>")
83
 
84
  msg_input = gr.MultimodalTextbox(
85
  show_label=False,
86
+ placeholder="Nhập nội dung Master cần hỏi...",
87
  container=False
88
  )
89
 
90
  # Xử lý sự kiện
91
  msg_input.submit(chat_engine, [msg_input, chatbot], [msg_input, chatbot])
92
+ new_chat_btn.click(lambda: [], None, chatbot) # Trả về mảng rỗng để xóa chat
93
 
94
  # 5. LAUNCH
95
  if __name__ == "__main__":