Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
from diffusers import StableDiffusionInpaintPipeline
|
5 |
+
import torch
|
6 |
+
from diffusers.utils import load_image
|
7 |
+
from diffusers.pipelines.stable_diffusion import safety_checker
|
8 |
+
def sc(self, clip_input, images) :
|
9 |
+
return images, [False for i in images]
|
10 |
+
safety_checker.StableDiffusionSafetyChecker.forward = sc
|
11 |
+
|
12 |
+
|
13 |
+
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
14 |
+
"runwayml/stable-diffusion-inpainting",
|
15 |
+
revision="fp16",
|
16 |
+
torch_dtype=torch.float16,
|
17 |
+
)
|
18 |
+
|
19 |
+
def inpaint_image(image, mask, prompt, negative_prompt):
|
20 |
+
# Convert PIL images to numpy arrays if needed
|
21 |
+
# Process your images as required by your inpainting model
|
22 |
+
n_image = pipe(prompt, image=image, mask_image=mask, guidance_scale=5,height=int(8*round(pil_img.height/8)), width=int(8*round(pil_img.width/8)), num_inference_steps=70,negative_prompt=negative_prompt).images[0]
|
23 |
+
return n_image
|
24 |
+
|
25 |
+
def process_files(image_file, mask_file, prompt, negative_prompt):
|
26 |
+
image = Image.open(image_file)
|
27 |
+
mask = Image.open(mask_file)
|
28 |
+
return inpaint_image(image, mask, prompt, negative_prompt)
|
29 |
+
|
30 |
+
with gr.Blocks() as demo:
|
31 |
+
gr.Markdown("## Inpainting App")
|
32 |
+
|
33 |
+
with gr.Row():
|
34 |
+
with gr.Column():
|
35 |
+
image_input = gr.File(label="Input Image", type="filepath")
|
36 |
+
mask_input = gr.File(label="Mask Image", type="filepath")
|
37 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
|
38 |
+
negative_prompt_input = gr.Textbox(label="Negative Prompt", placeholder="Enter your negative prompt here...")
|
39 |
+
|
40 |
+
submit_button = gr.Button("Inpaint")
|
41 |
+
|
42 |
+
with gr.Column():
|
43 |
+
output_image = gr.Image(type="pil", label="Inpainted Image")
|
44 |
+
|
45 |
+
submit_button.click(
|
46 |
+
fn=process_files,
|
47 |
+
inputs=[image_input, mask_input, prompt_input, negative_prompt_input],
|
48 |
+
outputs=output_image
|
49 |
+
)
|
50 |
+
|
51 |
+
# Launch the interface
|
52 |
+
demo.launch()
|