File size: 1,969 Bytes
d3f11d2
 
 
 
 
 
 
d43c89d
 
 
d3f11d2
 
d43c89d
d3f11d2
1e37fa2
d3f11d2
0293a9d
d3f11d2
d43c89d
d3f11d2
d43c89d
 
 
d3f11d2
 
d43c89d
d3f11d2
 
 
 
 
d43c89d
d3f11d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d43c89d
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
import gradio as gr
from PIL import Image
import numpy as np
from diffusers import StableDiffusionInpaintPipeline
import torch
from diffusers.utils import load_image
from diffusers.pipelines.stable_diffusion import safety_checker
# Bypass the safety checker
def sc(self, clip_input, images):
    return images, [False for _ in images]
safety_checker.StableDiffusionSafetyChecker.forward = sc

# Initialize the inpainting pipeline
pipe = StableDiffusionInpaintPipeline.from_pretrained(
    "Sanster/anything-4.0-inpainting"
)
pipe.to('cpu')

# Inpainting function
def inpaint_image(image, mask, prompt, negative_prompt):
    n_image = pipe(prompt, image=image, mask_image=mask, guidance_scale=5,
                   height=int(8*round(image.height/8)), width=int(8*round(image.width/8)),
                   num_inference_steps=70, negative_prompt=negative_prompt).images[0]  
    return n_image

# Processing uploaded files
def process_files(image_file, mask_file, prompt, negative_prompt):
    image = Image.open(image_file)
    mask = Image.open(mask_file)
    return inpaint_image(image, mask, prompt, negative_prompt)

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## Inpainting App")

    with gr.Row():
        with gr.Column():
            image_input = gr.File(label="Input Image", type="filepath")
            mask_input = gr.File(label="Mask Image", type="filepath")
            prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
            negative_prompt_input = gr.Textbox(label="Negative Prompt", placeholder="Enter your negative prompt here...")

            submit_button = gr.Button("Inpaint")

        with gr.Column():
            output_image = gr.Image(type="pil", label="Inpainted Image")

    submit_button.click(
        fn=process_files,
        inputs=[image_input, mask_input, prompt_input, negative_prompt_input],
        outputs=output_image
    )

# Launch the interface
demo.launch(show_error=True)