File size: 1,300 Bytes
9a91ecf
 
 
893010c
 
 
 
 
 
 
 
 
 
211e0f2
9a91ecf
 
 
 
893010c
9a91ecf
 
 
 
 
893010c
9a91ecf
893010c
9a91ecf
 
 
893010c
 
9a91ecf
 
 
 
 
893010c
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
import gradio as gr
from PIL import Image
import torch, gc
from diffusers import StableDiffusionInpaintPipeline

def inpaint_with_mask(image: Image.Image, mask: Image.Image, prompt: str = "a scenic landscape") -> Image.Image:
    image = image.resize((512, 512))
    mask = mask.resize((512, 512)).convert("L")

    # ๐Ÿง  Load Inpainting Pipeline (SD 1.5-based, CPU compatible)
    pipe = StableDiffusionInpaintPipeline.from_pretrained(
        "runwayml/stable-diffusion-inpainting",
        torch_dtype=torch.float32
    ).to("cpu")

    # ๐Ÿ–Œ๏ธ Inpaint
    result = pipe(prompt=prompt, image=image, mask_image=mask).images[0]

    # ๐Ÿงน Unload model to free memory
    del pipe
    gc.collect()

    return result

# ๐ŸŽ›๏ธ Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## ๐ŸŽจ Inpaint with Stable Diffusion (CPU Safe โ€“ SD 1.5)")
    with gr.Row():
        image_input = gr.Image(label="Original Image", type="pil")
        mask_input = gr.Image(label="Mask (white = inpaint)", type="pil")
    prompt_input = gr.Textbox(label="Prompt", value="a scenic landscape")
    output = gr.Image(label="Inpainted Output")

    run_btn = gr.Button("Inpaint")

    run_btn.click(fn=inpaint_with_mask, inputs=[image_input, mask_input, prompt_input], outputs=output)

demo.launch(show_error=True)