Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,8 @@ from PIL import Image
|
|
| 7 |
# === AUTO HARDWARE ===
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
torch_dtype = torch.float16 if device == "cuda" else torch.float32
|
| 10 |
-
IMG_SIZE =
|
| 11 |
-
STEPS = 50 if device == "cuda" else
|
| 12 |
|
| 13 |
# === GLOBALE VARIABLEN ===
|
| 14 |
pipe_txt2img = None
|
|
@@ -19,10 +19,12 @@ def load_txt2img():
|
|
| 19 |
if pipe_txt2img is None:
|
| 20 |
print("Lade Text-to-Image Pipeline...")
|
| 21 |
pipe_txt2img = StableDiffusionPipeline.from_pretrained(
|
| 22 |
-
"runwayml/stable-diffusion-v1-5",
|
|
|
|
|
|
|
|
|
|
| 23 |
).to(device)
|
| 24 |
-
|
| 25 |
-
pipe_txt2img.enable_attention_slicing()
|
| 26 |
return pipe_txt2img
|
| 27 |
|
| 28 |
def load_img2img():
|
|
@@ -30,46 +32,71 @@ def load_img2img():
|
|
| 30 |
if pipe_img2img is None:
|
| 31 |
print("Lade Img2Img Pipeline...")
|
| 32 |
pipe_img2img = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 33 |
-
"runwayml/stable-diffusion-v1-5",
|
|
|
|
|
|
|
| 34 |
).to(device)
|
| 35 |
-
|
| 36 |
-
pipe_img2img.enable_attention_slicing()
|
| 37 |
return pipe_img2img
|
| 38 |
|
| 39 |
# === FUNKTIONEN ===
|
| 40 |
def text_to_image(prompt):
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def img_to_image(image, prompt="", neg_prompt="", strength=0.75):
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# === UI ===
|
| 59 |
with gr.Blocks() as app:
|
| 60 |
-
gr.Markdown("# Text-to-Image + Img2Img\n**
|
| 61 |
|
| 62 |
with gr.Tab("Text to Image"):
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
with gr.Tab("Bild zu Schneelandschaft"):
|
| 68 |
img_in = gr.Image(type="pil")
|
| 69 |
-
prompt = gr.Textbox(
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
strength = gr.Slider(0.6, 0.9, 0.75)
|
| 72 |
-
img_out = gr.Image(label="Ergebnis
|
| 73 |
-
gr.Button("Generieren")
|
|
|
|
| 74 |
|
| 75 |
app.launch(share=True)
|
|
|
|
| 7 |
# === AUTO HARDWARE ===
|
| 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 if device == "cuda" else 384
|
| 11 |
+
STEPS = 50 if device == "cuda" else 40
|
| 12 |
|
| 13 |
# === GLOBALE VARIABLEN ===
|
| 14 |
pipe_txt2img = None
|
|
|
|
| 19 |
if pipe_txt2img is None:
|
| 20 |
print("Lade Text-to-Image Pipeline...")
|
| 21 |
pipe_txt2img = StableDiffusionPipeline.from_pretrained(
|
| 22 |
+
"runwayml/stable-diffusion-v1-5",
|
| 23 |
+
torch_dtype=torch_dtype,
|
| 24 |
+
# Kompatibilität mit diffusers 0.29.2
|
| 25 |
+
use_safetensors=True
|
| 26 |
).to(device)
|
| 27 |
+
pipe_txt2img.enable_attention_slicing()
|
|
|
|
| 28 |
return pipe_txt2img
|
| 29 |
|
| 30 |
def load_img2img():
|
|
|
|
| 32 |
if pipe_img2img is None:
|
| 33 |
print("Lade Img2Img Pipeline...")
|
| 34 |
pipe_img2img = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 35 |
+
"runwayml/stable-diffusion-v1-5",
|
| 36 |
+
torch_dtype=torch_dtype,
|
| 37 |
+
use_safetensors=True
|
| 38 |
).to(device)
|
| 39 |
+
pipe_img2img.enable_attention_slicing()
|
|
|
|
| 40 |
return pipe_img2img
|
| 41 |
|
| 42 |
# === FUNKTIONEN ===
|
| 43 |
def text_to_image(prompt):
|
| 44 |
+
try:
|
| 45 |
+
pipe = load_txt2img()
|
| 46 |
+
result = pipe(
|
| 47 |
+
prompt=prompt,
|
| 48 |
+
height=IMG_SIZE,
|
| 49 |
+
width=IMG_SIZE,
|
| 50 |
+
num_inference_steps=STEPS,
|
| 51 |
+
guidance_scale=7.5,
|
| 52 |
+
)
|
| 53 |
+
return result.images[0]
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Fehler in text_to_image: {e}")
|
| 56 |
+
return None
|
| 57 |
|
| 58 |
def img_to_image(image, prompt="", neg_prompt="", strength=0.75):
|
| 59 |
+
try:
|
| 60 |
+
pipe = load_img2img()
|
| 61 |
+
img = image.convert("RGB").resize((IMG_SIZE, IMG_SIZE))
|
| 62 |
+
result = pipe(
|
| 63 |
+
prompt=prompt or "snowy winter landscape",
|
| 64 |
+
negative_prompt=neg_prompt or "blur, furniture, grass, table",
|
| 65 |
+
image=img,
|
| 66 |
+
strength=strength,
|
| 67 |
+
guidance_scale=7.5,
|
| 68 |
+
num_inference_steps=STEPS
|
| 69 |
+
)
|
| 70 |
+
return result.images[0]
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"Fehler in img_to_image: {e}")
|
| 73 |
+
return None
|
| 74 |
|
| 75 |
# === UI ===
|
| 76 |
with gr.Blocks() as app:
|
| 77 |
+
gr.Markdown("# Text-to-Image + Img2Img\n**Optimierte Version für bessere Ergebnisse**")
|
| 78 |
|
| 79 |
with gr.Tab("Text to Image"):
|
| 80 |
+
gr.Markdown("### Tipp: Verwende englische Prompts für beste Ergebnisse")
|
| 81 |
+
txt_in = gr.Textbox(
|
| 82 |
+
placeholder="z.B. 'a snowman in a forest, winter, snowy trees, highly detailed'",
|
| 83 |
+
lines=2,
|
| 84 |
+
value="a snowman in a forest, winter, snowy trees, highly detailed"
|
| 85 |
+
)
|
| 86 |
+
txt_out = gr.Image(label="Ergebnis")
|
| 87 |
+
txt_btn = gr.Button("Generieren")
|
| 88 |
+
txt_btn.click(text_to_image, txt_in, txt_out)
|
| 89 |
|
| 90 |
with gr.Tab("Bild zu Schneelandschaft"):
|
| 91 |
img_in = gr.Image(type="pil")
|
| 92 |
+
prompt = gr.Textbox(
|
| 93 |
+
value="girl hugging snowman in snowy landscape, realistic, winter",
|
| 94 |
+
lines=2
|
| 95 |
+
)
|
| 96 |
+
neg = gr.Textbox(value="table, tarp, grass, blur, furniture", lines=2)
|
| 97 |
strength = gr.Slider(0.6, 0.9, 0.75)
|
| 98 |
+
img_out = gr.Image(label="Ergebnis")
|
| 99 |
+
img_btn = gr.Button("Generieren")
|
| 100 |
+
img_btn.click(img_to_image, [img_in, prompt, neg, strength], img_out)
|
| 101 |
|
| 102 |
app.launch(share=True)
|