Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,103 +2,103 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
|
|
|
| 5 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 6 |
MODEL = "gpt2"
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def chat(message, history):
|
| 11 |
-
"""Safe chat function"""
|
| 12 |
try:
|
|
|
|
|
|
|
| 13 |
prompt = f"Q: {message}\nA:"
|
| 14 |
response = client.text_generation(
|
| 15 |
-
prompt,
|
| 16 |
-
max_new_tokens=
|
| 17 |
temperature=0.8,
|
| 18 |
-
do_sample=True
|
| 19 |
-
pad_token_id=50256
|
| 20 |
)
|
| 21 |
-
|
| 22 |
-
return reply or "🤖 Chào bạn! Hỏi mình nhé!"
|
| 23 |
except:
|
| 24 |
-
return "🤖 NodeAI sẵn sàng hỗ trợ!"
|
| 25 |
|
| 26 |
def respond(message, history):
|
| 27 |
-
"""Chat handler -
|
| 28 |
-
|
| 29 |
-
reply = chat(message, history)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
"
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
return "", history
|
| 38 |
|
| 39 |
-
with gr.Blocks(title="NodeAI Pro") as demo:
|
| 40 |
-
#
|
| 41 |
gr.HTML("""
|
| 42 |
-
<div style=
|
| 43 |
-
color: white; text-align: center;
|
| 44 |
-
<h1 style=
|
| 45 |
-
<p style=
|
| 46 |
-
<div style=
|
| 47 |
-
border-radius: 25px; display: inline-block; font-weight: bold;
|
| 48 |
-
✅ GPT-2
|
| 49 |
</div>
|
| 50 |
</div>
|
| 51 |
""")
|
| 52 |
|
| 53 |
with gr.Row():
|
| 54 |
-
# Sidebar
|
| 55 |
-
with gr.Column(scale=1, min_width=
|
| 56 |
-
gr.
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
gr.Markdown("**gpt2** (Siêu ổn định)")
|
| 60 |
-
gr.Markdown("**Tokens:** 80 | **Temp:** 0.8")
|
| 61 |
|
| 62 |
-
with gr.Accordion("
|
| 63 |
gr.HTML("""
|
| 64 |
-
<div style=
|
| 65 |
background: linear-gradient(135deg, #10b981, #059669);
|
| 66 |
-
color: white; border-radius:
|
| 67 |
-
<h3>✅
|
| 68 |
-
<p>
|
| 69 |
</div>
|
| 70 |
""")
|
| 71 |
gr.LoginButton()
|
| 72 |
|
| 73 |
-
gr.
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
-
# Main
|
| 78 |
with gr.Column(scale=4):
|
| 79 |
chatbot = gr.Chatbot(
|
| 80 |
-
height=
|
| 81 |
-
|
| 82 |
-
|
| 83 |
)
|
| 84 |
|
| 85 |
with gr.Row():
|
| 86 |
-
|
| 87 |
-
placeholder="💭 Hỏi NodeAI bất cứ điều gì... (Enter
|
| 88 |
scale=7,
|
| 89 |
-
lines=2
|
|
|
|
| 90 |
)
|
| 91 |
-
|
| 92 |
|
| 93 |
-
# Events
|
| 94 |
-
|
| 95 |
-
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
demo.launch(
|
| 99 |
theme=gr.themes.Soft(primary_hue="blue"),
|
| 100 |
-
server_name="0.0.0.0",
|
| 101 |
server_port=7860,
|
| 102 |
-
share=False,
|
| 103 |
-
|
| 104 |
)
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# HF Token
|
| 6 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 7 |
MODEL = "gpt2"
|
| 8 |
|
| 9 |
+
def generate_response(message):
|
| 10 |
+
"""Simple text generation"""
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
+
from huggingface_hub import InferenceClient
|
| 13 |
+
client = InferenceClient(token=HF_TOKEN, model=MODEL)
|
| 14 |
prompt = f"Q: {message}\nA:"
|
| 15 |
response = client.text_generation(
|
| 16 |
+
prompt,
|
| 17 |
+
max_new_tokens=60,
|
| 18 |
temperature=0.8,
|
| 19 |
+
do_sample=True
|
|
|
|
| 20 |
)
|
| 21 |
+
return response[0].generated_text[len(prompt):].strip() or "🤖 Chào bạn!"
|
|
|
|
| 22 |
except:
|
| 23 |
+
return "🤖 NodeAI sẵn sàng hỗ trợ bạn!"
|
| 24 |
|
| 25 |
def respond(message, history):
|
| 26 |
+
"""Chat handler - ĐÚNG FORMAT GRADIO 6.9.0"""
|
| 27 |
+
reply = generate_response(message)
|
|
|
|
| 28 |
|
| 29 |
+
# ✅ FORMAT CHUẨN: list of dicts với role/content
|
| 30 |
+
new_history = history + [{
|
| 31 |
+
"role": "user",
|
| 32 |
+
"content": [{"type": "text", "text": message}]
|
| 33 |
+
}, {
|
| 34 |
+
"role": "assistant",
|
| 35 |
+
"content": [{"type": "text", "text": reply}]
|
| 36 |
+
}]
|
| 37 |
|
| 38 |
+
return "", new_history
|
|
|
|
| 39 |
|
| 40 |
+
with gr.Blocks(title="🚀 NodeAI Pro") as demo:
|
| 41 |
+
# Header đẹp
|
| 42 |
gr.HTML("""
|
| 43 |
+
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 44 |
+
color: white; padding: 2rem; text-align: center; border-radius: 20px; margin-bottom: 2rem;">
|
| 45 |
+
<h1 style="font-size: 2.5rem; margin: 0;">🚀 NodeAI Pro</h1>
|
| 46 |
+
<p style="font-size: 1.2rem; opacity: 0.9;">AI Chatbot Tiếng Việt Siêu Thông Minh</p>
|
| 47 |
+
<div style="background: rgba(255,255,255,0.2); padding: 0.8rem 1.5rem;
|
| 48 |
+
border-radius: 25px; display: inline-block; font-weight: bold;">
|
| 49 |
+
✅ GPT-2 | Zero Config | Production Ready
|
| 50 |
</div>
|
| 51 |
</div>
|
| 52 |
""")
|
| 53 |
|
| 54 |
with gr.Row():
|
| 55 |
+
# Sidebar gọn nhẹ
|
| 56 |
+
with gr.Column(scale=1, min_width=300):
|
| 57 |
+
with gr.Accordion("⚙️ Cài đặt", open=True):
|
| 58 |
+
gr.Markdown("**Model:** `gpt2` (100% stable)")
|
| 59 |
+
gr.Markdown("**Tokens:** 60 | **Temp:** 0.8")
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
with gr.Accordion("🔐 Hugging Face"):
|
| 62 |
gr.HTML("""
|
| 63 |
+
<div style="text-align: center; padding: 1.5rem;
|
| 64 |
background: linear-gradient(135deg, #10b981, #059669);
|
| 65 |
+
color: white; border-radius: 12px;">
|
| 66 |
+
<h3>✅ Connected</h3>
|
| 67 |
+
<p>HF Token Active</p>
|
| 68 |
</div>
|
| 69 |
""")
|
| 70 |
gr.LoginButton()
|
| 71 |
|
| 72 |
+
with gr.Accordion("📚 Lịch sử chat"):
|
| 73 |
+
history_view = gr.Chatbot(height=250)
|
| 74 |
+
gr.Button("🗑️ Xóa lịch sử", scale=1, variant="stop")
|
| 75 |
|
| 76 |
+
# Main chat đẹp
|
| 77 |
with gr.Column(scale=4):
|
| 78 |
chatbot = gr.Chatbot(
|
| 79 |
+
height=500,
|
| 80 |
+
show_label=False,
|
| 81 |
+
avatar_images=("🧑💻", "🤖")
|
| 82 |
)
|
| 83 |
|
| 84 |
with gr.Row():
|
| 85 |
+
message_input = gr.Textbox(
|
| 86 |
+
placeholder="💭 Hỏi NodeAI bất cứ điều gì... (Enter)",
|
| 87 |
scale=7,
|
| 88 |
+
lines=2,
|
| 89 |
+
show_label=False
|
| 90 |
)
|
| 91 |
+
send_button = gr.Button("📤", scale=1, variant="primary")
|
| 92 |
|
| 93 |
+
# Events đơn giản
|
| 94 |
+
message_input.submit(respond, [message_input, chatbot], [message_input, chatbot])
|
| 95 |
+
send_button.click(respond, [message_input, chatbot], [message_input, chatbot])
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
demo.launch(
|
| 99 |
theme=gr.themes.Soft(primary_hue="blue"),
|
| 100 |
+
server_name="0.0.0.0",
|
| 101 |
server_port=7860,
|
| 102 |
+
share=False,
|
| 103 |
+
show_error=True
|
| 104 |
)
|