Update app.py
Browse files
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 |
-
#
|
| 9 |
-
|
| 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 |
-
#
|
| 16 |
-
response =
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
except Exception as e:
|
| 19 |
-
|
| 20 |
|
| 21 |
def image_fn(prompt):
|
| 22 |
if not prompt: return None
|
| 23 |
try:
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
-
print(f"Görsel
|
| 27 |
return None
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
with gr.Tabs():
|
| 32 |
-
with gr.TabItem("💬 Sohbet"):
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
with gr.TabItem("🎨 Görsel"):
|
| 39 |
with gr.Row():
|
| 40 |
with gr.Column():
|
| 41 |
-
inp = gr.Textbox(label="Görsel
|
| 42 |
-
|
| 43 |
with gr.Column():
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
| 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()
|