minddd / app.py
Zenkad's picture
Update app.py
1413a24 verified
import os
import requests
import gradio as gr
# 🔐 Hugging Face API bağlantısı
API_URL = "/static-proxy?url=https%3A%2F%2Fapi-inference.huggingface.co%2Fmodels%2Fmistralai%2FMixtral-8x7B-Instruct-v0.1%26quot%3B%3C%2Fspan%3E%3C!-- HTML_TAG_END -->
HF_TOKEN = os.getenv("HF_TOKEN") # Hugging Face Secret olarak ekledin ya, burada çağrılıyor
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
# 💬 Chat fonksiyonu
def chat(message, history):
history = history or []
convo = [{"role": "system", "content": "You are ZenkaMind, a helpful Turkish AI assistant."}]
for user, bot in history:
convo.append({"role": "user", "content": user})
convo.append({"role": "assistant", "content": bot})
convo.append({"role": "user", "content": message})
payload = {
"inputs": convo,
"parameters": {"max_new_tokens": 300, "temperature": 0.7},
"options": {"wait_for_model": True},
}
try:
r = requests.post(API_URL, headers=HEADERS, json=payload, timeout=60)
data = r.json()
if isinstance(data, list) and "generated_text" in data[0]:
reply = data[0]["generated_text"]
elif isinstance(data, dict) and "error" in data:
reply = f"⚠️ Hata: {data['error']}"
else:
reply = "⚠️ Model boş yanıt döndürdü veya bağlantı kesildi."
except Exception as e:
reply = f"❌ Sunucu hatası: {str(e)}"
history.append((message, reply))
return history, history
# 🎨 Tema ve Arayüz
theme = gr.themes.Soft(
primary_hue="blue",
neutral_hue="slate",
).set(
body_background_fill="#0f172a",
block_background_fill="#1e293b",
block_label_text_color="#38bdf8",
)
with gr.Blocks(theme=theme, title="ZenkaMind v12") as demo:
gr.Markdown(
"""
<h1 style="text-align:center;color:#38bdf8;">🧠 ZenkaMind v12</h1>
<p style="text-align:center;color:#94a3b8;">
Türkçe yapay zekâ sohbet asistanı — <strong>Mixtral 8x7B</strong> modeliyle çalışır.<br>
© 2025 ZenkaMind Bilişim & Teknoloji
</p>
"""
)
chatbot = gr.Chatbot(height=500, label="ZenkaMind Sohbet Ekranı")
user_msg = gr.Textbox(
placeholder="Mesajınızı yazın ve Enter’a basın...",
show_label=False,
autofocus=True,
)
send_btn = gr.Button("🚀 Gönder")
clear_btn = gr.Button("🧹 Sohbeti Temizle")
# Bağlantılar
user_msg.submit(chat, [user_msg, chatbot], [chatbot, chatbot])
send_btn.click(chat, [user_msg, chatbot], [chatbot, chatbot])
clear_btn.click(lambda: None, None, chatbot, queue=False)
demo.launch()