HiPer / app.py
HiPer0's picture
Initial commit
fda222f
# import os
# import pdb
# from PIL import Image
import gradio as gr
from src.utils.gradio_utils import *
if __name__=="__main__":
step_dict = {'800': 800, '900': 900, '1000': 1000, '1100': 1100}
with gr.Blocks(css=CSS_main) as demo:
gr.HTML(HTML_header)
with gr.Row():
# col A: Optimize personalized embedding
with gr.Column(scale=2) as gc_left:
gr.HTML(" <center> <p style='font-size:150%;'> [Step 1] Optimize personalized embedding </p> </center>")
img_in_real = gr.Image(type="pil", label="Start by uploading the source image", elem_id="input_image").style(height=300, width=300)
gr.Examples( examples="src_image", inputs=[img_in_real])
prompt = gr.Textbox(value="a standing dog", label="Source text prompt (Describe the source image)", interactive=True)
n_hiper = gr.Slider(5, 10, 5, label="Number of personalized embedding (Tips! 5 for animals / 10 for humans)", interactive=True, step=1)
btn_optimize = gr.Button("Optimize", label="")
fpath_z_gen = gr.Textbox(value="placeholder", visible=False)
gr.HTML(" <center> <p style='font-size:150%;'> See the [Step 1] results with different optimization steps </p> </center>")
with gr.Row():
with gr.Column(scale=0.3, min_width=0.7) as gc_left:
# btn_source = gr.Button("Source image", label="")
btn_opt_step800 = gr.Button("Step 800", label="")
btn_opt_step900 = gr.Button("Step 900", label="")
btn_opt_step1000 = gr.Button("Step 1000", label="")
btn_opt_step1100 = gr.Button("Step 1100", label="")
with gr.Column(scale=0.5, min_width=0.8) as gc_left:
img_src = gr.Image(type="pil", label="Source image", visible=True).style(height=250, width=250)
with gr.Column(scale=0.5, min_width=0.8) as gc_left:
img_out_opt = gr.Image(type="pil", label="Optimization step output", visible=True).style(height=250, width=250)
# col B: Generate target image
with gr.Column(scale=2) as gc_left:
gr.HTML(" <center> <p style='font-size:150%;'> [Step 2] Generate target image </p> </center>")
with gr.Row():
with gr.Column():
dest = gr.Textbox(value="a sitting dog", label="Target text prompt", interactive=True)
step = gr.Radio(["Step 800", "Step 900", "Step 1000", "Step 1100"], value="Step 1000", label="Training optimization step \n (Refer to the personalized results corresponding to each optimization step listed in the left column.)")
seed = gr.Number(value=111111, label="Random seed", interactive=True)
with gr.Row():
btn_generate = gr.Button("Generate", label="")
img_out = gr.Image(type="pil", label="Output Image", visible=True)
with gr.Accordion("Instruction", open=True):
gr.Textbox("In NVIDIA GeForce GTX 3090, [step 1] takes about 3.5 minutes and [step 2] takes about 1 minute.", show_label=False)
gr.Textbox("In NVIDIA T4, [step 1] takes about 8 minutes and [step 2] takes about 1.5 minutes.", show_label=False)
gr.Textbox("At [step 1], put the desired source image and write the source text that describes the source image. If it is difficult to describe, you can use a noun such as 'a dog' or 'a woman.' Then decide on the number of desired personalized embeddings.", show_label=False)
gr.Textbox("After [step 1], you can check the personalized results with different optimization steps and select the optimization step. First, check if the image at step 1000 has a subject similar to the source image. In the paper, we use the 1000 step for optimization almost.", show_label=False)
gr.Textbox("At [step 2], write the derised target text. Then, refer to the generated personalized image in the bottom left and choose an optimization. If the desired image is not obtained, try another random seed.", show_label=False)
############
btn_optimize.click(launch_optimize, [img_in_real, prompt, n_hiper], [fpath_z_gen, img_src])
def fn_set_none():
return gr.update(value=None)
btn_optimize.click(fn_set_none, [], img_in_real)
# btn_optimize.click(set_visible_true, [], img_in_synth)
btn_optimize.click(set_visible_false, [], img_in_real)
############
def fn_clear_all():
return gr.update(value=None), gr.update(value=None), gr.update(value=None)
img_in_real.clear(fn_clear_all, [], [img_out, img_in_real])#, img_in_synth])
# img_in_real.clear(set_visible_true, [], img_in_synth)
img_in_real.clear(set_visible_false, [], img_in_real)
img_out.clear(fn_clear_all, [], [img_out, img_in_real])#, img_in_synth])
############
btn_generate.click(launch_main,
[
dest, step,
fpath_z_gen, seed,
],
[img_out]
)
############
btn_opt_step800.click(launch_opt800, [],[img_out_opt])
btn_opt_step900.click(launch_opt900, [],[img_out_opt])
btn_opt_step1000.click(launch_opt1000, [],[img_out_opt])
btn_opt_step1100.click(launch_opt1100, [],[img_out_opt])
gr.HTML("<hr>")
gr.close_all()
demo.queue(concurrency_count=1)
demo.launch(debug=True)
# demo.queue(concurrency_count=1)
# demo.launch(server_port=2222, server_name="0.0.0.0", debug=True,share=True)