Fabrice-TIERCELIN
commited on
Commit
•
0539164
1
Parent(s):
943f713
Run examples on click
Browse files- gradio_demo.py +47 -15
gradio_demo.py
CHANGED
@@ -67,6 +67,23 @@ if torch.cuda.device_count() > 0:
|
|
67 |
else:
|
68 |
llava_agent = None
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
def update_seed(is_randomize_seed, seed):
|
71 |
if is_randomize_seed:
|
72 |
return random.randint(0, 2147483647)
|
@@ -198,14 +215,7 @@ def stage2_process(
|
|
198 |
print("Final prompt: " + str(a_prompt))
|
199 |
noisy_image = noisy_image if denoise_image is None else denoise_image
|
200 |
if 1 < downscale:
|
201 |
-
input_height, input_width, input_channel =
|
202 |
-
print('downscale')
|
203 |
-
print(downscale)
|
204 |
-
print(input_width)
|
205 |
-
print(input_height)
|
206 |
-
print(input_width // downscale)
|
207 |
-
print(input_height // downscale)
|
208 |
-
print((input_width // downscale, input_height // downscale))
|
209 |
noisy_image = np.array(Image.fromarray(noisy_image).resize((input_width // downscale, input_height // downscale), Image.LANCZOS))
|
210 |
|
211 |
if allocation == 1:
|
@@ -390,7 +400,7 @@ def restore(
|
|
390 |
hours = math.floor(minutes / 60)
|
391 |
minutes = minutes - (hours * 60)
|
392 |
information = ("Start the process again if you want a different result. " if randomize_seed else "") + \
|
393 |
-
"Wait " + str(allocation) + " min before a new run to avoid quota penalty. " + \
|
394 |
"The image(s) has(ve) been generated in " + \
|
395 |
((str(hours) + " h, ") if hours != 0 else "") + \
|
396 |
((str(minutes) + " min, ") if hours != 0 or minutes != 0 else "") + \
|
@@ -474,7 +484,7 @@ title_html = """
|
|
474 |
This demo can handle huge images but the process will be aborted if it lasts more than 10 min.
|
475 |
Please leave a message in discussion if you encounter issues.
|
476 |
|
477 |
-
<p><center><a href="https://arxiv.org/abs/2401.13627">Paper</a>   <a href="http://supir.xpixel.group/">Project Page</a>   <a href="https://
|
478 |
"""
|
479 |
|
480 |
|
@@ -503,8 +513,13 @@ with gr.Blocks(title="SUPIR") as interface:
|
|
503 |
gr.HTML(title_html)
|
504 |
|
505 |
input_image = gr.Image(label="Input", show_label=True, type="numpy", height=600, elem_id="image-input")
|
|
|
|
|
|
|
|
|
|
|
506 |
with gr.Group():
|
507 |
-
prompt = gr.Textbox(label="Image description", info="Help the AI understand what the image represents; describe as much as possible; I advise you to write in English
|
508 |
prompt_hint = gr.HTML("You can use a <a href='"'https://huggingface.co/spaces/MaziyarPanahi/llava-llama-3-8b'"'>LlaVa space</a> to auto-generate the description of your image.")
|
509 |
upscale = gr.Radio([["x1", 1], ["x2", 2], ["x3", 3], ["x4", 4], ["x5", 5], ["x6", 6], ["x7", 7], ["x8", 8]], label="Upscale factor", info="Resolution x1 to x8", value=2, interactive=True)
|
510 |
allocation = gr.Radio([["1 min", 1], ["2 min", 2], ["3 min", 3], ["4 min", 4], ["5 min", 5], ["6 min", 6], ["7 min", 7], ["8 min", 8], ["9 min", 9], ["10 min", 10]], label="GPU allocation time", info="lower=May abort run, higher=Quota penalty for next runs", value=6, interactive=True)
|
@@ -591,6 +606,7 @@ with gr.Blocks(title="SUPIR") as interface:
|
|
591 |
event_id = gr.Textbox(label="Event ID", value="", visible=False)
|
592 |
|
593 |
gr.Examples(
|
|
|
594 |
fn = stage2_process,
|
595 |
inputs = [
|
596 |
input_image,
|
@@ -696,6 +712,25 @@ with gr.Blocks(title="SUPIR") as interface:
|
|
696 |
with gr.Row():
|
697 |
gr.Markdown(claim_md)
|
698 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
699 |
denoise_button.click(fn = check, inputs = [
|
700 |
input_image
|
701 |
], outputs = [], queue = False, show_progress = False).success(fn = stage1_process, inputs = [
|
@@ -724,10 +759,7 @@ with gr.Blocks(title="SUPIR") as interface:
|
|
724 |
seed
|
725 |
], queue = False, show_progress = False).then(fn = check, inputs = [
|
726 |
input_image
|
727 |
-
], outputs = [], queue = False, show_progress = False).success(fn
|
728 |
-
fb_score,
|
729 |
-
fb_text
|
730 |
-
], queue = False, show_progress = False).success(fn=stage2_process, inputs = [
|
731 |
input_image,
|
732 |
denoise_image,
|
733 |
prompt,
|
|
|
67 |
else:
|
68 |
llava_agent = None
|
69 |
|
70 |
+
def check_upload(input_image):
|
71 |
+
if input_image is None:
|
72 |
+
raise gr.Error("Please provide an image to restore.")
|
73 |
+
return [gr.update(visible = False)] * 2
|
74 |
+
|
75 |
+
def rotate_anti_90(image_array):
|
76 |
+
if image_array is None:
|
77 |
+
raise gr.Error("Please provide an image to rotate.")
|
78 |
+
|
79 |
+
return np.array(list(zip(*image_array))[::-1])
|
80 |
+
|
81 |
+
def rotate_90(image_array):
|
82 |
+
if image_array is None:
|
83 |
+
raise gr.Error("Please provide an image to rotate.")
|
84 |
+
|
85 |
+
return np.array(list(zip(*image_array[::-1])))
|
86 |
+
|
87 |
def update_seed(is_randomize_seed, seed):
|
88 |
if is_randomize_seed:
|
89 |
return random.randint(0, 2147483647)
|
|
|
215 |
print("Final prompt: " + str(a_prompt))
|
216 |
noisy_image = noisy_image if denoise_image is None else denoise_image
|
217 |
if 1 < downscale:
|
218 |
+
input_height, input_width, input_channel = noisy_image.shape
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
noisy_image = np.array(Image.fromarray(noisy_image).resize((input_width // downscale, input_height // downscale), Image.LANCZOS))
|
220 |
|
221 |
if allocation == 1:
|
|
|
400 |
hours = math.floor(minutes / 60)
|
401 |
minutes = minutes - (hours * 60)
|
402 |
information = ("Start the process again if you want a different result. " if randomize_seed else "") + \
|
403 |
+
"Wait " + str(allocation) + " min before a new run to avoid quota penalty or use another computer. " + \
|
404 |
"The image(s) has(ve) been generated in " + \
|
405 |
((str(hours) + " h, ") if hours != 0 else "") + \
|
406 |
((str(minutes) + " min, ") if hours != 0 or minutes != 0 else "") + \
|
|
|
484 |
This demo can handle huge images but the process will be aborted if it lasts more than 10 min.
|
485 |
Please leave a message in discussion if you encounter issues.
|
486 |
|
487 |
+
<p><center><a href="https://arxiv.org/abs/2401.13627">Paper</a>   <a href="http://supir.xpixel.group/">Project Page</a>   <a href="https://huggingface.co/blog/MonsterMMORPG/supir-sota-image-upscale-better-than-magnific-ai">Local Install Guide</a></center></p>
|
488 |
"""
|
489 |
|
490 |
|
|
|
513 |
gr.HTML(title_html)
|
514 |
|
515 |
input_image = gr.Image(label="Input", show_label=True, type="numpy", height=600, elem_id="image-input")
|
516 |
+
with gr.Row():
|
517 |
+
with gr.Column():
|
518 |
+
rotate_anti_90_button = gr.Button(value="⤴ Rotate -90°", elem_id="rotate_anti_90_button", visible=False)
|
519 |
+
with gr.Column():
|
520 |
+
rotate_90_button = gr.Button(value="⤵ Rotate +90°", elem_id="rotate_90_button", visible=False)
|
521 |
with gr.Group():
|
522 |
+
prompt = gr.Textbox(label="Image description", info="Help the AI understand what the image represents; describe as much as possible, especially the details we can't see on the original image; I advise you to write in English because other languages may not be handled", value="", placeholder="A 27 years old woman, walking, in Santiago, morning, Summer, photorealistic", lines=3)
|
523 |
prompt_hint = gr.HTML("You can use a <a href='"'https://huggingface.co/spaces/MaziyarPanahi/llava-llama-3-8b'"'>LlaVa space</a> to auto-generate the description of your image.")
|
524 |
upscale = gr.Radio([["x1", 1], ["x2", 2], ["x3", 3], ["x4", 4], ["x5", 5], ["x6", 6], ["x7", 7], ["x8", 8]], label="Upscale factor", info="Resolution x1 to x8", value=2, interactive=True)
|
525 |
allocation = gr.Radio([["1 min", 1], ["2 min", 2], ["3 min", 3], ["4 min", 4], ["5 min", 5], ["6 min", 6], ["7 min", 7], ["8 min", 8], ["9 min", 9], ["10 min", 10]], label="GPU allocation time", info="lower=May abort run, higher=Quota penalty for next runs", value=6, interactive=True)
|
|
|
606 |
event_id = gr.Textbox(label="Event ID", value="", visible=False)
|
607 |
|
608 |
gr.Examples(
|
609 |
+
run_on_click = True,
|
610 |
fn = stage2_process,
|
611 |
inputs = [
|
612 |
input_image,
|
|
|
712 |
with gr.Row():
|
713 |
gr.Markdown(claim_md)
|
714 |
|
715 |
+
input_image.upload(fn = check_upload, inputs = [
|
716 |
+
input_image
|
717 |
+
], outputs = [
|
718 |
+
rotate_anti_90_button,
|
719 |
+
rotate_90_button
|
720 |
+
], queue = False, show_progress = False)
|
721 |
+
|
722 |
+
rotate_anti_90_button.click(fn = rotate_anti_90, inputs = [
|
723 |
+
input_image
|
724 |
+
], outputs = [
|
725 |
+
input_image
|
726 |
+
], queue = False, show_progress = False)
|
727 |
+
|
728 |
+
rotate_90_button.click(fn = rotate_90, inputs = [
|
729 |
+
input_image
|
730 |
+
], outputs = [
|
731 |
+
input_image
|
732 |
+
], queue = False, show_progress = False)
|
733 |
+
|
734 |
denoise_button.click(fn = check, inputs = [
|
735 |
input_image
|
736 |
], outputs = [], queue = False, show_progress = False).success(fn = stage1_process, inputs = [
|
|
|
759 |
seed
|
760 |
], queue = False, show_progress = False).then(fn = check, inputs = [
|
761 |
input_image
|
762 |
+
], outputs = [], queue = False, show_progress = False).success(fn=stage2_process, inputs = [
|
|
|
|
|
|
|
763 |
input_image,
|
764 |
denoise_image,
|
765 |
prompt,
|