File size: 3,974 Bytes
d519da8
 
1891461
d519da8
 
 
 
 
 
 
d4188d5
d519da8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42a2bba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d519da8
6eaf879
d519da8
08f207d
d519da8
 
 
 
 
 
 
 
d4188d5
d519da8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import subprocess
from PIL import Image
import gradio as gr

# First, running the inversion command to obtain the input noise that will reconstruct the image. 
# It will save the inversion as output/test_cat/inversion/image-name.pt 
# BLIP-generated caption prompt is saved as output/test_cat/prompt/image-name.txt - eg., a painting of a cat sitting on top of a ball
def inversion(image_in, progress=gr.Progress(track_tqdm=True)):
  progress(0, desc="Starting...")
  # saving the input image
  image_in.save("assets/test_images/cats/input_image.png")
  # Run the script file
  subprocess.run(["python", "src/inversion.py", "--input_image", "input_image.png", "--results_folder", "output/test_cat"])
  # Open the text file with blip caption
  with open("output/test_cat/prompt/input_image.txt", "r") as file:
      # Read the file 
      prompt = file.read()
  return "output/test_cat/inversion/input_image.pt", prompt


# Performing image editing with the editing directions 
# This will save the edited image as output/test_cat/edit/image-name.png
def image_edit(task_name, progress=gr.Progress(track_tqdm=True)):
  progress(0, desc="Starting...")
  # Run the script file
  subprocess.run(["python", "src/edit_real.py", "--inversion", "output/test_cat/inversion/input_image.pt", 
                  "--prompt", "output/test_cat/prompt/input_image.txt", "--task_name", task_name,
                  "--results_folder", "output/test_cat/"])
  return "output/test_cat/edit/input_image.png"
  
def set_visible_true():
    return gr.update(visible=True)

def set_visible_False():
    return gr.update(visible=False)

#Gradio Blocks API
with gr.Blocks() as demo:
  gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
                    <div
                style="
                    display: inline-flex;
                    align-items: center;
                    gap: 0.8rem;
                    font-size: 1.75rem;
                "
                >
                <h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
                    Pix2Pix - Zero-shot Image-to-Image Translation
                </h1>
                </div>
                <p style="margin-bottom: 10px; font-size: 94%">
                This is an unofficial demo for <a href="https://pix2pixzero.github.io/" target="_blank">Pix2PixZero</a>. 
                Please visit their website and <a href="https://github.com/pix2pixzero/pix2pix-zero" target="_blank">github repo</a> for more details.
                </p></div>""")
  with gr.Row():
    image_in = gr.Image(type="pil", label="Start by uploading an image of a Cat or a Dog that you want to Translate")
    with gr.Column():
      btn_inversion = gr.Button("Get input noise and caption",visible=False )
      with gr.Row():
        blip_prompt = gr.Textbox(visible=False)
        inversion_file = gr.File(visible=False)
        #task_name = gr.Textbox()
  with gr.Row():
    image_out = gr.Image(visible=False)
    with gr.Column():
      task_name_radio  = gr.Radio(choices = ["cat2dog", "dog2cat",], type="value", visible=False, label="Select a task that you want to accomplish") #, value="cat2dog"),
      btn_imageedit = gr.Button(value="Translate the image!",visible=False)
    
    
    btn_inversion.click(inversion,[image_in],[inversion_file, blip_prompt])
    btn_inversion.click(set_visible_true, [], task_name_radio)  #inversion_file, blip_prompt,
    btn_inversion.click(set_visible_False, [], btn_inversion)

    task_name_radio.change(set_visible_true, [], btn_imageedit)
    task_name_radio.change(set_visible_true, [], image_out)

    btn_imageedit.click(image_edit,[task_name_radio],[image_out])
    
    image_in.clear(set_visible_true, [], btn_inversion)
    image_in.change(set_visible_true, [], btn_inversion)
    image_in.change(set_visible_true, [], blip_prompt)
    image_in.change(set_visible_true, [], inversion_file)

demo.queue(concurrency_count=3)
demo.launch(debug=True)