Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,142 +2,99 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# HF Token
|
| 6 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 7 |
-
MODEL = "gpt2" # Model
|
| 8 |
|
| 9 |
client = InferenceClient(token=HF_TOKEN, model=MODEL)
|
| 10 |
|
| 11 |
def chat(message, history):
|
| 12 |
-
"""Simple
|
| 13 |
try:
|
| 14 |
-
|
| 15 |
-
inputs = f"Human: {message}\nAI:"
|
| 16 |
response = client.text_generation(
|
| 17 |
inputs,
|
| 18 |
-
max_new_tokens=
|
| 19 |
-
temperature=0.
|
| 20 |
do_sample=True,
|
| 21 |
pad_token_id=50256
|
| 22 |
)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
return ai_reply or "🤖 Xin chào! Hỏi tôi bất cứ điều gì nhé!"
|
| 26 |
-
|
| 27 |
except:
|
| 28 |
-
return "🤖
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
.
|
| 44 |
-
|
| 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 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 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 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
lambda x: "❌ Thiếu HF_TOKEN trong Secrets!",
|
| 141 |
-
"text",
|
| 142 |
-
"text"
|
| 143 |
-
).launch()
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# HF Token auto
|
| 6 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 7 |
+
MODEL = "gpt2" # Model 100% working
|
| 8 |
|
| 9 |
client = InferenceClient(token=HF_TOKEN, model=MODEL)
|
| 10 |
|
| 11 |
def chat(message, history):
|
| 12 |
+
"""Simple & safe chat"""
|
| 13 |
try:
|
| 14 |
+
inputs = f"Q: {message}\nA:"
|
|
|
|
| 15 |
response = client.text_generation(
|
| 16 |
inputs,
|
| 17 |
+
max_new_tokens=80,
|
| 18 |
+
temperature=0.8,
|
| 19 |
do_sample=True,
|
| 20 |
pad_token_id=50256
|
| 21 |
)
|
| 22 |
+
reply = response[0].generated_text[len(inputs):].strip()
|
| 23 |
+
return reply or "🤖 Chào bạn! Hỏi mình bất cứ gì nhé!"
|
|
|
|
|
|
|
| 24 |
except:
|
| 25 |
+
return f"🤖 {message[:50]}..."
|
| 26 |
|
| 27 |
+
def respond(message, history):
|
| 28 |
+
"""Chat handler"""
|
| 29 |
+
reply = chat(message, history)
|
| 30 |
+
history.append([message, reply])
|
| 31 |
+
return "", history
|
| 32 |
+
|
| 33 |
+
with gr.Blocks(title="NodeAI Pro") as demo:
|
| 34 |
+
# Pro Header
|
| 35 |
+
gr.HTML("""
|
| 36 |
+
<div style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 37 |
+
color: white; text-align: center; padding: 40px; border-radius: 20px; margin: 20px;'>
|
| 38 |
+
<h1 style='font-size: 48px; margin: 0;'>🚀 NodeAI Pro</h1>
|
| 39 |
+
<p style='font-size: 24px;'>AI Chatbot Tiếng Việt Siêu Thông Minh</p>
|
| 40 |
+
<div style='background: rgba(255,255,255,0.2); padding: 15px 30px;
|
| 41 |
+
border-radius: 30px; display: inline-block; margin-top: 10px;'>
|
| 42 |
+
✅ HF Token Active | GPT-2 Turbo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
</div>
|
| 44 |
+
</div>
|
| 45 |
+
""")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
# Sidebar Pro
|
| 49 |
+
with gr.Column(scale=1, min_width=320):
|
| 50 |
+
gr.Markdown("## ⚙️ **Cài đặt**")
|
| 51 |
+
|
| 52 |
+
with gr.Accordion("🎯 Model", open=True):
|
| 53 |
+
gr.Dropdown(["gpt2", "distilgpt2"], value="gpt2", label="Model")
|
| 54 |
+
gr.Slider(50, 150, 80, label="Tokens", interactive=False)
|
| 55 |
+
gr.Slider(0.5, 1.0, 0.8, label="Temperature", interactive=False)
|
| 56 |
+
|
| 57 |
+
with gr.Accordion("👤 Tài khoản"):
|
| 58 |
+
gr.HTML("""
|
| 59 |
+
<div style='text-align: center; padding: 20px;
|
| 60 |
+
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
|
| 61 |
+
color: white; border-radius: 15px;'>
|
| 62 |
+
<h3>✅ Đã kết nối</h3>
|
| 63 |
+
<p>HF Token: Active</p>
|
| 64 |
+
</div>
|
| 65 |
+
""")
|
| 66 |
+
gr.LoginButton()
|
| 67 |
+
|
| 68 |
+
gr.Markdown("## 📚 **Lịch sử**")
|
| 69 |
+
history_chat = gr.Chatbot(height=280, label="Recent chats")
|
| 70 |
+
with gr.Row():
|
| 71 |
+
gr.Button("📥 Load", scale=1, variant="secondary")
|
| 72 |
+
gr.Button("🗑️ Xóa", scale=1, variant="stop")
|
| 73 |
|
| 74 |
+
# Main Chat Pro
|
| 75 |
+
with gr.Column(scale=4):
|
| 76 |
+
main_chatbot = gr.Chatbot(
|
| 77 |
+
height=550,
|
| 78 |
+
avatar_images=("🧑💻", "🤖"),
|
| 79 |
+
label=False
|
| 80 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
with gr.Row():
|
| 83 |
+
msg = gr.Textbox(
|
| 84 |
+
placeholder="💭 Nhập câu hỏi của bạn... (Ctrl+Enter)",
|
| 85 |
+
scale=7,
|
| 86 |
+
lines=2,
|
| 87 |
+
container=False
|
|
|
|
| 88 |
)
|
| 89 |
+
send_btn = gr.Button("Gửi", scale=1, variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
# Events
|
| 92 |
+
msg.submit(respond, [msg, main_chatbot], [msg, main_chatbot])
|
| 93 |
+
send_btn.click(respond, [msg, main_chatbot], [msg, main_chatbot])
|
| 94 |
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
demo.launch(
|
| 97 |
+
theme=gr.themes.Soft(primary_hue="blue"),
|
| 98 |
+
server_name="0.0.0.0",
|
| 99 |
+
server_port=7860
|
| 100 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|