r3gm commited on
Commit
0c60a8e
·
verified ·
1 Parent(s): 6aae7a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -25
app.py CHANGED
@@ -6,12 +6,14 @@ from stablepy import (
6
  SCHEDULE_PREDICTION_TYPE_OPTIONS,
7
  check_scheduler_compatibility,
8
  TASK_AND_PREPROCESSORS,
 
9
  )
10
  from constants import (
11
  DIRECTORY_MODELS,
12
  DIRECTORY_LORAS,
13
  DIRECTORY_VAES,
14
  DIRECTORY_EMBEDS,
 
15
  DOWNLOAD_MODEL,
16
  DOWNLOAD_VAE,
17
  DOWNLOAD_LORA,
@@ -76,7 +78,7 @@ torch.backends.cuda.matmul.allow_tf32 = True
76
  # os.environ["PYTORCH_NO_CUDA_MEMORY_CACHING"] = "1"
77
  print(os.getenv("SPACES_ZERO_GPU"))
78
 
79
- directories = [DIRECTORY_MODELS, DIRECTORY_LORAS, DIRECTORY_VAES, DIRECTORY_EMBEDS]
80
  for directory in directories:
81
  os.makedirs(directory, exist_ok=True)
82
 
@@ -121,6 +123,7 @@ flux_pipe = FluxPipeline.from_pretrained(
121
  components = flux_pipe.components
122
  components.pop("transformer", None)
123
  delete_model(flux_repo)
 
124
 
125
  #######################
126
  # GUI
@@ -310,8 +313,8 @@ class GuiSD:
310
  syntax_weights,
311
  upscaler_model_path,
312
  upscaler_increases_size,
313
- esrgan_tile,
314
- esrgan_tile_overlap,
315
  hires_steps,
316
  hires_denoising_strength,
317
  hires_sampler,
@@ -375,6 +378,9 @@ class GuiSD:
375
  mode_ip2,
376
  scale_ip2,
377
  pag_scale,
 
 
 
378
  ):
379
  info_state = html_template_message("Navigating latent space...")
380
  yield info_state, gr.update(), gr.update()
@@ -418,16 +424,13 @@ class GuiSD:
418
  if task == "inpaint" and not image_mask:
419
  raise ValueError("No mask image found: Specify one in 'Image Mask'")
420
 
421
- if upscaler_model_path in UPSCALER_KEYS[:9]:
422
  upscaler_model = upscaler_model_path
423
  else:
424
- directory_upscalers = 'upscalers'
425
- os.makedirs(directory_upscalers, exist_ok=True)
426
-
427
  url_upscaler = UPSCALER_DICT_GUI[upscaler_model_path]
428
 
429
  if not os.path.exists(f"./upscalers/{url_upscaler.split('/')[-1]}"):
430
- download_things(directory_upscalers, url_upscaler, HF_TOKEN)
431
 
432
  upscaler_model = f"./upscalers/{url_upscaler.split('/')[-1]}"
433
 
@@ -531,8 +534,8 @@ class GuiSD:
531
  "t2i_adapter_conditioning_factor": float(t2i_adapter_conditioning_factor),
532
  "upscaler_model_path": upscaler_model,
533
  "upscaler_increases_size": upscaler_increases_size,
534
- "esrgan_tile": esrgan_tile,
535
- "esrgan_tile_overlap": esrgan_tile_overlap,
536
  "hires_steps": hires_steps,
537
  "hires_denoising_strength": hires_denoising_strength,
538
  "hires_prompt": hires_prompt,
@@ -547,6 +550,9 @@ class GuiSD:
547
  "ip_adapter_model": params_ip_model,
548
  "ip_adapter_mode": params_ip_mode,
549
  "ip_adapter_scale": params_ip_scale,
 
 
 
550
  }
551
 
552
  # kwargs for diffusers pipeline
@@ -694,22 +700,24 @@ def sd_gen_generate_pipeline(*args):
694
 
695
 
696
  @spaces.GPU(duration=15)
697
- def esrgan_upscale(image, upscaler_name, upscaler_size):
698
  if image is None: return None
699
 
700
  from stablepy.diffusers_vanilla.utils import save_pil_image_with_metadata
701
- from stablepy import UpscalerESRGAN
702
 
 
703
  exif_image = extract_exif_data(image)
704
 
705
- url_upscaler = UPSCALER_DICT_GUI[upscaler_name]
706
- directory_upscalers = 'upscalers'
707
- os.makedirs(directory_upscalers, exist_ok=True)
708
- if not os.path.exists(f"./upscalers/{url_upscaler.split('/')[-1]}"):
709
- download_things(directory_upscalers, url_upscaler, HF_TOKEN)
 
710
 
711
- scaler_beta = UpscalerESRGAN(0, 0)
712
- image_up = scaler_beta.upscale(image, upscaler_size, f"./upscalers/{url_upscaler.split('/')[-1]}")
713
 
714
  image_path = save_pil_image_with_metadata(image_up, f'{os.getcwd()}/up_images', exif_image)
715
 
@@ -900,8 +908,8 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
900
 
901
  upscaler_model_path_gui = gr.Dropdown(label="Upscaler", choices=UPSCALER_KEYS, value=UPSCALER_KEYS[0])
902
  upscaler_increases_size_gui = gr.Slider(minimum=1.1, maximum=4., step=0.1, value=1.2, label="Upscale by")
903
- esrgan_tile_gui = gr.Slider(minimum=0, value=0, maximum=500, step=1, label="ESRGAN Tile")
904
- esrgan_tile_overlap_gui = gr.Slider(minimum=1, maximum=200, step=1, value=8, label="ESRGAN Tile Overlap")
905
  hires_steps_gui = gr.Slider(minimum=0, value=30, maximum=100, step=1, label="Hires Steps")
906
  hires_denoising_strength_gui = gr.Slider(minimum=0.1, maximum=1.0, step=0.01, value=0.55, label="Hires Denoising Strength")
907
  hires_sampler_gui = gr.Dropdown(label="Hires Sampler", choices=POST_PROCESSING_SAMPLER, value=POST_PROCESSING_SAMPLER[0])
@@ -950,6 +958,14 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
950
  [lora1_gui, lora2_gui, lora3_gui, lora4_gui, lora5_gui, lora6_gui, lora7_gui, new_lora_status]
951
  )
952
 
 
 
 
 
 
 
 
 
953
  with gr.Accordion("IP-Adapter", open=False, visible=True):
954
 
955
  IP_MODELS = sorted(list(set(IP_ADAPTERS_SD + IP_ADAPTERS_SDXL)))
@@ -1190,8 +1206,11 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
1190
 
1191
  with gr.Row():
1192
  with gr.Column():
 
 
 
1193
  image_up_tab = gr.Image(label="Image", type="pil", sources=["upload"])
1194
- upscaler_tab = gr.Dropdown(label="Upscaler", choices=UPSCALER_KEYS[9:], value=UPSCALER_KEYS[11])
1195
  upscaler_size_tab = gr.Slider(minimum=1., maximum=4., step=0.1, value=1.1, label="Upscale by")
1196
  generate_button_up_tab = gr.Button(value="START UPSCALE", variant="primary")
1197
 
@@ -1199,7 +1218,7 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
1199
  result_up_tab = gr.Image(label="Result", type="pil", interactive=False, format="png")
1200
 
1201
  generate_button_up_tab.click(
1202
- fn=esrgan_upscale,
1203
  inputs=[image_up_tab, upscaler_tab, upscaler_size_tab],
1204
  outputs=[result_up_tab],
1205
  )
@@ -1271,8 +1290,8 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
1271
  prompt_syntax_gui,
1272
  upscaler_model_path_gui,
1273
  upscaler_increases_size_gui,
1274
- esrgan_tile_gui,
1275
- esrgan_tile_overlap_gui,
1276
  hires_steps_gui,
1277
  hires_denoising_strength_gui,
1278
  hires_sampler_gui,
@@ -1336,6 +1355,9 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
1336
  mode_ip2,
1337
  scale_ip2,
1338
  pag_scale_gui,
 
 
 
1339
  load_lora_cpu_gui,
1340
  verbose_info_gui,
1341
  gpu_duration_gui,
 
6
  SCHEDULE_PREDICTION_TYPE_OPTIONS,
7
  check_scheduler_compatibility,
8
  TASK_AND_PREPROCESSORS,
9
+ FACE_RESTORATION_MODELS,
10
  )
11
  from constants import (
12
  DIRECTORY_MODELS,
13
  DIRECTORY_LORAS,
14
  DIRECTORY_VAES,
15
  DIRECTORY_EMBEDS,
16
+ DIRECTORY_UPSCALERS,
17
  DOWNLOAD_MODEL,
18
  DOWNLOAD_VAE,
19
  DOWNLOAD_LORA,
 
78
  # os.environ["PYTORCH_NO_CUDA_MEMORY_CACHING"] = "1"
79
  print(os.getenv("SPACES_ZERO_GPU"))
80
 
81
+ directories = [DIRECTORY_MODELS, DIRECTORY_LORAS, DIRECTORY_VAES, DIRECTORY_EMBEDS, DIRECTORY_UPSCALERS]
82
  for directory in directories:
83
  os.makedirs(directory, exist_ok=True)
84
 
 
123
  components = flux_pipe.components
124
  components.pop("transformer", None)
125
  delete_model(flux_repo)
126
+ # components = None
127
 
128
  #######################
129
  # GUI
 
313
  syntax_weights,
314
  upscaler_model_path,
315
  upscaler_increases_size,
316
+ upscaler_tile_size,
317
+ upscaler_tile_overlap,
318
  hires_steps,
319
  hires_denoising_strength,
320
  hires_sampler,
 
378
  mode_ip2,
379
  scale_ip2,
380
  pag_scale,
381
+ face_restoration_model,
382
+ face_restoration_visibility,
383
+ face_restoration_weight,
384
  ):
385
  info_state = html_template_message("Navigating latent space...")
386
  yield info_state, gr.update(), gr.update()
 
424
  if task == "inpaint" and not image_mask:
425
  raise ValueError("No mask image found: Specify one in 'Image Mask'")
426
 
427
+ if "https://" not in str(UPSCALER_DICT_GUI[upscaler_model_path]):
428
  upscaler_model = upscaler_model_path
429
  else:
 
 
 
430
  url_upscaler = UPSCALER_DICT_GUI[upscaler_model_path]
431
 
432
  if not os.path.exists(f"./upscalers/{url_upscaler.split('/')[-1]}"):
433
+ download_things(DIRECTORY_UPSCALERS, url_upscaler, HF_TOKEN)
434
 
435
  upscaler_model = f"./upscalers/{url_upscaler.split('/')[-1]}"
436
 
 
534
  "t2i_adapter_conditioning_factor": float(t2i_adapter_conditioning_factor),
535
  "upscaler_model_path": upscaler_model,
536
  "upscaler_increases_size": upscaler_increases_size,
537
+ "upscaler_tile_size": upscaler_tile_size,
538
+ "upscaler_tile_overlap": upscaler_tile_overlap,
539
  "hires_steps": hires_steps,
540
  "hires_denoising_strength": hires_denoising_strength,
541
  "hires_prompt": hires_prompt,
 
550
  "ip_adapter_model": params_ip_model,
551
  "ip_adapter_mode": params_ip_mode,
552
  "ip_adapter_scale": params_ip_scale,
553
+ "face_restoration_model": face_restoration_model,
554
+ "face_restoration_visibility": face_restoration_visibility,
555
+ "face_restoration_weight": face_restoration_weight,
556
  }
557
 
558
  # kwargs for diffusers pipeline
 
700
 
701
 
702
  @spaces.GPU(duration=15)
703
+ def process_upscale(image, upscaler_name, upscaler_size):
704
  if image is None: return None
705
 
706
  from stablepy.diffusers_vanilla.utils import save_pil_image_with_metadata
707
+ from stablepy import load_upscaler_model
708
 
709
+ image = image.convert("RGB")
710
  exif_image = extract_exif_data(image)
711
 
712
+ name_upscaler = UPSCALER_DICT_GUI[upscaler_name]
713
+
714
+ if "https://" in str(name_upscaler):
715
+ name_upscaler = f"./upscalers/{name_upscaler.split('/')[-1]}"
716
+ if not os.path.exists(name_upscaler):
717
+ download_things(DIRECTORY_UPSCALERS, name_upscaler, HF_TOKEN)
718
 
719
+ scaler_beta = load_upscaler_model(model=name_upscaler, tile=0, tile_overlap=8, device="cuda", half=True)
720
+ image_up = scaler_beta.upscale(image, upscaler_size, True)
721
 
722
  image_path = save_pil_image_with_metadata(image_up, f'{os.getcwd()}/up_images', exif_image)
723
 
 
908
 
909
  upscaler_model_path_gui = gr.Dropdown(label="Upscaler", choices=UPSCALER_KEYS, value=UPSCALER_KEYS[0])
910
  upscaler_increases_size_gui = gr.Slider(minimum=1.1, maximum=4., step=0.1, value=1.2, label="Upscale by")
911
+ upscaler_tile_size_gui = gr.Slider(minimum=0, maximum=512, step=16, value=0, label="Upscaler Tile Size", info="0 = no tiling")
912
+ upscaler_tile_overlap_gui = gr.Slider(minimum=0, maximum=48, step=1, value=8, label="Upscaler Tile Overlap")
913
  hires_steps_gui = gr.Slider(minimum=0, value=30, maximum=100, step=1, label="Hires Steps")
914
  hires_denoising_strength_gui = gr.Slider(minimum=0.1, maximum=1.0, step=0.01, value=0.55, label="Hires Denoising Strength")
915
  hires_sampler_gui = gr.Dropdown(label="Hires Sampler", choices=POST_PROCESSING_SAMPLER, value=POST_PROCESSING_SAMPLER[0])
 
958
  [lora1_gui, lora2_gui, lora3_gui, lora4_gui, lora5_gui, lora6_gui, lora7_gui, new_lora_status]
959
  )
960
 
961
+ with gr.Accordion("Face restoration", open=False, visible=True):
962
+
963
+ face_rest_options = [None] + FACE_RESTORATION_MODELS
964
+
965
+ face_restoration_model_gui = gr.Dropdown(label="Face restoration model", choices=face_rest_options, value=face_rest_options[0])
966
+ face_restoration_visibility_gui = gr.Slider(minimum=0., maximum=1., step=0.001, value=1., label="Visibility")
967
+ face_restoration_weight_gui = gr.Slider(minimum=0., maximum=1., step=0.001, value=.5, label="Weight", info="(0 = maximum effect, 1 = minimum effect)")
968
+
969
  with gr.Accordion("IP-Adapter", open=False, visible=True):
970
 
971
  IP_MODELS = sorted(list(set(IP_ADAPTERS_SD + IP_ADAPTERS_SDXL)))
 
1206
 
1207
  with gr.Row():
1208
  with gr.Column():
1209
+
1210
+ USCALER_TAB_KEYS = [name for name in UPSCALER_KEYS[9:]]
1211
+
1212
  image_up_tab = gr.Image(label="Image", type="pil", sources=["upload"])
1213
+ upscaler_tab = gr.Dropdown(label="Upscaler", choices=USCALER_TAB_KEYS, value=USCALER_TAB_KEYS[5])
1214
  upscaler_size_tab = gr.Slider(minimum=1., maximum=4., step=0.1, value=1.1, label="Upscale by")
1215
  generate_button_up_tab = gr.Button(value="START UPSCALE", variant="primary")
1216
 
 
1218
  result_up_tab = gr.Image(label="Result", type="pil", interactive=False, format="png")
1219
 
1220
  generate_button_up_tab.click(
1221
+ fn=process_upscale,
1222
  inputs=[image_up_tab, upscaler_tab, upscaler_size_tab],
1223
  outputs=[result_up_tab],
1224
  )
 
1290
  prompt_syntax_gui,
1291
  upscaler_model_path_gui,
1292
  upscaler_increases_size_gui,
1293
+ upscaler_tile_size_gui,
1294
+ upscaler_tile_overlap_gui,
1295
  hires_steps_gui,
1296
  hires_denoising_strength_gui,
1297
  hires_sampler_gui,
 
1355
  mode_ip2,
1356
  scale_ip2,
1357
  pag_scale_gui,
1358
+ face_restoration_model_gui,
1359
+ face_restoration_visibility_gui,
1360
+ face_restoration_weight_gui,
1361
  load_lora_cpu_gui,
1362
  verbose_info_gui,
1363
  gpu_duration_gui,