clement-bonnet commited on
Commit
ba5567b
·
1 Parent(s): dc5df7b

feat: optimal latent button

Browse files
Files changed (1) hide show
  1. app.py +32 -2
app.py CHANGED
@@ -4,6 +4,7 @@ from PIL import Image, ImageDraw
4
  from inference import generate_image
5
 
6
  TASK_TO_INDEX = {"Task 1": 0, "Task 2": 1, "Task 3": 2, "Task 4": 3}
 
7
 
8
 
9
  def create_marker_overlay(image_path: str, x: int, y: int) -> Image.Image:
@@ -49,6 +50,18 @@ def generate_output_image(image_idx: int, coords: tuple[int, int]) -> Image.Imag
49
  return generate_image(image_idx, x_norm, y_norm)
50
 
51
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  with gr.Blocks(
53
  css="""
54
  .radio-container {
@@ -71,6 +84,13 @@ with gr.Blocks(
71
  background-color: #f8f9fa !important;
72
  border-radius: 8px !important;
73
  }
 
 
 
 
 
 
 
74
  """
75
  ) as demo:
76
  gr.Markdown(
@@ -130,6 +150,10 @@ with gr.Blocks(
130
  show_fullscreen_button=False,
131
  )
132
 
 
 
 
 
133
  # Documentation section
134
  with gr.Column(elem_classes="documentation"):
135
  gr.Markdown(
@@ -144,8 +168,7 @@ with gr.Blocks(
144
  3. **Coordinate Selection**: Click anywhere in the heatmap to specify where in the latent space you want to generate from
145
  4. **Generation**: The model generates a new image based on your selected coordinates
146
 
147
- ### Sample Results
148
- Here are some example outputs from our method:
149
  """
150
  )
151
 
@@ -186,4 +209,11 @@ with gr.Blocks(
186
  outputs=output_image,
187
  )
188
 
 
 
 
 
 
 
 
189
  demo.launch()
 
4
  from inference import generate_image
5
 
6
  TASK_TO_INDEX = {"Task 1": 0, "Task 2": 1, "Task 3": 2, "Task 4": 3}
7
+ TASK_OPTIMAL_COORDS = {0: (325, 326), 1: (59, 1126), 2: (47, 102), 3: (497, 933)}
8
 
9
 
10
  def create_marker_overlay(image_path: str, x: int, y: int) -> Image.Image:
 
50
  return generate_image(image_idx, x_norm, y_norm)
51
 
52
 
53
+ def find_optimal_latent(image_idx: int) -> tuple[Image.Image, tuple[int, int], Image.Image]:
54
+ """
55
+ Simulate clicking at the optimal coordinates for the current task
56
+ Returns the marked heatmap, coordinates, and generated output image
57
+ """
58
+ x, y = TASK_OPTIMAL_COORDS[image_idx]
59
+ heatmap_path = f"imgs/heatmap_{image_idx}.png"
60
+ marked_heatmap = create_marker_overlay(heatmap_path, x, y)
61
+ output_img = generate_output_image(image_idx, (x, y))
62
+ return marked_heatmap, (x, y), output_img
63
+
64
+
65
  with gr.Blocks(
66
  css="""
67
  .radio-container {
 
84
  background-color: #f8f9fa !important;
85
  border-radius: 8px !important;
86
  }
87
+ .optimal-button {
88
+ margin-top: 1rem !important;
89
+ margin-bottom: 1rem !important;
90
+ width: 200px !important;
91
+ margin-left: auto !important;
92
+ margin-right: auto !important;
93
+ }
94
  """
95
  ) as demo:
96
  gr.Markdown(
 
150
  show_fullscreen_button=False,
151
  )
152
 
153
+ # Add the Find Optimal Latent button with a container for centering
154
+ with gr.Column():
155
+ optimal_button = gr.Button("Find Optimal Latent", elem_classes="optimal-button")
156
+
157
  # Documentation section
158
  with gr.Column(elem_classes="documentation"):
159
  gr.Markdown(
 
168
  3. **Coordinate Selection**: Click anywhere in the heatmap to specify where in the latent space you want to generate from
169
  4. **Generation**: The model generates a new image based on your selected coordinates
170
 
171
+ The "Find Optimal Latent" button automatically selects pre-determined optimal coordinates for the current task.
 
172
  """
173
  )
174
 
 
209
  outputs=output_image,
210
  )
211
 
212
+ # Add event handler for the optimal latent button
213
+ optimal_button.click(
214
+ fn=find_optimal_latent,
215
+ inputs=[selected_idx],
216
+ outputs=[coord_selector, coords, output_image],
217
+ )
218
+
219
  demo.launch()