Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,20 +3,15 @@ from huggingface_hub import InferenceClient
|
|
| 3 |
|
| 4 |
MODEL = "mistralai/Mistral-7B-Instruct-v0.1"
|
| 5 |
|
| 6 |
-
# In-memory chat history
|
| 7 |
chat_history = {}
|
| 8 |
|
| 9 |
-
def respond(message, history, system_message, max_tokens, temperature, top_p
|
| 10 |
-
"""
|
| 11 |
-
if not hf_token or not hf_token.token:
|
| 12 |
-
yield "❌ Vui lòng đăng nhập Hugging Face (nút Login sidebar)!"
|
| 13 |
-
return
|
| 14 |
-
|
| 15 |
try:
|
| 16 |
-
|
|
|
|
| 17 |
messages = [{"role": "system", "content": system_message}]
|
| 18 |
|
| 19 |
-
# Convert history to messages format
|
| 20 |
for user_msg, assistant_msg in history:
|
| 21 |
messages.append({"role": "user", "content": str(user_msg)})
|
| 22 |
if assistant_msg:
|
|
@@ -35,71 +30,53 @@ def respond(message, history, system_message, max_tokens, temperature, top_p, hf
|
|
| 35 |
if chunk.choices[0].delta.content:
|
| 36 |
response += chunk.choices[0].delta.content
|
| 37 |
yield response
|
| 38 |
-
|
| 39 |
except Exception as e:
|
| 40 |
-
yield f"❌ Lỗi
|
| 41 |
|
| 42 |
-
def load_history(
|
| 43 |
-
"""
|
| 44 |
-
|
| 45 |
-
return []
|
| 46 |
-
|
| 47 |
-
user_id = hash(hf_token.token) % 1000000
|
| 48 |
-
if user_id in chat_history:
|
| 49 |
-
return chat_history[user_id][-10:]
|
| 50 |
-
return []
|
| 51 |
|
| 52 |
-
def clear_history(
|
| 53 |
-
"""
|
| 54 |
-
if hf_token and hf_token.token:
|
| 55 |
-
user_id = hash(hf_token.token) % 1000000
|
| 56 |
-
chat_history[user_id] = []
|
| 57 |
return []
|
| 58 |
|
| 59 |
with gr.Blocks(title="NodeAI") as demo:
|
| 60 |
-
gr.Markdown("# 🚀 NodeAI -
|
| 61 |
|
| 62 |
with gr.Row():
|
| 63 |
# Sidebar
|
| 64 |
with gr.Column(scale=1, min_width=350):
|
| 65 |
gr.Markdown("## 🔐 Hugging Face")
|
| 66 |
-
gr.LoginButton() # ✅
|
| 67 |
|
| 68 |
-
gr.Markdown("## 📚 Lịch sử
|
| 69 |
-
history_display = gr.Chatbot(
|
| 70 |
-
height=300,
|
| 71 |
-
label="Lịch sử tin nhắn",
|
| 72 |
-
avatar_images=("👤", "🤖")
|
| 73 |
-
)
|
| 74 |
|
| 75 |
with gr.Row():
|
| 76 |
-
load_btn = gr.Button("📥 Load
|
| 77 |
-
clear_btn = gr.Button("🗑️ Clear
|
| 78 |
|
| 79 |
# Main Chat
|
| 80 |
with gr.Column(scale=4):
|
| 81 |
chatbot = gr.ChatInterface(
|
| 82 |
-
respond, # ✅
|
| 83 |
additional_inputs=[
|
| 84 |
-
gr.Textbox(
|
| 85 |
-
value="Bạn là NodeAI - trợ lý AI thông minh, trả lời tiếng Việt.",
|
| 86 |
-
label="System prompt"
|
| 87 |
-
),
|
| 88 |
gr.Slider(1, 1024, value=512, step=1, label="Max tokens"),
|
| 89 |
gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
|
| 90 |
gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p"),
|
| 91 |
],
|
| 92 |
-
title="💬 NodeAI Chat"
|
| 93 |
-
description="**Đăng nhập HF → Chat → Auto save history theo user**"
|
| 94 |
)
|
| 95 |
|
| 96 |
-
# ✅
|
| 97 |
load_btn.click(load_history, outputs=history_display)
|
| 98 |
clear_btn.click(clear_history, outputs=history_display)
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
| 101 |
demo.launch(
|
| 102 |
-
theme=gr.themes.Soft(primary_hue="blue"),
|
| 103 |
server_name="0.0.0.0",
|
| 104 |
server_port=7860
|
| 105 |
)
|
|
|
|
| 3 |
|
| 4 |
MODEL = "mistralai/Mistral-7B-Instruct-v0.1"
|
| 5 |
|
|
|
|
| 6 |
chat_history = {}
|
| 7 |
|
| 8 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 9 |
+
"""6 args - ChatInterface tự inject OAuth token"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
+
# Gradio tự inject token qua ChatInterface
|
| 12 |
+
client = InferenceClient(model=MODEL)
|
| 13 |
messages = [{"role": "system", "content": system_message}]
|
| 14 |
|
|
|
|
| 15 |
for user_msg, assistant_msg in history:
|
| 16 |
messages.append({"role": "user", "content": str(user_msg)})
|
| 17 |
if assistant_msg:
|
|
|
|
| 30 |
if chunk.choices[0].delta.content:
|
| 31 |
response += chunk.choices[0].delta.content
|
| 32 |
yield response
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
+
yield f"❌ Lỗi: {str(e)}"
|
| 35 |
|
| 36 |
+
def load_history():
|
| 37 |
+
"""No args - Gradio tự handle OAuth"""
|
| 38 |
+
return [] # Placeholder - cần session storage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
def clear_history():
|
| 41 |
+
"""No args"""
|
|
|
|
|
|
|
|
|
|
| 42 |
return []
|
| 43 |
|
| 44 |
with gr.Blocks(title="NodeAI") as demo:
|
| 45 |
+
gr.Markdown("# 🚀 NodeAI - AI Chat")
|
| 46 |
|
| 47 |
with gr.Row():
|
| 48 |
# Sidebar
|
| 49 |
with gr.Column(scale=1, min_width=350):
|
| 50 |
gr.Markdown("## 🔐 Hugging Face")
|
| 51 |
+
gr.LoginButton() # ✅ Chỉ cần này
|
| 52 |
|
| 53 |
+
gr.Markdown("## 📚 Lịch sử")
|
| 54 |
+
history_display = gr.Chatbot(height=300, avatar_images=("👤", "🤖"))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
+
load_btn = gr.Button("📥 Load", variant="secondary")
|
| 58 |
+
clear_btn = gr.Button("🗑️ Clear", variant="stop")
|
| 59 |
|
| 60 |
# Main Chat
|
| 61 |
with gr.Column(scale=4):
|
| 62 |
chatbot = gr.ChatInterface(
|
| 63 |
+
respond, # ✅ 6 args tự động
|
| 64 |
additional_inputs=[
|
| 65 |
+
gr.Textbox(value="Bạn là NodeAI - trợ lý AI tiếng Việt.", label="System"),
|
|
|
|
|
|
|
|
|
|
| 66 |
gr.Slider(1, 1024, value=512, step=1, label="Max tokens"),
|
| 67 |
gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
|
| 68 |
gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p"),
|
| 69 |
],
|
| 70 |
+
title="💬 NodeAI Chat"
|
|
|
|
| 71 |
)
|
| 72 |
|
| 73 |
+
# ✅ Event listeners KHÔNG cần inputs
|
| 74 |
load_btn.click(load_history, outputs=history_display)
|
| 75 |
clear_btn.click(clear_history, outputs=history_display)
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
demo.launch(
|
| 79 |
+
theme=gr.themes.Soft(primary_hue="blue"),
|
| 80 |
server_name="0.0.0.0",
|
| 81 |
server_port=7860
|
| 82 |
)
|