lamgamepro1 commited on
Commit
dd0284e
·
verified ·
1 Parent(s): 4f974f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -56
app.py CHANGED
@@ -8,92 +8,87 @@ client = InferenceClient("gpt2", token=HF_TOKEN)
8
 
9
  # 2. CSS Custom cho Giao diện Pro
10
  custom_css = """
11
- .header-container {
12
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
13
- padding: 2rem;
14
- border-radius: 12px;
15
  color: white;
16
  text-align: center;
17
  margin-bottom: 20px;
18
- box-shadow: 0 4px 6px rgba(0,0,0,0.1);
19
  }
20
- .sidebar-info {
21
- padding: 15px;
22
- background: #f8f9fa;
23
- border-radius: 8px;
24
- height: 100%;
25
- }
26
- #chatbot-container {
27
- height: 600px !important;
28
  }
29
  """
30
 
31
  def chat_fn(message, history):
32
- # Định dạng msg đúng yêu cầu Gradio 6.9.0:
33
- # [{"role": "user", "content": [{"type": "text", "text": msg}]}]
34
- # Gradio tự động đóng gói 'message' vào format này khi chatbot type="messages"
35
 
36
- user_input = message[-1]["content"][0]["text"] if isinstance(message, list) else message
 
37
 
38
- # Gọi API Inference thật (GPT-2)
39
  response_text = ""
 
40
  try:
41
- # Generate văn bản từ prompt
42
- full_response = client.text_generation(
43
- user_input,
44
- max_new_tokens=150,
45
- temperature=0.7,
46
- stream=False # GPT-2 Inference API đôi khi ổn định hơn với False
47
- )
48
- response_text = full_response
49
  except Exception as e:
50
- response_text = f"⚠️ Lỗi kết nối API: {str(e)}"
51
-
52
- # Cập nhật lịch sử theo format chuẩn
53
- history.append({"role": "user", "content": [{"type": "text", "text": user_input}]})
54
  history.append({"role": "assistant", "content": [{"type": "text", "text": response_text}]})
55
 
 
56
  return "", history
57
 
58
- with gr.Blocks(css=custom_css, title="NodeAI Pro") as demo:
59
- # Header Pro
60
- with gr.Div(elem_classes="header-container"):
61
- gr.Markdown("# 🤖 NodeAI Pro")
62
- gr.Markdown("Trợ lý AI hiệu năng cao - Powered by Hugging Face")
 
 
 
63
 
64
  with gr.Row():
65
- # Sidebar (chiếm 1 phần)
66
- with gr.Column(scale=1, variant="panel"):
67
- gr.Markdown("### ⚙️ Thông số")
68
- gr.LoginButton()
69
- gr.Markdown("---")
70
- gr.Markdown("**Trạng thái Token:** ✅ Đã kết nối")
71
- gr.Markdown("**Model:** `gpt2` (Inference API)")
72
- gr.Markdown("**Phiên bản:** NodeAI v1.0.4")
 
73
 
74
- # Chat Main (chiếm 3 phần)
75
  with gr.Column(scale=3):
76
  chatbot = gr.Chatbot(
77
  label="Hội thoại",
78
- type="messages", # ĐẶT Ở ĐÂY để fix lỗi "Data incompatible"
79
- elem_id="chatbot-container",
80
  bubble_full_width=False
81
  )
82
- msg_input = gr.Textbox(
83
- placeholder="Nhập câu hỏi tại đây...",
84
- show_label=False,
85
- container=False
86
- )
87
 
88
  with gr.Row():
89
- clear_btn = gr.Button("🗑️ Xóa lịch sử")
90
- submit_btn = gr.Button("🚀 Gửi tin nhắn", variant="primary")
 
 
 
 
 
 
91
 
92
- # Logic xử lý
93
- # Lưu ý: history ở đây được lưu trữ trong chatbot state
94
- msg_input.submit(chat_fn, [msg_input, chatbot], [msg_input, chatbot])
95
  submit_btn.click(chat_fn, [msg_input, chatbot], [msg_input, chatbot])
96
  clear_btn.click(lambda: [], None, chatbot)
97
 
 
98
  if __name__ == "__main__":
99
- demo.launch()
 
8
 
9
  # 2. CSS Custom cho Giao diện Pro
10
  custom_css = """
11
+ .header-pro {
12
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
13
+ padding: 30px;
14
+ border-radius: 15px;
15
  color: white;
16
  text-align: center;
17
  margin-bottom: 20px;
 
18
  }
19
+ .sidebar-panel {
20
+ background-color: #f0f2f6;
21
+ padding: 20px;
22
+ border-radius: 10px;
 
 
 
 
23
  }
24
  """
25
 
26
  def chat_fn(message, history):
27
+ # Trích xuất nội dung từ định dạng tin nhắn mới của Gradio 6.9.0
28
+ # message: [{"role": "user", "content": [{"type": "text", "text": "..."}]}]
29
+ user_input = message[-1]["content"][0]["text"]
30
 
31
+ # Khởi tạo tin nhắn phản hồi của Assistant theo format Gradio 6
32
+ history.append({"role": "user", "content": [{"type": "text", "text": user_input}]})
33
 
 
34
  response_text = ""
35
+ # Gọi API thực tế
36
  try:
37
+ # GPT-2 sinh văn bản dựa trên input
38
+ output = client.text_generation(user_input, max_new_tokens=100)
39
+ response_text = output
 
 
 
 
 
40
  except Exception as e:
41
+ response_text = f"⚠️ Lỗi API: {str(e)}"
42
+
 
 
43
  history.append({"role": "assistant", "content": [{"type": "text", "text": response_text}]})
44
 
45
+ # Trả về chuỗi rỗng cho ô input và history mới
46
  return "", history
47
 
48
+ with gr.Blocks(title="NodeAI Pro") as demo:
49
+ # Header Pro dùng HTML để tránh lỗi gr.Div
50
+ gr.HTML("""
51
+ <div class="header-pro">
52
+ <h1>🤖 NodeAI Pro</h1>
53
+ <p>Hệ thống AI Assistant tối ưu cho hiệu suất</p>
54
+ </div>
55
+ """)
56
 
57
  with gr.Row():
58
+ # Sidebar (Sử dụng integer scale)
59
+ with gr.Column(scale=1):
60
+ with gr.Group():
61
+ gr.Markdown("### ⚙️ Control Panel")
62
+ gr.LoginButton()
63
+ gr.Markdown("---")
64
+ gr.Markdown(f"**Status:** {'🟢 Ready' if HF_TOKEN else '🔴 Missing Token'}")
65
+ gr.Markdown("**Model:** `GPT-2` Standard")
66
+ gr.Markdown("**Version:** 6.9.0")
67
 
68
+ # Chat Main
69
  with gr.Column(scale=3):
70
  chatbot = gr.Chatbot(
71
  label="Hội thoại",
72
+ type="messages", # Định dạng tin nhắn chuẩn Gradio 6
73
+ height=500,
74
  bubble_full_width=False
75
  )
 
 
 
 
 
76
 
77
  with gr.Row():
78
+ msg_input = gr.Textbox(
79
+ placeholder="Gõ lệnh tại đây...",
80
+ show_label=False,
81
+ scale=4
82
+ )
83
+ submit_btn = gr.Button("Gửi", variant="primary", scale=1)
84
+
85
+ clear_btn = gr.Button("🗑️ Clear Chat", size="sm")
86
 
87
+ # Xử lý sự kiện
88
+ submit_event = msg_input.submit(chat_fn, [msg_input, chatbot], [msg_input, chatbot])
 
89
  submit_btn.click(chat_fn, [msg_input, chatbot], [msg_input, chatbot])
90
  clear_btn.click(lambda: [], None, chatbot)
91
 
92
+ # 3. CHÍNH XÁC: Truyền CSS vào launch() theo yêu cầu của Gradio 6.0+
93
  if __name__ == "__main__":
94
+ demo.launch(css=custom_css)