openfree commited on
Commit
4694933
1 Parent(s): ee08c46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -208
app.py CHANGED
@@ -5,8 +5,7 @@ import logging
5
  import torch
6
  from PIL import Image
7
  import spaces
8
- from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL, AutoPipelineForImage2Image, FluxControlNetModel
9
- from diffusers.pipelines import FluxControlNetPipeline
10
  from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
11
  from diffusers.utils import load_image
12
  from huggingface_hub import hf_hub_download, HfFileSystem, ModelCard, snapshot_download
@@ -21,7 +20,7 @@ import numpy as np
21
  import warnings
22
 
23
 
24
- huggingface_token = os.getenv("HUGGINFACE_TOKEN")
25
 
26
 
27
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en", device="cpu")
@@ -61,23 +60,6 @@ pipe_i2i = AutoPipelineForImage2Image.from_pretrained(
61
  torch_dtype=dtype
62
  ).to(device)
63
 
64
- # Upscale을 위한 ControlNet 설정
65
- controlnet = FluxControlNetModel.from_pretrained(
66
- "jasperai/Flux.1-dev-Controlnet-Upscaler", torch_dtype=torch.bfloat16
67
- ).to(device)
68
-
69
- # Upscale 파이프라인 설정 (기존 pipe 재사용)
70
- pipe_upscale = FluxControlNetPipeline(
71
- vae=pipe.vae,
72
- text_encoder=pipe.text_encoder,
73
- text_encoder_2=pipe.text_encoder_2,
74
- tokenizer=pipe.tokenizer,
75
- tokenizer_2=pipe.tokenizer_2,
76
- transformer=pipe.transformer,
77
- scheduler=pipe.scheduler,
78
- controlnet=controlnet
79
- ).to(device)
80
-
81
  MAX_SEED = 2**32 - 1
82
  MAX_PIXEL_BUDGET = 1024 * 1024
83
 
@@ -556,109 +538,6 @@ css = '''
556
  footer {visibility: hidden;}
557
  '''
558
 
559
- # 업스케일 관련 함수 추가
560
- def process_input(input_image, upscale_factor, **kwargs):
561
- w, h = input_image.size
562
- w_original, h_original = w, h
563
- aspect_ratio = w / h
564
-
565
- was_resized = False
566
-
567
- max_size = int(np.sqrt(MAX_PIXEL_BUDGET / (upscale_factor ** 2)))
568
- if w > max_size or h > max_size:
569
- if w > h:
570
- w_new = max_size
571
- h_new = int(w_new / aspect_ratio)
572
- else:
573
- h_new = max_size
574
- w_new = int(h_new * aspect_ratio)
575
-
576
- input_image = input_image.resize((w_new, h_new), Image.LANCZOS)
577
- was_resized = True
578
- gr.Info(f"Input image resized to {w_new}x{h_new} to fit within pixel budget after upscaling.")
579
-
580
- # resize to multiple of 8
581
- w, h = input_image.size
582
- w = w - w % 8
583
- h = h - h % 8
584
-
585
- return input_image.resize((w, h)), w_original, h_original, was_resized
586
-
587
- from PIL import Image
588
- import numpy as np
589
-
590
- @spaces.GPU
591
- def infer_upscale(
592
- seed,
593
- randomize_seed,
594
- input_image,
595
- num_inference_steps,
596
- upscale_factor,
597
- controlnet_conditioning_scale,
598
- progress=gr.Progress(track_tqdm=True),
599
- ):
600
- if input_image is None:
601
- return None, seed, gr.update(), gr.update(), gr.update(), gr.update(), gr.update(visible=True, value="Please upload an image for upscaling.")
602
-
603
- try:
604
- if randomize_seed:
605
- seed = random.randint(0, MAX_SEED)
606
-
607
- input_image, w_original, h_original, was_resized = process_input(input_image, upscale_factor)
608
-
609
- # rescale with upscale factor
610
- w, h = input_image.size
611
- control_image = input_image.resize((w * upscale_factor, h * upscale_factor), Image.LANCZOS)
612
-
613
- generator = torch.Generator(device=device).manual_seed(seed)
614
-
615
- gr.Info("Upscaling image...")
616
- # 모든 텐서를 동일한 디바이스로 이동
617
- pipe_upscale.to(device)
618
-
619
- # Ensure the image is in RGB format
620
- if control_image.mode != 'RGB':
621
- control_image = control_image.convert('RGB')
622
-
623
- # Convert to tensor and add batch dimension
624
- control_image = torch.from_numpy(np.array(control_image)).permute(2, 0, 1).float().unsqueeze(0).to(device) / 255.0
625
-
626
- with torch.no_grad():
627
- image = pipe_upscale(
628
- prompt="",
629
- control_image=control_image,
630
- controlnet_conditioning_scale=controlnet_conditioning_scale,
631
- num_inference_steps=num_inference_steps,
632
- guidance_scale=3.5,
633
- generator=generator,
634
- ).images[0]
635
-
636
- # Convert the image back to PIL Image
637
- if isinstance(image, torch.Tensor):
638
- image = image.cpu().permute(1, 2, 0).numpy()
639
-
640
- # Ensure the image data is in the correct range
641
- image = np.clip(image * 255, 0, 255).astype(np.uint8)
642
- image = Image.fromarray(image)
643
-
644
- if was_resized:
645
- gr.Info(
646
- f"Resizing output image to targeted {w_original * upscale_factor}x{h_original * upscale_factor} size."
647
- )
648
- image = image.resize((w_original * upscale_factor, h_original * upscale_factor), Image.LANCZOS)
649
-
650
- return image, seed, num_inference_steps, upscale_factor, controlnet_conditioning_scale, gr.update(), gr.update(visible=False)
651
- except Exception as e:
652
- print(f"Error in infer_upscale: {str(e)}")
653
- import traceback
654
- traceback.print_exc()
655
- return None, seed, gr.update(), gr.update(), gr.update(), gr.update(), gr.update(visible=True, value=f"Error: {str(e)}")
656
-
657
- def check_upscale_input(input_image, *args):
658
- if input_image is None:
659
- return gr.update(interactive=False), *args, gr.update(visible=True, value="Please upload an image for upscaling.")
660
- return gr.update(interactive=True), *args, gr.update(visible=False)
661
-
662
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as app:
663
  loras_state = gr.State(loras)
664
  selected_indices = gr.State([])
@@ -742,49 +621,6 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as a
742
  randomize_seed = gr.Checkbox(True, label="Randomize seed")
743
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
744
 
745
- # 업스케일 관련 UI 추가
746
- with gr.Row():
747
- upscale_button = gr.Button("Upscale", interactive=False)
748
-
749
- with gr.Row():
750
- with gr.Column(scale=4):
751
- upscale_input = gr.Image(label="Input Image for Upscaling", type="pil")
752
- with gr.Column(scale=1):
753
- upscale_steps = gr.Slider(
754
- label="Number of Inference Steps for Upscaling",
755
- minimum=8,
756
- maximum=50,
757
- step=1,
758
- value=28,
759
- )
760
- upscale_factor = gr.Slider(
761
- label="Upscale Factor",
762
- minimum=1,
763
- maximum=4,
764
- step=1,
765
- value=4,
766
- )
767
- controlnet_conditioning_scale = gr.Slider(
768
- label="Controlnet Conditioning Scale",
769
- minimum=0.1,
770
- maximum=1.0,
771
- step=0.05,
772
- value=0.5, # 기본값을 0.5로 낮춤
773
- )
774
- upscale_seed = gr.Slider(
775
- label="Seed for Upscaling",
776
- minimum=0,
777
- maximum=MAX_SEED,
778
- step=1,
779
- value=42,
780
- )
781
- upscale_randomize_seed = gr.Checkbox(label="Randomize seed for Upscaling", value=True)
782
- upscale_error = gr.Markdown(visible=False, value="Please provide an input image for upscaling.")
783
-
784
- with gr.Row():
785
- upscale_result = gr.Image(label="Upscaled Image", type="pil")
786
- upscale_seed_output = gr.Number(label="Seed Used", precision=0)
787
-
788
  gallery.select(
789
  update_selection,
790
  inputs=[selected_indices, loras_state, width, height],
@@ -809,8 +645,6 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as a
809
  outputs=[selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3]
810
  )
811
 
812
-
813
-
814
  randomize_button.click(
815
  randomize_loras,
816
  inputs=[selected_indices, loras_state],
@@ -840,46 +674,6 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as a
840
  outputs=history_gallery,
841
  )
842
 
843
- upscale_input.upload(
844
- lambda x: gr.update(interactive=x is not None),
845
- inputs=[upscale_input],
846
- outputs=[upscale_button]
847
- )
848
-
849
- upscale_error = gr.Markdown(visible=False, value="")
850
-
851
- upscale_button.click(
852
- infer_upscale,
853
- inputs=[
854
- upscale_seed,
855
- upscale_randomize_seed,
856
- upscale_input,
857
- upscale_steps,
858
- upscale_factor,
859
- controlnet_conditioning_scale,
860
- ],
861
- outputs=[
862
- upscale_result,
863
- upscale_seed_output,
864
- upscale_steps,
865
- upscale_factor,
866
- controlnet_conditioning_scale,
867
- upscale_randomize_seed,
868
- upscale_error
869
- ],
870
- ).then(
871
- infer_upscale,
872
- inputs=[
873
- upscale_seed,
874
- upscale_randomize_seed,
875
- upscale_input,
876
- upscale_steps,
877
- upscale_factor,
878
- controlnet_conditioning_scale,
879
- ],
880
- outputs=[upscale_result, upscale_seed_output]
881
- )
882
-
883
  if __name__ == "__main__":
884
  app.queue(max_size=20)
885
  app.launch(debug=True)
 
5
  import torch
6
  from PIL import Image
7
  import spaces
8
+ from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL, AutoPipelineForImage2Image
 
9
  from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
10
  from diffusers.utils import load_image
11
  from huggingface_hub import hf_hub_download, HfFileSystem, ModelCard, snapshot_download
 
20
  import warnings
21
 
22
 
23
+ huggingface_token = os.getenv("HF_TOKEN")
24
 
25
 
26
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en", device="cpu")
 
60
  torch_dtype=dtype
61
  ).to(device)
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  MAX_SEED = 2**32 - 1
64
  MAX_PIXEL_BUDGET = 1024 * 1024
65
 
 
538
  footer {visibility: hidden;}
539
  '''
540
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
541
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, delete_cache=(60, 3600)) as app:
542
  loras_state = gr.State(loras)
543
  selected_indices = gr.State([])
 
621
  randomize_seed = gr.Checkbox(True, label="Randomize seed")
622
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
623
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
624
  gallery.select(
625
  update_selection,
626
  inputs=[selected_indices, loras_state, width, height],
 
645
  outputs=[selected_info_1, selected_info_2, selected_info_3, selected_indices, lora_scale_1, lora_scale_2, lora_scale_3, lora_image_1, lora_image_2, lora_image_3]
646
  )
647
 
 
 
648
  randomize_button.click(
649
  randomize_loras,
650
  inputs=[selected_indices, loras_state],
 
674
  outputs=history_gallery,
675
  )
676
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
677
  if __name__ == "__main__":
678
  app.queue(max_size=20)
679
  app.launch(debug=True)