clement-bonnet commited on
Commit
fd1210f
·
1 Parent(s): 4cac868

feat: some documentation on the app

Browse files
Files changed (1) hide show
  1. app.py +43 -26
app.py CHANGED
@@ -10,21 +10,13 @@ def create_marker_overlay(image_path: str, x: int, y: int) -> Image.Image:
10
  """
11
  Creates an image with a marker at the specified coordinates
12
  """
13
- # Load the base image
14
  base_image = Image.open(image_path)
15
-
16
- # Create a copy to draw on
17
  marked_image = base_image.copy()
18
  draw = ImageDraw.Draw(marked_image)
19
-
20
- # Define marker properties
21
  marker_size = 10
22
  marker_color = "red"
23
-
24
- # Draw marker
25
  draw.line([x - marker_size, y, x + marker_size, y], fill=marker_color, width=2)
26
  draw.line([x, y - marker_size, x, y + marker_size], fill=marker_color, width=2)
27
-
28
  return marked_image
29
 
30
 
@@ -44,16 +36,10 @@ def process_coord_click(image_idx: int, evt: gr.SelectData) -> tuple[Image.Image
44
  Returns both the generated image and the marked coordinate selector
45
  """
46
  x, y = evt.index[0], evt.index[1]
47
- # Create normalized coordinates for generation
48
  x_norm, y_norm = x / 1155, y / 1155
49
-
50
- # Generate the output image
51
  generated_image = generate_image(image_idx, x_norm, y_norm)
52
-
53
- # Create marked coordinate selector
54
  heatmap_path = f"imgs/heatmap_{image_idx}.png"
55
  marked_selector = create_marker_overlay(heatmap_path, x, y)
56
-
57
  return generated_image, marked_selector
58
 
59
 
@@ -73,6 +59,12 @@ with gr.Blocks(
73
  height: 100% !important;
74
  object-fit: contain !important;
75
  }
 
 
 
 
 
 
76
  """
77
  ) as demo:
78
  gr.Markdown(
@@ -83,21 +75,21 @@ with gr.Blocks(
83
  )
84
 
85
  with gr.Row():
86
- # Left column: Radio selection, reference image, and output
87
  with gr.Column(scale=1):
88
- # State variable to track selected image index
89
  selected_idx = gr.State(value=0)
90
 
91
- # Radio buttons with container class
92
  with gr.Column(elem_classes="radio-container"):
93
  task_select = gr.Radio(
94
  choices=["Task 1", "Task 2", "Task 3", "Task 4"],
95
  value="Task 1",
96
  label="Select Task",
 
97
  interactive=True,
98
  )
99
 
100
- # Reference image component that updates based on selection
101
  reference_image = gr.Image(
102
  value="imgs/pattern_0.png",
103
  show_label=False,
@@ -108,9 +100,9 @@ with gr.Blocks(
108
  show_fullscreen_button=False,
109
  )
110
 
111
- # Generated image output moved below reference image
112
  output_image = gr.Image(
113
- label="Generated Output",
114
  height=300,
115
  width=450,
116
  show_download_button=False,
@@ -118,14 +110,14 @@ with gr.Blocks(
118
  interactive=False,
119
  )
120
 
121
- # Right column: Larger coordinate selector
122
  with gr.Column(scale=1):
123
- # Coordinate selector with container class for proper scaling
 
124
  with gr.Column(elem_classes="coordinate-container"):
125
  coord_selector = gr.Image(
126
  value="imgs/heatmap_0.png",
127
- label="Click to select (x, y) coordinates in the latent space",
128
- show_label=True,
129
  interactive=False,
130
  sources=[],
131
  container=True,
@@ -133,14 +125,39 @@ with gr.Blocks(
133
  show_fullscreen_button=False,
134
  )
135
 
136
- # Handle radio button selection
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  task_select.change(
138
  fn=lambda x: update_reference_image(TASK_TO_INDEX[x]),
139
  inputs=[task_select],
140
  outputs=[reference_image, selected_idx, coord_selector],
141
  )
142
 
143
- # Handle coordinate selection - now updates both output image and coord_selector
144
  coord_selector.select(
145
  process_coord_click,
146
  inputs=[selected_idx],
 
10
  """
11
  Creates an image with a marker at the specified coordinates
12
  """
 
13
  base_image = Image.open(image_path)
 
 
14
  marked_image = base_image.copy()
15
  draw = ImageDraw.Draw(marked_image)
 
 
16
  marker_size = 10
17
  marker_color = "red"
 
 
18
  draw.line([x - marker_size, y, x + marker_size, y], fill=marker_color, width=2)
19
  draw.line([x, y - marker_size, x, y + marker_size], fill=marker_color, width=2)
 
20
  return marked_image
21
 
22
 
 
36
  Returns both the generated image and the marked coordinate selector
37
  """
38
  x, y = evt.index[0], evt.index[1]
 
39
  x_norm, y_norm = x / 1155, y / 1155
 
 
40
  generated_image = generate_image(image_idx, x_norm, y_norm)
 
 
41
  heatmap_path = f"imgs/heatmap_{image_idx}.png"
42
  marked_selector = create_marker_overlay(heatmap_path, x, y)
 
43
  return generated_image, marked_selector
44
 
45
 
 
59
  height: 100% !important;
60
  object-fit: contain !important;
61
  }
62
+ .documentation {
63
+ margin-top: 2rem !important;
64
+ padding: 1rem !important;
65
+ background-color: #f8f9fa !important;
66
+ border-radius: 8px !important;
67
+ }
68
  """
69
  ) as demo:
70
  gr.Markdown(
 
75
  )
76
 
77
  with gr.Row():
78
+ # Left column
79
  with gr.Column(scale=1):
 
80
  selected_idx = gr.State(value=0)
81
 
82
+ # gr.Markdown("### Select Task")
83
  with gr.Column(elem_classes="radio-container"):
84
  task_select = gr.Radio(
85
  choices=["Task 1", "Task 2", "Task 3", "Task 4"],
86
  value="Task 1",
87
  label="Select Task",
88
+ # show_label=False,
89
  interactive=True,
90
  )
91
 
92
+ gr.Markdown("### Reference Pattern")
93
  reference_image = gr.Image(
94
  value="imgs/pattern_0.png",
95
  show_label=False,
 
100
  show_fullscreen_button=False,
101
  )
102
 
103
+ gr.Markdown("### Generated Output")
104
  output_image = gr.Image(
105
+ show_label=False,
106
  height=300,
107
  width=450,
108
  show_download_button=False,
 
110
  interactive=False,
111
  )
112
 
113
+ # Right column
114
  with gr.Column(scale=1):
115
+ gr.Markdown("### Coordinate Selector")
116
+ gr.Markdown("Click anywhere in the image below to select (x, y) coordinates in the latent space")
117
  with gr.Column(elem_classes="coordinate-container"):
118
  coord_selector = gr.Image(
119
  value="imgs/heatmap_0.png",
120
+ show_label=False,
 
121
  interactive=False,
122
  sources=[],
123
  container=True,
 
125
  show_fullscreen_button=False,
126
  )
127
 
128
+ # Documentation section
129
+ with gr.Column(elem_classes="documentation"):
130
+ gr.Markdown(
131
+ """
132
+ ## Method Documentation
133
+
134
+ ### How It Works
135
+ This interactive demo showcases our novel image generation method that uses coordinate-based control. The process works as follows:
136
+
137
+ 1. **Task Selection**: Choose one of four different pattern generation tasks
138
+ 2. **Reference Pattern**: View the target pattern for the selected task
139
+ 3. **Coordinate Selection**: Click anywhere in the heatmap to specify where in the latent space you want to generate from
140
+ 4. **Generation**: The model generates a new image based on your selected coordinates
141
+
142
+ ### Sample Results
143
+ Here are some example outputs from our method:
144
+
145
+ ![LPN Diagram](imgs/lpn_diagram.png)
146
+
147
+ ### Technical Details
148
+ Our approach uses a novel coordinate-conditioning mechanism that allows precise control over the generated patterns. The heatmap visualization shows the distribution of pattern characteristics across the latent space.
149
+
150
+ For more information, please refer to our [paper](https://arxiv.org/pdf/2411.08706) or GitHub [repository](https://github.com/clement-bonnet/lpn).
151
+ """
152
+ )
153
+
154
+ # Event handlers
155
  task_select.change(
156
  fn=lambda x: update_reference_image(TASK_TO_INDEX[x]),
157
  inputs=[task_select],
158
  outputs=[reference_image, selected_idx, coord_selector],
159
  )
160
 
 
161
  coord_selector.select(
162
  process_coord_click,
163
  inputs=[selected_idx],