Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# app.py - KORRIGIERTE VERSION
|
| 2 |
import gradio as gr
|
| 3 |
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
|
| 4 |
import torch
|
|
@@ -9,7 +8,6 @@ import time
|
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
| 11 |
IMG_SIZE = 512
|
| 12 |
-
STEPS = 35
|
| 13 |
|
| 14 |
print(f"Running on: {device}")
|
| 15 |
|
|
@@ -28,6 +26,11 @@ def load_txt2img():
|
|
| 28 |
safety_checker=None,
|
| 29 |
requires_safety_checker=False
|
| 30 |
).to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
pipe_txt2img.enable_attention_slicing()
|
| 32 |
return pipe_txt2img
|
| 33 |
|
|
@@ -42,16 +45,22 @@ def load_img2img():
|
|
| 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:
|
| 51 |
if not prompt or not prompt.strip():
|
| 52 |
return None
|
| 53 |
|
| 54 |
print(f"Starting generation for: {prompt}")
|
|
|
|
| 55 |
start_time = time.time()
|
| 56 |
|
| 57 |
pipe = load_txt2img()
|
|
@@ -60,8 +69,8 @@ def text_to_image(prompt):
|
|
| 60 |
prompt=prompt,
|
| 61 |
height=IMG_SIZE,
|
| 62 |
width=IMG_SIZE,
|
| 63 |
-
num_inference_steps=
|
| 64 |
-
guidance_scale=
|
| 65 |
).images[0]
|
| 66 |
|
| 67 |
end_time = time.time()
|
|
@@ -75,23 +84,23 @@ def text_to_image(prompt):
|
|
| 75 |
traceback.print_exc()
|
| 76 |
return None
|
| 77 |
|
| 78 |
-
def img_to_image(image, prompt
|
| 79 |
try:
|
| 80 |
if image is None:
|
| 81 |
return None
|
| 82 |
|
| 83 |
-
print(f"Image-to-Image mit Strength: {strength}")
|
| 84 |
start_time = time.time()
|
| 85 |
|
| 86 |
pipe = load_img2img()
|
| 87 |
-
img_resized = image.convert("RGB").resize((
|
| 88 |
|
| 89 |
result = pipe(
|
| 90 |
prompt=prompt,
|
| 91 |
image=img_resized,
|
| 92 |
strength=strength,
|
| 93 |
-
num_inference_steps=
|
| 94 |
-
guidance_scale=
|
| 95 |
)
|
| 96 |
|
| 97 |
end_time = time.time()
|
|
@@ -111,11 +120,33 @@ with gr.Blocks() as app:
|
|
| 111 |
|
| 112 |
with gr.Tab("Text zu Bild"):
|
| 113 |
gr.Markdown("**Beschreibe dein gewünschtes Bild:**")
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
generate_btn = gr.Button("🎨 Bild generieren", variant="primary")
|
| 120 |
txt_output = gr.Image(
|
| 121 |
label="Generiertes Bild",
|
|
@@ -124,7 +155,7 @@ with gr.Blocks() as app:
|
|
| 124 |
|
| 125 |
generate_btn.click(
|
| 126 |
fn=text_to_image,
|
| 127 |
-
inputs=txt_input,
|
| 128 |
outputs=txt_output,
|
| 129 |
concurrency_limit=1
|
| 130 |
)
|
|
@@ -147,17 +178,28 @@ with gr.Blocks() as app:
|
|
| 147 |
)
|
| 148 |
|
| 149 |
with gr.Row():
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
with gr.Row():
|
| 156 |
gr.Markdown(
|
| 157 |
-
"**
|
| 158 |
-
"• **
|
| 159 |
-
"• **
|
| 160 |
-
"• **
|
| 161 |
)
|
| 162 |
|
| 163 |
transform_btn = gr.Button("🔄 Bild transformieren", variant="primary")
|
|
@@ -170,7 +212,7 @@ with gr.Blocks() as app:
|
|
| 170 |
|
| 171 |
transform_btn.click(
|
| 172 |
fn=img_to_image,
|
| 173 |
-
inputs=[img_input, img_prompt, strength_slider],
|
| 174 |
outputs=img_output,
|
| 175 |
concurrency_limit=1
|
| 176 |
)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
|
| 3 |
import torch
|
|
|
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
| 10 |
IMG_SIZE = 512
|
|
|
|
| 11 |
|
| 12 |
print(f"Running on: {device}")
|
| 13 |
|
|
|
|
| 26 |
safety_checker=None,
|
| 27 |
requires_safety_checker=False
|
| 28 |
).to(device)
|
| 29 |
+
|
| 30 |
+
# DPMSolver für Text-to-Image
|
| 31 |
+
from diffusers import DPMSolverMultistepScheduler
|
| 32 |
+
pipe_txt2img.scheduler = DPMSolverMultistepScheduler.from_config(pipe_txt2img.scheduler.config)
|
| 33 |
+
|
| 34 |
pipe_txt2img.enable_attention_slicing()
|
| 35 |
return pipe_txt2img
|
| 36 |
|
|
|
|
| 45 |
safety_checker=None,
|
| 46 |
requires_safety_checker=False
|
| 47 |
).to(device)
|
| 48 |
+
|
| 49 |
+
# DPMSolver für Image-to-Image
|
| 50 |
+
from diffusers import DPMSolverMultistepScheduler
|
| 51 |
+
pipe_img2img.scheduler = DPMSolverMultistepScheduler.from_config(pipe_img2img.scheduler.config)
|
| 52 |
+
|
| 53 |
pipe_img2img.enable_attention_slicing()
|
| 54 |
return pipe_img2img
|
| 55 |
|
| 56 |
# === FUNKTIONEN ===
|
| 57 |
+
def text_to_image(prompt, steps, guidance_scale):
|
| 58 |
try:
|
| 59 |
if not prompt or not prompt.strip():
|
| 60 |
return None
|
| 61 |
|
| 62 |
print(f"Starting generation for: {prompt}")
|
| 63 |
+
print(f"Parameters - Steps: {steps}, Guidance Scale: {guidance_scale}")
|
| 64 |
start_time = time.time()
|
| 65 |
|
| 66 |
pipe = load_txt2img()
|
|
|
|
| 69 |
prompt=prompt,
|
| 70 |
height=IMG_SIZE,
|
| 71 |
width=IMG_SIZE,
|
| 72 |
+
num_inference_steps=steps,
|
| 73 |
+
guidance_scale=guidance_scale,
|
| 74 |
).images[0]
|
| 75 |
|
| 76 |
end_time = time.time()
|
|
|
|
| 84 |
traceback.print_exc()
|
| 85 |
return None
|
| 86 |
|
| 87 |
+
def img_to_image(image, prompt, strength, steps, guidance_scale):
|
| 88 |
try:
|
| 89 |
if image is None:
|
| 90 |
return None
|
| 91 |
|
| 92 |
+
print(f"Image-to-Image mit Strength: {strength}, Steps: {steps}, Guidance: {guidance_scale}")
|
| 93 |
start_time = time.time()
|
| 94 |
|
| 95 |
pipe = load_img2img()
|
| 96 |
+
img_resized = image.convert("RGB").resize((IMG_SIZE, IMG_SIZE))
|
| 97 |
|
| 98 |
result = pipe(
|
| 99 |
prompt=prompt,
|
| 100 |
image=img_resized,
|
| 101 |
strength=strength,
|
| 102 |
+
num_inference_steps=steps,
|
| 103 |
+
guidance_scale=guidance_scale
|
| 104 |
)
|
| 105 |
|
| 106 |
end_time = time.time()
|
|
|
|
| 120 |
|
| 121 |
with gr.Tab("Text zu Bild"):
|
| 122 |
gr.Markdown("**Beschreibe dein gewünschtes Bild:**")
|
| 123 |
+
|
| 124 |
+
with gr.Row():
|
| 125 |
+
txt_input = gr.Textbox(
|
| 126 |
+
placeholder="z.B. a red apple on a wooden table, photorealistic",
|
| 127 |
+
lines=2,
|
| 128 |
+
label="Prompt (Englisch)"
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
with gr.Row():
|
| 132 |
+
with gr.Column():
|
| 133 |
+
txt_steps = gr.Slider(
|
| 134 |
+
minimum=10, maximum=100, value=35, step=1,
|
| 135 |
+
label="Steps (Qualität vs. Geschwindigkeit)"
|
| 136 |
+
)
|
| 137 |
+
with gr.Column():
|
| 138 |
+
txt_guidance = gr.Slider(
|
| 139 |
+
minimum=1.0, maximum=20.0, value=7.5, step=0.5,
|
| 140 |
+
label="Guidance Scale (Prompt-Treue)"
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
with gr.Row():
|
| 144 |
+
gr.Markdown(
|
| 145 |
+
"**Parameter-Erklärung:** "
|
| 146 |
+
"• **Steps:** Mehr = bessere Qualität, aber langsamer "
|
| 147 |
+
"• **Guidance:** Höher = stärkere Prompt-Treue, kann verzerren "
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
generate_btn = gr.Button("🎨 Bild generieren", variant="primary")
|
| 151 |
txt_output = gr.Image(
|
| 152 |
label="Generiertes Bild",
|
|
|
|
| 155 |
|
| 156 |
generate_btn.click(
|
| 157 |
fn=text_to_image,
|
| 158 |
+
inputs=[txt_input, txt_steps, txt_guidance],
|
| 159 |
outputs=txt_output,
|
| 160 |
concurrency_limit=1
|
| 161 |
)
|
|
|
|
| 178 |
)
|
| 179 |
|
| 180 |
with gr.Row():
|
| 181 |
+
with gr.Column():
|
| 182 |
+
strength_slider = gr.Slider(
|
| 183 |
+
minimum=0.1, maximum=0.9, value=0.4, step=0.05,
|
| 184 |
+
label="Strength (Veränderungs-Stärke)"
|
| 185 |
+
)
|
| 186 |
+
with gr.Column():
|
| 187 |
+
img_steps = gr.Slider(
|
| 188 |
+
minimum=10, maximum=100, value=35, step=1,
|
| 189 |
+
label="Steps (Qualität vs. Geschwindigkeit)"
|
| 190 |
+
)
|
| 191 |
+
with gr.Column():
|
| 192 |
+
img_guidance = gr.Slider(
|
| 193 |
+
minimum=1.0, maximum=20.0, value=7.5, step=0.5,
|
| 194 |
+
label="Guidance Scale (Prompt-Treue)"
|
| 195 |
+
)
|
| 196 |
|
| 197 |
with gr.Row():
|
| 198 |
gr.Markdown(
|
| 199 |
+
"**Parameter-Erklärung:** "
|
| 200 |
+
"• **Strength:** Niedrig = behält Original, Hoch = starke Veränderung "
|
| 201 |
+
"• **Steps:** Mehr = bessere Qualität, aber langsamer "
|
| 202 |
+
"• **Guidance:** Höher = stärkere Prompt-Treue "
|
| 203 |
)
|
| 204 |
|
| 205 |
transform_btn = gr.Button("🔄 Bild transformieren", variant="primary")
|
|
|
|
| 212 |
|
| 213 |
transform_btn.click(
|
| 214 |
fn=img_to_image,
|
| 215 |
+
inputs=[img_input, img_prompt, strength_slider, img_steps, img_guidance],
|
| 216 |
outputs=img_output,
|
| 217 |
concurrency_limit=1
|
| 218 |
)
|