Teotonix commited on
Commit
4625971
·
verified ·
1 Parent(s): 2c5c4cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -31
app.py CHANGED
@@ -4,55 +4,51 @@ import os
4
 
5
  # Secret olarak eklediğin Token'ı al
6
  token = os.getenv("HF_TOKEN")
7
-
8
- # İstemciyi başlatıyoruz
9
  client = InferenceClient(token=token)
10
 
11
  def chat_fn(message, history):
12
  try:
13
- response = ""
14
- # Mistral-7B, ücretsiz API'da en kararlı çalışan modeldir
15
- for msg in client.chat_completion(
16
- model="mistralai/Mistral-7B-Instruct-v0.3",
17
- messages=[{"role": "user", "content": message}],
18
- max_tokens=500,
19
- stream=True,
20
- ):
21
- # Hata kontrolü: Eğer mesaj içeriği varsa ekle
22
- if msg.choices and len(msg.choices) > 0:
23
- token_str = msg.choices[0].delta.content
24
- if token_str:
25
- response += token_str
26
- yield response
27
  except Exception as e:
28
- yield f"Sohbet Hatası: Teknik bir aksaklık oldu, lütfen tekrar deneyin. (Detay: {str(e)})"
29
 
30
  def image_fn(prompt):
31
  if not prompt: return None
32
  try:
33
  # Görsel oluşturma
34
- image = client.text_to_image(prompt, model="stabilityai/sdxl-turbo")
35
- return image
36
  except Exception as e:
37
- print(f"Görsel Hatası: {e}")
38
  return None
39
 
40
  # Arayüz Ayarları
41
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
42
- gr.Markdown("<h1 style='text-align: center;'>🚀 Maind AI Studio</h1>")
43
 
44
  with gr.Tabs():
45
- with gr.TabItem("💬 Akıllı Sohbet"):
46
- gr.ChatInterface(fn=chat_fn)
 
 
 
 
47
 
48
- with gr.TabItem("🎨 Görsel Oluştur"):
49
  with gr.Row():
50
  with gr.Column():
51
- inp = gr.Textbox(label="Görsel Tarifi (İngilizce)", placeholder="Örn: A cute cat astronaut...")
52
- btn = gr.Button("Sihri Başlat ✨", variant="primary")
53
  with gr.Column():
54
- out = gr.Image(label="Üretilen Görsel")
55
-
56
- btn.click(fn=image_fn, inputs=inp, outputs=out)
57
 
58
- demo.queue().launch()
 
4
 
5
  # Secret olarak eklediğin Token'ı al
6
  token = os.getenv("HF_TOKEN")
 
 
7
  client = InferenceClient(token=token)
8
 
9
  def chat_fn(message, history):
10
  try:
11
+ # Chat arayüzünü bozmamak için basit bir prompt yapısı kuruyoruz
12
+ prompt = f"User: {message}\nAssistant:"
13
+
14
+ # En kararlı çalışan Llama-3 modelini doğrudan metin üretme ile çağırıyoruz
15
+ response = client.text_generation(
16
+ model="meta-llama/Meta-Llama-3-8B-Instruct",
17
+ prompt=prompt,
18
+ max_new_tokens=500,
19
+ stream=False # Hata riskini azaltmak için stream'i kapattık
20
+ )
21
+ return response
 
 
 
22
  except Exception as e:
23
+ return f"Hata: {str(e)}"
24
 
25
  def image_fn(prompt):
26
  if not prompt: return None
27
  try:
28
  # Görsel oluşturma
29
+ return client.text_to_image(prompt, model="stabilityai/sdxl-turbo")
 
30
  except Exception as e:
 
31
  return None
32
 
33
  # Arayüz Ayarları
34
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
35
+ gr.Markdown("<h1 style='text-align: center; color: #38bdf8;'>🚀 Maind AI Studio</h1>")
36
 
37
  with gr.Tabs():
38
+ with gr.TabItem("💬 Sohbet"):
39
+ # ChatInterface bazen karmaşık hata verebilir, manuel kurguluyoruz
40
+ msg = gr.Textbox(label="Mesajınızı yazın")
41
+ out_txt = gr.Textbox(label="Yanıt", interactive=False)
42
+ btn_txt = gr.Button("Gönder", variant="primary")
43
+ btn_txt.click(chat_fn, msg, out_txt)
44
 
45
+ with gr.TabItem("🎨 Görsel"):
46
  with gr.Row():
47
  with gr.Column():
48
+ inp = gr.Textbox(label="Görsel Tarifi (İngilizce)")
49
+ btn_img = gr.Button("Çiz ✨", variant="primary")
50
  with gr.Column():
51
+ out_img = gr.Image()
52
+ btn_img.click(image_fn, inp, out_img)
 
53
 
54
+ demo.launch()