Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,104 +2,98 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Chỉ dùng HF_TOKEN
|
| 6 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 7 |
-
MODEL = "gpt2"
|
| 8 |
|
| 9 |
def smart_chat(message):
|
| 10 |
-
"""AI
|
| 11 |
try:
|
| 12 |
client = InferenceClient(token=HF_TOKEN, model=MODEL)
|
| 13 |
-
prompt = f"
|
| 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
|
| 23 |
except:
|
| 24 |
-
# Smart fallback
|
| 25 |
-
|
| 26 |
-
|
| 27 |
"xin chào": "Chào bạn! 😊 NodeAI sẵn sàng hỗ trợ!",
|
| 28 |
-
"hello": "Xin chào! Rất vui được chat với bạn!
|
| 29 |
-
"bạn là ai": "🚀 Mình là
|
| 30 |
-
"làm gì": "Mình chat 24/7, giải đáp mọi thắc mắc! 😄",
|
| 31 |
-
"cảm ơn": "Không có gì! Rất vui được giúp bạn! ✨",
|
| 32 |
"default": "🤖 Hiểu rồi! Bạn cần giúp gì tiếp theo?"
|
| 33 |
}
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def respond(message, history):
|
| 37 |
-
"""
|
| 38 |
if not message.strip():
|
| 39 |
return "", history
|
| 40 |
|
| 41 |
reply = smart_chat(message)
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
|
|
|
| 45 |
with gr.Blocks(title="🚀 NodeAI Pro") as demo:
|
| 46 |
-
# Header Pro
|
| 47 |
gr.HTML("""
|
| 48 |
-
<div style='
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
<div style='
|
| 56 |
-
background: rgba(255,255,255,0.2); padding: 1rem 2.5rem;
|
| 57 |
-
border-radius: 50px; display: inline-block; font-weight: bold;
|
| 58 |
-
'>
|
| 59 |
-
✅ HF_TOKEN Active | GPT-2 + Smart AI
|
| 60 |
</div>
|
| 61 |
</div>
|
| 62 |
""")
|
| 63 |
|
| 64 |
with gr.Row():
|
| 65 |
-
# Sidebar
|
| 66 |
-
with gr.Column(scale=1, min_width=
|
| 67 |
-
with gr.Accordion("
|
| 68 |
-
gr.Markdown("**Model:** `gpt2`
|
| 69 |
-
gr.Markdown("**
|
| 70 |
-
gr.Markdown("**Status:** ✅ **LIVE**")
|
| 71 |
|
| 72 |
with gr.Accordion("🔐 HF Token"):
|
| 73 |
-
gr.HTML("""
|
| 74 |
-
<div style='
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
color: white; border-radius: 15px;
|
| 78 |
-
'>
|
| 79 |
<h3>✅ Connected</h3>
|
| 80 |
-
<p>HF_TOKEN detected</p>
|
| 81 |
</div>
|
| 82 |
""")
|
| 83 |
gr.LoginButton()
|
| 84 |
-
|
| 85 |
-
with gr.Accordion("📚 Recent Chats"):
|
| 86 |
-
history_view = gr.Chatbot(height=300)
|
| 87 |
-
gr.Button("🗑️ Clear", variant="stop")
|
| 88 |
|
| 89 |
-
# Main Chat
|
| 90 |
with gr.Column(scale=4):
|
| 91 |
chatbot = gr.Chatbot(
|
| 92 |
-
height=
|
| 93 |
avatar_images=("🧑", "🤖"),
|
| 94 |
show_label=False
|
| 95 |
)
|
| 96 |
|
| 97 |
with gr.Row():
|
| 98 |
msg = gr.Textbox(
|
| 99 |
-
placeholder="💭 Hỏi NodeAI
|
| 100 |
scale=7,
|
| 101 |
-
lines=2
|
| 102 |
-
show_label=False
|
| 103 |
)
|
| 104 |
send_btn = gr.Button("📤", scale=1, variant="primary")
|
| 105 |
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
|
|
|
| 5 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 6 |
+
MODEL = "gpt2"
|
| 7 |
|
| 8 |
def smart_chat(message):
|
| 9 |
+
"""AI + Smart fallback"""
|
| 10 |
try:
|
| 11 |
client = InferenceClient(token=HF_TOKEN, model=MODEL)
|
| 12 |
+
prompt = f"Q: {message}\nA: "
|
| 13 |
response = client.text_generation(
|
| 14 |
prompt,
|
| 15 |
+
max_new_tokens=60,
|
| 16 |
temperature=0.8,
|
| 17 |
+
do_sample=True
|
|
|
|
| 18 |
)
|
| 19 |
+
reply = response[0].generated_text[len(prompt):].strip()
|
| 20 |
+
return reply if reply else "🤖 Chào bạn!"
|
| 21 |
except:
|
| 22 |
+
# Smart fallback
|
| 23 |
+
msg_lower = message.lower()
|
| 24 |
+
fallbacks = {
|
| 25 |
"xin chào": "Chào bạn! 😊 NodeAI sẵn sàng hỗ trợ!",
|
| 26 |
+
"hello": "Xin chào! Rất vui được chat với bạn!",
|
| 27 |
+
"bạn là ai": "🚀 Mình là NodeAI Pro - AI trợ lý thông minh!",
|
|
|
|
|
|
|
| 28 |
"default": "🤖 Hiểu rồi! Bạn cần giúp gì tiếp theo?"
|
| 29 |
}
|
| 30 |
+
for key, value in fallbacks.items():
|
| 31 |
+
if key in msg_lower:
|
| 32 |
+
return value
|
| 33 |
+
return fallbacks["default"]
|
| 34 |
|
| 35 |
def respond(message, history):
|
| 36 |
+
"""✅ CORRECT GRADIO 6.9.0 FORMAT"""
|
| 37 |
if not message.strip():
|
| 38 |
return "", history
|
| 39 |
|
| 40 |
reply = smart_chat(message)
|
| 41 |
+
|
| 42 |
+
# ✅ FORMAT ĐÚNG: list of dicts role/content
|
| 43 |
+
new_history = history + [{
|
| 44 |
+
"role": "user",
|
| 45 |
+
"content": [{"type": "text", "text": message}]
|
| 46 |
+
}, {
|
| 47 |
+
"role": "assistant",
|
| 48 |
+
"content": [{"type": "text", "text": reply}]
|
| 49 |
+
}]
|
| 50 |
+
|
| 51 |
+
return "", new_history
|
| 52 |
|
| 53 |
+
# UI Pro
|
| 54 |
with gr.Blocks(title="🚀 NodeAI Pro") as demo:
|
|
|
|
| 55 |
gr.HTML("""
|
| 56 |
+
<div style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 57 |
+
color: white; padding: 2.5rem; text-align: center; border-radius: 20px;'>
|
| 58 |
+
<h1 style='font-size: 2.8rem;'>🚀 NodeAI Pro</h1>
|
| 59 |
+
<p style='font-size: 1.3rem;'>AI Chatbot Tiếng Việt • HF_TOKEN Only</p>
|
| 60 |
+
<div style='background: rgba(255,255,255,0.2); padding: 1rem 2rem;
|
| 61 |
+
border-radius: 30px; display: inline-block; font-weight: bold;'>
|
| 62 |
+
✅ GPT-2 Active
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
</div>
|
| 64 |
</div>
|
| 65 |
""")
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
+
# Sidebar
|
| 69 |
+
with gr.Column(scale=1, min_width=320):
|
| 70 |
+
with gr.Accordion("⚙️ Settings", open=True):
|
| 71 |
+
gr.Markdown("**Model:** `gpt2`")
|
| 72 |
+
gr.Markdown("**Status:** ✅ LIVE")
|
|
|
|
| 73 |
|
| 74 |
with gr.Accordion("🔐 HF Token"):
|
| 75 |
+
status = gr.HTML("""
|
| 76 |
+
<div style='padding: 1.5rem; text-align: center;
|
| 77 |
+
background: linear-gradient(135deg, #10b981, #059669);
|
| 78 |
+
color: white; border-radius: 12px;'>
|
|
|
|
|
|
|
| 79 |
<h3>✅ Connected</h3>
|
|
|
|
| 80 |
</div>
|
| 81 |
""")
|
| 82 |
gr.LoginButton()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# Main Chat
|
| 85 |
with gr.Column(scale=4):
|
| 86 |
chatbot = gr.Chatbot(
|
| 87 |
+
height=550,
|
| 88 |
avatar_images=("🧑", "🤖"),
|
| 89 |
show_label=False
|
| 90 |
)
|
| 91 |
|
| 92 |
with gr.Row():
|
| 93 |
msg = gr.Textbox(
|
| 94 |
+
placeholder="💭 Hỏi NodeAI bất cứ điều gì...",
|
| 95 |
scale=7,
|
| 96 |
+
lines=2
|
|
|
|
| 97 |
)
|
| 98 |
send_btn = gr.Button("📤", scale=1, variant="primary")
|
| 99 |
|