r3gm commited on
Commit
d348ae1
1 Parent(s): c2216aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -18
app.py CHANGED
@@ -22,8 +22,10 @@ from stablepy import (
22
  SDXL_TASKS,
23
  )
24
  import time
 
25
  # import urllib.parse
26
 
 
27
  print(os.getenv("SPACES_ZERO_GPU"))
28
 
29
  # - **Download SD 1.5 Models**
@@ -905,6 +907,45 @@ def sd_gen_generate_pipeline(*args):
905
  print(msg_task_complete)
906
 
907
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
908
  dynamic_gpu_duration.zerogpu = True
909
  sd_gen_generate_pipeline.zerogpu = True
910
  sd_gen = GuiSD()
@@ -1066,9 +1107,9 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
1066
  with gr.Accordion("Hires fix", open=False, visible=True):
1067
 
1068
  upscaler_model_path_gui = gr.Dropdown(label="Upscaler", choices=UPSCALER_KEYS, value=UPSCALER_KEYS[0])
1069
- upscaler_increases_size_gui = gr.Slider(minimum=1.1, maximum=6., step=0.1, value=1.4, label="Upscale by")
1070
- esrgan_tile_gui = gr.Slider(minimum=0, value=100, maximum=500, step=1, label="ESRGAN Tile")
1071
- esrgan_tile_overlap_gui = gr.Slider(minimum=1, maximum=200, step=1, value=10, label="ESRGAN Tile Overlap")
1072
  hires_steps_gui = gr.Slider(minimum=0, value=30, maximum=100, step=1, label="Hires Steps")
1073
  hires_denoising_strength_gui = gr.Slider(minimum=0.1, maximum=1.0, step=0.01, value=0.55, label="Hires Denoising Strength")
1074
  hires_sampler_gui = gr.Dropdown(label="Hires Sampler", choices=POST_PROCESSING_SAMPLER, value=POST_PROCESSING_SAMPLER[0])
@@ -1510,20 +1551,6 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
1510
  btn_send.click(send_img, [img_source, img_result], [image_control, image_mask_gui])
1511
 
1512
  with gr.Tab("PNG Info"):
1513
- def extract_exif_data(image):
1514
- if image is None: return ""
1515
-
1516
- try:
1517
- metadata_keys = ['parameters', 'metadata', 'prompt', 'Comment']
1518
-
1519
- for key in metadata_keys:
1520
- if key in image.info:
1521
- return image.info[key]
1522
-
1523
- return str(image.info)
1524
-
1525
- except Exception as e:
1526
- return f"Error extracting metadata: {str(e)}"
1527
 
1528
  with gr.Row():
1529
  with gr.Column():
@@ -1538,6 +1565,24 @@ with gr.Blocks(theme="NoCrypt/miku", css=CSS) as app:
1538
  outputs=[result_metadata],
1539
  )
1540
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1541
  generate_button.click(
1542
  fn=sd_gen.load_new_model,
1543
  inputs=[
@@ -1668,4 +1713,4 @@ app.launch(
1668
  show_error=True,
1669
  debug=True,
1670
  allowed_paths=["./images/"],
1671
- )
 
22
  SDXL_TASKS,
23
  )
24
  import time
25
+ from PIL import ImageFile
26
  # import urllib.parse
27
 
28
+ ImageFile.LOAD_TRUNCATED_IMAGES = True
29
  print(os.getenv("SPACES_ZERO_GPU"))
30
 
31
  # - **Download SD 1.5 Models**
 
907
  print(msg_task_complete)
908
 
909
 
910
+ def extract_exif_data(image):
911
+ if image is None: return ""
912
+
913
+ try:
914
+ metadata_keys = ['parameters', 'metadata', 'prompt', 'Comment']
915
+
916
+ for key in metadata_keys:
917
+ if key in image.info:
918
+ return image.info[key]
919
+
920
+ return str(image.info)
921
+
922
+ except Exception as e:
923
+ return f"Error extracting metadata: {str(e)}"
924
+
925
+
926
+ @spaces.GPU(duration=20)
927
+ def esrgan_upscale(image, upscaler_name, upscaler_size):
928
+ if image is None: return None
929
+
930
+ from stablepy.diffusers_vanilla.utils import save_pil_image_with_metadata
931
+ from stablepy import UpscalerESRGAN
932
+
933
+ exif_image = extract_exif_data(image)
934
+
935
+ url_upscaler = UPSCALER_DICT_GUI[upscaler_name]
936
+ directory_upscalers = 'upscalers'
937
+ os.makedirs(directory_upscalers, exist_ok=True)
938
+ if not os.path.exists(f"./upscalers/{url_upscaler.split('/')[-1]}"):
939
+ download_things(directory_upscalers, url_upscaler, HF_TOKEN)
940
+
941
+ scaler_beta = UpscalerESRGAN(0, 0)
942
+ image_up = scaler_beta.upscale(image, upscaler_size, f"./upscalers/{url_upscaler.split('/')[-1]}")
943
+
944
+ image_path = save_pil_image_with_metadata(image_up, f'{os.getcwd()}/up_images', exif_image)
945
+
946
+ return image_path
947
+
948
+
949
  dynamic_gpu_duration.zerogpu = True
950
  sd_gen_generate_pipeline.zerogpu = True
951
  sd_gen = GuiSD()
 
1107
  with gr.Accordion("Hires fix", open=False, visible=True):
1108
 
1109
  upscaler_model_path_gui = gr.Dropdown(label="Upscaler", choices=UPSCALER_KEYS, value=UPSCALER_KEYS[0])
1110
+ upscaler_increases_size_gui = gr.Slider(minimum=1.1, maximum=4., step=0.1, value=1.2, label="Upscale by")
1111
+ esrgan_tile_gui = gr.Slider(minimum=0, value=0, maximum=500, step=1, label="ESRGAN Tile")
1112
+ esrgan_tile_overlap_gui = gr.Slider(minimum=1, maximum=200, step=1, value=8, label="ESRGAN Tile Overlap")
1113
  hires_steps_gui = gr.Slider(minimum=0, value=30, maximum=100, step=1, label="Hires Steps")
1114
  hires_denoising_strength_gui = gr.Slider(minimum=0.1, maximum=1.0, step=0.01, value=0.55, label="Hires Denoising Strength")
1115
  hires_sampler_gui = gr.Dropdown(label="Hires Sampler", choices=POST_PROCESSING_SAMPLER, value=POST_PROCESSING_SAMPLER[0])
 
1551
  btn_send.click(send_img, [img_source, img_result], [image_control, image_mask_gui])
1552
 
1553
  with gr.Tab("PNG Info"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1554
 
1555
  with gr.Row():
1556
  with gr.Column():
 
1565
  outputs=[result_metadata],
1566
  )
1567
 
1568
+ with gr.Tab("Upscaler"):
1569
+
1570
+ with gr.Row():
1571
+ with gr.Column():
1572
+ image_up_tab = gr.Image(label="Image", type="pil", sources=["upload"])
1573
+ upscaler_tab = gr.Dropdown(label="Upscaler", choices=UPSCALER_KEYS[9:], value=UPSCALER_KEYS[11])
1574
+ upscaler_size_tab = gr.Slider(minimum=1., maximum=4., step=0.1, value=1.1, label="Upscale by")
1575
+ generate_button_up_tab = gr.Button(value="START UPSCALE", variant="primary")
1576
+
1577
+ with gr.Column():
1578
+ result_up_tab = gr.Image(label="Result", type="pil", interactive=False, format="png")
1579
+
1580
+ generate_button_up_tab.click(
1581
+ fn=esrgan_upscale,
1582
+ inputs=[image_up_tab, upscaler_tab, upscaler_size_tab],
1583
+ outputs=[result_up_tab],
1584
+ )
1585
+
1586
  generate_button.click(
1587
  fn=sd_gen.load_new_model,
1588
  inputs=[
 
1713
  show_error=True,
1714
  debug=True,
1715
  allowed_paths=["./images/"],
1716
+ )