lamgamepro1 commited on
Commit
201b949
·
verified ·
1 Parent(s): 977e4df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -62
app.py CHANGED
@@ -2,72 +2,36 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
 
 
5
  HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
 
6
 
7
- def ai_real_chat(message):
8
- """AI THẬT - Model + Task đúng"""
9
- client = InferenceClient(token=HF_TOKEN)
10
-
11
  try:
12
- # BART CNN - summarization task (working thật!)
13
- result = client.summarization(
14
- f"{message}. Hãy tóm tắt và trả lời thông minh.",
15
- model="facebook/bart-large-cnn"
16
- )
17
- return result[0]['summary_text']
18
-
19
- except:
20
- # GPT2 fallback (text-generation thật)
21
- try:
22
- result = client.text_generation(
23
- f"Tóm tắt: {message}",
24
- model="gpt2",
25
- max_new_tokens=60
26
- )
27
- return result[0].generated_text[len(f"Tóm tắt: {message}"):]
28
- except:
29
- return "NodeAI: Tôi đã hiểu câu hỏi của bạn!"
30
 
31
- def respond(message, history):
32
- """FORMAT GRADIO 6.9.0"""
33
- if not message.strip():
34
- return "", history
35
-
36
- reply = ai_real_chat(message)
37
-
38
- # FORMAT ĐÚNG
39
- user_msg = {"role": "user", "content": [{"type": "text", "text": message}]}
40
- ai_msg = {"role": "assistant", "content": [{"type": "text", "text": reply}]}
41
-
42
- return "", history + [user_msg, ai_msg]
43
 
44
- with gr.Blocks(title="🚀 NodeAI Pro") as demo:
45
- gr.HTML("""
46
- <div style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
47
- color: white; padding: 3rem; text-align: center; border-radius: 25px;'>
48
- <h1 style='font-size: 3rem;'>🚀 NodeAI Pro</h1>
49
- <p style='font-size: 1.4rem;'>AI THẬT • BART + GPT2</p>
50
- </div>
51
- """)
52
-
53
- with gr.Row():
54
- with gr.Column(scale=1):
55
- gr.Markdown("**✅ HF_TOKEN Active**")
56
- gr.Markdown("**🤖 BART-large-cnn**")
57
- gr.LoginButton()
58
-
59
- with gr.Column(scale=4):
60
- chatbot = gr.Chatbot(height=600)
61
- with gr.Row():
62
- msg = gr.Textbox(placeholder="💭 Chat...", scale=7, lines=2)
63
- send_btn = gr.Button("📤", scale=1, variant="primary")
64
-
65
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
66
- send_btn.click(respond, [msg, chatbot], [msg, chatbot])
67
 
68
  if __name__ == "__main__":
69
- demo.launch(
70
- theme=gr.themes.Soft(),
71
- server_name="0.0.0.0",
72
- server_port=7860
73
- )
 
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
+ # 1. Khởi tạo Client với GPT-2
6
+ # Token được lấy tự động từ Secret HF_TOKEN của Space
7
  HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
8
+ client = InferenceClient("gpt2", token=HF_TOKEN)
9
 
10
+ def respond(message, history):
11
+ # 2. Xử lý định dạng tin nhắn của Gradio 6.9.0
12
+ # Trích xuất text từ cấu trúc: [{"role": "user", "content": [{"type": "text", "text": msg}]}]
 
13
  try:
14
+ user_prompt = message[0]["content"][0]["text"]
15
+ except (IndexKey, KeyError, TypeError):
16
+ user_prompt = str(message)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # 3. Gọi API Inference (không dùng fallback)
19
+ response = ""
20
+ # Stream kết quả từ GPT-2
21
+ for token in client.text_generation(user_prompt, max_new_tokens=100, stream=True):
22
+ response += token
23
+ yield response
 
 
 
 
 
 
24
 
25
+ # 4. Giao diện UI Pro với ChatInterface
26
+ demo = gr.ChatInterface(
27
+ fn=respond,
28
+ title="🤖 NodeAI Pro",
29
+ description="Hệ thống trợ lý trí tuệ nhân tạo thế hệ mới",
30
+ type="messages", # Bắt buộc cho định dạng tin nhắn mới
31
+ fill_height=True,
32
+ # Thêm nút đăng nhập HF để quản lý token status
33
+ head=gr.LoginButton() if not HF_TOKEN else None
34
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  if __name__ == "__main__":
37
+ demo.launch()