Update app.py
Browse files
app.py
CHANGED
@@ -18,6 +18,7 @@ import gc
|
|
18 |
device = "cuda"
|
19 |
dtype = torch.float16
|
20 |
|
|
|
21 |
css = """
|
22 |
#img-display-container {
|
23 |
max-height: 50vh;
|
@@ -30,11 +31,23 @@ css = """
|
|
30 |
}
|
31 |
"""
|
32 |
|
|
|
33 |
def filter_items(
|
34 |
colors_list: Union[List, np.ndarray],
|
35 |
items_list: Union[List, np.ndarray],
|
36 |
items_to_remove: Union[List, np.ndarray]
|
37 |
) -> Tuple[Union[List, np.ndarray], Union[List, np.ndarray]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
filtered_colors = []
|
39 |
filtered_items = []
|
40 |
for color, item in zip(colors_list, items_list):
|
@@ -43,11 +56,21 @@ def filter_items(
|
|
43 |
filtered_items.append(item)
|
44 |
return filtered_colors, filtered_items
|
45 |
|
46 |
-
def get_segmentation_pipeline(
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
return image_processor, image_segmentor
|
50 |
|
|
|
51 |
@torch.inference_mode()
|
52 |
@spaces.GPU
|
53 |
def segment_image(
|
@@ -55,6 +78,19 @@ def segment_image(
|
|
55 |
image_processor: AutoImageProcessor,
|
56 |
image_segmentor: UperNetForSemanticSegmentation
|
57 |
) -> Image:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
pixel_values = image_processor(image, return_tensors="pt").pixel_values
|
59 |
with torch.no_grad():
|
60 |
outputs = image_segmentor(pixel_values)
|
@@ -69,11 +105,15 @@ def segment_image(
|
|
69 |
seg_image = Image.fromarray(color_seg).convert('RGB')
|
70 |
return seg_image
|
71 |
|
|
|
72 |
def get_depth_pipeline():
|
73 |
-
feature_extractor = AutoImageProcessor.from_pretrained("LiheYoung/depth-anything-large-hf",
|
74 |
-
|
|
|
|
|
75 |
return feature_extractor, depth_estimator
|
76 |
|
|
|
77 |
@torch.inference_mode()
|
78 |
@spaces.GPU
|
79 |
def get_depth_image(
|
@@ -101,31 +141,53 @@ def get_depth_image(
|
|
101 |
image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))
|
102 |
return image
|
103 |
|
|
|
104 |
def resize_dimensions(dimensions, target_size):
|
|
|
|
|
|
|
|
|
105 |
width, height = dimensions
|
106 |
|
107 |
-
if
|
|
|
108 |
return dimensions
|
109 |
|
|
|
110 |
if width > height:
|
|
|
111 |
aspect_ratio = height / width
|
|
|
112 |
return (target_size, int(target_size * aspect_ratio))
|
113 |
else:
|
|
|
114 |
aspect_ratio = width / height
|
|
|
115 |
return (int(target_size * aspect_ratio), target_size)
|
116 |
|
|
|
117 |
def flush():
|
118 |
gc.collect()
|
119 |
torch.cuda.empty_cache()
|
120 |
-
|
|
|
121 |
class ControlNetDepthDesignModelMulti:
|
|
|
|
|
122 |
def __init__(self):
|
|
|
|
|
|
|
123 |
self.seed = 323*111
|
124 |
self.neg_prompt = "window, door, low resolution, banner, logo, watermark, text, deformed, blurry, out of focus, surreal, ugly, beginner"
|
125 |
-
self.
|
|
|
126 |
|
127 |
@spaces.GPU
|
128 |
-
def generate_design(self, empty_room_image: Image, prompt: str, guidance_scale: int = 10, num_steps: int = 50, strength: float =
|
|
|
|
|
|
|
129 |
print(prompt)
|
130 |
flush()
|
131 |
self.generator = torch.Generator(device=device).manual_seed(self.seed)
|
@@ -158,6 +220,7 @@ class ControlNetDepthDesignModelMulti:
|
|
158 |
|
159 |
image_depth = get_depth_image(image, depth_feature_extractor, depth_estimator)
|
160 |
|
|
|
161 |
flush()
|
162 |
new_width_ip = int(new_width / 8) * 8
|
163 |
new_height_ip = int(new_height / 8) * 8
|
@@ -190,12 +253,13 @@ class ControlNetDepthDesignModelMulti:
|
|
190 |
|
191 |
return design_image
|
192 |
|
|
|
193 |
def create_demo(model):
|
194 |
gr.Markdown("### Stable Design demo")
|
195 |
with gr.Row():
|
196 |
with gr.Column():
|
197 |
input_image = gr.Image(label="Input Image", type='pil', elem_id='img-display-input')
|
198 |
-
input_text = gr.Textbox(label='Prompt', placeholder='
|
199 |
with gr.Accordion('Advanced options', open=False):
|
200 |
num_steps = gr.Slider(label='Steps',
|
201 |
minimum=1,
|
@@ -223,50 +287,60 @@ def create_demo(model):
|
|
223 |
maximum=1.0,
|
224 |
value=0.9,
|
225 |
step=0.1)
|
226 |
-
a_prompt = gr.Textbox(
|
227 |
-
|
|
|
228 |
n_prompt = gr.Textbox(
|
229 |
label='Negative Prompt',
|
230 |
-
value="low resolution, banner, logo, watermark, deformed, blurry, out of focus, surreal, ugly, beginner")
|
231 |
submit = gr.Button("Submit")
|
232 |
|
233 |
with gr.Column():
|
234 |
design_image = gr.Image(label="Output Mask", elem_id='img-display-output')
|
235 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
236 |
submit.click(on_submit, inputs=[input_image, input_text, num_steps, guidance_scale, seed, strength, a_prompt, n_prompt, img_size], outputs=design_image)
|
|
|
|
|
237 |
|
238 |
-
def on_submit(image, text, num_steps, guidance_scale, seed, strength, a_prompt, n_prompt, img_size):
|
239 |
-
model.seed = seed
|
240 |
-
model.neg_prompt = n_prompt
|
241 |
-
model.additional_quality_suffix = a_prompt
|
242 |
-
with torch.no_grad():
|
243 |
-
out_img = model.generate_design(image, text, guidance_scale=guidance_scale, num_steps=num_steps, strength=strength, img_size=img_size)
|
244 |
-
return out_img
|
245 |
|
246 |
-
controlnet_depth
|
247 |
"controlnet_depth", torch_dtype=dtype, use_safetensors=True)
|
248 |
controlnet_seg = ControlNetModel.from_pretrained(
|
249 |
"own_controlnet", torch_dtype=dtype, use_safetensors=True)
|
250 |
|
251 |
pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
|
252 |
"SG161222/Realistic_Vision_V5.1_noVAE",
|
|
|
253 |
controlnet=[controlnet_depth, controlnet_seg],
|
254 |
safety_checker=None,
|
255 |
torch_dtype=dtype
|
256 |
)
|
257 |
|
258 |
-
pipe.load_ip_adapter("h94/IP-Adapter", subfolder="models",
|
|
|
259 |
pipe.set_ip_adapter_scale(0.4)
|
260 |
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
261 |
pipe = pipe.to(device)
|
262 |
-
|
263 |
-
|
264 |
guide_pipe = guide_pipe.to(device)
|
265 |
-
|
266 |
seg_image_processor, image_segmentor = get_segmentation_pipeline()
|
267 |
depth_feature_extractor, depth_estimator = get_depth_pipeline()
|
268 |
depth_estimator = depth_estimator.to(device)
|
269 |
|
|
|
270 |
def main():
|
271 |
model = ControlNetDepthDesignModelMulti()
|
272 |
print('Models uploaded successfully')
|
@@ -275,7 +349,7 @@ def main():
|
|
275 |
description = """
|
276 |
WELCOME
|
277 |
"""
|
278 |
-
with gr.Blocks(
|
279 |
gr.Markdown(title)
|
280 |
gr.Markdown(description)
|
281 |
|
@@ -283,6 +357,6 @@ def main():
|
|
283 |
|
284 |
demo.queue().launch(share=False)
|
285 |
|
|
|
286 |
if __name__ == '__main__':
|
287 |
-
main()
|
288 |
-
|
|
|
18 |
device = "cuda"
|
19 |
dtype = torch.float16
|
20 |
|
21 |
+
|
22 |
css = """
|
23 |
#img-display-container {
|
24 |
max-height: 50vh;
|
|
|
31 |
}
|
32 |
"""
|
33 |
|
34 |
+
|
35 |
def filter_items(
|
36 |
colors_list: Union[List, np.ndarray],
|
37 |
items_list: Union[List, np.ndarray],
|
38 |
items_to_remove: Union[List, np.ndarray]
|
39 |
) -> Tuple[Union[List, np.ndarray], Union[List, np.ndarray]]:
|
40 |
+
"""
|
41 |
+
Filters items and their corresponding colors from given lists, excluding
|
42 |
+
specified items.
|
43 |
+
Args:
|
44 |
+
colors_list: A list or numpy array of colors corresponding to items.
|
45 |
+
items_list: A list or numpy array of items.
|
46 |
+
items_to_remove: A list or numpy array of items to be removed.
|
47 |
+
Returns:
|
48 |
+
A tuple of two lists or numpy arrays: filtered colors and filtered
|
49 |
+
items.
|
50 |
+
"""
|
51 |
filtered_colors = []
|
52 |
filtered_items = []
|
53 |
for color, item in zip(colors_list, items_list):
|
|
|
56 |
filtered_items.append(item)
|
57 |
return filtered_colors, filtered_items
|
58 |
|
59 |
+
def get_segmentation_pipeline(
|
60 |
+
) -> Tuple[AutoImageProcessor, UperNetForSemanticSegmentation]:
|
61 |
+
"""Method to load the segmentation pipeline
|
62 |
+
Returns:
|
63 |
+
Tuple[AutoImageProcessor, UperNetForSemanticSegmentation]: segmentation pipeline
|
64 |
+
"""
|
65 |
+
image_processor = AutoImageProcessor.from_pretrained(
|
66 |
+
"openmmlab/upernet-convnext-small"
|
67 |
+
)
|
68 |
+
image_segmentor = UperNetForSemanticSegmentation.from_pretrained(
|
69 |
+
"openmmlab/upernet-convnext-small"
|
70 |
+
)
|
71 |
return image_processor, image_segmentor
|
72 |
|
73 |
+
|
74 |
@torch.inference_mode()
|
75 |
@spaces.GPU
|
76 |
def segment_image(
|
|
|
78 |
image_processor: AutoImageProcessor,
|
79 |
image_segmentor: UperNetForSemanticSegmentation
|
80 |
) -> Image:
|
81 |
+
"""
|
82 |
+
Segments an image using a semantic segmentation model.
|
83 |
+
Args:
|
84 |
+
image (Image): The input image to be segmented.
|
85 |
+
image_processor (AutoImageProcessor): The processor to prepare the
|
86 |
+
image for segmentation.
|
87 |
+
image_segmentor (UperNetForSemanticSegmentation): The semantic
|
88 |
+
segmentation model used to identify different segments in the image.
|
89 |
+
Returns:
|
90 |
+
Image: The segmented image with each segment colored differently based
|
91 |
+
on its identified class.
|
92 |
+
"""
|
93 |
+
# image_processor, image_segmentor = get_segmentation_pipeline()
|
94 |
pixel_values = image_processor(image, return_tensors="pt").pixel_values
|
95 |
with torch.no_grad():
|
96 |
outputs = image_segmentor(pixel_values)
|
|
|
105 |
seg_image = Image.fromarray(color_seg).convert('RGB')
|
106 |
return seg_image
|
107 |
|
108 |
+
|
109 |
def get_depth_pipeline():
|
110 |
+
feature_extractor = AutoImageProcessor.from_pretrained("LiheYoung/depth-anything-large-hf",
|
111 |
+
torch_dtype=dtype)
|
112 |
+
depth_estimator = AutoModelForDepthEstimation.from_pretrained("LiheYoung/depth-anything-large-hf",
|
113 |
+
torch_dtype=dtype)
|
114 |
return feature_extractor, depth_estimator
|
115 |
|
116 |
+
|
117 |
@torch.inference_mode()
|
118 |
@spaces.GPU
|
119 |
def get_depth_image(
|
|
|
141 |
image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))
|
142 |
return image
|
143 |
|
144 |
+
|
145 |
def resize_dimensions(dimensions, target_size):
|
146 |
+
"""
|
147 |
+
Resize PIL to target size while maintaining aspect ratio
|
148 |
+
If smaller than target size leave it as is
|
149 |
+
"""
|
150 |
width, height = dimensions
|
151 |
|
152 |
+
# Check if both dimensions are smaller than the target size
|
153 |
+
if width < target_size and height < target_size:
|
154 |
return dimensions
|
155 |
|
156 |
+
# Determine the larger side
|
157 |
if width > height:
|
158 |
+
# Calculate the aspect ratio
|
159 |
aspect_ratio = height / width
|
160 |
+
# Resize dimensions
|
161 |
return (target_size, int(target_size * aspect_ratio))
|
162 |
else:
|
163 |
+
# Calculate the aspect ratio
|
164 |
aspect_ratio = width / height
|
165 |
+
# Resize dimensions
|
166 |
return (int(target_size * aspect_ratio), target_size)
|
167 |
|
168 |
+
|
169 |
def flush():
|
170 |
gc.collect()
|
171 |
torch.cuda.empty_cache()
|
172 |
+
|
173 |
+
|
174 |
class ControlNetDepthDesignModelMulti:
|
175 |
+
""" Produces random noise images """
|
176 |
+
|
177 |
def __init__(self):
|
178 |
+
""" Initialize your model(s) here """
|
179 |
+
#os.environ['HF_HUB_OFFLINE'] = "True"
|
180 |
+
|
181 |
self.seed = 323*111
|
182 |
self.neg_prompt = "window, door, low resolution, banner, logo, watermark, text, deformed, blurry, out of focus, surreal, ugly, beginner"
|
183 |
+
self.control_items = ["windowpane;window", "door;double;door"]
|
184 |
+
self.additional_quality_suffix = "interior design, 4K, high resolution, photorealistic"
|
185 |
|
186 |
@spaces.GPU
|
187 |
+
def generate_design(self, empty_room_image: Image, prompt: str, guidance_scale: int = 10, num_steps: int = 50, strength: float =0.9, img_size: int = 640) -> Image:
|
188 |
+
"""
|
189 |
+
Given an image.
|
190 |
+
"""
|
191 |
print(prompt)
|
192 |
flush()
|
193 |
self.generator = torch.Generator(device=device).manual_seed(self.seed)
|
|
|
220 |
|
221 |
image_depth = get_depth_image(image, depth_feature_extractor, depth_estimator)
|
222 |
|
223 |
+
# generate image that would be used as IP-adapter
|
224 |
flush()
|
225 |
new_width_ip = int(new_width / 8) * 8
|
226 |
new_height_ip = int(new_height / 8) * 8
|
|
|
253 |
|
254 |
return design_image
|
255 |
|
256 |
+
|
257 |
def create_demo(model):
|
258 |
gr.Markdown("### Stable Design demo")
|
259 |
with gr.Row():
|
260 |
with gr.Column():
|
261 |
input_image = gr.Image(label="Input Image", type='pil', elem_id='img-display-input')
|
262 |
+
input_text = gr.Textbox(label='Prompt', placeholder='Please upload your image first', lines=2)
|
263 |
with gr.Accordion('Advanced options', open=False):
|
264 |
num_steps = gr.Slider(label='Steps',
|
265 |
minimum=1,
|
|
|
287 |
maximum=1.0,
|
288 |
value=0.9,
|
289 |
step=0.1)
|
290 |
+
a_prompt = gr.Textbox(
|
291 |
+
label='Added Prompt',
|
292 |
+
value="4K, high resolution, photorealistic")
|
293 |
n_prompt = gr.Textbox(
|
294 |
label='Negative Prompt',
|
295 |
+
value=" low resolution, banner, logo, watermark, deformed, blurry, out of focus, surreal, ugly, beginner")
|
296 |
submit = gr.Button("Submit")
|
297 |
|
298 |
with gr.Column():
|
299 |
design_image = gr.Image(label="Output Mask", elem_id='img-display-output')
|
300 |
|
301 |
+
|
302 |
+
def on_submit(image, text, num_steps, guidance_scale, seed, strength, a_prompt, n_prompt, img_size):
|
303 |
+
model.seed = seed
|
304 |
+
model.neg_prompt = n_prompt
|
305 |
+
model.additional_quality_suffix = a_prompt
|
306 |
+
|
307 |
+
with torch.no_grad():
|
308 |
+
out_img = model.generate_design(image, text, guidance_scale=guidance_scale, num_steps=num_steps, strength=strength, img_size=img_size)
|
309 |
+
|
310 |
+
return out_img
|
311 |
+
|
312 |
submit.click(on_submit, inputs=[input_image, input_text, num_steps, guidance_scale, seed, strength, a_prompt, n_prompt, img_size], outputs=design_image)
|
313 |
+
examples = gr.Examples(examples=[["imgs/bedroom_1.jpg", "An elegantly appointed bedroom in the Art Deco style, featuring a grand king-size bed with geometric bedding, a luxurious velvet armchair, and a mirrored nightstand that reflects the room's opulence. Art Deco-inspired artwork adds a touch of glamour"], ["imgs/bedroom_2.jpg", "A bedroom that exudes French country charm with a soft upholstered bed, walls adorned with floral wallpaper, and a vintage wooden wardrobe. A crystal chandelier casts a warm, inviting glow over the space"], ["imgs/dinning_room_1.jpg", "A cozy dining room that captures the essence of rustic charm with a solid wooden farmhouse table at its core, surrounded by an eclectic mix of mismatched chairs. An antique sideboard serves as a statement piece, and the ambiance is warmly lit by a series of quaint Edison bulbs dangling from the ceiling"], ["imgs/dinning_room_3.jpg", "A dining room that epitomizes contemporary elegance, anchored by a sleek, minimalist dining table paired with stylish modern chairs. Artistic lighting fixtures create a focal point above, while the surrounding minimalist decor ensures the space feels open, airy, and utterly modern"], ["imgs/image_1.jpg", "A glamorous master bedroom in Hollywood Regency style, boasting a plush tufted headboard, mirrored furniture reflecting elegance, luxurious fabrics in rich textures, and opulent gold accents for a touch of luxury."], ["imgs/image_2.jpg", "A vibrant living room with a tropical theme, complete with comfortable rattan furniture, large leafy plants bringing the outdoors in, bright cushions adding pops of color, and bamboo blinds for natural light control."], ["imgs/living_room_1.jpg", "A stylish living room embracing mid-century modern aesthetics, featuring a vintage teak coffee table at its center, complemented by a classic sunburst clock on the wall and a cozy shag rug underfoot, creating a warm and inviting atmosphere"]],
|
314 |
+
inputs=[input_image, input_text], cache_examples=False)
|
315 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
316 |
|
317 |
+
controlnet_depth= ControlNetModel.from_pretrained(
|
318 |
"controlnet_depth", torch_dtype=dtype, use_safetensors=True)
|
319 |
controlnet_seg = ControlNetModel.from_pretrained(
|
320 |
"own_controlnet", torch_dtype=dtype, use_safetensors=True)
|
321 |
|
322 |
pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
|
323 |
"SG161222/Realistic_Vision_V5.1_noVAE",
|
324 |
+
#"models/runwayml--stable-diffusion-inpainting",
|
325 |
controlnet=[controlnet_depth, controlnet_seg],
|
326 |
safety_checker=None,
|
327 |
torch_dtype=dtype
|
328 |
)
|
329 |
|
330 |
+
pipe.load_ip_adapter("h94/IP-Adapter", subfolder="models",
|
331 |
+
weight_name="ip-adapter_sd15.bin")
|
332 |
pipe.set_ip_adapter_scale(0.4)
|
333 |
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
334 |
pipe = pipe.to(device)
|
335 |
+
guide_pipe = StableDiffusionXLPipeline.from_pretrained("segmind/SSD-1B",
|
336 |
+
torch_dtype=dtype, use_safetensors=True, variant="fp16")
|
337 |
guide_pipe = guide_pipe.to(device)
|
338 |
+
|
339 |
seg_image_processor, image_segmentor = get_segmentation_pipeline()
|
340 |
depth_feature_extractor, depth_estimator = get_depth_pipeline()
|
341 |
depth_estimator = depth_estimator.to(device)
|
342 |
|
343 |
+
|
344 |
def main():
|
345 |
model = ControlNetDepthDesignModelMulti()
|
346 |
print('Models uploaded successfully')
|
|
|
349 |
description = """
|
350 |
WELCOME
|
351 |
"""
|
352 |
+
with gr.Blocks() as demo:
|
353 |
gr.Markdown(title)
|
354 |
gr.Markdown(description)
|
355 |
|
|
|
357 |
|
358 |
demo.queue().launch(share=False)
|
359 |
|
360 |
+
|
361 |
if __name__ == '__main__':
|
362 |
+
main()
|
|