Zenkad commited on
Commit
c7a1da2
·
verified ·
1 Parent(s): cf4739c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -47
app.py CHANGED
@@ -1,47 +1,62 @@
1
- import os, requests, gradio as gr
2
-
3
- API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
4
- HF_TOKEN = os.getenv("HF_TOKEN")
5
- HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
6
-
7
- def chat(message, history):
8
- history = history or []
9
- convo = [{"role":"system","content":"You are ZenkaMind, a helpful Turkish AI assistant."}]
10
- for human, ai in history:
11
- convo += [{"role":"user","content":human},{"role":"assistant","content":ai}]
12
- convo.append({"role":"user","content":message})
13
-
14
- payload = {"inputs": convo, "parameters":{"max_new_tokens":300,"temperature":0.7}, "options":{"wait_for_model":True}}
15
- try:
16
- r = requests.post(API_URL, headers=HEADERS, json=payload, timeout=60)
17
- data = r.json()
18
- if isinstance(data, list) and "generated_text" in data[0]:
19
- reply = data[0]["generated_text"][-1]["content"]
20
- elif isinstance(data, list) and isinstance(data[0], dict) and "generated_text" in data[0]:
21
- reply = data[0]["generated_text"]
22
- else:
23
- reply = data.get("error","⚠️ Modelden yanıt alınamadı.")
24
- except Exception as e:
25
- reply = f"❌ Hata: {e}"
26
-
27
- history.append((message, reply))
28
- return history, history
29
-
30
- theme = gr.themes.Soft(primary_hue="blue", neutral_hue="slate").set(
31
- body_background_fill="#0f172a",
32
- block_background_fill="#1e293b",
33
- block_title_text_color="#38bdf8"
34
- )
35
-
36
- with gr.Blocks(theme=theme, title="ZenkaMind v11") as demo:
37
- gr.Markdown("<h1 style='text-align:center;color:#38bdf8'>🧠 ZenkaMind v11</h1>"
38
- "<p style='text-align:center;color:#94a3b8'>Türkçe yapay zekâ sohbet asistanı — Mixtral 8x7B</p>")
39
- chat_ui = gr.Chatbot(height=460, label="ZenkaMind")
40
- msg = gr.Textbox(placeholder="Mesajınızı yazın ve Enter’a basın…", show_label=False)
41
- clear = gr.Button("🧹 Temizle")
42
-
43
- msg.submit(chat, [msg, chat_ui], [chat_ui, chat_ui])
44
- clear.click(lambda: None, None, chat_ui, queue=False)
45
-
46
- gr.Markdown("<p style='text-align:center;font-size:12px;color:#64748b'>© 2025 Zenkamind Bilişim & Teknoloji</p>")
47
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()