lamgamepro1 commited on
Commit
1c8f96e
·
verified ·
1 Parent(s): 6056250

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -39
app.py CHANGED
@@ -2,99 +2,103 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
- # HF Token auto
6
  HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
7
- MODEL = "gpt2" # Model 100% working
8
 
9
  client = InferenceClient(token=HF_TOKEN, model=MODEL)
10
 
11
  def chat(message, history):
12
- """Simple & safe chat"""
13
  try:
14
- inputs = f"Q: {message}\nA:"
15
  response = client.text_generation(
16
- inputs,
17
  max_new_tokens=80,
18
  temperature=0.8,
19
  do_sample=True,
20
  pad_token_id=50256
21
  )
22
- reply = response[0].generated_text[len(inputs):].strip()
23
- return reply or "🤖 Chào bạn! Hỏi mình bất cứ gì nhé!"
24
  except:
25
- return f"🤖 {message[:50]}..."
26
 
27
  def respond(message, history):
28
- """Chat handler"""
 
29
  reply = chat(message, history)
30
- history.append([message, reply])
 
 
 
 
 
 
31
  return "", history
32
 
33
  with gr.Blocks(title="NodeAI Pro") as demo:
34
  # Pro Header
35
  gr.HTML("""
36
  <div style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
37
- color: white; text-align: center; padding: 40px; border-radius: 20px; margin: 20px;'>
38
  <h1 style='font-size: 48px; margin: 0;'>🚀 NodeAI Pro</h1>
39
- <p style='font-size: 24px;'>AI Chatbot Tiếng Việt Siêu Thông Minh</p>
40
- <div style='background: rgba(255,255,255,0.2); padding: 15px 30px;
41
- border-radius: 30px; display: inline-block; margin-top: 10px;'>
42
- ✅ HF Token Active | GPT-2 Turbo
43
  </div>
44
  </div>
45
  """)
46
 
47
  with gr.Row():
48
- # Sidebar Pro
49
  with gr.Column(scale=1, min_width=320):
50
  gr.Markdown("## ⚙️ **Cài đặt**")
51
 
52
- with gr.Accordion("🎯 Model", open=True):
53
- gr.Dropdown(["gpt2", "distilgpt2"], value="gpt2", label="Model")
54
- gr.Slider(50, 150, 80, label="Tokens", interactive=False)
55
- gr.Slider(0.5, 1.0, 0.8, label="Temperature", interactive=False)
56
 
57
- with gr.Accordion("👤 Tài khoản"):
58
  gr.HTML("""
59
  <div style='text-align: center; padding: 20px;
60
- background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
61
- color: white; border-radius: 15px;'>
62
- <h3>✅ Đã kết nối</h3>
63
- <p>HF Token: Active</p>
64
  </div>
65
  """)
66
  gr.LoginButton()
67
 
68
  gr.Markdown("## 📚 **Lịch sử**")
69
- history_chat = gr.Chatbot(height=280, label="Recent chats")
70
- with gr.Row():
71
- gr.Button("📥 Load", scale=1, variant="secondary")
72
- gr.Button("🗑️ Xóa", scale=1, variant="stop")
73
 
74
- # Main Chat Pro
75
  with gr.Column(scale=4):
76
- main_chatbot = gr.Chatbot(
77
  height=550,
78
  avatar_images=("🧑‍💻", "🤖"),
79
- label=False
80
  )
81
 
82
  with gr.Row():
83
  msg = gr.Textbox(
84
- placeholder="💭 Nhập câu hỏi của bạn... (Ctrl+Enter)",
85
  scale=7,
86
- lines=2,
87
- container=False
88
  )
89
- send_btn = gr.Button("Gửi", scale=1, variant="primary")
90
 
91
  # Events
92
- msg.submit(respond, [msg, main_chatbot], [msg, main_chatbot])
93
- send_btn.click(respond, [msg, main_chatbot], [msg, main_chatbot])
94
 
95
  if __name__ == "__main__":
96
  demo.launch(
97
  theme=gr.themes.Soft(primary_hue="blue"),
98
  server_name="0.0.0.0",
99
- server_port=7860
 
 
100
  )
 
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
 
5
  HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
6
+ MODEL = "gpt2"
7
 
8
  client = InferenceClient(token=HF_TOKEN, model=MODEL)
9
 
10
  def chat(message, history):
11
+ """Safe chat function"""
12
  try:
13
+ prompt = f"Q: {message}\nA:"
14
  response = client.text_generation(
15
+ prompt,
16
  max_new_tokens=80,
17
  temperature=0.8,
18
  do_sample=True,
19
  pad_token_id=50256
20
  )
21
+ reply = response[0].generated_text[len(prompt):].strip()
22
+ return reply or "🤖 Chào bạn! Hỏi mình nhé!"
23
  except:
24
+ return "🤖 NodeAI sẵn sàng hỗ trợ!"
25
 
26
  def respond(message, history):
27
+ """Chat handler - đúng format Gradio 6.9.0"""
28
+ # ✅ Chatbot format: [[{"role": "user", "content": msg}, {"role": "assistant", "content": reply}]]
29
  reply = chat(message, history)
30
+
31
+ new_message = {
32
+ "user": [{"role": "user", "content": message}],
33
+ "assistant": [{"role": "assistant", "content": reply}]
34
+ }
35
+
36
+ history.append(new_message)
37
  return "", history
38
 
39
  with gr.Blocks(title="NodeAI Pro") as demo:
40
  # Pro Header
41
  gr.HTML("""
42
  <div style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
43
+ color: white; text-align: center; padding: 40px; border-radius: 20px; margin: 20px 0;'>
44
  <h1 style='font-size: 48px; margin: 0;'>🚀 NodeAI Pro</h1>
45
+ <p style='font-size: 24px; opacity: 0.95;'>AI Chatbot Tiếng Việt Siêu Thông Minh</p>
46
+ <div style='background: rgba(255,255,255,0.2); padding: 12px 25px;
47
+ border-radius: 25px; display: inline-block; font-weight: bold;'>
48
+ ✅ GPT-2 Active | Zero Config
49
  </div>
50
  </div>
51
  """)
52
 
53
  with gr.Row():
54
+ # Sidebar
55
  with gr.Column(scale=1, min_width=320):
56
  gr.Markdown("## ⚙️ **Cài đặt**")
57
 
58
+ with gr.Accordion("🤖 Model", open=True):
59
+ gr.Markdown("**gpt2** (Siêu ổn định)")
60
+ gr.Markdown("**Tokens:** 80 | **Temp:** 0.8")
 
61
 
62
+ with gr.Accordion("👤 Kết nối"):
63
  gr.HTML("""
64
  <div style='text-align: center; padding: 20px;
65
+ background: linear-gradient(135deg, #10b981, #059669);
66
+ color: white; border-radius: 15px; margin: 10px 0;'>
67
+ <h3>✅ HF Token OK</h3>
68
+ <p>Ready to chat!</p>
69
  </div>
70
  """)
71
  gr.LoginButton()
72
 
73
  gr.Markdown("## 📚 **Lịch sử**")
74
+ history_display = gr.Chatbot(height=280)
75
+ gr.Button("🗑️ Xóa lịch sử", variant="stop")
 
 
76
 
77
+ # Main Chat
78
  with gr.Column(scale=4):
79
+ chatbot = gr.Chatbot(
80
  height=550,
81
  avatar_images=("🧑‍💻", "🤖"),
82
+ show_label=False
83
  )
84
 
85
  with gr.Row():
86
  msg = gr.Textbox(
87
+ placeholder="💭 Hỏi NodeAI bất cứ điều gì... (Enter để gửi)",
88
  scale=7,
89
+ lines=2
 
90
  )
91
+ send_btn = gr.Button("📤 Gửi", scale=1, variant="primary")
92
 
93
  # Events
94
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
95
+ send_btn.click(respond, [msg, chatbot], [msg, chatbot])
96
 
97
  if __name__ == "__main__":
98
  demo.launch(
99
  theme=gr.themes.Soft(primary_hue="blue"),
100
  server_name="0.0.0.0",
101
+ server_port=7860,
102
+ share=False, # ✅ Fix asyncio error
103
+ ssr_mode=False # ✅ Fix SSR warnings
104
  )