pooyanrg commited on
Commit
24e3d7d
·
1 Parent(s): c27bfaf
Files changed (1) hide show
  1. app.py +37 -28
app.py CHANGED
@@ -9,13 +9,6 @@ from utils.tokenization_clip import SimpleTokenizer as ClipTokenizer
9
  from fastapi.staticfiles import StaticFiles
10
  from fileservice import app
11
 
12
-
13
- html_text = """
14
- <div id="container">
15
- <canvas id="canvas" width="512" height="512"></canvas><img id="canvas-background" style="display:none;"/>
16
- </div>
17
- """
18
-
19
  def image_to_tensor(image_path):
20
  image = Image.open(image_path).convert('RGB')
21
 
@@ -220,8 +213,9 @@ def process_example(image_path_bef, image_path_aft):
220
  return image_bef_grid, image_aft_grid # Reset selected cells and store original image
221
 
222
  def display_image(image_path):
223
- image_grid, _, _ = add_grid_to_image(image_path, 14)
224
- return image_grid, []
 
225
 
226
  with gr.Blocks() as demo:
227
  gr.Markdown("# TAB: Transformer Attention Bottleneck")
@@ -236,32 +230,47 @@ with gr.Blocks() as demo:
236
 
237
  selected_cells_bef = gr.State([])
238
  selected_cells_aft = gr.State([])
 
 
 
 
239
 
240
  with gr.Row():
241
  with gr.Column(scale=1):
242
  # Input components with grid overlay
243
  image_bef = gr.Image(type="filepath", visible=True)
244
  image_aft = gr.Image(type="filepath", visible=True)
 
245
 
246
  predict_btn = gr.Button("Predict")
247
 
248
  with gr.Column(scale=1):
249
 
250
- image_display_with_grid_bef = gr.Image(type="pil", label="Before Image with Grid")
251
- image_display_with_grid_aft = gr.Image(type="pil", label="After Image with Grid")
 
 
 
 
 
 
 
 
 
 
252
 
253
- # Add click event to the displayed image
254
- image_display_with_grid_bef.select(
255
- handle_click,
256
- inputs=[image_display_with_grid_bef, selected_cells_bef, image_bef],
257
- outputs=[image_display_with_grid_bef, selected_cells_bef]
258
- )
259
-
260
- image_display_with_grid_aft.select(
261
- handle_click,
262
- inputs=[image_display_with_grid_aft, selected_cells_aft, image_aft],
263
- outputs=[image_display_with_grid_aft, selected_cells_aft]
264
- )
265
 
266
  with gr.Row():
267
  with gr.Column(scale=1):
@@ -294,7 +303,7 @@ with gr.Blocks() as demo:
294
  selected_info_bef = gr.Textbox(label="Selected patches on before")
295
  selected_info_aft = gr.Textbox(label="Selected patches on after")
296
 
297
- html = gr.HTML(html_text)
298
 
299
  # Connect the predict button to the prediction function
300
  predict_btn.click(
@@ -304,16 +313,16 @@ with gr.Blocks() as demo:
304
  )
305
 
306
  image_bef.change(
307
- fn=None,
308
  inputs=[image_bef],
309
- outputs=[],
310
  _js="(image) => { initializeEditor(); importBackground(image); return []; }",
311
  )
312
 
313
  image_aft.change(
314
- fn=None,
315
  inputs=[image_aft],
316
- outputs=[],
317
  _js="(image) => { initializeEditor(); importBackground(image); return []; }",
318
  )
319
 
 
9
  from fastapi.staticfiles import StaticFiles
10
  from fileservice import app
11
 
 
 
 
 
 
 
 
12
  def image_to_tensor(image_path):
13
  image = Image.open(image_path).convert('RGB')
14
 
 
213
  return image_bef_grid, image_aft_grid # Reset selected cells and store original image
214
 
215
  def display_image(image_path):
216
+ w, h = Image.open(image_path).convert('RGB').size
217
+ return w, h
218
+
219
 
220
  with gr.Blocks() as demo:
221
  gr.Markdown("# TAB: Transformer Attention Bottleneck")
 
230
 
231
  selected_cells_bef = gr.State([])
232
  selected_cells_aft = gr.State([])
233
+
234
+ height = gr.State(value=320)
235
+ width = gr.State(value=480)
236
+
237
 
238
  with gr.Row():
239
  with gr.Column(scale=1):
240
  # Input components with grid overlay
241
  image_bef = gr.Image(type="filepath", visible=True)
242
  image_aft = gr.Image(type="filepath", visible=True)
243
+
244
 
245
  predict_btn = gr.Button("Predict")
246
 
247
  with gr.Column(scale=1):
248
 
249
+ html_text = f"""
250
+ <div id="container">
251
+ <canvas id="canvas" width="{width}" height="{height}"></canvas><img id="canvas-background" style="display:none;"/>
252
+ </div>
253
+ """
254
+
255
+ html_bef = gr.HTML(html_text)
256
+ html_aft = gr.HTML(html_text)
257
+
258
+
259
+ # image_display_with_grid_bef = gr.Image(type="pil", label="Before Image with Grid")
260
+ # image_display_with_grid_aft = gr.Image(type="pil", label="After Image with Grid")
261
 
262
+ # # Add click event to the displayed image
263
+ # image_display_with_grid_bef.select(
264
+ # handle_click,
265
+ # inputs=[image_display_with_grid_bef, selected_cells_bef, image_bef],
266
+ # outputs=[image_display_with_grid_bef, selected_cells_bef]
267
+ # )
268
+
269
+ # image_display_with_grid_aft.select(
270
+ # handle_click,
271
+ # inputs=[image_display_with_grid_aft, selected_cells_aft, image_aft],
272
+ # outputs=[image_display_with_grid_aft, selected_cells_aft]
273
+ # )
274
 
275
  with gr.Row():
276
  with gr.Column(scale=1):
 
303
  selected_info_bef = gr.Textbox(label="Selected patches on before")
304
  selected_info_aft = gr.Textbox(label="Selected patches on after")
305
 
306
+
307
 
308
  # Connect the predict button to the prediction function
309
  predict_btn.click(
 
313
  )
314
 
315
  image_bef.change(
316
+ fn=display_image,
317
  inputs=[image_bef],
318
+ outputs=[width, height],
319
  _js="(image) => { initializeEditor(); importBackground(image); return []; }",
320
  )
321
 
322
  image_aft.change(
323
+ fn=display_image,
324
  inputs=[image_aft],
325
+ outputs=[width, height],
326
  _js="(image) => { initializeEditor(); importBackground(image); return []; }",
327
  )
328