Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,62 @@
|
|
| 1 |
-
import os, requests, gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
reply =
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, requests, gradio as gr
|
| 2 |
+
|
| 3 |
+
# 🔗 Hugging Face API bağlantısı
|
| 4 |
+
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 5 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
+
HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 7 |
+
|
| 8 |
+
# 💬 Ana sohbet fonksiyonu
|
| 9 |
+
def chat(message, history):
|
| 10 |
+
history = history or []
|
| 11 |
+
payload = {
|
| 12 |
+
"inputs": message,
|
| 13 |
+
"parameters": {"max_new_tokens": 300, "temperature": 0.7},
|
| 14 |
+
"options": {"wait_for_model": True}
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
r = requests.post(API_URL, headers=HEADERS, json=payload, timeout=60)
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
data = r.json()
|
| 22 |
+
except ValueError:
|
| 23 |
+
reply = "⚠️ Model boş yanıt döndürdü veya bağlantı kesildi."
|
| 24 |
+
history.append((message, reply))
|
| 25 |
+
return history, history
|
| 26 |
+
|
| 27 |
+
# 🔍 Yanıt kontrolü
|
| 28 |
+
if isinstance(data, list) and "generated_text" in data[0]:
|
| 29 |
+
reply = data[0]["generated_text"]
|
| 30 |
+
else:
|
| 31 |
+
reply = data.get("generated_text") or data.get("error", "⚠️ Modelden yanıt alınamadı.")
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
reply = f"❌ Hata: {e}"
|
| 35 |
+
|
| 36 |
+
history.append((message, reply))
|
| 37 |
+
return history, history
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# 🎨 Tema ve arayüz ayarları
|
| 41 |
+
theme = gr.themes.Soft(primary_hue="blue", neutral_hue="slate").set(
|
| 42 |
+
body_background_fill="#0f172a",
|
| 43 |
+
block_background_fill="#1e293b",
|
| 44 |
+
block_title_text_color="#38bdf8"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
with gr.Blocks(theme=theme, title="ZenkaMind v11") as demo:
|
| 48 |
+
gr.Markdown(
|
| 49 |
+
"<h1 style='text-align:center;color:#38bdf8'>🧠 ZenkaMind v11</h1>"
|
| 50 |
+
"<p style='text-align:center;color:#94a3b8'>Türkçe yapay zekâ sohbet asistanı — Mixtral 8x7B</p>"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
chat_ui = gr.Chatbot(height=460, label="ZenkaMind")
|
| 54 |
+
msg = gr.Textbox(placeholder="Mesajınızı yazın ve Enter’a basın…", show_label=False)
|
| 55 |
+
clear = gr.Button("🧹 Temizle")
|
| 56 |
+
|
| 57 |
+
msg.submit(chat, [msg, chat_ui], [chat_ui, chat_ui])
|
| 58 |
+
clear.click(lambda: None, None, chat_ui, queue=False)
|
| 59 |
+
|
| 60 |
+
gr.Markdown("<p style='text-align:center;font-size:12px;color:#64748b'>© 2025 ZenkaMind Bilişim & Teknoloji</p>")
|
| 61 |
+
|
| 62 |
+
demo.launch()
|