Update app.py
Browse files
app.py
CHANGED
@@ -2,54 +2,112 @@ import os
|
|
2 |
import gradio as gr
|
3 |
from PIL import Image
|
4 |
|
5 |
-
#
|
6 |
-
BASE_PATH = "
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
# Predefined dipole options based on folder structure
|
11 |
-
dipole_options = sorted(
|
12 |
-
[f"Path {i}" for i in range(len(os.listdir(IMAGES_DIR)))]
|
13 |
-
)
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
image_dir = os.path.join(IMAGES_DIR, f"path_{index:03d}")
|
19 |
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
#
|
28 |
-
def
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Gradio Interface
|
32 |
def build_interface():
|
33 |
-
with gr.
|
34 |
-
gr.Markdown("# ContraCLIP
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
# Launch the Interface
|
52 |
-
demo.launch()
|
53 |
|
54 |
if __name__ == "__main__":
|
55 |
-
build_interface()
|
|
|
|
2 |
import gradio as gr
|
3 |
from PIL import Image
|
4 |
|
5 |
+
# Define global paths
|
6 |
+
BASE_PATH = "disentangled-image-editing-final-project/ContraCLIP/experiments/wip"
|
7 |
+
LATENT_CODES_DIR = os.path.join(BASE_PATH, "results")
|
8 |
+
SEMANTIC_DIPOLES_FILE = os.path.join(LATENT_CODES_DIR, "semantic_dipoles.json")
|
9 |
+
DEFAULT_IMAGE = "original_image.jpg"
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Load semantic dipoles
|
12 |
+
with open(SEMANTIC_DIPOLES_FILE, "r") as f:
|
13 |
+
semantic_dipoles = json.load(f)
|
|
|
14 |
|
15 |
+
# Helper to list all latent code folders
|
16 |
+
latent_code_folders = sorted(
|
17 |
+
[
|
18 |
+
folder
|
19 |
+
for folder in os.listdir(LATENT_CODES_DIR)
|
20 |
+
if os.path.isdir(os.path.join(LATENT_CODES_DIR, folder))
|
21 |
+
]
|
22 |
+
)
|
23 |
|
24 |
+
# Display predefined image paths based on semantic dipole index
|
25 |
+
def load_dipole_paths(latent_code):
|
26 |
+
latent_path = os.path.join(LATENT_CODES_DIR, latent_code, "paths_images")
|
27 |
+
paths = sorted(
|
28 |
+
[
|
29 |
+
f"path_{i:03d}"
|
30 |
+
for i in range(len(os.listdir(latent_path)))
|
31 |
+
]
|
32 |
+
)
|
33 |
+
return paths
|
34 |
|
35 |
+
# Function to display images
|
36 |
+
def display_image(latent_code, semantic_dipole, frame_idx):
|
37 |
+
index = semantic_dipoles.index(semantic_dipole)
|
38 |
+
path_dir = os.path.join(LATENT_CODES_DIR, latent_code, "paths_images", f"path_{index:03d}")
|
39 |
+
frame_image_path = os.path.join(path_dir, f"{frame_idx:06d}.jpg")
|
40 |
+
if not os.path.exists(frame_image_path):
|
41 |
+
return f"Image not found: {frame_image_path}"
|
42 |
+
return Image.open(frame_image_path)
|
43 |
+
|
44 |
+
# Function to display GAN latent space interactive plot
|
45 |
+
def display_interactive_plot(latent_code):
|
46 |
+
html_file = os.path.join(LATENT_CODES_DIR, latent_code, "interactive_latent_space.html")
|
47 |
+
if not os.path.exists(html_file):
|
48 |
+
return f"Interactive file not found: {html_file}"
|
49 |
+
with open(html_file, "r") as file:
|
50 |
+
return file.read()
|
51 |
|
52 |
# Gradio Interface
|
53 |
def build_interface():
|
54 |
+
with gr.Blocks() as demo:
|
55 |
+
gr.Markdown("# ContraCLIP-based Image Editing and Visualization Demo")
|
56 |
+
|
57 |
+
with gr.Row():
|
58 |
+
with gr.Column():
|
59 |
+
gr.Markdown("### Select Latent Code and Semantic Dipole")
|
60 |
+
latent_code_dropdown = gr.Dropdown(
|
61 |
+
latent_code_folders,
|
62 |
+
label="Latent Code",
|
63 |
+
value=latent_code_folders[0],
|
64 |
+
)
|
65 |
+
semantic_dipole_dropdown = gr.Dropdown(
|
66 |
+
semantic_dipoles,
|
67 |
+
label="Semantic Dipole",
|
68 |
+
value=semantic_dipoles[0],
|
69 |
+
)
|
70 |
+
frame_slider = gr.Slider(
|
71 |
+
0, 32, step=1, label="Frame Index"
|
72 |
+
)
|
73 |
+
|
74 |
+
with gr.Column():
|
75 |
+
image_display = gr.Image(label="Image Preview")
|
76 |
+
html_display = gr.HTML(label="Interactive Latent Space")
|
77 |
+
|
78 |
+
# Update image based on latent code, semantic dipole, and frame index
|
79 |
+
def update_image(latent_code, semantic_dipole, frame_idx):
|
80 |
+
return display_image(latent_code, semantic_dipole, frame_idx)
|
81 |
+
|
82 |
+
# Update HTML display for the selected latent code
|
83 |
+
def update_html(latent_code):
|
84 |
+
return display_interactive_plot(latent_code)
|
85 |
+
|
86 |
+
# Link dropdowns and slider
|
87 |
+
frame_slider.change(
|
88 |
+
update_image,
|
89 |
+
[latent_code_dropdown, semantic_dipole_dropdown, frame_slider],
|
90 |
+
[image_display],
|
91 |
+
)
|
92 |
+
latent_code_dropdown.change(
|
93 |
+
update_html, [latent_code_dropdown], [html_display]
|
94 |
+
)
|
95 |
+
|
96 |
+
# Set up initial values
|
97 |
+
demo.load(
|
98 |
+
lambda: display_image(latent_code_folders[0], semantic_dipoles[0], 0),
|
99 |
+
inputs=[],
|
100 |
+
outputs=[image_display],
|
101 |
+
)
|
102 |
+
demo.load(
|
103 |
+
lambda: display_interactive_plot(latent_code_folders[0]),
|
104 |
+
inputs=[],
|
105 |
+
outputs=[html_display],
|
106 |
+
)
|
107 |
+
|
108 |
+
return demo
|
109 |
|
|
|
|
|
110 |
|
111 |
if __name__ == "__main__":
|
112 |
+
interface = build_interface()
|
113 |
+
interface.launch()
|