Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,17 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
MODEL = "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def respond(message, history, system_message, max_tokens, temperature, top_p, hf_token):
|
| 8 |
-
"""HF InferenceClient với OAuth login"""
|
| 9 |
if not hf_token or not hf_token.token:
|
| 10 |
-
yield "❌ Vui lòng đăng nhập Hugging Face (
|
| 11 |
return
|
| 12 |
|
| 13 |
try:
|
|
@@ -18,31 +22,15 @@ def respond(message, history, system_message, max_tokens, temperature, top_p, hf
|
|
| 18 |
|
| 19 |
response = ""
|
| 20 |
for message in client.chat_completion(
|
| 21 |
-
messages,
|
| 22 |
-
|
| 23 |
-
stream=True,
|
| 24 |
-
temperature=temperature,
|
| 25 |
-
top_p=top_p,
|
| 26 |
):
|
| 27 |
-
if message.choices[0].delta.content:
|
| 28 |
response += message.choices[0].delta.content
|
| 29 |
yield response
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
-
yield f"❌ Lỗi
|
| 32 |
-
|
| 33 |
-
# User chat history (in-memory)
|
| 34 |
-
chat_history = {}
|
| 35 |
-
|
| 36 |
-
def get_user_id(hf_token):
|
| 37 |
-
"""Lấy user_id từ token"""
|
| 38 |
-
return hash(hf_token.token) % 1000000 if hf_token and hf_token.token else None
|
| 39 |
-
|
| 40 |
-
def save_chat(user_id, message, response):
|
| 41 |
-
if user_id not in chat_history:
|
| 42 |
-
chat_history[user_id] = []
|
| 43 |
-
chat_history[user_id].append({"user": message, "ai": response})
|
| 44 |
-
if len(chat_history[user_id]) > 20:
|
| 45 |
-
chat_history[user_id] = chat_history[user_id][-20:]
|
| 46 |
|
| 47 |
def load_history(hf_token):
|
| 48 |
user_id = get_user_id(hf_token)
|
|
@@ -50,25 +38,29 @@ def load_history(hf_token):
|
|
| 50 |
return [[h["user"], h["ai"]] for h in chat_history[user_id][-10:]]
|
| 51 |
return []
|
| 52 |
|
| 53 |
-
def
|
| 54 |
user_id = get_user_id(hf_token)
|
| 55 |
-
if user_id:
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
with gr.Blocks(
|
| 60 |
-
gr.Markdown("# 🚀 NodeAI - HF Login +
|
| 61 |
|
| 62 |
with gr.Row():
|
| 63 |
# Sidebar
|
| 64 |
with gr.Column(scale=1, min_width=350):
|
| 65 |
gr.Markdown("## 🔐 Hugging Face")
|
| 66 |
-
|
| 67 |
-
gr.LoginButton()
|
| 68 |
-
gr.LogoutButton()
|
| 69 |
|
| 70 |
gr.Markdown("## 📚 Lịch sử Chat")
|
| 71 |
-
history_display = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
with gr.Row():
|
| 74 |
load_btn = gr.Button("📥 Load", variant="secondary")
|
|
@@ -88,12 +80,17 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
|
| 88 |
gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p"),
|
| 89 |
],
|
| 90 |
title="💬 NodeAI Chat",
|
| 91 |
-
description="**Đăng nhập HF → Chat →
|
| 92 |
)
|
| 93 |
|
| 94 |
-
#
|
| 95 |
load_btn.click(load_history, gr.OAuthToken(), history_display)
|
| 96 |
-
clear_btn.click(
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
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 get_user_id(hf_token):
|
| 10 |
+
return hash(hf_token.token) % 1000000 if hf_token and hf_token.token else None
|
| 11 |
|
| 12 |
def respond(message, history, system_message, max_tokens, temperature, top_p, hf_token):
|
|
|
|
| 13 |
if not hf_token or not hf_token.token:
|
| 14 |
+
yield "❌ Vui lòng đăng nhập Hugging Face (nút Login sidebar)!"
|
| 15 |
return
|
| 16 |
|
| 17 |
try:
|
|
|
|
| 22 |
|
| 23 |
response = ""
|
| 24 |
for message in client.chat_completion(
|
| 25 |
+
messages, max_tokens=max_tokens, stream=True,
|
| 26 |
+
temperature=temperature, top_p=top_p
|
|
|
|
|
|
|
|
|
|
| 27 |
):
|
| 28 |
+
if message.choices and message.choices[0].delta.content:
|
| 29 |
response += message.choices[0].delta.content
|
| 30 |
yield response
|
| 31 |
+
|
| 32 |
except Exception as e:
|
| 33 |
+
yield f"❌ Lỗi: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def load_history(hf_token):
|
| 36 |
user_id = get_user_id(hf_token)
|
|
|
|
| 38 |
return [[h["user"], h["ai"]] for h in chat_history[user_id][-10:]]
|
| 39 |
return []
|
| 40 |
|
| 41 |
+
def save_history(hf_token, message, response):
|
| 42 |
user_id = get_user_id(hf_token)
|
| 43 |
+
if user_id and hf_token.token:
|
| 44 |
+
if user_id not in chat_history:
|
| 45 |
+
chat_history[user_id] = []
|
| 46 |
+
chat_history[user_id].append({"user": message, "ai": response})
|
| 47 |
+
if len(chat_history[user_id]) > 20:
|
| 48 |
+
chat_history[user_id] = chat_history[user_id][-20:]
|
| 49 |
|
| 50 |
+
with gr.Blocks(title="NodeAI") as demo: # ❌ NO theme ở đây
|
| 51 |
+
gr.Markdown("# 🚀 NodeAI - HF Login + History")
|
| 52 |
|
| 53 |
with gr.Row():
|
| 54 |
# Sidebar
|
| 55 |
with gr.Column(scale=1, min_width=350):
|
| 56 |
gr.Markdown("## 🔐 Hugging Face")
|
| 57 |
+
login_btn = gr.LoginButton() # ✅ CHỈ LoginButton
|
|
|
|
|
|
|
| 58 |
|
| 59 |
gr.Markdown("## 📚 Lịch sử Chat")
|
| 60 |
+
history_display = gr.Chatbot(
|
| 61 |
+
height=300, label="Lịch sử",
|
| 62 |
+
avatar_images=("👤", "🤖")
|
| 63 |
+
)
|
| 64 |
|
| 65 |
with gr.Row():
|
| 66 |
load_btn = gr.Button("📥 Load", variant="secondary")
|
|
|
|
| 80 |
gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p"),
|
| 81 |
],
|
| 82 |
title="💬 NodeAI Chat",
|
| 83 |
+
description="**Đăng nhập HF sidebar → Chat → Auto save history**"
|
| 84 |
)
|
| 85 |
|
| 86 |
+
# Events
|
| 87 |
load_btn.click(load_history, gr.OAuthToken(), history_display)
|
| 88 |
+
clear_btn.click(lambda: [], outputs=history_display)
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
+
# ✅ Theme ở launch() - Gradio 6.0 syntax
|
| 92 |
+
demo.launch(
|
| 93 |
+
theme=gr.themes.Soft(primary_hue="blue"),
|
| 94 |
+
server_name="0.0.0.0",
|
| 95 |
+
server_port=7860
|
| 96 |
+
)
|