Update app.py
Browse files
app.py
CHANGED
|
@@ -2,35 +2,25 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
token = os.getenv("HF_TOKEN")
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
text_client = InferenceClient("TinyLlama/TinyLlama-1.1B-Chat-v1.0", token=token)
|
| 13 |
-
image_client = InferenceClient("stabilityai/sdxl-turbo", token=token)
|
| 14 |
|
| 15 |
def chat_fn(message, history):
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
messages=[{"role": "user", "content": message}],
|
| 21 |
-
max_tokens=500,
|
| 22 |
-
stream=True,
|
| 23 |
-
):
|
| 24 |
-
token_str = msg.choices[0].delta.content
|
| 25 |
-
response += token_str
|
| 26 |
-
yield response
|
| 27 |
except Exception as e:
|
| 28 |
-
|
| 29 |
|
| 30 |
def image_fn(prompt):
|
| 31 |
if not prompt: return None
|
| 32 |
try:
|
| 33 |
-
# Görsel oluşturma
|
| 34 |
return image_client.text_to_image(prompt)
|
| 35 |
except Exception as e:
|
| 36 |
print(f"Görsel hatası: {e}")
|
|
@@ -40,14 +30,18 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 40 |
gr.Markdown("# 🚀 Maind AI Studio")
|
| 41 |
with gr.Tabs():
|
| 42 |
with gr.TabItem("💬 Sohbet"):
|
| 43 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
with gr.TabItem("🎨 Görsel"):
|
| 45 |
with gr.Row():
|
| 46 |
with gr.Column():
|
| 47 |
-
inp = gr.Textbox(label="
|
| 48 |
-
|
| 49 |
with gr.Column():
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
demo.launch()
|
|
|
|
| 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}")
|
|
|
|
| 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()
|