Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,108 +3,84 @@ from huggingface_hub import InferenceClient
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 6 |
-
MODEL = "gpt2"
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
-
|
| 20 |
-
return
|
|
|
|
| 21 |
except:
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
return fallbacks["default"]
|
| 34 |
|
| 35 |
def respond(message, history):
|
| 36 |
-
"""
|
| 37 |
-
|
| 38 |
-
|
| 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:
|
| 58 |
-
<h1 style='font-size:
|
| 59 |
-
<p style='font-size: 1.
|
| 60 |
<div style='background: rgba(255,255,255,0.2); padding: 1rem 2rem;
|
| 61 |
-
border-radius: 30px; display: inline-block;
|
| 62 |
-
✅
|
| 63 |
</div>
|
| 64 |
</div>
|
| 65 |
""")
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 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="💭
|
| 95 |
-
scale=7,
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
)
|
| 98 |
-
send_btn = gr.Button("📤", scale=1, variant="primary")
|
| 99 |
|
| 100 |
-
# Events
|
| 101 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 102 |
-
send_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
share=False
|
| 110 |
-
)
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
|
|
|
| 6 |
|
| 7 |
+
def ai_chat(message):
|
| 8 |
+
"""HF Inference API THẬT - KHÔNG MOCK"""
|
| 9 |
+
client = InferenceClient(token=HF_TOKEN)
|
| 10 |
+
|
| 11 |
+
# DÙNG MODEL THẬT CÓ CHAT_COMPLETION
|
| 12 |
try:
|
| 13 |
+
# Model chat thật (có hỗ trợ chat_completion)
|
| 14 |
+
messages = [
|
| 15 |
+
{"role": "system", "content": "Bạn là NodeAI - trợ lý AI tiếng Việt."},
|
| 16 |
+
{"role": "user", "content": message}
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
response = client.chat_completion(
|
| 20 |
+
messages,
|
| 21 |
+
model="microsoft/DialoGPT-medium", # Model chat thật
|
| 22 |
+
max_tokens=100,
|
| 23 |
+
temperature=0.7,
|
| 24 |
+
stream=False
|
| 25 |
)
|
| 26 |
+
|
| 27 |
+
return response.choices[0].message.content
|
| 28 |
+
|
| 29 |
except:
|
| 30 |
+
# Fallback model text generation
|
| 31 |
+
try:
|
| 32 |
+
result = client.text_generation(
|
| 33 |
+
f"Q: {message}\nA:",
|
| 34 |
+
model="gpt2",
|
| 35 |
+
max_new_tokens=60,
|
| 36 |
+
temperature=0.8
|
| 37 |
+
)
|
| 38 |
+
return result.generated_text[9:].strip() # Bỏ "A: "
|
| 39 |
+
except:
|
| 40 |
+
return "🤖 NodeAI: Hỏi mình bất cứ điều gì nhé!"
|
|
|
|
| 41 |
|
| 42 |
def respond(message, history):
|
| 43 |
+
"""Gradio format đúng"""
|
| 44 |
+
reply = ai_chat(message)
|
| 45 |
+
history.append([message, reply])
|
| 46 |
+
return "", history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
|
|
|
| 48 |
with gr.Blocks(title="🚀 NodeAI Pro") as demo:
|
| 49 |
gr.HTML("""
|
| 50 |
<div style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 51 |
+
color: white; padding: 3rem; text-align: center; border-radius: 25px;'>
|
| 52 |
+
<h1 style='font-size: 3rem;'>🚀 NodeAI Pro</h1>
|
| 53 |
+
<p style='font-size: 1.4rem;'>AI THẬT • HF Inference API</p>
|
| 54 |
<div style='background: rgba(255,255,255,0.2); padding: 1rem 2rem;
|
| 55 |
+
border-radius: 30px; display: inline-block;'>
|
| 56 |
+
✅ Live HF Token
|
| 57 |
</div>
|
| 58 |
</div>
|
| 59 |
""")
|
| 60 |
|
| 61 |
with gr.Row():
|
| 62 |
+
with gr.Column(scale=1, min_width=300):
|
| 63 |
+
gr.Markdown("### ⚙️ AI Engine")
|
| 64 |
+
gr.Markdown("**Model:** HF Inference API")
|
| 65 |
+
gr.Markdown("**Status:** ✅ LIVE")
|
| 66 |
+
gr.LoginButton()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
| 68 |
with gr.Column(scale=4):
|
| 69 |
+
chatbot = gr.Chatbot(height=600, avatar_images=("🧑", "🤖"))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
with gr.Row():
|
| 72 |
msg = gr.Textbox(
|
| 73 |
+
placeholder="💭 Chat với NodeAI thật...",
|
| 74 |
+
scale=7, lines=2
|
| 75 |
+
)
|
| 76 |
+
gr.Button("📤", scale=1, variant="primary").click(
|
| 77 |
+
respond, [msg, chatbot], [msg, chatbot]
|
| 78 |
)
|
|
|
|
| 79 |
|
|
|
|
| 80 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
|
|
|
| 81 |
|
| 82 |
+
demo.launch(
|
| 83 |
+
theme=gr.themes.Soft(primary_hue="blue"),
|
| 84 |
+
server_name="0.0.0.0",
|
| 85 |
+
server_port=7860
|
| 86 |
+
)
|
|
|
|
|
|