nick_93 commited on
Commit
882873e
1 Parent(s): ee0bcfb
Files changed (1) hide show
  1. app.py +23 -9
app.py CHANGED
@@ -12,6 +12,7 @@ from colors import ade_palette
12
  from utils import map_colors_rgb
13
  from diffusers import StableDiffusionXLPipeline
14
  import gradio as gr
 
15
 
16
  device = "cpu"#"cuda"
17
  dtype = torch.float32#torch.float16
@@ -167,6 +168,11 @@ def resize_dimensions(dimensions, target_size):
167
  return (int(target_size * aspect_ratio), target_size)
168
 
169
 
 
 
 
 
 
170
  class ControlNetDepthDesignModelMulti:
171
  """ Produces random noise images """
172
  def __init__(self):
@@ -204,7 +210,7 @@ class ControlNetDepthDesignModelMulti:
204
  self.depth_feature_extractor, self.depth_estimator = get_depth_pipeline()
205
  self.depth_estimator = self.depth_estimator.to(device)
206
 
207
- def generate_design(self, empty_room_image: Image, prompt: str, guidance_scale: int = 10, num_steps: int = 50, strength: float =0.9) -> Image:
208
  """
209
  Given an image of an empty room and a prompt
210
  generate the designed room according to the prompt
@@ -216,12 +222,13 @@ class ControlNetDepthDesignModelMulti:
216
  If the size is not the same the submission will fail.
217
  """
218
  print(prompt)
 
219
  self.generator = torch.Generator(device=device).manual_seed(self.seed)
220
 
221
  pos_prompt = prompt + f', {self.additional_quality_suffix}'
222
 
223
  orig_w, orig_h = empty_room_image.size
224
- new_width, new_height = resize_dimensions(empty_room_image.size, 768)
225
  input_image = empty_room_image.resize((new_width, new_height))
226
  real_seg = np.array(segment_image(input_image,
227
  self.seg_image_processor,
@@ -247,6 +254,7 @@ class ControlNetDepthDesignModelMulti:
247
  image_depth = get_depth_image(image, self.depth_feature_extractor, self.depth_estimator)
248
 
249
  # generate image that would be used as IP-adapter
 
250
  new_width_ip = int(new_width / 8) * 8
251
  new_height_ip = int(new_height / 8) * 8
252
  ip_image = self.guide_pipe(pos_prompt,
@@ -256,7 +264,7 @@ class ControlNetDepthDesignModelMulti:
256
  width=new_width_ip,
257
  generator=[self.generator]).images[0]
258
 
259
-
260
  generated_image = self.pipe(
261
  prompt=pos_prompt,
262
  negative_prompt=self.neg_prompt,
@@ -270,7 +278,8 @@ class ControlNetDepthDesignModelMulti:
270
  control_image=[image_depth, segmentation_cond_image],
271
  controlnet_conditioning_scale=[0.5, 0.5]
272
  ).images[0]
273
-
 
274
  design_image = generated_image.resize(
275
  (orig_w, orig_h), Image.Resampling.LANCZOS
276
  )
@@ -278,7 +287,7 @@ class ControlNetDepthDesignModelMulti:
278
  return design_image
279
 
280
 
281
- def create_refseg_demo(model):
282
  gr.Markdown("### Stable Design demo")
283
  with gr.Row():
284
  with gr.Column():
@@ -290,6 +299,11 @@ def create_refseg_demo(model):
290
  maximum=50,
291
  value=50,
292
  step=1)
 
 
 
 
 
293
  guidance_scale = gr.Slider(label='Guidance Scale',
294
  minimum=0.1,
295
  maximum=30.0,
@@ -318,17 +332,17 @@ def create_refseg_demo(model):
318
  design_image = gr.Image(label="Output Mask", elem_id='img-display-output')
319
 
320
 
321
- def on_submit(image, text, num_steps, guidance_scale, seed, strength, a_prompt, n_prompt):
322
  model.seed = seed
323
  model.neg_prompt = n_prompt
324
  model.additional_quality_suffix = a_prompt
325
 
326
  with torch.no_grad():
327
- out_img = model.generate_design(image, text, guidance_scale=guidance_scale, num_steps=num_steps, strength=strength)
328
 
329
  return out_img
330
 
331
- submit.click(on_submit, inputs=[input_image, input_text, num_steps, guidance_scale, seed, strength, a_prompt, n_prompt], outputs=design_image)
332
  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"]],
333
  inputs=[input_image, input_text])
334
 
@@ -346,7 +360,7 @@ def main():
346
  gr.Markdown(title)
347
  gr.Markdown(description)
348
 
349
- create_refseg_demo(model)
350
  gr.HTML('''<br><br><br><center>You can duplicate this Space to skip the queue:<a href="https://huggingface.co/spaces/MykolaL/StableDesign?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a><br>
351
  <p><img src="https://visitor-badge.glitch.me/badge?page_id=MykolaL/StableDesign" alt="visitors"></p></center>''')
352
 
 
12
  from utils import map_colors_rgb
13
  from diffusers import StableDiffusionXLPipeline
14
  import gradio as gr
15
+ import gc
16
 
17
  device = "cpu"#"cuda"
18
  dtype = torch.float32#torch.float16
 
168
  return (int(target_size * aspect_ratio), target_size)
169
 
170
 
171
+ def flush():
172
+ gc.collect()
173
+ torch.cuda.empty_cache()
174
+
175
+
176
  class ControlNetDepthDesignModelMulti:
177
  """ Produces random noise images """
178
  def __init__(self):
 
210
  self.depth_feature_extractor, self.depth_estimator = get_depth_pipeline()
211
  self.depth_estimator = self.depth_estimator.to(device)
212
 
213
+ 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:
214
  """
215
  Given an image of an empty room and a prompt
216
  generate the designed room according to the prompt
 
222
  If the size is not the same the submission will fail.
223
  """
224
  print(prompt)
225
+ flush()
226
  self.generator = torch.Generator(device=device).manual_seed(self.seed)
227
 
228
  pos_prompt = prompt + f', {self.additional_quality_suffix}'
229
 
230
  orig_w, orig_h = empty_room_image.size
231
+ new_width, new_height = resize_dimensions(empty_room_image.size, img_size)
232
  input_image = empty_room_image.resize((new_width, new_height))
233
  real_seg = np.array(segment_image(input_image,
234
  self.seg_image_processor,
 
254
  image_depth = get_depth_image(image, self.depth_feature_extractor, self.depth_estimator)
255
 
256
  # generate image that would be used as IP-adapter
257
+ flush()
258
  new_width_ip = int(new_width / 8) * 8
259
  new_height_ip = int(new_height / 8) * 8
260
  ip_image = self.guide_pipe(pos_prompt,
 
264
  width=new_width_ip,
265
  generator=[self.generator]).images[0]
266
 
267
+ flush()
268
  generated_image = self.pipe(
269
  prompt=pos_prompt,
270
  negative_prompt=self.neg_prompt,
 
278
  control_image=[image_depth, segmentation_cond_image],
279
  controlnet_conditioning_scale=[0.5, 0.5]
280
  ).images[0]
281
+
282
+ flush()
283
  design_image = generated_image.resize(
284
  (orig_w, orig_h), Image.Resampling.LANCZOS
285
  )
 
287
  return design_image
288
 
289
 
290
+ def create_demo(model):
291
  gr.Markdown("### Stable Design demo")
292
  with gr.Row():
293
  with gr.Column():
 
299
  maximum=50,
300
  value=50,
301
  step=1)
302
+ img_size = gr.Slider(label='Image size',
303
+ minimum=256,
304
+ maximum=768,
305
+ value=768,
306
+ step=64)
307
  guidance_scale = gr.Slider(label='Guidance Scale',
308
  minimum=0.1,
309
  maximum=30.0,
 
332
  design_image = gr.Image(label="Output Mask", elem_id='img-display-output')
333
 
334
 
335
+ def on_submit(image, text, num_steps, guidance_scale, seed, strength, a_prompt, n_prompt, img_size):
336
  model.seed = seed
337
  model.neg_prompt = n_prompt
338
  model.additional_quality_suffix = a_prompt
339
 
340
  with torch.no_grad():
341
+ out_img = model.generate_design(image, text, guidance_scale=guidance_scale, num_steps=num_steps, strength=strength, img_size=img_size)
342
 
343
  return out_img
344
 
345
+ submit.click(on_submit, inputs=[input_image, input_text, num_steps, guidance_scale, seed, strength, a_prompt, n_prompt, img_size], outputs=design_image)
346
  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"]],
347
  inputs=[input_image, input_text])
348
 
 
360
  gr.Markdown(title)
361
  gr.Markdown(description)
362
 
363
+ create_demo(model)
364
  gr.HTML('''<br><br><br><center>You can duplicate this Space to skip the queue:<a href="https://huggingface.co/spaces/MykolaL/StableDesign?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a><br>
365
  <p><img src="https://visitor-badge.glitch.me/badge?page_id=MykolaL/StableDesign" alt="visitors"></p></center>''')
366