LPX55 commited on
Commit
6520f90
·
verified ·
1 Parent(s): 37ce0a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -31
app.py CHANGED
@@ -17,6 +17,7 @@ MODELS = {
17
  "Juggernaut-XL-V9-GE-RDPhoto2": "AiWise/Juggernaut-XL-V9-GE-RDPhoto2-Lightning_4S",
18
  "SatPony-Lightning": "John6666/satpony-lightning-v2-sdxl"
19
  }
 
20
  def init_pipeline(model_name):
21
  config_file = hf_hub_download(
22
  "xinsir/controlnet-union-sdxl-1.0",
@@ -50,22 +51,24 @@ def init_pipeline(model_name):
50
  # Initialize with the default model
51
  default_model_name = "RealVisXL V5.0 Lightning"
52
  pipe = init_pipeline(default_model_name)
 
53
 
54
  def update_pipeline(model_selection):
55
- global pipe
56
- if pipe.config.model_name != MODELS[model_selection]:
 
57
  pipe = init_pipeline(model_selection)
 
58
  return pipe
59
-
60
  @spaces.GPU(duration=12)
61
  def fill_image(prompt, image, model_selection, paste_back):
62
- print(f"Received image: {image}")
63
  global pipe
64
  update_pipeline(model_selection)
 
65
  if image is None:
66
  yield None, None
67
  return
68
-
69
  (
70
  prompt_embeds,
71
  negative_prompt_embeds,
@@ -78,7 +81,6 @@ def fill_image(prompt, image, model_selection, paste_back):
78
  binary_mask = alpha_channel.point(lambda p: p > 0 and 255)
79
  cnet_image = source.copy()
80
  cnet_image.paste(0, (0, 0), binary_mask)
81
-
82
  for image in pipe(
83
  prompt_embeds=prompt_embeds,
84
  negative_prompt_embeds=negative_prompt_embeds,
@@ -87,7 +89,6 @@ def fill_image(prompt, image, model_selection, paste_back):
87
  image=cnet_image,
88
  ):
89
  yield image, cnet_image
90
-
91
  print(f"{model_selection=}")
92
  print(f"{paste_back=}")
93
  if paste_back:
@@ -196,9 +197,9 @@ def preview_image_and_mask(image, width, height, overlap_percentage, resize_opti
196
  return preview
197
 
198
  @spaces.GPU(duration=12)
199
- def inpaint(prompt, image, inpaint_model, paste_back):
200
  global pipe
201
- update_pipeline(inpaint_model)
202
  mask = Image.fromarray(image["mask"]).convert("L")
203
  image = Image.fromarray(image["image"])
204
  inpaint_final_prompt = f"score_9, score_8_up, score_7_up, {prompt}"
@@ -206,7 +207,7 @@ def inpaint(prompt, image, inpaint_model, paste_back):
206
  if paste_back:
207
  result.paste(image, (0, 0), Image.fromarray(255 - np.array(mask)))
208
  return result
209
-
210
  @spaces.GPU(duration=12)
211
  def outpaint(image, width, height, overlap_percentage, num_inference_steps, resize_option, custom_resize_percentage, prompt_input, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
212
  background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)
@@ -237,7 +238,7 @@ def outpaint(image, width, height, overlap_percentage, num_inference_steps, resi
237
  @spaces.GPU(duration=12)
238
  def infer(image, width, height, overlap_percentage, num_inference_steps, resize_option, custom_resize_percentage, prompt_input, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
239
  global pipe
240
- update_pipeline(model_selection) # Added this line
241
  background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)
242
  if not can_expand(background.width, background.height, width, height, alignment):
243
  alignment = "Middle"
@@ -348,9 +349,11 @@ with gr.Blocks(css=css, fill_height=True) as demo:
348
  with gr.Column():
349
  model_selection = gr.Dropdown(
350
  choices=list(MODELS.keys()),
351
- value="RealVisXL V5.0 Lightning",
352
  label="Model",
353
  )
 
 
354
  with gr.Row():
355
  run_button = gr.Button("Generate")
356
  paste_back = gr.Checkbox(True, label="Paste back original")
@@ -362,8 +365,8 @@ with gr.Blocks(css=css, fill_height=True) as demo:
362
  interactive=False,
363
  label="Generated Image",
364
  )
 
365
  use_as_input_button = gr.Button("Use as Input Image", visible=False)
366
- loading_message = gr.Label(label="Status", value="", visible=False) # Added loading message label
367
 
368
  use_as_input_button.click(
369
  fn=use_output_as_input, inputs=[result], outputs=[input_image]
@@ -379,7 +382,7 @@ with gr.Blocks(css=css, fill_height=True) as demo:
379
  ).then(
380
  fn=lambda: gr.update(value="Loading Model...", visible=True), # Show loading message
381
  inputs=None,
382
- outputs=[loading_message, use_as_input_button]
383
  ).then(
384
  fn=fill_image,
385
  inputs=[prompt, input_image, model_selection, paste_back],
@@ -387,12 +390,12 @@ with gr.Blocks(css=css, fill_height=True) as demo:
387
  ).then(
388
  fn=lambda: gr.update(value="Model Loaded", visible=True), # Show loaded message
389
  inputs=None,
390
- outputs=[loading_message],
391
  queue=False
392
  ).then(
393
  fn=lambda: gr.update(value="", visible=False), # Hide loading message
394
  inputs=None,
395
- outputs=[loading_message],
396
  queue=False
397
  ).then(
398
  fn=lambda: gr.update(visible=True),
@@ -410,7 +413,7 @@ with gr.Blocks(css=css, fill_height=True) as demo:
410
  ).then(
411
  fn=lambda: gr.update(value="Loading Model...", visible=True), # Show loading message
412
  inputs=None,
413
- outputs=[loading_message, use_as_input_button]
414
  ).then(
415
  fn=fill_image,
416
  inputs=[prompt, input_image, model_selection, paste_back],
@@ -418,12 +421,12 @@ with gr.Blocks(css=css, fill_height=True) as demo:
418
  ).then(
419
  fn=lambda: gr.update(value="Model Loaded", visible=True), # Show loaded message
420
  inputs=None,
421
- outputs=[loading_message],
422
  queue=False
423
  ).then(
424
  fn=lambda: gr.update(value="", visible=False), # Hide loading message
425
  inputs=None,
426
- outputs=[loading_message],
427
  queue=False
428
  ).then(
429
  fn=lambda: gr.update(visible=True),
@@ -477,7 +480,7 @@ with gr.Blocks(css=css, fill_height=True) as demo:
477
  overlap_percentage = gr.Slider(
478
  label="Mask overlap (%)",
479
  minimum=1,
480
- maximum=50,
481
  value=10,
482
  step=1
483
  )
@@ -517,10 +520,12 @@ with gr.Blocks(css=css, fill_height=True) as demo:
517
  interactive=False,
518
  label="Generated Image",
519
  )
520
- use_as_input_button_outpaint = gr.Button("Use as Input Image", visible=False)
521
- history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", interactive=False)
522
- preview_image = gr.Image(label="Preview")
523
  loading_message_outpaint = gr.Label(label="Status", value="", visible=False) # Added loading message label
 
 
 
 
 
524
 
525
 
526
  target_ratio.change(
@@ -556,6 +561,7 @@ with gr.Blocks(css=css, fill_height=True) as demo:
556
  inputs=[result_outpaint],
557
  outputs=[input_image_outpaint]
558
  )
 
559
  runout_button.click(
560
  fn=clear_result,
561
  inputs=None,
@@ -563,7 +569,7 @@ with gr.Blocks(css=css, fill_height=True) as demo:
563
  ).then(
564
  fn=lambda: gr.update(value="Loading Model...", visible=True), # Show loading message
565
  inputs=None,
566
- outputs=[loading_message_outpaint, use_as_input_button_outpaint]
567
  ).then(
568
  fn=infer,
569
  inputs=[input_image_outpaint, width_slider, height_slider, overlap_percentage, num_inference_steps,
@@ -573,7 +579,7 @@ with gr.Blocks(css=css, fill_height=True) as demo:
573
  ).then(
574
  fn=lambda: gr.update(value="Model Loaded", visible=True), # Show loaded message
575
  inputs=None,
576
- outputs=[loading_message_outpaint],
577
  queue=False
578
  ).then(
579
  fn=lambda x, history: update_history(x[1], history),
@@ -582,21 +588,25 @@ with gr.Blocks(css=css, fill_height=True) as demo:
582
  ).then(
583
  fn=lambda: gr.update(value="", visible=False), # Hide loading message
584
  inputs=None,
585
- outputs=[loading_message_outpaint],
586
  queue=False
587
  ).then(
588
  fn=lambda: gr.update(visible=True),
589
  inputs=None,
590
- outputs=[use_as_input_button_outpaint],
591
  )
592
  prompt_input.submit(
593
  fn=clear_result,
594
  inputs=None,
595
  outputs=result_outpaint,
 
 
 
 
596
  ).then(
597
  fn=lambda: gr.update(value="Loading Model...", visible=True), # Show loading message
598
  inputs=None,
599
- outputs=[loading_message_outpaint, use_as_input_button_outpaint]
600
  ).then(
601
  fn=infer,
602
  inputs=[input_image_outpaint, width_slider, height_slider, overlap_percentage, num_inference_steps,
@@ -606,7 +616,7 @@ with gr.Blocks(css=css, fill_height=True) as demo:
606
  ).then(
607
  fn=lambda: gr.update(value="Model Loaded", visible=True), # Show loaded message
608
  inputs=None,
609
- outputs=[loading_message_outpaint],
610
  queue=False
611
  ).then(
612
  fn=lambda x, history: update_history(x[1], history),
@@ -615,12 +625,12 @@ with gr.Blocks(css=css, fill_height=True) as demo:
615
  ).then(
616
  fn=lambda: gr.update(value="", visible=False), # Hide loading message
617
  inputs=None,
618
- outputs=[loading_message_outpaint],
619
  queue=False
620
  ).then(
621
  fn=lambda: gr.update(visible=True),
622
  inputs=None,
623
- outputs=[use_as_input_button_outpaint],
624
  )
625
  preview_button.click(
626
  fn=preview_image_and_mask,
 
17
  "Juggernaut-XL-V9-GE-RDPhoto2": "AiWise/Juggernaut-XL-V9-GE-RDPhoto2-Lightning_4S",
18
  "SatPony-Lightning": "John6666/satpony-lightning-v2-sdxl"
19
  }
20
+
21
  def init_pipeline(model_name):
22
  config_file = hf_hub_download(
23
  "xinsir/controlnet-union-sdxl-1.0",
 
51
  # Initialize with the default model
52
  default_model_name = "RealVisXL V5.0 Lightning"
53
  pipe = init_pipeline(default_model_name)
54
+ loaded_model_name = default_model_name # Track the loaded model
55
 
56
  def update_pipeline(model_selection):
57
+ global pipe, loaded_model_name
58
+ if model_selection != loaded_model_name:
59
+ print(f"Loading new model: {model_selection}")
60
  pipe = init_pipeline(model_selection)
61
+ loaded_model_name = model_selection
62
  return pipe
63
+
64
  @spaces.GPU(duration=12)
65
  def fill_image(prompt, image, model_selection, paste_back):
 
66
  global pipe
67
  update_pipeline(model_selection)
68
+ print(f"Received image: {image}")
69
  if image is None:
70
  yield None, None
71
  return
 
72
  (
73
  prompt_embeds,
74
  negative_prompt_embeds,
 
81
  binary_mask = alpha_channel.point(lambda p: p > 0 and 255)
82
  cnet_image = source.copy()
83
  cnet_image.paste(0, (0, 0), binary_mask)
 
84
  for image in pipe(
85
  prompt_embeds=prompt_embeds,
86
  negative_prompt_embeds=negative_prompt_embeds,
 
89
  image=cnet_image,
90
  ):
91
  yield image, cnet_image
 
92
  print(f"{model_selection=}")
93
  print(f"{paste_back=}")
94
  if paste_back:
 
197
  return preview
198
 
199
  @spaces.GPU(duration=12)
200
+ def inpaint(prompt, image, model_selection, paste_back):
201
  global pipe
202
+ update_pipeline(model_selection)
203
  mask = Image.fromarray(image["mask"]).convert("L")
204
  image = Image.fromarray(image["image"])
205
  inpaint_final_prompt = f"score_9, score_8_up, score_7_up, {prompt}"
 
207
  if paste_back:
208
  result.paste(image, (0, 0), Image.fromarray(255 - np.array(mask)))
209
  return result
210
+
211
  @spaces.GPU(duration=12)
212
  def outpaint(image, width, height, overlap_percentage, num_inference_steps, resize_option, custom_resize_percentage, prompt_input, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
213
  background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)
 
238
  @spaces.GPU(duration=12)
239
  def infer(image, width, height, overlap_percentage, num_inference_steps, resize_option, custom_resize_percentage, prompt_input, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
240
  global pipe
241
+ update_pipeline(model_selection) # Ensure model_selection is defined
242
  background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)
243
  if not can_expand(background.width, background.height, width, height, alignment):
244
  alignment = "Middle"
 
349
  with gr.Column():
350
  model_selection = gr.Dropdown(
351
  choices=list(MODELS.keys()),
352
+ value=default_model_name,
353
  label="Model",
354
  )
355
+ loading_message = gr.Label(label="Status", value="", visible=False) # Added loading message label
356
+
357
  with gr.Row():
358
  run_button = gr.Button("Generate")
359
  paste_back = gr.Checkbox(True, label="Paste back original")
 
365
  interactive=False,
366
  label="Generated Image",
367
  )
368
+
369
  use_as_input_button = gr.Button("Use as Input Image", visible=False)
 
370
 
371
  use_as_input_button.click(
372
  fn=use_output_as_input, inputs=[result], outputs=[input_image]
 
382
  ).then(
383
  fn=lambda: gr.update(value="Loading Model...", visible=True), # Show loading message
384
  inputs=None,
385
+ outputs=loading_message
386
  ).then(
387
  fn=fill_image,
388
  inputs=[prompt, input_image, model_selection, paste_back],
 
390
  ).then(
391
  fn=lambda: gr.update(value="Model Loaded", visible=True), # Show loaded message
392
  inputs=None,
393
+ outputs=loading_message,
394
  queue=False
395
  ).then(
396
  fn=lambda: gr.update(value="", visible=False), # Hide loading message
397
  inputs=None,
398
+ outputs=loading_message,
399
  queue=False
400
  ).then(
401
  fn=lambda: gr.update(visible=True),
 
413
  ).then(
414
  fn=lambda: gr.update(value="Loading Model...", visible=True), # Show loading message
415
  inputs=None,
416
+ outputs=loading_message
417
  ).then(
418
  fn=fill_image,
419
  inputs=[prompt, input_image, model_selection, paste_back],
 
421
  ).then(
422
  fn=lambda: gr.update(value="Model Loaded", visible=True), # Show loaded message
423
  inputs=None,
424
+ outputs=loading_message,
425
  queue=False
426
  ).then(
427
  fn=lambda: gr.update(value="", visible=False), # Hide loading message
428
  inputs=None,
429
+ outputs=loading_message,
430
  queue=False
431
  ).then(
432
  fn=lambda: gr.update(visible=True),
 
480
  overlap_percentage = gr.Slider(
481
  label="Mask overlap (%)",
482
  minimum=1,
483
+ maximum=100,
484
  value=10,
485
  step=1
486
  )
 
520
  interactive=False,
521
  label="Generated Image",
522
  )
 
 
 
523
  loading_message_outpaint = gr.Label(label="Status", value="", visible=False) # Added loading message label
524
+ use_as_input_button_outpaint = gr.Button("Use as Input Image", visible=False)
525
+ with gr.Accordion():
526
+ history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", interactive=False)
527
+ preview_image = gr.Image(label="Preview")
528
+
529
 
530
 
531
  target_ratio.change(
 
561
  inputs=[result_outpaint],
562
  outputs=[input_image_outpaint]
563
  )
564
+
565
  runout_button.click(
566
  fn=clear_result,
567
  inputs=None,
 
569
  ).then(
570
  fn=lambda: gr.update(value="Loading Model...", visible=True), # Show loading message
571
  inputs=None,
572
+ outputs=loading_message_outpaint
573
  ).then(
574
  fn=infer,
575
  inputs=[input_image_outpaint, width_slider, height_slider, overlap_percentage, num_inference_steps,
 
579
  ).then(
580
  fn=lambda: gr.update(value="Model Loaded", visible=True), # Show loaded message
581
  inputs=None,
582
+ outputs=loading_message_outpaint,
583
  queue=False
584
  ).then(
585
  fn=lambda x, history: update_history(x[1], history),
 
588
  ).then(
589
  fn=lambda: gr.update(value="", visible=False), # Hide loading message
590
  inputs=None,
591
+ outputs=loading_message_outpaint,
592
  queue=False
593
  ).then(
594
  fn=lambda: gr.update(visible=True),
595
  inputs=None,
596
+ outputs=use_as_input_button_outpaint,
597
  )
598
  prompt_input.submit(
599
  fn=clear_result,
600
  inputs=None,
601
  outputs=result_outpaint,
602
+ ).then(
603
+ fn=lambda: gr.update(visible=False),
604
+ inputs=None,
605
+ outputs=use_as_input_button_outpaint,
606
  ).then(
607
  fn=lambda: gr.update(value="Loading Model...", visible=True), # Show loading message
608
  inputs=None,
609
+ outputs=loading_message_outpaint
610
  ).then(
611
  fn=infer,
612
  inputs=[input_image_outpaint, width_slider, height_slider, overlap_percentage, num_inference_steps,
 
616
  ).then(
617
  fn=lambda: gr.update(value="Model Loaded", visible=True), # Show loaded message
618
  inputs=None,
619
+ outputs=loading_message_outpaint,
620
  queue=False
621
  ).then(
622
  fn=lambda x, history: update_history(x[1], history),
 
625
  ).then(
626
  fn=lambda: gr.update(value="", visible=False), # Hide loading message
627
  inputs=None,
628
+ outputs=loading_message_outpaint,
629
  queue=False
630
  ).then(
631
  fn=lambda: gr.update(visible=True),
632
  inputs=None,
633
+ outputs=use_as_input_button_outpaint,
634
  )
635
  preview_button.click(
636
  fn=preview_image_and_mask,