Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,93 +2,97 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# 1. Khởi tạo Client (
|
| 6 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
|
|
|
| 7 |
client = InferenceClient("gpt2", token=HF_TOKEN)
|
| 8 |
|
| 9 |
-
# 2. CSS
|
| 10 |
custom_css = """
|
| 11 |
.header-pro {
|
| 12 |
-
background: linear-gradient(135deg, #
|
| 13 |
-
padding:
|
| 14 |
-
border-radius:
|
| 15 |
color: white;
|
| 16 |
text-align: center;
|
| 17 |
margin-bottom: 20px;
|
| 18 |
}
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
border-radius: 10px;
|
| 23 |
}
|
| 24 |
"""
|
| 25 |
|
| 26 |
-
def
|
| 27 |
-
#
|
| 28 |
-
#
|
| 29 |
-
user_input = message[-1]["content"][0]["text"]
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
# Gọi API thực tế
|
| 36 |
try:
|
| 37 |
-
# GPT-2
|
| 38 |
-
|
| 39 |
-
|
| 40 |
except Exception as e:
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
# Trả về chuỗi rỗng cho ô input và history mới
|
| 46 |
return "", history
|
| 47 |
|
| 48 |
with gr.Blocks(title="NodeAI Pro") as demo:
|
| 49 |
-
# Header
|
| 50 |
gr.HTML("""
|
| 51 |
<div class="header-pro">
|
| 52 |
-
<h1>
|
| 53 |
-
<p>Hệ thống
|
| 54 |
</div>
|
| 55 |
""")
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
-
# Sidebar
|
| 59 |
with gr.Column(scale=1):
|
| 60 |
with gr.Group():
|
| 61 |
-
gr.Markdown("###
|
| 62 |
gr.LoginButton()
|
| 63 |
gr.Markdown("---")
|
| 64 |
-
gr.Markdown(f"**
|
| 65 |
-
gr.Markdown("**Model:** `
|
| 66 |
-
gr.Markdown("**
|
| 67 |
-
|
| 68 |
-
# Chat
|
| 69 |
-
with gr.Column(scale=
|
| 70 |
chatbot = gr.Chatbot(
|
| 71 |
label="Hội thoại",
|
| 72 |
-
|
| 73 |
-
height=
|
| 74 |
-
bubble_full_width=False
|
| 75 |
)
|
| 76 |
|
| 77 |
with gr.Row():
|
| 78 |
msg_input = gr.Textbox(
|
| 79 |
-
placeholder="
|
| 80 |
show_label=False,
|
| 81 |
-
scale=
|
| 82 |
)
|
| 83 |
submit_btn = gr.Button("Gửi", variant="primary", scale=1)
|
| 84 |
|
| 85 |
-
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
|
| 89 |
-
submit_btn.click(
|
| 90 |
-
|
| 91 |
|
| 92 |
-
# 3. CHÍNH XÁC: Truyền CSS vào launch() theo yêu cầu của Gradio 6.0+
|
| 93 |
if __name__ == "__main__":
|
|
|
|
| 94 |
demo.launch(css=custom_css)
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# 1. Khởi tạo Client (Sử dụng Secret HF_TOKEN)
|
| 6 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 7 |
+
# Sử dụng gpt2 để sinh văn bản (text-generation)
|
| 8 |
client = InferenceClient("gpt2", token=HF_TOKEN)
|
| 9 |
|
| 10 |
+
# 2. CSS chuyên nghiệp
|
| 11 |
custom_css = """
|
| 12 |
.header-pro {
|
| 13 |
+
background: linear-gradient(135deg, #1e3a8a 0%, #7e22ce 100%);
|
| 14 |
+
padding: 25px;
|
| 15 |
+
border-radius: 12px;
|
| 16 |
color: white;
|
| 17 |
text-align: center;
|
| 18 |
margin-bottom: 20px;
|
| 19 |
}
|
| 20 |
+
#chatbot-style {
|
| 21 |
+
border-radius: 10px !important;
|
| 22 |
+
border: 1px solid #e5e7eb !important;
|
|
|
|
| 23 |
}
|
| 24 |
"""
|
| 25 |
|
| 26 |
+
def respond(message, history):
|
| 27 |
+
# Định dạng msg đúng yêu cầu Gradio 6.9.0 của bạn:
|
| 28 |
+
# [{"role": "user", "content": [{"type": "text", "text": msg}]}]
|
|
|
|
| 29 |
|
| 30 |
+
# Lấy text từ message (Gradio 6 truyền list các dict tin nhắn)
|
| 31 |
+
if isinstance(message, list):
|
| 32 |
+
user_text = message[-1]["content"][0]["text"]
|
| 33 |
+
else:
|
| 34 |
+
user_text = message
|
| 35 |
+
|
| 36 |
+
# Cấu trúc tin nhắn mới để lưu vào history
|
| 37 |
+
user_msg = {"role": "user", "content": [{"type": "text", "text": user_text}]}
|
| 38 |
+
history.append(user_msg)
|
| 39 |
|
| 40 |
+
# Gọi API Inference thực tế
|
|
|
|
| 41 |
try:
|
| 42 |
+
# GPT-2 trả về text nối tiếp từ input
|
| 43 |
+
api_output = client.text_generation(user_text, max_new_tokens=100)
|
| 44 |
+
bot_text = api_output
|
| 45 |
except Exception as e:
|
| 46 |
+
bot_text = f"❌ API Error: {str(e)}"
|
| 47 |
|
| 48 |
+
bot_msg = {"role": "assistant", "content": [{"type": "text", "text": bot_text}]}
|
| 49 |
+
history.append(bot_msg)
|
| 50 |
|
|
|
|
| 51 |
return "", history
|
| 52 |
|
| 53 |
with gr.Blocks(title="NodeAI Pro") as demo:
|
| 54 |
+
# Header UI Pro
|
| 55 |
gr.HTML("""
|
| 56 |
<div class="header-pro">
|
| 57 |
+
<h1 style='margin:0'>🚀 NodeAI Pro</h1>
|
| 58 |
+
<p style='margin:0; opacity: 0.8;'>Hệ thống Trợ lý AI Thông minh (V6.9.0)</p>
|
| 59 |
</div>
|
| 60 |
""")
|
| 61 |
|
| 62 |
with gr.Row():
|
| 63 |
+
# Sidebar
|
| 64 |
with gr.Column(scale=1):
|
| 65 |
with gr.Group():
|
| 66 |
+
gr.Markdown("### 🛠️ Hệ thống")
|
| 67 |
gr.LoginButton()
|
| 68 |
gr.Markdown("---")
|
| 69 |
+
gr.Markdown(f"**Token:** {'✅ OK' if HF_TOKEN else '⚠️ Thiếu'}")
|
| 70 |
+
gr.Markdown("**Model:** `gpt2` (Real API)")
|
| 71 |
+
gr.Markdown("**Trạng thái:** Sẵn sàng")
|
| 72 |
+
|
| 73 |
+
# Chat area
|
| 74 |
+
with gr.Column(scale=4):
|
| 75 |
chatbot = gr.Chatbot(
|
| 76 |
label="Hội thoại",
|
| 77 |
+
elem_id="chatbot-style",
|
| 78 |
+
height=550
|
|
|
|
| 79 |
)
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
msg_input = gr.Textbox(
|
| 83 |
+
placeholder="Nhập yêu cầu cho NodeAI Pro...",
|
| 84 |
show_label=False,
|
| 85 |
+
scale=9
|
| 86 |
)
|
| 87 |
submit_btn = gr.Button("Gửi", variant="primary", scale=1)
|
| 88 |
|
| 89 |
+
clear = gr.Button("🗑️ Xóa hội thoại", size="sm")
|
| 90 |
|
| 91 |
+
# Sự kiện điều khiển
|
| 92 |
+
msg_input.submit(respond, [msg_input, chatbot], [msg_input, chatbot])
|
| 93 |
+
submit_btn.click(respond, [msg_input, chatbot], [msg_input, chatbot])
|
| 94 |
+
clear.click(lambda: [], None, chatbot)
|
| 95 |
|
|
|
|
| 96 |
if __name__ == "__main__":
|
| 97 |
+
# CSS được đẩy vào launch() theo chuẩn Gradio 6
|
| 98 |
demo.launch(css=custom_css)
|