Zenkad commited on
Commit
5df962c
·
verified ·
1 Parent(s): 157fd07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -35
app.py CHANGED
@@ -1,59 +1,49 @@
1
- import os
2
- import requests
3
  import gradio as gr
 
 
4
 
5
- # API Ayarları
6
  API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
7
- HF_TOKEN = os.getenv("HF_TOKEN") # Hugging Face Secret’ta HF_TOKEN olarak ekledin
8
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
9
 
10
- # ✅ Chat Fonksiyonu
11
  def chat(message, history):
12
- history = history or []
13
- convo = [
14
- {"role": "system", "content": "You are ZenkaMind, a helpful and emotional Turkish AI assistant. Speak fluently and naturally."}
15
- ]
16
- for human, ai in history:
17
- convo.append({"role": "user", "content": human})
18
- convo.append({"role": "assistant", "content": ai})
19
- convo.append({"role": "user", "content": message})
20
-
21
- payload = {
22
- "inputs": convo,
23
- "parameters": {"max_new_tokens": 300, "temperature": 0.7},
24
- "options": {"wait_for_model": True}
25
- }
26
-
27
  try:
28
- res = requests.post(API_URL, headers=HEADERS, json=payload, timeout=60)
29
- data = res.json()
30
-
31
- if isinstance(data, list) and "generated_text" in data[0]:
32
- reply = data[0]["generated_text"]
33
- elif isinstance(data, dict) and "error" in data:
34
- reply = f"⚠️ Model hatası: {data['error']}"
 
 
 
 
 
 
35
  else:
36
- reply = "⚠️ Şu anda modellerden yanıt alınamadı, lütfen birkaç saniye sonra tekrar deneyin."
 
37
  except Exception as e:
38
- reply = f"❌ Sunucu hatası: {e}"
39
 
40
  history.append((message, reply))
41
  return history, history
42
 
43
- # ✅ Tema Ayarları
44
  theme = gr.themes.Soft(primary_hue="blue", neutral_hue="slate").set(
45
  body_background_fill="#0f172a",
46
  block_background_fill="#1e293b",
47
  block_title_text_color="#38bdf8"
48
  )
49
 
50
- # Arayüz
51
- with gr.Blocks(theme=theme, title="ZenkaMind v11 - Stable") as demo:
52
  gr.Markdown("<h1 style='text-align:center;color:#38bdf8'>🧠 ZenkaMind v11</h1>"
53
  "<p style='text-align:center;color:#94a3b8'>Türkçe yapay zekâ sohbet asistanı — Mixtral 8x7B</p>")
54
- chatbot = gr.Chatbot(label="ZenkaMind Sohbet Penceresi", height=460)
55
- msg = gr.Textbox(placeholder="Mesajınızı yazın ve Enter’a basın...", show_label=False)
56
- clear = gr.Button("🧹 Sohbeti Temizle")
 
57
 
58
  msg.submit(chat, [msg, chatbot], [chatbot, chatbot])
59
  clear.click(lambda: None, None, chatbot, queue=False)
 
 
 
1
  import gradio as gr
2
+ import requests
3
+ import os
4
 
5
+ HF_TOKEN = os.getenv("HF_TOKEN")
6
  API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
 
7
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
8
 
 
9
  def chat(message, history):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  try:
11
+ history = history or []
12
+ payload = {
13
+ "inputs": f"Kullanıcı: {message}\nAsistan:",
14
+ "parameters": {"max_new_tokens": 400, "temperature": 0.7},
15
+ "options": {"wait_for_model": True}
16
+ }
17
+ response = requests.post(API_URL, headers=HEADERS, json=payload, timeout=60)
18
+ if response.status_code != 200:
19
+ return [(message, f"⚠️ Sunucu hatası ({response.status_code})")]
20
+
21
+ data = response.json()
22
+ if isinstance(data, list) and len(data) > 0 and "generated_text" in data[0]:
23
+ reply = data[0]["generated_text"].split("Asistan:")[-1].strip()
24
  else:
25
+ reply = "⚠️ Model boş yanıt döndürdü veya bağlantı kesildi."
26
+
27
  except Exception as e:
28
+ reply = f"❌ Hata: {e}"
29
 
30
  history.append((message, reply))
31
  return history, history
32
 
33
+
34
  theme = gr.themes.Soft(primary_hue="blue", neutral_hue="slate").set(
35
  body_background_fill="#0f172a",
36
  block_background_fill="#1e293b",
37
  block_title_text_color="#38bdf8"
38
  )
39
 
40
+ with gr.Blocks(theme=theme, title="ZenkaMind v11") as demo:
 
41
  gr.Markdown("<h1 style='text-align:center;color:#38bdf8'>🧠 ZenkaMind v11</h1>"
42
  "<p style='text-align:center;color:#94a3b8'>Türkçe yapay zekâ sohbet asistanı — Mixtral 8x7B</p>")
43
+
44
+ chatbot = gr.Chatbot(height=460, label="ZenkaMind Sohbeti")
45
+ msg = gr.Textbox(placeholder="Bir şey yazın ve Enter’a basın...", show_label=False)
46
+ clear = gr.Button("🧹 Temizle")
47
 
48
  msg.submit(chat, [msg, chatbot], [chatbot, chatbot])
49
  clear.click(lambda: None, None, chatbot, queue=False)