Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,19 +4,24 @@ import torch
|
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 7 |
-
text2img_pipe = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token=True)
|
| 8 |
-
img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, use_auth_token=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def generate_text2image(prompt):
|
| 11 |
-
image = text2img_pipe(prompt, height=
|
| 12 |
return image
|
| 13 |
|
| 14 |
-
def generate_img2image(image, prompt, strength=0.35):
|
| 15 |
if image is None:
|
| 16 |
raise gr.Error("Bitte lade ein Bild hoch!")
|
| 17 |
-
image = image.convert("RGB").resize((
|
| 18 |
try:
|
| 19 |
-
result = img2img_pipe(prompt=prompt, image=image, strength=strength, guidance_scale=6.0).images[0]
|
| 20 |
if result is None or all(pixel == 0 for pixel in result.tobytes()): # Prüfe auf schwarzes Bild
|
| 21 |
raise gr.Error("Generierung fehlgeschlagen – Ergebnis ist leer oder schwarz. Versuche einen höheren strength-Wert (z.B. 0.4) oder einen detaillierteren Prompt.")
|
| 22 |
return result
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
model_id = "runwayml/stable-diffusion-v1-5"
|
| 7 |
+
text2img_pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, use_auth_token=True)
|
| 8 |
+
img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.float16, use_auth_token=True)
|
| 9 |
+
|
| 10 |
+
# Automatische Geräteauswahl (GPU, falls verfügbar)
|
| 11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
text2img_pipe = text2img_pipe.to(device)
|
| 13 |
+
img2img_pipe = img2img_pipe.to(device)
|
| 14 |
|
| 15 |
def generate_text2image(prompt):
|
| 16 |
+
image = text2img_pipe(prompt, height=256, width=256).images[0] # Erhöhte Auflösung für GPU
|
| 17 |
return image
|
| 18 |
|
| 19 |
+
def generate_img2image(image, prompt, strength=0.35):
|
| 20 |
if image is None:
|
| 21 |
raise gr.Error("Bitte lade ein Bild hoch!")
|
| 22 |
+
image = image.convert("RGB").resize((256, 256)) # Erhöhte Auflösung für GPU
|
| 23 |
try:
|
| 24 |
+
result = img2img_pipe(prompt=prompt, image=image, strength=strength, guidance_scale=6.0).images[0]
|
| 25 |
if result is None or all(pixel == 0 for pixel in result.tobytes()): # Prüfe auf schwarzes Bild
|
| 26 |
raise gr.Error("Generierung fehlgeschlagen – Ergebnis ist leer oder schwarz. Versuche einen höheren strength-Wert (z.B. 0.4) oder einen detaillierteren Prompt.")
|
| 27 |
return result
|