Spaces:
Runtime error
Runtime error
File size: 4,009 Bytes
4c321e9 bddee0a 4c321e9 2c362fc 4c321e9 2c362fc bddee0a 2c362fc bddee0a 4c321e9 2c362fc 4c321e9 2c362fc 4c321e9 2c362fc 4c321e9 2c362fc 4c321e9 2c362fc bddee0a 2c362fc 4c321e9 2c362fc bddee0a 2c362fc 4c321e9 bddee0a 4c321e9 2c362fc 4c321e9 2c362fc 4c321e9 2c362fc 4c321e9 2c362fc 4c321e9 2c362fc 4c321e9 2c362fc 4c321e9 2c362fc 4c321e9 2c362fc 4c321e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | import gradio as gr
import torch
from diffusers import StableDiffusionInpaintPipeline
from PIL import Image
# ------------------------------------------------------------------------------
# 1) PIPELINE LADEN
# ------------------------------------------------------------------------------
MODEL_ID = "runwayml/stable-diffusion-inpainting"
print("Lade Inpainting-Pipeline... (kann einen Moment dauern)")
pipe = StableDiffusionInpaintPipeline.from_pretrained(
MODEL_ID,
torch_dtype=torch.float32
)
# CPU-Modus (gratis, aber langsam); wenn GPU verfügbar, pipe.to("cuda") für schnellere Ergebnisse
pipe.to("cpu")
# Wir reduzieren z. B. die Inference-Steps, damit es schneller geht
DEFAULT_STEPS = 20
GUIDANCE_SCALE = 7.5
IMG_SIZE = 512 # Wir verkleinern Bilder auf 512x512, um CPU-Zeit zu sparen
# ------------------------------------------------------------------------------
# 2) INPAINT-FUNKTION mit manuellem Masking
# ------------------------------------------------------------------------------
def inpaint_masked(img, mask, prompt):
"""
Nimmt ein Originalbild (img) + eine vom User gezeichnete Maske (mask).
Nur in maskierten Bereichen wird etwas verändert, z. B. Möbel eingefügt.
Der Rest bleibt unverändert.
"""
if img is None or mask is None:
return None
# 1) Auf 512x512 verkleinern (Bild & Maske gleich groß halten!)
img = img.resize((IMG_SIZE, IMG_SIZE), resample=Image.LANCZOS)
mask = mask.resize((IMG_SIZE, IMG_SIZE), resample=Image.LANCZOS)
# 2) Pipeline-Aufruf
result = pipe(
prompt=prompt,
image=img,
mask_image=mask,
num_inference_steps=DEFAULT_STEPS,
guidance_scale=GUIDANCE_SCALE
).images[0]
return result
# ------------------------------------------------------------------------------
# 3) GRADIO INTERFACE
# ------------------------------------------------------------------------------
def build_app():
with gr.Blocks() as demo:
gr.Markdown("# 🏠 Virtuelles Home Staging mit manueller Maske")
gr.Markdown(
"1. Lade dein Zimmerfoto hoch.\n"
"2. **Male in der Maske** die Bereiche aus, wo Möbel erscheinen sollen.\n"
" - Weiß = Hier darf die KI etwas ändern.\n"
" - Schwarz = Bleibt unverändert.\n"
"3. Prompt eingeben, auf **Inpaint** klicken."
)
with gr.Row():
with gr.Column():
input_image = gr.Image(
label="(1) Zimmerfoto hochladen",
type="pil"
)
mask_image = gr.Image(
label="(2) Maske hier zeichnen (weiß = veränderbar)",
tool="sketch", # erlaubt freies Zeichnen
type="pil"
)
prompt_text = gr.Textbox(
label="(3) Prompt: Was soll eingefügt werden?",
value="Add modern furniture, keep walls the same",
lines=2
)
run_button = gr.Button("Inpaint")
with gr.Column():
result_image = gr.Image(label="Ergebnis")
# Button-Event: Klick -> inpaint_masked()
run_button.click(
fn=inpaint_masked,
inputs=[input_image, mask_image, prompt_text],
outputs=[result_image]
)
gr.Markdown(
"**Tipps:**\n"
"- Standard-Inpainting erfordert weiße Stellen für Bereiche, die verändert werden dürfen.\n"
"- Du kannst z. B. nur den Fußboden weiß maskieren, damit dort Möbel entstehen.\n"
"- Für bessere Qualität könntest du `DEFAULT_STEPS` oder `IMG_SIZE` anpassen."
)
return demo
# ------------------------------------------------------------------------------
# 4) START
# ------------------------------------------------------------------------------
demo = build_app()
if __name__ == "__main__":
demo.launch()
|