Teotonix commited on
Commit
693726b
·
verified ·
1 Parent(s): 1a90d60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -24
app.py CHANGED
@@ -2,46 +2,61 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
- # Token'ı al
6
  token = os.getenv("HF_TOKEN")
7
 
8
- # Modelleri doğrudan URL ile tanımlayarak "Router" hatasını engelliyoruz
9
- # Bu yöntem 403 Forbidden hatasını genellikle çözer
10
- text_client = InferenceClient(model="https://api-inference.huggingface.co/models/TinyLlama/TinyLlama-1.1B-Chat-v1.0", token=token)
11
- image_client = InferenceClient(model="https://api-inference.huggingface.co/models/stabilityai/sdxl-turbo", token=token)
12
 
13
  def chat_fn(message, history):
14
  try:
15
- # ChatCompletion yerine doğrudan text_generation kullanıyoruz (Daha güvenli)
16
- response = text_client.text_generation(message, max_new_tokens=500)
17
- return response
 
 
 
 
 
 
 
 
 
18
  except Exception as e:
19
- return f"Hata: {str(e)}"
20
 
21
  def image_fn(prompt):
22
  if not prompt: return None
23
  try:
24
- return image_client.text_to_image(prompt)
 
 
 
25
  except Exception as e:
26
- print(f"Görsel hatası: {e}")
27
  return None
28
 
29
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
30
- gr.Markdown("# 🚀 Maind AI Studio")
 
 
31
  with gr.Tabs():
32
- with gr.TabItem("💬 Sohbet"):
33
- msg = gr.Textbox(label="Mesajın")
34
- out_txt = gr.Textbox(label="Maind AI Yanıtı")
35
- btn_txt = gr.Button("Gönder")
36
- btn_txt.click(chat_fn, msg, out_txt)
 
37
 
38
- with gr.TabItem("🎨 Görsel"):
39
  with gr.Row():
40
  with gr.Column():
41
- inp = gr.Textbox(label="Görsel Tarifi")
42
- btn_img = gr.Button("Çiz")
43
  with gr.Column():
44
- out_img = gr.Image()
45
- btn_img.click(image_fn, inp, out_img)
 
46
 
47
- demo.launch()
 
 
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
+ # Secret olarak eklediğin yeni 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
+ # Yeni router sistemi üzerinden sohbet
14
+ response = ""
15
+ for msg in client.chat_completion(
16
+ model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
17
+ messages=[{"role": "user", "content": message}],
18
+ max_tokens=500,
19
+ stream=True,
20
+ ):
21
+ token_str = msg.choices[0].delta.content
22
+ if token_str:
23
+ response += token_str
24
+ yield response
25
  except Exception as e:
26
+ yield f"Sohbet Hatası: {str(e)}"
27
 
28
  def image_fn(prompt):
29
  if not prompt: return None
30
  try:
31
+ # Yeni router sistemi üzerinden görsel (SDXL Turbo)
32
+ # Bu model hızlı olduğu için tercih edildi
33
+ image = client.text_to_image(prompt, model="stabilityai/sdxl-turbo")
34
+ return image
35
  except Exception as e:
36
+ print(f"Görsel Hatası: {e}")
37
  return None
38
 
39
+ # Şık ve Tek Sayfada Arayüz
40
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="cyan")) as demo:
41
+ gr.Markdown("<h1 style='text-align: center;'>🚀 Maind AI Studio</h1>")
42
+
43
  with gr.Tabs():
44
+ with gr.TabItem("💬 Akıllı Sohbet"):
45
+ gr.ChatInterface(
46
+ fn=chat_fn,
47
+ examples=["Merhaba, nasılsın?", "Bana bir hikaye anlat."],
48
+ cache_examples=False
49
+ )
50
 
51
+ with gr.TabItem("🎨 Görsel Oluştur"):
52
  with gr.Row():
53
  with gr.Column():
54
+ inp = gr.Textbox(label="Hayalindeki Görsel", placeholder="Futuristic city, neon lights, 4k...")
55
+ btn = gr.Button("Sihri Başlat ✨", variant="primary")
56
  with gr.Column():
57
+ out = gr.Image(label="Üretilen Görsel")
58
+
59
+ btn.click(fn=image_fn, inputs=inp, outputs=out)
60
 
61
+ # Kuyruğu başlat ve yayına al
62
+ demo.queue().launch()