Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,68 +2,142 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
|
| 6 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
messages.append({"role": "user", "content": str(user_msg or "")})
|
| 17 |
-
if assistant_msg:
|
| 18 |
-
messages.append({"role": "assistant", "content": str(assistant_msg)})
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
)
|
| 30 |
-
# ✅ SAFE ACCESS - Không lỗi index
|
| 31 |
-
try:
|
| 32 |
-
if (hasattr(chunk, 'choices') and
|
| 33 |
-
len(chunk.choices) > 0 and
|
| 34 |
-
hasattr(chunk.choices[0], 'delta') and
|
| 35 |
-
hasattr(chunk.choices[0].delta, 'content') and
|
| 36 |
-
chunk.choices[0].delta.content):
|
| 37 |
-
|
| 38 |
-
content = chunk.choices[0].delta.content
|
| 39 |
-
response += content
|
| 40 |
-
yield response
|
| 41 |
-
except (IndexError, AttributeError):
|
| 42 |
-
continue # Skip chunk lỗi
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
respond,
|
| 56 |
-
additional_inputs=[
|
| 57 |
-
gr.Textbox(
|
| 58 |
-
value="Bạn là NodeAI - trợ lý AI tiếng Việt thân thiện.",
|
| 59 |
-
label="System prompt"
|
| 60 |
-
),
|
| 61 |
-
gr.Slider(1, 512, value=256, step=1, label="Max tokens"),
|
| 62 |
-
gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Temperature"),
|
| 63 |
-
gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p"),
|
| 64 |
-
],
|
| 65 |
-
title="💬 Chat ngay!"
|
| 66 |
-
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# HF Token tự động (không cần config)
|
| 6 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 7 |
+
MODEL = "gpt2" # Model siêu ổn định, không lỗi
|
| 8 |
|
| 9 |
+
client = InferenceClient(token=HF_TOKEN, model=MODEL)
|
| 10 |
+
|
| 11 |
+
def chat(message, history):
|
| 12 |
+
"""Simple chat - Pro UI"""
|
| 13 |
try:
|
| 14 |
+
# Generate response
|
| 15 |
+
inputs = f"Human: {message}\nAI:"
|
| 16 |
+
response = client.text_generation(
|
| 17 |
+
inputs,
|
| 18 |
+
max_new_tokens=100,
|
| 19 |
+
temperature=0.7,
|
| 20 |
+
do_sample=True,
|
| 21 |
+
pad_token_id=50256
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
ai_reply = response[0].generated_text[len(inputs):].strip()
|
| 25 |
+
return ai_reply or "🤖 Xin chào! Hỏi tôi bất cứ điều gì nhé!"
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
except:
|
| 28 |
+
return "🤖 NodeAI sẵn sàng hỗ trợ bạn!"
|
| 29 |
+
|
| 30 |
+
# Pro UI Components
|
| 31 |
+
def create_pro_ui():
|
| 32 |
+
with gr.Blocks(
|
| 33 |
+
theme=gr.themes.Soft(
|
| 34 |
+
primary_hue="blue",
|
| 35 |
+
secondary_hue="sky",
|
| 36 |
+
neutral_hue="slate",
|
| 37 |
+
),
|
| 38 |
+
title="NodeAI Pro",
|
| 39 |
+
css="""
|
| 40 |
+
.gradio-container {
|
| 41 |
+
max-width: 1400px !important;
|
| 42 |
+
}
|
| 43 |
+
.chatbot {
|
| 44 |
+
height: 600px !important;
|
| 45 |
+
}
|
| 46 |
+
.status-indicator {
|
| 47 |
+
background: linear-gradient(45deg, #3b82f6, #1d4ed8);
|
| 48 |
+
color: white;
|
| 49 |
+
padding: 10px 20px;
|
| 50 |
+
border-radius: 25px;
|
| 51 |
+
font-weight: bold;
|
| 52 |
+
}
|
| 53 |
+
"""
|
| 54 |
+
) as demo:
|
| 55 |
|
| 56 |
+
# Header Pro
|
| 57 |
+
gr.HTML("""
|
| 58 |
+
<div style='text-align: center; padding: 30px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 20px; margin-bottom: 30px;'>
|
| 59 |
+
<h1 style='font-size: 3em; margin: 0;'>🚀 NodeAI Pro</h1>
|
| 60 |
+
<p style='font-size: 1.5em; opacity: 0.9;'>Trợ lý AI Tiếng Việt Siêu Thông Minh</p>
|
| 61 |
+
<div class='status-indicator'>✅ HF Token Ready | GPT-2 Turbo</div>
|
| 62 |
+
</div>
|
| 63 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
with gr.Row():
|
| 66 |
+
# Sidebar Pro
|
| 67 |
+
with gr.Column(scale=1, min_width=350):
|
| 68 |
+
gr.HTML("<h2>⚙️ Settings</h2>")
|
| 69 |
+
|
| 70 |
+
with gr.Accordion("🎯 Model Settings", open=True):
|
| 71 |
+
model_dropdown = gr.Dropdown(
|
| 72 |
+
choices=["gpt2", "distilgpt2", "gpt2-medium"],
|
| 73 |
+
value="gpt2",
|
| 74 |
+
label="Model"
|
| 75 |
+
)
|
| 76 |
+
temp_slider = gr.Slider(0.1, 1.0, 0.7, step=0.1, label="Temperature")
|
| 77 |
+
token_slider = gr.Slider(50, 200, 100, step=10, label="Max Tokens")
|
| 78 |
+
|
| 79 |
+
with gr.Accordion("👤 User Info"):
|
| 80 |
+
gr.HTML("""
|
| 81 |
+
<div style='text-align: center; padding: 20px; background: #f3f4f6; border-radius: 15px;'>
|
| 82 |
+
<h3>👋 Chào mừng!</h3>
|
| 83 |
+
<p>HF Token: ✅ Connected</p>
|
| 84 |
+
<p>Sessions: 0</p>
|
| 85 |
+
</div>
|
| 86 |
+
""")
|
| 87 |
+
gr.LoginButton(scale=1)
|
| 88 |
+
|
| 89 |
+
with gr.Accordion("📚 Chat History"):
|
| 90 |
+
history_display = gr.Chatbot(height=250, label="Recent chats")
|
| 91 |
+
gr.Button("📥 Load History", scale=0.5, variant="secondary")
|
| 92 |
+
gr.Button("🗑��� Clear", scale=0.5, variant="stop")
|
| 93 |
|
| 94 |
+
# Main Chat Pro
|
| 95 |
+
with gr.Column(scale=4):
|
| 96 |
+
chatbot = gr.Chatbot(
|
| 97 |
+
height=600,
|
| 98 |
+
avatar_images=("🧑", "🤖"),
|
| 99 |
+
show_label=False,
|
| 100 |
+
bubble_full_width=False
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
msg_input = gr.Textbox(
|
| 105 |
+
placeholder="💭 Hỏi NodeAI bất cứ điều gì...",
|
| 106 |
+
container=True,
|
| 107 |
+
scale=7,
|
| 108 |
+
show_label=False,
|
| 109 |
+
lines=2
|
| 110 |
+
)
|
| 111 |
+
send_btn = gr.Button("📤", scale=1, variant="primary")
|
| 112 |
+
mic_btn = gr.Button("🎤", scale=1, variant="secondary")
|
| 113 |
+
|
| 114 |
+
# Events
|
| 115 |
+
def respond_pro(message, history, model, temp, tokens):
|
| 116 |
+
reply = chat(message, history)
|
| 117 |
+
history.append([message, reply])
|
| 118 |
+
return "", history
|
| 119 |
+
|
| 120 |
+
msg_input.submit(
|
| 121 |
+
respond_pro,
|
| 122 |
+
inputs=[msg_input, chatbot, model_dropdown, temp_slider, token_slider],
|
| 123 |
+
outputs=[msg_input, chatbot]
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
send_btn.click(
|
| 127 |
+
respond_pro,
|
| 128 |
+
inputs=[msg_input, chatbot, model_dropdown, temp_slider, token_slider],
|
| 129 |
+
outputs=[msg_input, chatbot]
|
| 130 |
+
)
|
| 131 |
|
| 132 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
+
# Launch Pro
|
| 135 |
+
if HF_TOKEN:
|
| 136 |
+
demo = create_pro_ui()
|
| 137 |
+
demo.launch()
|
| 138 |
+
else:
|
| 139 |
+
gr.Interface(
|
| 140 |
+
lambda x: "❌ Thiếu HF_TOKEN trong Secrets!",
|
| 141 |
+
"text",
|
| 142 |
+
"text"
|
| 143 |
+
).launch()
|