Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,92 +8,87 @@ client = InferenceClient("gpt2", token=HF_TOKEN)
|
|
| 8 |
|
| 9 |
# 2. CSS Custom cho Giao diện Pro
|
| 10 |
custom_css = """
|
| 11 |
-
.header-
|
| 12 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 13 |
-
padding:
|
| 14 |
-
border-radius:
|
| 15 |
color: white;
|
| 16 |
text-align: center;
|
| 17 |
margin-bottom: 20px;
|
| 18 |
-
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 19 |
}
|
| 20 |
-
.sidebar-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
border-radius:
|
| 24 |
-
height: 100%;
|
| 25 |
-
}
|
| 26 |
-
#chatbot-container {
|
| 27 |
-
height: 600px !important;
|
| 28 |
}
|
| 29 |
"""
|
| 30 |
|
| 31 |
def chat_fn(message, history):
|
| 32 |
-
#
|
| 33 |
-
# [{"role": "user", "content": [{"type": "text", "text":
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
# Gọi API Inference thật (GPT-2)
|
| 39 |
response_text = ""
|
|
|
|
| 40 |
try:
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
max_new_tokens=150,
|
| 45 |
-
temperature=0.7,
|
| 46 |
-
stream=False # GPT-2 Inference API đôi khi ổn định hơn với False
|
| 47 |
-
)
|
| 48 |
-
response_text = full_response
|
| 49 |
except Exception as e:
|
| 50 |
-
response_text = f"⚠️ Lỗi
|
| 51 |
-
|
| 52 |
-
# Cập nhật lịch sử theo format chuẩn
|
| 53 |
-
history.append({"role": "user", "content": [{"type": "text", "text": user_input}]})
|
| 54 |
history.append({"role": "assistant", "content": [{"type": "text", "text": response_text}]})
|
| 55 |
|
|
|
|
| 56 |
return "", history
|
| 57 |
|
| 58 |
-
with gr.Blocks(
|
| 59 |
-
# Header Pro
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
with gr.Row():
|
| 65 |
-
# Sidebar (
|
| 66 |
-
with gr.Column(scale=1
|
| 67 |
-
gr.
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
-
# Chat Main
|
| 75 |
with gr.Column(scale=3):
|
| 76 |
chatbot = gr.Chatbot(
|
| 77 |
label="Hội thoại",
|
| 78 |
-
type="messages", #
|
| 79 |
-
|
| 80 |
bubble_full_width=False
|
| 81 |
)
|
| 82 |
-
msg_input = gr.Textbox(
|
| 83 |
-
placeholder="Nhập câu hỏi tại đây...",
|
| 84 |
-
show_label=False,
|
| 85 |
-
container=False
|
| 86 |
-
)
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
-
msg_input.submit(chat_fn, [msg_input, chatbot], [msg_input, chatbot])
|
| 95 |
submit_btn.click(chat_fn, [msg_input, chatbot], [msg_input, chatbot])
|
| 96 |
clear_btn.click(lambda: [], None, chatbot)
|
| 97 |
|
|
|
|
| 98 |
if __name__ == "__main__":
|
| 99 |
-
demo.launch()
|
|
|
|
| 8 |
|
| 9 |
# 2. CSS Custom cho Giao diện Pro
|
| 10 |
custom_css = """
|
| 11 |
+
.header-pro {
|
| 12 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 13 |
+
padding: 30px;
|
| 14 |
+
border-radius: 15px;
|
| 15 |
color: white;
|
| 16 |
text-align: center;
|
| 17 |
margin-bottom: 20px;
|
|
|
|
| 18 |
}
|
| 19 |
+
.sidebar-panel {
|
| 20 |
+
background-color: #f0f2f6;
|
| 21 |
+
padding: 20px;
|
| 22 |
+
border-radius: 10px;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
}
|
| 24 |
"""
|
| 25 |
|
| 26 |
def chat_fn(message, history):
|
| 27 |
+
# Trích xuất nội dung từ định dạng tin nhắn mới của Gradio 6.9.0
|
| 28 |
+
# message: [{"role": "user", "content": [{"type": "text", "text": "..."}]}]
|
| 29 |
+
user_input = message[-1]["content"][0]["text"]
|
| 30 |
|
| 31 |
+
# Khởi tạo tin nhắn phản hồi của Assistant theo format Gradio 6
|
| 32 |
+
history.append({"role": "user", "content": [{"type": "text", "text": user_input}]})
|
| 33 |
|
|
|
|
| 34 |
response_text = ""
|
| 35 |
+
# Gọi API thực tế
|
| 36 |
try:
|
| 37 |
+
# GPT-2 sinh văn bản dựa trên input
|
| 38 |
+
output = client.text_generation(user_input, max_new_tokens=100)
|
| 39 |
+
response_text = output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
+
response_text = f"⚠️ Lỗi API: {str(e)}"
|
| 42 |
+
|
|
|
|
|
|
|
| 43 |
history.append({"role": "assistant", "content": [{"type": "text", "text": response_text}]})
|
| 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 Pro dùng HTML để tránh lỗi gr.Div
|
| 50 |
+
gr.HTML("""
|
| 51 |
+
<div class="header-pro">
|
| 52 |
+
<h1>🤖 NodeAI Pro</h1>
|
| 53 |
+
<p>Hệ thống AI Assistant tối ưu cho hiệu suất</p>
|
| 54 |
+
</div>
|
| 55 |
+
""")
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
+
# Sidebar (Sử dụng integer scale)
|
| 59 |
+
with gr.Column(scale=1):
|
| 60 |
+
with gr.Group():
|
| 61 |
+
gr.Markdown("### ⚙️ Control Panel")
|
| 62 |
+
gr.LoginButton()
|
| 63 |
+
gr.Markdown("---")
|
| 64 |
+
gr.Markdown(f"**Status:** {'🟢 Ready' if HF_TOKEN else '🔴 Missing Token'}")
|
| 65 |
+
gr.Markdown("**Model:** `GPT-2` Standard")
|
| 66 |
+
gr.Markdown("**Version:** 6.9.0")
|
| 67 |
|
| 68 |
+
# Chat Main
|
| 69 |
with gr.Column(scale=3):
|
| 70 |
chatbot = gr.Chatbot(
|
| 71 |
label="Hội thoại",
|
| 72 |
+
type="messages", # Định dạng tin nhắn chuẩn Gradio 6
|
| 73 |
+
height=500,
|
| 74 |
bubble_full_width=False
|
| 75 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
with gr.Row():
|
| 78 |
+
msg_input = gr.Textbox(
|
| 79 |
+
placeholder="Gõ lệnh tại đây...",
|
| 80 |
+
show_label=False,
|
| 81 |
+
scale=4
|
| 82 |
+
)
|
| 83 |
+
submit_btn = gr.Button("Gửi", variant="primary", scale=1)
|
| 84 |
+
|
| 85 |
+
clear_btn = gr.Button("🗑️ Clear Chat", size="sm")
|
| 86 |
|
| 87 |
+
# Xử lý sự kiện
|
| 88 |
+
submit_event = msg_input.submit(chat_fn, [msg_input, chatbot], [msg_input, chatbot])
|
|
|
|
| 89 |
submit_btn.click(chat_fn, [msg_input, chatbot], [msg_input, chatbot])
|
| 90 |
clear_btn.click(lambda: [], None, chatbot)
|
| 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)
|