Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,16 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import numpy as np
|
4 |
-
from diffusers import
|
5 |
from PIL import Image, ImageDraw
|
6 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
7 |
import spaces
|
8 |
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
-
# Load
|
12 |
-
pipe =
|
13 |
-
"
|
14 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
15 |
).to(device)
|
16 |
|
@@ -19,7 +19,7 @@ processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
|
19 |
detector = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50").to(device)
|
20 |
|
21 |
@spaces.GPU
|
22 |
-
def
|
23 |
if input_image is None or prompt == "":
|
24 |
return None
|
25 |
|
@@ -34,7 +34,6 @@ def detect_and_remove(input_image, prompt):
|
|
34 |
mask = Image.new("L", input_image.size, 0)
|
35 |
draw = ImageDraw.Draw(mask)
|
36 |
|
37 |
-
# Draw boxes for "person" class only
|
38 |
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
39 |
if detector.config.id2label[label.item()] == "person":
|
40 |
box = [int(i) for i in box.tolist()]
|
@@ -43,26 +42,28 @@ def detect_and_remove(input_image, prompt):
|
|
43 |
if np.array(mask).sum() == 0:
|
44 |
return "No human detected."
|
45 |
|
46 |
-
# Inpainting
|
47 |
output = pipe(
|
48 |
prompt=prompt,
|
|
|
49 |
image=input_image,
|
50 |
-
mask_image=mask
|
|
|
|
|
51 |
).images[0]
|
52 |
|
53 |
return output
|
54 |
|
55 |
# Gradio UI
|
56 |
with gr.Blocks() as demo:
|
57 |
-
gr.Markdown("##
|
58 |
|
59 |
with gr.Row():
|
60 |
input_image = gr.Image(type="pil", label="Input Image")
|
61 |
output_image = gr.Image(type="pil", label="Output Image")
|
62 |
|
63 |
-
prompt_text = gr.Textbox(label="Prompt", placeholder="Example: Replace humans with
|
64 |
submit = gr.Button("Submit")
|
65 |
|
66 |
-
submit.click(
|
67 |
|
68 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import numpy as np
|
4 |
+
from diffusers import StableDiffusionXLInpaintPipeline
|
5 |
from PIL import Image, ImageDraw
|
6 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
7 |
import spaces
|
8 |
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
+
# Load RealVisXL Inpainting model
|
12 |
+
pipe = StableDiffusionXLInpaintPipeline.from_pretrained(
|
13 |
+
"SG161222/RealVisXL_V4.0_Inpainting", # ✅ Use inpainting-specific model
|
14 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
15 |
).to(device)
|
16 |
|
|
|
19 |
detector = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50").to(device)
|
20 |
|
21 |
@spaces.GPU
|
22 |
+
def detect_and_replace(input_image, prompt):
|
23 |
if input_image is None or prompt == "":
|
24 |
return None
|
25 |
|
|
|
34 |
mask = Image.new("L", input_image.size, 0)
|
35 |
draw = ImageDraw.Draw(mask)
|
36 |
|
|
|
37 |
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
38 |
if detector.config.id2label[label.item()] == "person":
|
39 |
box = [int(i) for i in box.tolist()]
|
|
|
42 |
if np.array(mask).sum() == 0:
|
43 |
return "No human detected."
|
44 |
|
|
|
45 |
output = pipe(
|
46 |
prompt=prompt,
|
47 |
+
negative_prompt="low quality, blurry, extra limbs, bad anatomy, ugly, deformed, poorly drawn",
|
48 |
image=input_image,
|
49 |
+
mask_image=mask,
|
50 |
+
guidance_scale=7.5,
|
51 |
+
num_inference_steps=30
|
52 |
).images[0]
|
53 |
|
54 |
return output
|
55 |
|
56 |
# Gradio UI
|
57 |
with gr.Blocks() as demo:
|
58 |
+
gr.Markdown("## Realistic Human Replacement with SDXL")
|
59 |
|
60 |
with gr.Row():
|
61 |
input_image = gr.Image(type="pil", label="Input Image")
|
62 |
output_image = gr.Image(type="pil", label="Output Image")
|
63 |
|
64 |
+
prompt_text = gr.Textbox(label="Prompt", placeholder="Example: Replace humans with fantasy characters")
|
65 |
submit = gr.Button("Submit")
|
66 |
|
67 |
+
submit.click(detect_and_replace, inputs=[input_image, prompt_text], outputs=output_image)
|
68 |
|
69 |
+
demo.launch()
|