jon-pascal
commited on
Commit
•
033619b
1
Parent(s):
2d27e8d
Update app.py
Browse files
app.py
CHANGED
@@ -205,9 +205,7 @@ def restore_in_Xmin(
|
|
205 |
|
206 |
if input_format.lower() not in ['png', 'webp', 'jpg', 'jpeg', 'gif', 'bmp', 'heic']:
|
207 |
gr.Warning('Invalid image format. Please use a supported image format.')
|
208 |
-
return None, None, None
|
209 |
-
|
210 |
-
output_format = "png"
|
211 |
|
212 |
if prompt is None:
|
213 |
prompt = ""
|
@@ -234,7 +232,7 @@ def restore_in_Xmin(
|
|
234 |
|
235 |
if torch.cuda.device_count() == 0:
|
236 |
gr.Warning('Set this space to GPU config to make it work.')
|
237 |
-
return [input_image_path, denoise_image], gr.update(
|
238 |
|
239 |
if model_select != model.current_model:
|
240 |
print('Loading model: ' + model_select)
|
@@ -339,14 +337,11 @@ def restore_on_gpu(
|
|
339 |
start = time.time()
|
340 |
print('Starting GPU restoration...')
|
341 |
|
342 |
-
output_format = "png"
|
343 |
-
|
344 |
torch.cuda.set_device(SUPIR_device)
|
345 |
|
346 |
with torch.no_grad():
|
347 |
# Convert input image to NumPy array and ensure it has 3 channels
|
348 |
input_image = HWC3(np.array(Image.open(input_image_path)))
|
349 |
-
|
350 |
input_image = upscale_image(input_image, upscale, unit_resolution=32, min_size=min_size)
|
351 |
LQ = input_image / 255.0
|
352 |
LQ = np.power(LQ, gamma_correction)
|
@@ -382,8 +377,13 @@ def restore_on_gpu(
|
|
382 |
information = ("Start the process again if you want a different result. " if seed is not None else "") + \
|
383 |
"The image has been enhanced successfully."
|
384 |
|
385 |
-
#
|
386 |
-
|
|
|
|
|
|
|
|
|
|
|
387 |
|
388 |
def load_and_reset(param_setting):
|
389 |
print('Resetting parameters...')
|
@@ -420,18 +420,10 @@ def load_and_reset(param_setting):
|
|
420 |
return edm_steps, s_cfg, s_stage2, s_stage1, s_churn, s_noise, a_prompt, n_prompt, color_fix_type, linear_CFG, \
|
421 |
linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select
|
422 |
|
423 |
-
def log_information(
|
424 |
print('Logging information...')
|
425 |
-
if
|
426 |
-
|
427 |
-
print(result[0])
|
428 |
-
|
429 |
-
def on_select_result(result_slider, result_gallery, evt: gr.SelectData):
|
430 |
-
print('Result selected.')
|
431 |
-
if result_gallery is not None:
|
432 |
-
for i, result in enumerate(result_gallery):
|
433 |
-
print(result[0])
|
434 |
-
return [result_slider[0], result_gallery[evt.index][0]]
|
435 |
|
436 |
title_html = """
|
437 |
<h1><center>Maree's Magical Photo Tool</center></h1>
|
@@ -647,17 +639,11 @@ with gr.Blocks() as interface:
|
|
647 |
visible=False
|
648 |
)
|
649 |
result_slider = ImageSlider(
|
650 |
-
label='
|
651 |
show_label=False,
|
652 |
interactive=False,
|
653 |
elem_id="slider1",
|
654 |
-
show_download_button=
|
655 |
-
)
|
656 |
-
result_gallery = gr.Gallery(
|
657 |
-
label='Downloadable results',
|
658 |
-
show_label=True,
|
659 |
-
interactive=False,
|
660 |
-
elem_id="gallery1"
|
661 |
)
|
662 |
|
663 |
input_image.upload(
|
@@ -718,21 +704,17 @@ with gr.Blocks() as interface:
|
|
718 |
],
|
719 |
outputs=[
|
720 |
result_slider,
|
721 |
-
result_gallery,
|
722 |
restore_information,
|
723 |
reset_btn
|
724 |
]
|
725 |
).success(
|
726 |
fn=log_information,
|
727 |
-
inputs=[
|
728 |
outputs=[],
|
729 |
queue=False,
|
730 |
show_progress=False
|
731 |
)
|
732 |
|
733 |
-
result_gallery.change(on_select_result, [result_slider, result_gallery], result_slider)
|
734 |
-
result_gallery.select(on_select_result, [result_slider, result_gallery], result_slider)
|
735 |
-
|
736 |
restart_button.click(
|
737 |
fn=load_and_reset,
|
738 |
inputs=[param_setting],
|
|
|
205 |
|
206 |
if input_format.lower() not in ['png', 'webp', 'jpg', 'jpeg', 'gif', 'bmp', 'heic']:
|
207 |
gr.Warning('Invalid image format. Please use a supported image format.')
|
208 |
+
return None, None, None
|
|
|
|
|
209 |
|
210 |
if prompt is None:
|
211 |
prompt = ""
|
|
|
232 |
|
233 |
if torch.cuda.device_count() == 0:
|
234 |
gr.Warning('Set this space to GPU config to make it work.')
|
235 |
+
return [input_image_path, denoise_image], gr.update(value="GPU not available."), gr.update(visible=True)
|
236 |
|
237 |
if model_select != model.current_model:
|
238 |
print('Loading model: ' + model_select)
|
|
|
337 |
start = time.time()
|
338 |
print('Starting GPU restoration...')
|
339 |
|
|
|
|
|
340 |
torch.cuda.set_device(SUPIR_device)
|
341 |
|
342 |
with torch.no_grad():
|
343 |
# Convert input image to NumPy array and ensure it has 3 channels
|
344 |
input_image = HWC3(np.array(Image.open(input_image_path)))
|
|
|
345 |
input_image = upscale_image(input_image, upscale, unit_resolution=32, min_size=min_size)
|
346 |
LQ = input_image / 255.0
|
347 |
LQ = np.power(LQ, gamma_correction)
|
|
|
377 |
information = ("Start the process again if you want a different result. " if seed is not None else "") + \
|
378 |
"The image has been enhanced successfully."
|
379 |
|
380 |
+
# Save the result image to a temporary file for downloading
|
381 |
+
result_image = Image.fromarray(results[0])
|
382 |
+
result_image_path = f"result_{uuid.uuid4().hex}.png"
|
383 |
+
result_image.save(result_image_path)
|
384 |
+
|
385 |
+
# Update the result slider with the before and after images
|
386 |
+
return [input_image_path, result_image_path], gr.update(value=information, visible=True), gr.update(visible=True)
|
387 |
|
388 |
def load_and_reset(param_setting):
|
389 |
print('Resetting parameters...')
|
|
|
420 |
return edm_steps, s_cfg, s_stage2, s_stage1, s_churn, s_noise, a_prompt, n_prompt, color_fix_type, linear_CFG, \
|
421 |
linear_s_stage2, spt_linear_CFG, spt_linear_s_stage2, model_select
|
422 |
|
423 |
+
def log_information(result_slider):
|
424 |
print('Logging information...')
|
425 |
+
if result_slider is not None:
|
426 |
+
print(result_slider)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
427 |
|
428 |
title_html = """
|
429 |
<h1><center>Maree's Magical Photo Tool</center></h1>
|
|
|
639 |
visible=False
|
640 |
)
|
641 |
result_slider = ImageSlider(
|
642 |
+
label='Result',
|
643 |
show_label=False,
|
644 |
interactive=False,
|
645 |
elem_id="slider1",
|
646 |
+
show_download_button=True # Enable the download button
|
|
|
|
|
|
|
|
|
|
|
|
|
647 |
)
|
648 |
|
649 |
input_image.upload(
|
|
|
704 |
],
|
705 |
outputs=[
|
706 |
result_slider,
|
|
|
707 |
restore_information,
|
708 |
reset_btn
|
709 |
]
|
710 |
).success(
|
711 |
fn=log_information,
|
712 |
+
inputs=[result_slider],
|
713 |
outputs=[],
|
714 |
queue=False,
|
715 |
show_progress=False
|
716 |
)
|
717 |
|
|
|
|
|
|
|
718 |
restart_button.click(
|
719 |
fn=load_and_reset,
|
720 |
inputs=[param_setting],
|