Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
try:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 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 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 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 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|