Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import os
|
2 |
import numpy as np
|
3 |
from typing import cast
|
4 |
-
from pydantic import NonNegativeInt
|
5 |
import torch
|
6 |
from PIL import Image, ImageOps
|
7 |
from diffusers import DiffusionPipeline
|
@@ -27,6 +26,7 @@ pipeline = DiffusionPipeline.from_pretrained(
|
|
27 |
torch_dtype=torch.bfloat16,
|
28 |
custom_pipeline=SUB_MODEL_REPO_ID,
|
29 |
).to(DEVICE)
|
|
|
30 |
|
31 |
|
32 |
def crop_divisible_by_16(image: Image.Image) -> Image.Image:
|
@@ -38,11 +38,12 @@ def crop_divisible_by_16(image: Image.Image) -> Image.Image:
|
|
38 |
|
39 |
@spaces.GPU(duration=150)
|
40 |
def predict(
|
41 |
-
image_and_mask: EditorValue
|
42 |
-
|
43 |
seed: int = 0,
|
44 |
num_inference_steps: int = 28,
|
45 |
-
|
|
|
46 |
condition_scale: float = 1.0,
|
47 |
progress: gr.Progress = gr.Progress(track_tqdm=True), # noqa: ARG001, B008
|
48 |
) -> Image.Image | None:
|
@@ -50,9 +51,15 @@ def predict(
|
|
50 |
if not image_and_mask:
|
51 |
gr.Info("Please upload an image and draw a mask")
|
52 |
return None
|
53 |
-
if not
|
54 |
gr.Info("Please upload a furniture reference image")
|
55 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
image_np = image_and_mask["background"]
|
57 |
image_np = cast(np.ndarray, image_np)
|
58 |
|
@@ -75,36 +82,45 @@ def predict(
|
|
75 |
subfolder=SUB_MODEL_SUBFOLDER,
|
76 |
)
|
77 |
|
78 |
-
|
79 |
# Resize to max dimension
|
80 |
-
|
|
|
81 |
# Ensure dimensions are multiple of 16 (for VAE)
|
82 |
-
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
87 |
# Invert the mask
|
88 |
-
|
89 |
|
90 |
-
# Image masked is the image with the mask applied (black background)
|
91 |
-
image_masked = Image.new("RGB", image.size, (0, 0, 0))
|
92 |
-
image_masked.paste(image, (0, 0), mask)
|
93 |
|
94 |
-
|
95 |
-
|
|
|
96 |
|
97 |
generator = torch.Generator(device="cpu").manual_seed(seed)
|
98 |
|
99 |
final_image = pipeline(
|
100 |
-
condition_image=
|
101 |
-
reference_image=furniture_reference,
|
102 |
-
condition_scale=condition_scale,
|
103 |
prompt="",
|
|
|
|
|
104 |
num_inference_steps=num_inference_steps,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
generator=generator,
|
106 |
max_sequence_length=512,
|
107 |
-
latent_lora=True,
|
108 |
).images[0]
|
109 |
|
110 |
return final_image
|
@@ -157,7 +173,7 @@ with gr.Blocks(css=css) as demo:
|
|
157 |
brush=gr.Brush(default_size=75, colors=["#000000"], color_mode="fixed"),
|
158 |
transforms=[],
|
159 |
)
|
160 |
-
|
161 |
label="Furniture Reference",
|
162 |
type="pil",
|
163 |
sources=["upload"],
|
@@ -197,12 +213,20 @@ with gr.Blocks(css=css) as demo:
|
|
197 |
value=1.0,
|
198 |
)
|
199 |
with gr.Column():
|
200 |
-
|
201 |
-
label="
|
202 |
-
minimum=
|
203 |
-
maximum=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
step=128,
|
205 |
-
value=
|
206 |
)
|
207 |
|
208 |
num_inference_steps = gr.Slider(
|
@@ -217,10 +241,11 @@ with gr.Blocks(css=css) as demo:
|
|
217 |
fn=predict,
|
218 |
inputs=[
|
219 |
image_and_mask,
|
220 |
-
|
221 |
seed,
|
222 |
num_inference_steps,
|
223 |
-
|
|
|
224 |
condition_scale,
|
225 |
],
|
226 |
# outputs=[image_slider],
|
|
|
1 |
import os
|
2 |
import numpy as np
|
3 |
from typing import cast
|
|
|
4 |
import torch
|
5 |
from PIL import Image, ImageOps
|
6 |
from diffusers import DiffusionPipeline
|
|
|
26 |
torch_dtype=torch.bfloat16,
|
27 |
custom_pipeline=SUB_MODEL_REPO_ID,
|
28 |
).to(DEVICE)
|
29 |
+
pipeline.post_init()
|
30 |
|
31 |
|
32 |
def crop_divisible_by_16(image: Image.Image) -> Image.Image:
|
|
|
38 |
|
39 |
@spaces.GPU(duration=150)
|
40 |
def predict(
|
41 |
+
image_and_mask: EditorValue,
|
42 |
+
condition_image: Image.Image | None,
|
43 |
seed: int = 0,
|
44 |
num_inference_steps: int = 28,
|
45 |
+
condition_size: int = 512,
|
46 |
+
target_size: int = 512,
|
47 |
condition_scale: float = 1.0,
|
48 |
progress: gr.Progress = gr.Progress(track_tqdm=True), # noqa: ARG001, B008
|
49 |
) -> Image.Image | None:
|
|
|
51 |
if not image_and_mask:
|
52 |
gr.Info("Please upload an image and draw a mask")
|
53 |
return None
|
54 |
+
if not condition_image:
|
55 |
gr.Info("Please upload a furniture reference image")
|
56 |
return None
|
57 |
+
|
58 |
+
pipeline.load(
|
59 |
+
SUB_MODEL_REPO_ID,
|
60 |
+
subfolder=SUB_MODEL_SUBFOLDER,
|
61 |
+
)
|
62 |
+
|
63 |
image_np = image_and_mask["background"]
|
64 |
image_np = cast(np.ndarray, image_np)
|
65 |
|
|
|
82 |
subfolder=SUB_MODEL_SUBFOLDER,
|
83 |
)
|
84 |
|
85 |
+
target_image = Image.fromarray(image_np)
|
86 |
# Resize to max dimension
|
87 |
+
target_image.thumbnail((target_size, target_size))
|
88 |
+
target_image = target_image.resize((target_size, target_size))
|
89 |
# Ensure dimensions are multiple of 16 (for VAE)
|
90 |
+
target_image = crop_divisible_by_16(target_image)
|
91 |
|
92 |
+
mask_image = Image.fromarray(mask_np)
|
93 |
+
mask_image.thumbnail((target_size, target_size))
|
94 |
+
mask_image = mask_image.resize((target_size, target_size))
|
95 |
+
mask_image = crop_divisible_by_16(mask_image)
|
96 |
# Invert the mask
|
97 |
+
mask_image = ImageOps.invert(mask_image)
|
98 |
|
99 |
+
# # Image masked is the image with the mask applied (black background)
|
100 |
+
# image_masked = Image.new("RGB", image.size, (0, 0, 0))
|
101 |
+
# image_masked.paste(image, (0, 0), mask)
|
102 |
|
103 |
+
condition_image.thumbnail((condition_size, condition_size))
|
104 |
+
condition_image = condition_image.resize((condition_size, condition_size))
|
105 |
+
condition_image = crop_divisible_by_16(condition_image)
|
106 |
|
107 |
generator = torch.Generator(device="cpu").manual_seed(seed)
|
108 |
|
109 |
final_image = pipeline(
|
110 |
+
condition_image=condition_image,
|
|
|
|
|
111 |
prompt="",
|
112 |
+
image=target_image,
|
113 |
+
mask_image=mask_image,
|
114 |
num_inference_steps=num_inference_steps,
|
115 |
+
height=target_size,
|
116 |
+
width=target_size,
|
117 |
+
union_cond_attn=True,
|
118 |
+
add_cond_attn=False,
|
119 |
+
latent_lora=False,
|
120 |
+
default_lora=False,
|
121 |
+
condition_scale=condition_scale,
|
122 |
generator=generator,
|
123 |
max_sequence_length=512,
|
|
|
124 |
).images[0]
|
125 |
|
126 |
return final_image
|
|
|
173 |
brush=gr.Brush(default_size=75, colors=["#000000"], color_mode="fixed"),
|
174 |
transforms=[],
|
175 |
)
|
176 |
+
condition_image = gr.Image(
|
177 |
label="Furniture Reference",
|
178 |
type="pil",
|
179 |
sources=["upload"],
|
|
|
213 |
value=1.0,
|
214 |
)
|
215 |
with gr.Column():
|
216 |
+
condition_size = gr.Slider(
|
217 |
+
label="Condition Size",
|
218 |
+
minimum=256,
|
219 |
+
maximum=1024,
|
220 |
+
step=128,
|
221 |
+
value=512,
|
222 |
+
)
|
223 |
+
|
224 |
+
target_size = gr.Slider(
|
225 |
+
label="Target Size",
|
226 |
+
minimum=256,
|
227 |
+
maximum=1024,
|
228 |
step=128,
|
229 |
+
value=512,
|
230 |
)
|
231 |
|
232 |
num_inference_steps = gr.Slider(
|
|
|
241 |
fn=predict,
|
242 |
inputs=[
|
243 |
image_and_mask,
|
244 |
+
condition_image,
|
245 |
seed,
|
246 |
num_inference_steps,
|
247 |
+
condition_size,
|
248 |
+
target_size,
|
249 |
condition_scale,
|
250 |
],
|
251 |
# outputs=[image_slider],
|