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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -54
app.py CHANGED
@@ -5,82 +5,79 @@ import os
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
- )
 
 
 
5
  HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
6
 
7
  def ai_chat(message):
8
+ """AI THẬT - HF Inference API"""
 
 
 
9
  try:
10
+ client = InferenceClient(token=HF_TOKEN)
11
+ # Text generation model ổn định
12
+ result = client.text_generation(
13
+ f"Q: {message}\nA:",
14
+ model="gpt2",
15
+ max_new_tokens=80,
 
 
 
 
16
  temperature=0.7,
17
+ do_sample=True
18
  )
19
+ return result[0].generated_text[9:].strip()
 
 
20
  except:
21
+ return f"🤖 {message[:50]}..."
 
 
 
 
 
 
 
 
 
 
22
 
23
  def respond(message, history):
24
+ """✅ FORMAT GRADIO 6.9.0 CHUẨN XÁC"""
25
+ if not message:
26
+ return "", history
27
+
28
  reply = ai_chat(message)
29
+
30
+ # GRADIO 6.9.0: list of dicts {"role": "user", "content": "..."}
31
+ new_msg = {
32
+ "role": "user",
33
+ "content": [{"type": "text", "text": message}]
34
+ }
35
+ new_reply = {
36
+ "role": "assistant",
37
+ "content": [{"type": "text", "text": reply}]
38
+ }
39
+
40
+ return "", history + [new_msg, new_reply]
41
 
42
  with gr.Blocks(title="🚀 NodeAI Pro") as demo:
43
  gr.HTML("""
44
  <div style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
45
+ color: white; padding: 2.5rem; text-align: center; border-radius: 20px;'>
46
+ <h1 style='font-size: 2.8rem;'>🚀 NodeAI Pro</h1>
47
+ <p style='font-size: 1.3rem;'>AI THẬT • HF Inference API</p>
 
 
 
 
48
  </div>
49
  """)
50
 
51
  with gr.Row():
52
  with gr.Column(scale=1, min_width=300):
53
+ gr.Markdown("# ⚙️ Status")
54
+ gr.Markdown("**✅ HF_TOKEN Active**")
55
+ gr.Markdown("**🤖 GPT-2 Live**")
56
  gr.LoginButton()
57
 
58
  with gr.Column(scale=4):
59
+ chatbot = gr.Chatbot(
60
+ height=600,
61
+ show_label=False,
62
+ avatar_images=("🧑", "🤖")
63
+ )
64
 
65
  with gr.Row():
66
  msg = gr.Textbox(
67
+ placeholder="💭 Chat với AI thật...",
68
+ scale=7,
69
+ lines=2
 
 
70
  )
71
+ send_btn = gr.Button("📤", scale=1, variant="primary")
72
 
73
+ # Events
74
+ msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
75
+ send_btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
76
 
77
+ if __name__ == "__main__":
78
+ demo.launch(
79
+ theme=gr.themes.Soft(primary_hue="blue"),
80
+ server_name="0.0.0.0",
81
+ server_port=7860,
82
+ share=False
83
+ )