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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -19
app.py CHANGED
@@ -1,13 +1,12 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
- import uuid
5
 
6
  # 1. CẤU HÌNH AI
7
  HF_TOKEN = os.environ.get("HUGGINGFACE_HUB_TOKEN")
8
  client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
9
 
10
- # 2. GIAO DIỆN CSS & JS (Dùng để dọn dẹp Header/Footer)
11
  custom_css = """
12
  footer, .built-with, header, #header { display: none !important; }
13
  .gradio-container { max-width: 100% !important; padding: 0 !important; }
@@ -32,37 +31,40 @@ js_cleaner = """
32
  " style="display:none;">
33
  """
34
 
35
- # 3. CHAT ENGINE (Sử dụng mảng history trong script)
36
  def chat_engine(message, history, profile: gr.OAuthProfile | None):
37
  text = message.get("text", "").strip()
38
  if not text:
39
- yield gr.update(value={"text": "", "files": []}), history
40
  return
41
 
42
- # Khởi tạo bối cảnh từ mảng history
43
  user_name = profile.name if profile else "Master"
 
44
  messages = [{"role": "system", "content": f"Bạn là NodeAI Genesis. Master: {user_name}."}]
45
 
46
- for msg in history:
47
- messages.append({"role": msg["role"], "content": msg["content"]})
 
48
 
49
  messages.append({"role": "user", "content": text})
50
 
51
- new_history = history + [{"role": "user", "content": text}]
 
52
  ans = ""
53
 
54
- yield gr.update(value={"text": "", "files": []}), new_history + [{"role": "assistant", "content": "..."}]
55
-
56
  try:
57
  for token in client.chat_completion(messages=messages, max_tokens=2048, stream=True):
58
  if token.choices[0].delta.content:
59
  ans += token.choices[0].delta.content
60
- yield gr.update(), new_history + [{"role": "assistant", "content": ans}]
 
 
61
  except Exception as e:
62
- yield gr.update(), new_history + [{"role": "assistant", "content": f"Lỗi: {str(e)}"}]
 
63
 
64
  # 4. GIAO DIỆN UI
65
- with gr.Blocks() as demo: # <--- ĐÃ XÓA css= TRONG NÀY ĐỂ HẾT LỖI
66
  gr.HTML(js_cleaner, visible=False)
67
 
68
  with gr.Row():
@@ -71,17 +73,24 @@ with gr.Blocks() as demo: # <--- ĐÃ XÓA css= TRONG NÀY ĐỂ HẾT LỖI
71
  gr.LoginButton(value="Đăng nhập", size="sm")
72
  new_chat_btn = gr.Button("+ CLEAR MEMORY", variant="stop")
73
  gr.Markdown("---")
74
- gr.Markdown("💡 **Script Memory**: Đang lưu cục bộ.")
75
 
76
  with gr.Column(scale=4):
77
- chatbot = gr.Chatbot(show_label=False, height="78vh", type="messages")
 
 
78
  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>")
79
- msg_input = gr.MultimodalTextbox(show_label=False, placeholder="Hỏi NodeAI...", container=False)
 
 
 
 
 
80
 
81
  # Xử lý sự kiện
82
  msg_input.submit(chat_engine, [msg_input, chatbot], [msg_input, chatbot])
83
- new_chat_btn.click(lambda: [], None, chatbot)
84
 
85
- # 5. KHỞI CHẠY (CSS ĐƯỢC ĐƯA VÀO ĐÂY)
86
  if __name__ == "__main__":
87
- demo.queue().launch(css=custom_css) # <--- CSS PHẢI NẰM Ở ĐÂY
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
 
4
 
5
  # 1. CẤU HÌNH AI
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
  " 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:
68
  gr.HTML(js_cleaner, visible=False)
69
 
70
  with gr.Row():
 
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__":
96
+ demo.queue().launch(css=custom_css)