Update app.py
Browse files
app.py
CHANGED
@@ -1,93 +1,87 @@
|
|
1 |
import gradio as gr
|
2 |
-
import torch
|
3 |
import numpy as np
|
4 |
-
|
5 |
from diffusers import StableDiffusionInpaintPipeline
|
6 |
-
from
|
7 |
from segment_anything import sam_model_registry, SamPredictor
|
8 |
-
import
|
9 |
-
|
|
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
16 |
|
17 |
-
# Load SAM
|
18 |
-
|
19 |
-
sam = sam_model_registry[
|
20 |
sam.to(device)
|
21 |
predictor = SamPredictor(sam)
|
22 |
|
23 |
-
# Load
|
|
|
24 |
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
25 |
-
|
26 |
-
torch_dtype=torch.float16 if device=="cuda" else torch.float32
|
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 |
-
return
|
58 |
-
|
59 |
-
def inpaint_background(image, mask, prompt="background"):
|
60 |
-
orig_size = image.size
|
61 |
-
# Resize to inpainting model's resolution
|
62 |
-
img512 = image.resize((512,512), Image.LANCZOS)
|
63 |
-
m512 = mask.resize((512,512), Image.LANCZOS)
|
64 |
-
result = pipe(prompt=prompt, image=img512, mask_image=m512).images[0]
|
65 |
-
return result.resize(orig_size, Image.LANCZOS), "Background inpainted."
|
66 |
-
@spaces.GPU
|
67 |
-
def replace_with_cartoon(image, mask, prompt="a cartoon human in place"):
|
68 |
-
orig_size = image.size
|
69 |
-
img512 = image.resize((512,512), Image.LANCZOS)
|
70 |
-
m512 = mask.resize((512,512), Image.LANCZOS)
|
71 |
-
result = pipe(prompt=prompt, image=img512, mask_image=m512).images[0]
|
72 |
-
return result.resize(orig_size, Image.LANCZOS), "Replaced with cartoon."
|
73 |
|
74 |
# Gradio UI
|
75 |
with gr.Blocks() as demo:
|
76 |
-
gr.Markdown("
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
detect_btn.click(detect_and_mask, inputs=[img, detect_prompt], outputs=[img, mask_out, status1])
|
90 |
-
bg_btn.click(inpaint_background, inputs=[img, mask_out], outputs=[out_img, status2])
|
91 |
-
cartoon_btn.click(replace_with_cartoon, inputs=[img, mask_out], outputs=[out_img, status2])
|
92 |
|
93 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import numpy as np
|
3 |
+
import torch
|
4 |
from diffusers import StableDiffusionInpaintPipeline
|
5 |
+
from PIL import Image
|
6 |
from segment_anything import sam_model_registry, SamPredictor
|
7 |
+
from huggingface_hub import hf_hub_download
|
8 |
+
|
9 |
+
# Device configuration
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
|
12 |
+
# Step 1: Download SAM model checkpoint from Hugging Face
|
13 |
+
checkpoint_path = hf_hub_download(
|
14 |
+
repo_id="Munaf1987/sam", # ✅ Your model repo
|
15 |
+
filename="sam_vit_h_4b8939.pth", # ✅ The exact filename in your repo
|
16 |
+
)
|
17 |
|
18 |
+
# Step 2: Load SAM model
|
19 |
+
model_type = "vit_h"
|
20 |
+
sam = sam_model_registry[model_type](checkpoint=checkpoint_path)
|
21 |
sam.to(device)
|
22 |
predictor = SamPredictor(sam)
|
23 |
|
24 |
+
# Step 3: Load Stable Diffusion Inpainting Pipeline
|
25 |
+
sta_diff_model = "stabilityai/stable-diffusion-2-inpainting"
|
26 |
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
27 |
+
sta_diff_model,
|
28 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
29 |
+
)
|
30 |
+
pipe = pipe.to(device)
|
31 |
+
|
32 |
+
# Global variable to store selected pixels
|
33 |
+
selected_pixels = []
|
34 |
+
|
35 |
+
|
36 |
+
def generate_mask(input_image, evt: gr.SelectData):
|
37 |
+
"""Generate mask based on user-selected points."""
|
38 |
+
selected_pixels.append((evt.index[0], evt.index[1])) # evt.index is (x, y)
|
39 |
+
|
40 |
+
predictor.set_image(np.array(input_image))
|
41 |
+
input_points = np.array(selected_pixels)
|
42 |
+
input_labels = np.ones(input_points.shape[0])
|
43 |
+
|
44 |
+
masks, _, _ = predictor.predict(
|
45 |
+
point_coords=input_points,
|
46 |
+
point_labels=input_labels,
|
47 |
+
multimask_output=False,
|
48 |
)
|
49 |
+
|
50 |
+
mask = masks[0] * 255
|
51 |
+
mask_image = Image.fromarray(mask.astype(np.uint8)).convert("L")
|
52 |
+
return mask_image
|
53 |
+
|
54 |
+
|
55 |
+
def inpaint(input_image, mask_image, prompt):
|
56 |
+
"""Run the inpainting model."""
|
57 |
+
if input_image is None or mask_image is None or prompt == "":
|
58 |
+
return None
|
59 |
+
|
60 |
+
# ✅ Resize mask but keep the input image original size
|
61 |
+
mask_image_resized = mask_image.resize(input_image.size)
|
62 |
+
|
63 |
+
output = pipe(
|
64 |
+
prompt=prompt,
|
65 |
+
image=input_image,
|
66 |
+
mask_image=mask_image_resized
|
67 |
+
).images[0]
|
68 |
+
|
69 |
+
return output
|
70 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# Gradio UI
|
73 |
with gr.Blocks() as demo:
|
74 |
+
gr.Markdown("## Stable Diffusion Inpainting with SAM Mask Selection")
|
75 |
+
|
76 |
+
with gr.Row():
|
77 |
+
input_image = gr.Image(type="pil", label="Input Image", interactive=True)
|
78 |
+
mask_display = gr.Image(type="pil", label="Generated Mask")
|
79 |
+
output_image = gr.Image(type="pil", label="Output Image")
|
80 |
+
|
81 |
+
prompt_text = gr.Textbox(label="Prompt", placeholder="Enter a prompt for inpainting")
|
82 |
+
submit = gr.Button("Submit")
|
83 |
+
|
84 |
+
input_image.select(generate_mask, inputs=input_image, outputs=mask_display)
|
85 |
+
submit.click(inpaint, inputs=[input_image, mask_display, prompt_text], outputs=output_image)
|
|
|
|
|
|
|
|
|
86 |
|
87 |
demo.launch()
|