inpaint / app.py
sdafd's picture
Update app.py
1e37fa2 verified
raw
history blame
1.97 kB
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)