Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,73 +4,76 @@ import requests
|
|
| 4 |
import json
|
| 5 |
|
| 6 |
groq_key = os.getenv("GROQ_API_KEY")
|
| 7 |
-
MODEL = "
|
| 8 |
|
| 9 |
status_msg = "❌ Chưa set GROQ_API_KEY"
|
| 10 |
if groq_key:
|
| 11 |
-
status_msg = f"✅ Ready -
|
| 12 |
|
| 13 |
def call_groq(messages):
|
|
|
|
| 14 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 15 |
headers = {
|
| 16 |
"Authorization": f"Bearer {groq_key}",
|
| 17 |
"Content-Type": "application/json"
|
| 18 |
}
|
| 19 |
|
|
|
|
| 20 |
data = {
|
| 21 |
-
"model": MODEL,
|
| 22 |
"messages": messages,
|
| 23 |
-
"
|
| 24 |
-
"temperature": 0.7
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
try:
|
| 28 |
-
response = requests.post(url, headers=headers, json=data, timeout=
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
-
if "model" in str(e).lower():
|
| 33 |
-
return "❌ Model không tồn tại. Dùng: llama-3.1-8b-instant"
|
| 34 |
return f"❌ Lỗi: {str(e)}"
|
| 35 |
|
| 36 |
def respond(message, history):
|
| 37 |
if not groq_key:
|
| 38 |
return "", history + [{"role": "assistant", "content": status_msg}]
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
messages.append({"role": "user", "content": message})
|
| 47 |
|
| 48 |
reply = call_groq(messages)
|
| 49 |
return "", history + [{"role": "assistant", "content": reply}]
|
| 50 |
|
| 51 |
with gr.Blocks(title="NodeAI") as demo:
|
| 52 |
-
gr.Markdown("# 🚀 NodeAI -
|
| 53 |
-
gr.Markdown("**⚡ Siêu nhanh Llama 3.1 - Miễn phí!**")
|
| 54 |
gr.Markdown(f"**Status:** {status_msg}")
|
| 55 |
|
| 56 |
-
chatbot = gr.Chatbot(
|
| 57 |
-
height=500,
|
| 58 |
-
show_label=False,
|
| 59 |
-
avatar_images=("👤", "🤖")
|
| 60 |
-
)
|
| 61 |
|
| 62 |
with gr.Row():
|
| 63 |
-
msg = gr.Textbox(placeholder="
|
| 64 |
-
send_btn = gr.Button("
|
| 65 |
-
clear_btn = gr.Button("🗑️
|
| 66 |
|
| 67 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 68 |
send_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 69 |
clear_btn.click(lambda: [], None, chatbot)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
-
demo.launch(
|
| 73 |
-
theme=gr.themes.Soft(primary_hue="blue"),
|
| 74 |
-
server_name="0.0.0.0",
|
| 75 |
-
server_port=7860
|
| 76 |
-
)
|
|
|
|
| 4 |
import json
|
| 5 |
|
| 6 |
groq_key = os.getenv("GROQ_API_KEY")
|
| 7 |
+
MODEL = "llama3-70b-8192" # ✅ Model ổn định 2026
|
| 8 |
|
| 9 |
status_msg = "❌ Chưa set GROQ_API_KEY"
|
| 10 |
if groq_key:
|
| 11 |
+
status_msg = f"✅ NodeAI Ready - {MODEL}"
|
| 12 |
|
| 13 |
def call_groq(messages):
|
| 14 |
+
"""Groq API - Format tối giản"""
|
| 15 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 16 |
headers = {
|
| 17 |
"Authorization": f"Bearer {groq_key}",
|
| 18 |
"Content-Type": "application/json"
|
| 19 |
}
|
| 20 |
|
| 21 |
+
# REQUEST SIÊU SẠCH - chỉ 4 field cần thiết
|
| 22 |
data = {
|
|
|
|
| 23 |
"messages": messages,
|
| 24 |
+
"model": MODEL,
|
| 25 |
+
"temperature": 0.7,
|
| 26 |
+
"max_tokens": 500
|
| 27 |
}
|
| 28 |
|
| 29 |
try:
|
| 30 |
+
response = requests.post(url, headers=headers, json=data, timeout=15)
|
| 31 |
+
|
| 32 |
+
if response.status_code != 200:
|
| 33 |
+
return f"❌ HTTP {response.status_code}\n{response.text[:150]}"
|
| 34 |
+
|
| 35 |
+
result = response.json()
|
| 36 |
+
return result["choices"][0]["message"]["content"]
|
| 37 |
+
|
| 38 |
except Exception as e:
|
|
|
|
|
|
|
| 39 |
return f"❌ Lỗi: {str(e)}"
|
| 40 |
|
| 41 |
def respond(message, history):
|
| 42 |
if not groq_key:
|
| 43 |
return "", history + [{"role": "assistant", "content": status_msg}]
|
| 44 |
|
| 45 |
+
# Messages format SẠCH
|
| 46 |
+
messages = [
|
| 47 |
+
{"role": "system", "content": "Bạn là NodeAI. Trả lời ngắn gọn, chính xác bằng tiếng Việt."}
|
| 48 |
+
]
|
| 49 |
|
| 50 |
+
# Thêm history (chỉ role/content)
|
| 51 |
+
for msg in history[-6:]: # Giới hạn 6 tin nhắn trước
|
| 52 |
+
if isinstance(msg, dict) and "role" in msg and "content" in msg:
|
| 53 |
+
messages.append({
|
| 54 |
+
"role": msg["role"],
|
| 55 |
+
"content": str(msg["content"])[:1000] # Giới hạn độ dài
|
| 56 |
+
})
|
| 57 |
|
| 58 |
+
messages.append({"role": "user", "content": str(message)[:1000]})
|
| 59 |
|
| 60 |
reply = call_groq(messages)
|
| 61 |
return "", history + [{"role": "assistant", "content": reply}]
|
| 62 |
|
| 63 |
with gr.Blocks(title="NodeAI") as demo:
|
| 64 |
+
gr.Markdown("# 🚀 NodeAI - GroqChat")
|
|
|
|
| 65 |
gr.Markdown(f"**Status:** {status_msg}")
|
| 66 |
|
| 67 |
+
chatbot = gr.Chatbot(height=500, avatar_images=("👤", "🤖"))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
+
msg = gr.Textbox(placeholder="Hỏi NodeAI...", scale=7)
|
| 71 |
+
send_btn = gr.Button("Gửi", variant="primary", scale=1)
|
| 72 |
+
clear_btn = gr.Button("🗑️", scale=1)
|
| 73 |
|
| 74 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 75 |
send_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 76 |
clear_btn.click(lambda: [], None, chatbot)
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|
| 79 |
+
demo.launch(theme=gr.themes.Soft(primary_hue="blue"))
|
|
|
|
|
|
|
|
|
|
|
|