openfree commited on
Commit
cd8768a
1 Parent(s): 8990a11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -29
app.py CHANGED
@@ -72,12 +72,10 @@ pipe_controlnet = FluxControlNetPipeline(
72
  tokenizer_2=pipe.tokenizer_2,
73
  transformer=pipe.transformer, # unet 대신 transformer 사용
74
  controlnet=controlnet,
75
- scheduler=pipe.scheduler
 
76
  ).to(device)
77
 
78
-
79
-
80
-
81
  MAX_SEED = 2**32 - 1
82
  MAX_PIXEL_BUDGET = 1024 * 1024
83
 
@@ -310,8 +308,8 @@ def remove_custom_lora(selected_indices, current_loras):
310
  @spaces.GPU(duration=75)
311
  def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, progress):
312
  print("Generating image...")
313
- pipe.to("cuda")
314
- generator = torch.Generator(device="cuda").manual_seed(seed)
315
  with calculateDuration("Generating image"):
316
  # Generate image
317
  for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
@@ -329,8 +327,8 @@ def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, progress)
329
 
330
  @spaces.GPU(duration=75)
331
  def generate_image_to_image(prompt_mash, image_input_path, image_strength, steps, cfg_scale, width, height, seed):
332
- pipe_i2i.to("cuda")
333
- generator = torch.Generator(device="cuda").manual_seed(seed)
334
  image_input = load_image(image_input_path)
335
  final_image = pipe_i2i(
336
  prompt=prompt_mash,
@@ -414,7 +412,7 @@ def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_ind
414
  # Generate image
415
  if image_input is not None:
416
  final_image = generate_image_to_image(prompt_mash, image_input, image_strength, steps, cfg_scale, width, height, seed)
417
- yield final_image, seed, gr.update(visible=False)
418
  else:
419
  image_generator = generate_image(prompt_mash, steps, seed, cfg_scale, width, height, progress)
420
  # Consume the generator to get the final image
@@ -518,8 +516,8 @@ def process_input(input_image, upscale_factor, **kwargs):
518
 
519
  return input_image.resize((w, h)), w_original, h_original, was_resized
520
 
521
- @spaces.GPU#(duration=42)
522
- def infer(
523
  seed,
524
  randomize_seed,
525
  input_image,
@@ -561,8 +559,8 @@ def infer(
561
  # resize to target desired size
562
  image = image.resize((w_original * upscale_factor, h_original * upscale_factor))
563
  image.save("output.jpg")
564
- # convert to numpy
565
- return [true_input_image, image, seed]
566
 
567
  css = '''
568
  #gen_btn{height: 100%}
@@ -592,6 +590,7 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as a
592
 
593
  loras_state = gr.State(loras)
594
  selected_indices = gr.State([])
 
595
  with gr.Tab("Generate"):
596
  with gr.Row():
597
  with gr.Column(scale=3):
@@ -660,44 +659,51 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as a
660
  randomize_seed = gr.Checkbox(True, label="Randomize seed")
661
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
662
 
 
 
 
 
 
 
 
 
 
 
 
663
  gallery.select(
664
- update_selection,
665
  inputs=[selected_indices, loras_state, width, height],
666
- outputs=[prompt, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, width, height, lora_image_1, lora_image_2])
 
667
  remove_button_1.click(
668
- remove_lora_1,
669
  inputs=[selected_indices, loras_state],
670
  outputs=[selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
671
  )
672
  remove_button_2.click(
673
- remove_lora_2,
674
  inputs=[selected_indices, loras_state],
675
  outputs=[selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
676
  )
677
  randomize_button.click(
678
- randomize_loras,
679
  inputs=[selected_indices, loras_state],
680
  outputs=[selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2, prompt]
681
  )
682
  add_custom_lora_button.click(
683
- add_custom_lora,
684
  inputs=[custom_lora, selected_indices, loras_state],
685
  outputs=[loras_state, gallery, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
686
  )
687
  remove_custom_lora_button.click(
688
- remove_custom_lora,
689
  inputs=[selected_indices, loras_state],
690
  outputs=[loras_state, gallery, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
691
  )
692
- gr.on(
693
- triggers=[generate_button.click, prompt.submit],
694
- fn=run_lora,
695
- inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_indices, lora_scale_1, lora_scale_2, randomize_seed, seed, width, height, loras_state],
696
- outputs=[result, seed, progress_bar]
697
- ).then( # Update the history gallery
698
- fn=lambda x, history: update_history(x, history),
699
  inputs=[result, history_gallery],
700
- outputs=history_gallery,
701
  )
702
 
703
  with gr.Tab("Upscale"):
@@ -737,8 +743,9 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as a
737
  with gr.Row():
738
  upscale_button = gr.Button("Upscale", variant="primary")
739
 
 
740
  upscale_button.click(
741
- infer,
742
  inputs=[
743
  seed_upscale,
744
  randomize_seed_upscale,
@@ -752,3 +759,4 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as a
752
 
753
  app.queue()
754
  app.launch()
 
 
72
  tokenizer_2=pipe.tokenizer_2,
73
  transformer=pipe.transformer, # unet 대신 transformer 사용
74
  controlnet=controlnet,
75
+ scheduler=pipe.scheduler,
76
+ torch_dtype=dtype
77
  ).to(device)
78
 
 
 
 
79
  MAX_SEED = 2**32 - 1
80
  MAX_PIXEL_BUDGET = 1024 * 1024
81
 
 
308
  @spaces.GPU(duration=75)
309
  def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, progress):
310
  print("Generating image...")
311
+ pipe.to(device)
312
+ generator = torch.Generator(device=device).manual_seed(seed)
313
  with calculateDuration("Generating image"):
314
  # Generate image
315
  for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
 
327
 
328
  @spaces.GPU(duration=75)
329
  def generate_image_to_image(prompt_mash, image_input_path, image_strength, steps, cfg_scale, width, height, seed):
330
+ pipe_i2i.to(device)
331
+ generator = torch.Generator(device=device).manual_seed(seed)
332
  image_input = load_image(image_input_path)
333
  final_image = pipe_i2i(
334
  prompt=prompt_mash,
 
412
  # Generate image
413
  if image_input is not None:
414
  final_image = generate_image_to_image(prompt_mash, image_input, image_strength, steps, cfg_scale, width, height, seed)
415
+ return final_image, seed, gr.update(visible=False)
416
  else:
417
  image_generator = generate_image(prompt_mash, steps, seed, cfg_scale, width, height, progress)
418
  # Consume the generator to get the final image
 
516
 
517
  return input_image.resize((w, h)), w_original, h_original, was_resized
518
 
519
+ @spaces.GPU(duration=75)
520
+ def infer_upscale(
521
  seed,
522
  randomize_seed,
523
  input_image,
 
559
  # resize to target desired size
560
  image = image.resize((w_original * upscale_factor, h_original * upscale_factor))
561
  image.save("output.jpg")
562
+ # convert to PIL Image
563
+ return [true_input_image, image]
564
 
565
  css = '''
566
  #gen_btn{height: 100%}
 
590
 
591
  loras_state = gr.State(loras)
592
  selected_indices = gr.State([])
593
+
594
  with gr.Tab("Generate"):
595
  with gr.Row():
596
  with gr.Column(scale=3):
 
659
  randomize_seed = gr.Checkbox(True, label="Randomize seed")
660
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
661
 
662
+ # 이벤트 핸들러 설정
663
+ generate_button.click(
664
+ fn=run_lora,
665
+ inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_indices, lora_scale_1, lora_scale_2, randomize_seed, seed, width, height, loras_state],
666
+ outputs=[result, seed, progress_bar]
667
+ )
668
+ prompt.submit(
669
+ fn=run_lora,
670
+ inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_indices, lora_scale_1, lora_scale_2, randomize_seed, seed, width, height, loras_state],
671
+ outputs=[result, seed, progress_bar]
672
+ )
673
  gallery.select(
674
+ fn=update_selection,
675
  inputs=[selected_indices, loras_state, width, height],
676
+ outputs=[prompt, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, width, height, lora_image_1, lora_image_2]
677
+ )
678
  remove_button_1.click(
679
+ fn=remove_lora_1,
680
  inputs=[selected_indices, loras_state],
681
  outputs=[selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
682
  )
683
  remove_button_2.click(
684
+ fn=remove_lora_2,
685
  inputs=[selected_indices, loras_state],
686
  outputs=[selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
687
  )
688
  randomize_button.click(
689
+ fn=randomize_loras,
690
  inputs=[selected_indices, loras_state],
691
  outputs=[selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2, prompt]
692
  )
693
  add_custom_lora_button.click(
694
+ fn=add_custom_lora,
695
  inputs=[custom_lora, selected_indices, loras_state],
696
  outputs=[loras_state, gallery, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
697
  )
698
  remove_custom_lora_button.click(
699
+ fn=remove_custom_lora,
700
  inputs=[selected_indices, loras_state],
701
  outputs=[loras_state, gallery, selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2]
702
  )
703
+ history_gallery.load(
704
+ fn=update_history,
 
 
 
 
 
705
  inputs=[result, history_gallery],
706
+ outputs=history_gallery
707
  )
708
 
709
  with gr.Tab("Upscale"):
 
743
  with gr.Row():
744
  upscale_button = gr.Button("Upscale", variant="primary")
745
 
746
+ # 업스케일 버튼 이벤트 핸들러
747
  upscale_button.click(
748
+ fn=infer_upscale,
749
  inputs=[
750
  seed_upscale,
751
  randomize_seed_upscale,
 
759
 
760
  app.queue()
761
  app.launch()
762
+