Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,97 +2,89 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# 1.
|
| 6 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 7 |
-
|
| 8 |
-
client = InferenceClient(
|
| 9 |
|
| 10 |
-
# 2. CSS
|
| 11 |
custom_css = """
|
| 12 |
-
.header-pro {
|
| 13 |
-
background: linear-gradient(135deg, #
|
| 14 |
-
padding:
|
| 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
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
user_text = message[-1]["content"][0]["text"]
|
| 33 |
else:
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
user_msg = {"role": "user", "content": [{"type": "text", "text":
|
| 38 |
history.append(user_msg)
|
| 39 |
|
| 40 |
-
# Gọi API Inference
|
| 41 |
try:
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 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
|
| 59 |
</div>
|
| 60 |
""")
|
| 61 |
|
| 62 |
with gr.Row():
|
| 63 |
-
# Sidebar
|
| 64 |
with gr.Column(scale=1):
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
gr.Markdown("**Trạng thái:** Sẵn sàng")
|
| 72 |
-
|
| 73 |
-
# Chat area
|
| 74 |
with gr.Column(scale=4):
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
elem_id="chatbot-style",
|
| 78 |
-
height=550
|
| 79 |
-
)
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
msg_input = gr.Textbox(
|
| 83 |
-
placeholder="Nhập
|
| 84 |
show_label=False,
|
| 85 |
-
scale=
|
| 86 |
)
|
| 87 |
-
submit_btn = gr.Button("
|
| 88 |
|
| 89 |
-
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
msg_input.submit(
|
| 93 |
-
submit_btn.click(
|
| 94 |
-
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
-
# CSS
|
| 98 |
demo.launch(css=custom_css)
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# 1. Cấu hình Model & Token (Sử dụng Llama-3.2 để Pro hơn)
|
| 6 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 7 |
+
model_id = "meta-llama/Llama-3.2-1B-Instruct"
|
| 8 |
+
client = InferenceClient(model=model_id, token=HF_TOKEN)
|
| 9 |
|
| 10 |
+
# 2. CSS tinh chỉnh cho Gradio 6.x
|
| 11 |
custom_css = """
|
| 12 |
+
.header-pro {
|
| 13 |
+
background: linear-gradient(135deg, #000428 0%, #004e92 100%);
|
| 14 |
+
padding: 20px; border-radius: 15px; color: white; text-align: center; margin-bottom: 20px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
}
|
| 16 |
+
.footer-info { font-size: 0.8em; text-align: center; color: #666; margin-top: 10px; }
|
| 17 |
"""
|
| 18 |
|
| 19 |
+
def chat_fn(message, history):
|
| 20 |
+
# Trích xuất text từ định dạng tin nhắn Gradio 6
|
| 21 |
+
if isinstance(message, dict):
|
| 22 |
+
user_input = message.get("text", "")
|
| 23 |
+
elif isinstance(message, list):
|
| 24 |
+
user_input = message[-1]["content"][0]["text"]
|
|
|
|
| 25 |
else:
|
| 26 |
+
user_input = str(message)
|
| 27 |
+
|
| 28 |
+
# Khởi tạo history nếu trống
|
| 29 |
+
if history is None: history = []
|
| 30 |
|
| 31 |
+
# Format tin nhắn người dùng
|
| 32 |
+
user_msg = {"role": "user", "content": [{"type": "text", "text": user_input}]}
|
| 33 |
history.append(user_msg)
|
| 34 |
|
| 35 |
+
# Gọi API Inference
|
| 36 |
try:
|
| 37 |
+
# Sử dụng chat_completion để có kết quả thông minh hơn
|
| 38 |
+
response = client.chat_completion(
|
| 39 |
+
messages=[{"role": "user", "content": user_input}],
|
| 40 |
+
max_tokens=500,
|
| 41 |
+
temperature=0.7
|
| 42 |
+
)
|
| 43 |
+
bot_res = response.choices[0].message.content
|
| 44 |
except Exception as e:
|
| 45 |
+
bot_res = f"⚠️ Lỗi kết nối: Hãy đảm bảo bạn đã add HF_TOKEN vào Settings. (Chi tiết: {str(e)})"
|
| 46 |
+
|
| 47 |
+
# Format tin nhắn Bot
|
| 48 |
+
bot_msg = {"role": "assistant", "content": [{"type": "text", "text": bot_res}]}
|
| 49 |
history.append(bot_msg)
|
| 50 |
|
| 51 |
return "", history
|
| 52 |
|
| 53 |
with gr.Blocks(title="NodeAI Pro") as demo:
|
|
|
|
| 54 |
gr.HTML("""
|
| 55 |
<div class="header-pro">
|
| 56 |
<h1 style='margin:0'>🚀 NodeAI Pro</h1>
|
| 57 |
+
<p style='margin:0'>High-Performance AI Assistant</p>
|
| 58 |
</div>
|
| 59 |
""")
|
| 60 |
|
| 61 |
with gr.Row():
|
|
|
|
| 62 |
with gr.Column(scale=1):
|
| 63 |
+
gr.Markdown("### 🛠️ Core Engine")
|
| 64 |
+
gr.LoginButton()
|
| 65 |
+
gr.Markdown(f"**Engine:** `Llama-3.2-1B`")
|
| 66 |
+
gr.Markdown(f"**API Status:** {'🟢 Online' if HF_TOKEN else '🔴 Offline'}")
|
| 67 |
+
clear_btn = gr.Button("🗑️ Reset Session", variant="secondary")
|
| 68 |
+
|
|
|
|
|
|
|
|
|
|
| 69 |
with gr.Column(scale=4):
|
| 70 |
+
# Không dùng type="messages" ở đây để tránh TypeError trên một số môi trường
|
| 71 |
+
chatbot = gr.Chatbot(label="NodeAI Chat Console", height=500)
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
with gr.Row():
|
| 74 |
msg_input = gr.Textbox(
|
| 75 |
+
placeholder="Nhập câu hỏi...",
|
| 76 |
show_label=False,
|
| 77 |
+
scale=8
|
| 78 |
)
|
| 79 |
+
submit_btn = gr.Button("Send", variant="primary", scale=2)
|
| 80 |
|
| 81 |
+
gr.HTML("<div class='footer-info'>NodeAI Pro v1.0.9 | Powered by Hugging Face Inference API</div>")
|
| 82 |
|
| 83 |
+
# Xử lý Logic
|
| 84 |
+
msg_input.submit(chat_fn, [msg_input, chatbot], [msg_input, chatbot])
|
| 85 |
+
submit_btn.click(chat_fn, [msg_input, chatbot], [msg_input, chatbot])
|
| 86 |
+
clear_btn.click(lambda: None, None, chatbot)
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
+
# Đưa CSS vào launch() để fix UserWarning
|
| 90 |
demo.launch(css=custom_css)
|