lamgamepro1 commited on
Commit
fa4f66f
·
verified ·
1 Parent(s): cdcf7a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -80
app.py CHANGED
@@ -3,108 +3,84 @@ from huggingface_hub import InferenceClient
3
  import os
4
 
5
  HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
6
- MODEL = "gpt2"
7
 
8
- def smart_chat(message):
9
- """AI + Smart fallback"""
 
 
 
10
  try:
11
- client = InferenceClient(token=HF_TOKEN, model=MODEL)
12
- prompt = f"Q: {message}\nA: "
13
- response = client.text_generation(
14
- prompt,
15
- max_new_tokens=60,
16
- temperature=0.8,
17
- do_sample=True
 
 
 
 
 
18
  )
19
- reply = response[0].generated_text[len(prompt):].strip()
20
- return reply if reply else "🤖 Chào bạn!"
 
21
  except:
22
- # Smart fallback
23
- msg_lower = message.lower()
24
- fallbacks = {
25
- "xin chào": "Chào bạn! 😊 NodeAI sẵn sàng hỗ trợ!",
26
- "hello": "Xin chào! Rất vui được chat với bạn!",
27
- "bạn là ai": "🚀 Mình là NodeAI Pro - AI trợ lý thông minh!",
28
- "default": "🤖 Hiểu rồi! Bạn cần giúp gì tiếp theo?"
29
- }
30
- for key, value in fallbacks.items():
31
- if key in msg_lower:
32
- return value
33
- return fallbacks["default"]
34
 
35
  def respond(message, history):
36
- """✅ CORRECT GRADIO 6.9.0 FORMAT"""
37
- if not message.strip():
38
- return "", history
39
-
40
- reply = smart_chat(message)
41
-
42
- # ✅ FORMAT ĐÚNG: list of dicts role/content
43
- new_history = history + [{
44
- "role": "user",
45
- "content": [{"type": "text", "text": message}]
46
- }, {
47
- "role": "assistant",
48
- "content": [{"type": "text", "text": reply}]
49
- }]
50
-
51
- return "", new_history
52
 
53
- # UI Pro
54
  with gr.Blocks(title="🚀 NodeAI Pro") as demo:
55
  gr.HTML("""
56
  <div style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
57
- color: white; padding: 2.5rem; text-align: center; border-radius: 20px;'>
58
- <h1 style='font-size: 2.8rem;'>🚀 NodeAI Pro</h1>
59
- <p style='font-size: 1.3rem;'>AI Chatbot Tiếng Việt • HF_TOKEN Only</p>
60
  <div style='background: rgba(255,255,255,0.2); padding: 1rem 2rem;
61
- border-radius: 30px; display: inline-block; font-weight: bold;'>
62
- ✅ GPT-2 Active
63
  </div>
64
  </div>
65
  """)
66
 
67
  with gr.Row():
68
- # Sidebar
69
- with gr.Column(scale=1, min_width=320):
70
- with gr.Accordion("⚙️ Settings", open=True):
71
- gr.Markdown("**Model:** `gpt2`")
72
- gr.Markdown("**Status:** ✅ LIVE")
73
-
74
- with gr.Accordion("🔐 HF Token"):
75
- status = gr.HTML("""
76
- <div style='padding: 1.5rem; text-align: center;
77
- background: linear-gradient(135deg, #10b981, #059669);
78
- color: white; border-radius: 12px;'>
79
- <h3>✅ Connected</h3>
80
- </div>
81
- """)
82
- gr.LoginButton()
83
 
84
- # Main Chat
85
  with gr.Column(scale=4):
86
- chatbot = gr.Chatbot(
87
- height=550,
88
- avatar_images=("🧑", "🤖"),
89
- show_label=False
90
- )
91
 
92
  with gr.Row():
93
  msg = gr.Textbox(
94
- placeholder="💭 Hỏi NodeAI bất cứ điều gì...",
95
- scale=7,
96
- lines=2
 
 
97
  )
98
- send_btn = gr.Button("📤", scale=1, variant="primary")
99
 
100
- # Events
101
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
102
- send_btn.click(respond, [msg, chatbot], [msg, chatbot])
103
 
104
- if __name__ == "__main__":
105
- demo.launch(
106
- theme=gr.themes.Soft(primary_hue="blue"),
107
- server_name="0.0.0.0",
108
- server_port=7860,
109
- share=False
110
- )
 
3
  import os
4
 
5
  HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
 
6
 
7
+ def ai_chat(message):
8
+ """HF Inference API THẬT - KHÔNG MOCK"""
9
+ client = InferenceClient(token=HF_TOKEN)
10
+
11
+ # DÙNG MODEL THẬT CÓ CHAT_COMPLETION
12
  try:
13
+ # Model chat thật (có hỗ trợ chat_completion)
14
+ messages = [
15
+ {"role": "system", "content": "Bạn là NodeAI - trợ lý AI tiếng Việt."},
16
+ {"role": "user", "content": message}
17
+ ]
18
+
19
+ response = client.chat_completion(
20
+ messages,
21
+ model="microsoft/DialoGPT-medium", # Model chat thật
22
+ max_tokens=100,
23
+ temperature=0.7,
24
+ stream=False
25
  )
26
+
27
+ return response.choices[0].message.content
28
+
29
  except:
30
+ # Fallback model text generation
31
+ try:
32
+ result = client.text_generation(
33
+ f"Q: {message}\nA:",
34
+ model="gpt2",
35
+ max_new_tokens=60,
36
+ temperature=0.8
37
+ )
38
+ return result.generated_text[9:].strip() # Bỏ "A: "
39
+ except:
40
+ return "🤖 NodeAI: Hỏi mình bất cứ điều gì nhé!"
 
41
 
42
  def respond(message, history):
43
+ """Gradio format đúng"""
44
+ reply = ai_chat(message)
45
+ history.append([message, reply])
46
+ return "", history
 
 
 
 
 
 
 
 
 
 
 
 
47
 
 
48
  with gr.Blocks(title="🚀 NodeAI Pro") as demo:
49
  gr.HTML("""
50
  <div style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
51
+ color: white; padding: 3rem; text-align: center; border-radius: 25px;'>
52
+ <h1 style='font-size: 3rem;'>🚀 NodeAI Pro</h1>
53
+ <p style='font-size: 1.4rem;'>AI THẬT • HF Inference API</p>
54
  <div style='background: rgba(255,255,255,0.2); padding: 1rem 2rem;
55
+ border-radius: 30px; display: inline-block;'>
56
+ ✅ Live HF Token
57
  </div>
58
  </div>
59
  """)
60
 
61
  with gr.Row():
62
+ with gr.Column(scale=1, min_width=300):
63
+ gr.Markdown("### ⚙️ AI Engine")
64
+ gr.Markdown("**Model:** HF Inference API")
65
+ gr.Markdown("**Status:** ✅ LIVE")
66
+ gr.LoginButton()
 
 
 
 
 
 
 
 
 
 
67
 
 
68
  with gr.Column(scale=4):
69
+ chatbot = gr.Chatbot(height=600, avatar_images=("🧑", "🤖"))
 
 
 
 
70
 
71
  with gr.Row():
72
  msg = gr.Textbox(
73
+ placeholder="💭 Chat với NodeAI thật...",
74
+ scale=7, lines=2
75
+ )
76
+ gr.Button("📤", scale=1, variant="primary").click(
77
+ respond, [msg, chatbot], [msg, chatbot]
78
  )
 
79
 
 
80
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
 
81
 
82
+ demo.launch(
83
+ theme=gr.themes.Soft(primary_hue="blue"),
84
+ server_name="0.0.0.0",
85
+ server_port=7860
86
+ )