Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py -
|
| 2 |
import gradio as gr
|
| 3 |
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
|
| 4 |
import torch
|
|
@@ -8,18 +8,19 @@ import time
|
|
| 8 |
# === OPTIMIERTE EINSTELLUNGEN ===
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
| 11 |
-
IMG_SIZE =
|
| 12 |
-
STEPS =
|
| 13 |
|
| 14 |
print(f"Running on: {device}")
|
| 15 |
|
| 16 |
# === PIPELINES ===
|
| 17 |
pipe_txt2img = None
|
|
|
|
| 18 |
|
| 19 |
-
def
|
| 20 |
global pipe_txt2img
|
| 21 |
if pipe_txt2img is None:
|
| 22 |
-
print("Loading model...")
|
| 23 |
pipe_txt2img = StableDiffusionPipeline.from_pretrained(
|
| 24 |
"runwayml/stable-diffusion-v1-5",
|
| 25 |
torch_dtype=torch_dtype,
|
|
@@ -30,6 +31,20 @@ def load_pipeline():
|
|
| 30 |
pipe_txt2img.enable_attention_slicing()
|
| 31 |
return pipe_txt2img
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# === FUNKTIONEN ===
|
| 34 |
def text_to_image(prompt):
|
| 35 |
try:
|
|
@@ -39,21 +54,20 @@ def text_to_image(prompt):
|
|
| 39 |
print(f"Starting generation for: {prompt}")
|
| 40 |
start_time = time.time()
|
| 41 |
|
| 42 |
-
pipe =
|
| 43 |
|
| 44 |
-
#
|
| 45 |
image = pipe(
|
| 46 |
prompt=prompt,
|
| 47 |
height=IMG_SIZE,
|
| 48 |
width=IMG_SIZE,
|
| 49 |
num_inference_steps=STEPS,
|
| 50 |
-
guidance_scale=7.
|
| 51 |
).images[0]
|
| 52 |
|
| 53 |
end_time = time.time()
|
| 54 |
print(f"✅ Bild generiert in {end_time - start_time:.2f} Sekunden")
|
| 55 |
|
| 56 |
-
# Sofortige Rückgabe
|
| 57 |
return image
|
| 58 |
|
| 59 |
except Exception as e:
|
|
@@ -62,35 +76,112 @@ def text_to_image(prompt):
|
|
| 62 |
traceback.print_exc()
|
| 63 |
return None
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
with gr.Blocks() as app:
|
| 67 |
gr.Markdown("# 🎨 AI Bild Generator")
|
| 68 |
|
| 69 |
with gr.Tab("Text zu Bild"):
|
| 70 |
-
gr.Markdown("**
|
| 71 |
txt_input = gr.Textbox(
|
| 72 |
-
placeholder="z.B. a red apple on a table",
|
| 73 |
-
lines=2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
)
|
| 75 |
-
generate_btn = gr.Button("🎨 Generieren", variant="primary")
|
| 76 |
-
txt_output = gr.Image(label="Ergebnis")
|
| 77 |
|
| 78 |
generate_btn.click(
|
| 79 |
fn=text_to_image,
|
| 80 |
inputs=txt_input,
|
| 81 |
outputs=txt_output,
|
| 82 |
-
# Wichtig: Queue konfigurieren
|
| 83 |
concurrency_limit=1
|
| 84 |
)
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
app.launch(
|
| 88 |
share=True,
|
| 89 |
server_name="0.0.0.0",
|
| 90 |
server_port=7860,
|
| 91 |
-
# Wichtige Timeout-Einstellungen:
|
| 92 |
max_file_size="10MB",
|
| 93 |
-
# Verhindert "Connection re-established":
|
| 94 |
ssl_verify=False,
|
| 95 |
quiet=True,
|
| 96 |
show_error=True
|
|
|
|
| 1 |
+
# app.py - OPTIMIERTE VERSION
|
| 2 |
import gradio as gr
|
| 3 |
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
|
| 4 |
import torch
|
|
|
|
| 8 |
# === OPTIMIERTE EINSTELLUNGEN ===
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
| 11 |
+
IMG_SIZE = 320 # Bessere Qualität aber noch schnell
|
| 12 |
+
STEPS = 20 # Mehr Schritte für bessere Qualität
|
| 13 |
|
| 14 |
print(f"Running on: {device}")
|
| 15 |
|
| 16 |
# === PIPELINES ===
|
| 17 |
pipe_txt2img = None
|
| 18 |
+
pipe_img2img = None
|
| 19 |
|
| 20 |
+
def load_txt2img():
|
| 21 |
global pipe_txt2img
|
| 22 |
if pipe_txt2img is None:
|
| 23 |
+
print("Loading Text-to-Image model...")
|
| 24 |
pipe_txt2img = StableDiffusionPipeline.from_pretrained(
|
| 25 |
"runwayml/stable-diffusion-v1-5",
|
| 26 |
torch_dtype=torch_dtype,
|
|
|
|
| 31 |
pipe_txt2img.enable_attention_slicing()
|
| 32 |
return pipe_txt2img
|
| 33 |
|
| 34 |
+
def load_img2img():
|
| 35 |
+
global pipe_img2img
|
| 36 |
+
if pipe_img2img is None:
|
| 37 |
+
print("Loading Image-to-Image model...")
|
| 38 |
+
pipe_img2img = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 39 |
+
"runwayml/stable-diffusion-v1-5",
|
| 40 |
+
torch_dtype=torch_dtype,
|
| 41 |
+
use_safetensors=True,
|
| 42 |
+
safety_checker=None,
|
| 43 |
+
requires_safety_checker=False
|
| 44 |
+
).to(device)
|
| 45 |
+
pipe_img2img.enable_attention_slicing()
|
| 46 |
+
return pipe_img2img
|
| 47 |
+
|
| 48 |
# === FUNKTIONEN ===
|
| 49 |
def text_to_image(prompt):
|
| 50 |
try:
|
|
|
|
| 54 |
print(f"Starting generation for: {prompt}")
|
| 55 |
start_time = time.time()
|
| 56 |
|
| 57 |
+
pipe = load_txt2img()
|
| 58 |
|
| 59 |
+
# Optimierte Generierung für bessere Qualität
|
| 60 |
image = pipe(
|
| 61 |
prompt=prompt,
|
| 62 |
height=IMG_SIZE,
|
| 63 |
width=IMG_SIZE,
|
| 64 |
num_inference_steps=STEPS,
|
| 65 |
+
guidance_scale=7.5, # Standard für bessere Qualität
|
| 66 |
).images[0]
|
| 67 |
|
| 68 |
end_time = time.time()
|
| 69 |
print(f"✅ Bild generiert in {end_time - start_time:.2f} Sekunden")
|
| 70 |
|
|
|
|
| 71 |
return image
|
| 72 |
|
| 73 |
except Exception as e:
|
|
|
|
| 76 |
traceback.print_exc()
|
| 77 |
return None
|
| 78 |
|
| 79 |
+
def img_to_image(image, prompt="", strength=0.75):
|
| 80 |
+
try:
|
| 81 |
+
if image is None:
|
| 82 |
+
return None
|
| 83 |
+
|
| 84 |
+
print(f"Image-to-Image mit Prompt: {prompt}")
|
| 85 |
+
start_time = time.time()
|
| 86 |
+
|
| 87 |
+
pipe = load_img2img()
|
| 88 |
+
img_resized = image.convert("RGB").resize((IMG_SIZE, IMG_SIZE))
|
| 89 |
+
|
| 90 |
+
result = pipe(
|
| 91 |
+
prompt=prompt,
|
| 92 |
+
image=img_resized,
|
| 93 |
+
strength=strength,
|
| 94 |
+
num_inference_steps=STEPS,
|
| 95 |
+
guidance_scale=7.5
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
end_time = time.time()
|
| 99 |
+
print(f"✅ Bild transformiert in {end_time - start_time:.2f} Sekunden")
|
| 100 |
+
|
| 101 |
+
return result.images[0]
|
| 102 |
+
|
| 103 |
+
except Exception as e:
|
| 104 |
+
print(f"❌ Fehler: {e}")
|
| 105 |
+
import traceback
|
| 106 |
+
traceback.print_exc()
|
| 107 |
+
return None
|
| 108 |
+
|
| 109 |
+
# === UI ===
|
| 110 |
with gr.Blocks() as app:
|
| 111 |
gr.Markdown("# 🎨 AI Bild Generator")
|
| 112 |
|
| 113 |
with gr.Tab("Text zu Bild"):
|
| 114 |
+
gr.Markdown("**Beschreibe dein gewünschtes Bild:**")
|
| 115 |
txt_input = gr.Textbox(
|
| 116 |
+
placeholder="z.B. a red apple on a wooden table, photorealistic",
|
| 117 |
+
lines=2,
|
| 118 |
+
label="Prompt (Englisch)"
|
| 119 |
+
)
|
| 120 |
+
generate_btn = gr.Button("🎨 Bild generieren", variant="primary")
|
| 121 |
+
txt_output = gr.Image(
|
| 122 |
+
label="Generiertes Bild",
|
| 123 |
+
show_download_button=True
|
| 124 |
)
|
|
|
|
|
|
|
| 125 |
|
| 126 |
generate_btn.click(
|
| 127 |
fn=text_to_image,
|
| 128 |
inputs=txt_input,
|
| 129 |
outputs=txt_output,
|
|
|
|
| 130 |
concurrency_limit=1
|
| 131 |
)
|
| 132 |
|
| 133 |
+
with gr.Tab("Bild zu Bild"):
|
| 134 |
+
gr.Markdown("**Lade ein Bild hoch und beschreibe die gewünschte Veränderung:**")
|
| 135 |
+
|
| 136 |
+
with gr.Row():
|
| 137 |
+
img_input = gr.Image(
|
| 138 |
+
type="pil",
|
| 139 |
+
label="Eingabebild",
|
| 140 |
+
height=300
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
with gr.Row():
|
| 144 |
+
img_prompt = gr.Textbox(
|
| 145 |
+
placeholder="z.B. convert to winter landscape with snow",
|
| 146 |
+
lines=2,
|
| 147 |
+
label="Transformations-Prompt (Englisch)"
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
with gr.Row():
|
| 151 |
+
strength_slider = gr.Slider(
|
| 152 |
+
0.6, 0.9, 0.75,
|
| 153 |
+
label="Stärke der Veränderung"
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
with gr.Row():
|
| 157 |
+
gr.Markdown(
|
| 158 |
+
"**Stärke-Einstellung:** "
|
| 159 |
+
"• **0.6-0.7:** Behält mehr vom Original bei "
|
| 160 |
+
"• **0.75:** Gute Balance (empfohlen) "
|
| 161 |
+
"• **0.8-0.9:** Stärkere Veränderungen"
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
transform_btn = gr.Button("🔄 Bild transformieren", variant="primary")
|
| 165 |
+
|
| 166 |
+
with gr.Row():
|
| 167 |
+
img_output = gr.Image(
|
| 168 |
+
label="Transformiertes Bild",
|
| 169 |
+
show_download_button=True
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
transform_btn.click(
|
| 173 |
+
fn=img_to_image,
|
| 174 |
+
inputs=[img_input, img_prompt, strength_slider],
|
| 175 |
+
outputs=img_output,
|
| 176 |
+
concurrency_limit=1
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
# === APP START ===
|
| 180 |
app.launch(
|
| 181 |
share=True,
|
| 182 |
server_name="0.0.0.0",
|
| 183 |
server_port=7860,
|
|
|
|
| 184 |
max_file_size="10MB",
|
|
|
|
| 185 |
ssl_verify=False,
|
| 186 |
quiet=True,
|
| 187 |
show_error=True
|