ysharma HF staff commited on
Commit
d519da8
1 Parent(s): e88341d

create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ from PIL import Image
3
+
4
+ # First, running the inversion command to obtain the input noise that will reconstruct the image.
5
+ # It will save the inversion as output/test_cat/inversion/image-name.pt
6
+ # 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
7
+ def inversion(image_in, progress=gr.Progress(track_tqdm=True)):
8
+ progress(0, desc="Starting...")
9
+ # saving the input image
10
+ image_in.save("input_image.png")
11
+ # Run the script file
12
+ subprocess.run(["python", "src/inversion.py", "--input_image", "input_image.png", "--results_folder", "output/test_cat"])
13
+ # Open the text file with blip caption
14
+ with open("output/test_cat/prompt/input_image.txt", "r") as file:
15
+ # Read the file
16
+ prompt = file.read()
17
+ return "output/test_cat/inversion/input_image.pt", prompt
18
+
19
+
20
+ # Performing image editing with the editing directions
21
+ # This will save the edited image as output/test_cat/edit/image-name.png
22
+ def image_edit(task_name, progress=gr.Progress(track_tqdm=True)):
23
+ progress(0, desc="Starting...")
24
+ # Run the script file
25
+ subprocess.run(["python", "src/edit_real.py", "--inversion", "output/test_cat/inversion/input_image.pt",
26
+ "--prompt", "output/test_cat/prompt/input_image.txt", "--task_name", task_name,
27
+ "--results_folder", "output/test_cat/"])
28
+ return "output/test_cat/edit/input_image.png"
29
+
30
+ def set_visible_true():
31
+ return gr.update(visible=True)
32
+
33
+ def set_visible_False():
34
+ return gr.update(visible=False)
35
+
36
+ #Gradio Blocks API
37
+ with gr.Blocks() as demo:
38
+ with gr.Row():
39
+ image_in = gr.Image(type="pil", label="Upload the image of a cat or a dog you want to translate")
40
+ with gr.Column():
41
+ btn_inversion = gr.Button("Get input noise",visible=False )
42
+ with gr.Row():
43
+ blip_prompt = gr.Textbox(visible=False)
44
+ inversion_file = gr.File(visible=False)
45
+ #task_name = gr.Textbox()
46
+ with gr.Row():
47
+ image_out = gr.Image(visible=False)
48
+ with gr.Column():
49
+ task_name_radio = gr.Radio(choices = ["cat2dog", "dog2cat",], type="value", visible=False, label="Select a task that you want to accomplish") #, value="cat2dog"),
50
+ btn_imageedit = gr.Button(value="Edit the image!",visible=False)
51
+
52
+
53
+ btn_inversion.click(inversion,[image_in],[inversion_file, blip_prompt])
54
+ btn_inversion.click(set_visible_true, [], task_name_radio) #inversion_file, blip_prompt,
55
+ btn_inversion.click(set_visible_False, [], btn_inversion)
56
+
57
+ task_name_radio.change(set_visible_true, [], btn_imageedit)
58
+ task_name_radio.change(set_visible_true, [], image_out)
59
+
60
+ btn_imageedit.click(image_edit,[task_name_radio],[image_out])
61
+
62
+ image_in.clear(set_visible_true, [], btn_inversion)
63
+ image_in.change(set_visible_true, [], btn_inversion)
64
+ image_in.change(set_visible_true, [], blip_prompt)
65
+ image_in.change(set_visible_true, [], inversion_file)
66
+
67
+ demo.queue(concurrency_count=3)
68
+ demo.launch(debug=True)