dattarij commited on
Commit
0c16b58
·
verified ·
1 Parent(s): ddc2866

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -13,6 +13,8 @@ DEFAULT_IMAGE = "original_image.jpg"
13
  # Load semantic dipoles
14
  with open(SEMANTIC_DIPOLES_FILE, "r") as f:
15
  semantic_dipoles = json.load(f)
 
 
16
 
17
  # Helper to list all latent code folders
18
  latent_code_folders = sorted(
@@ -34,23 +36,28 @@ def load_dipole_paths(latent_code):
34
  )
35
  return paths
36
 
37
- # Function to display images
38
- def display_image(latent_code, semantic_dipole, frame_idx):
39
- index = semantic_dipoles.index(semantic_dipole)
40
- path_dir = os.path.join(LATENT_CODES_DIR, latent_code, "paths_images", f"path_{index:03d}")
 
 
 
 
 
 
41
  frame_image_path = os.path.join(path_dir, f"{frame_idx:06d}.jpg")
 
42
  if not os.path.exists(frame_image_path):
43
- return f"Image not found: {frame_image_path}"
 
44
  return Image.open(frame_image_path)
45
 
46
  # Function to display GAN latent space interactive plot
47
  def display_interactive_plot(latent_code):
48
- html_file = os.path.join(LATENT_CODES_DIR, latent_code, f"interactive_latent_space_{latent_code}.html")
49
- if not os.path.exists(html_file):
50
- return f"Interactive file not found: {html_file}"
51
- # Embed the HTML file using an iframe
52
- iframe = f'<iframe src="{html_file}" width="100%" height="600px" style="border:none;"></iframe>'
53
- return iframe
54
 
55
  # Gradio Interface
56
  def build_interface():
@@ -66,9 +73,9 @@ def build_interface():
66
  value=latent_code_folders[0],
67
  )
68
  semantic_dipole_dropdown = gr.Dropdown(
69
- semantic_dipoles,
70
  label="Semantic Dipole",
71
- value=semantic_dipoles[0],
72
  )
73
  frame_slider = gr.Slider(
74
  0, 32, step=1, label="Frame Index"
 
13
  # Load semantic dipoles
14
  with open(SEMANTIC_DIPOLES_FILE, "r") as f:
15
  semantic_dipoles = json.load(f)
16
+ # Transform semantic_dipoles into "A -> B" format
17
+ formatted_dipoles = [f"{pair[0]} -> {pair[1]}" for pair in semantic_dipoles]
18
 
19
  # Helper to list all latent code folders
20
  latent_code_folders = sorted(
 
36
  )
37
  return paths
38
 
39
+ def display_image(latent_code, formatted_dipole, frame_idx):
40
+ # Reverse-map "A -> B" format back to the index in semantic_dipoles
41
+ try:
42
+ index = formatted_dipoles.index(formatted_dipole)
43
+ except ValueError:
44
+ return f"Error: Semantic dipole '{formatted_dipole}' not found in the list."
45
+
46
+ path_dir = os.path.join(
47
+ LATENT_CODES_DIR, latent_code, "paths_images", f"path_{index:03d}"
48
+ )
49
  frame_image_path = os.path.join(path_dir, f"{frame_idx:06d}.jpg")
50
+
51
  if not os.path.exists(frame_image_path):
52
+ return f"Image not found: {frame_image_path}."
53
+
54
  return Image.open(frame_image_path)
55
 
56
  # Function to display GAN latent space interactive plot
57
  def display_interactive_plot(latent_code):
58
+ file_path = f"files/{LATENT_CODES_DIR}/{latent_code}/interactive_latent_space_{latent_code}.html"
59
+ iframe_html = f'<iframe src="{file_path}" width="800" height="600" frameborder="0"></iframe>'
60
+ return iframe_html
 
 
 
61
 
62
  # Gradio Interface
63
  def build_interface():
 
73
  value=latent_code_folders[0],
74
  )
75
  semantic_dipole_dropdown = gr.Dropdown(
76
+ formatted_dipoles,
77
  label="Semantic Dipole",
78
+ value=formatted_dipoles[0], # Set default value
79
  )
80
  frame_slider = gr.Slider(
81
  0, 32, step=1, label="Frame Index"