wzzanthony7 commited on
Commit
f280192
·
verified ·
1 Parent(s): de507f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -15
app.py CHANGED
@@ -209,7 +209,7 @@ def generate_textual_description(box_info):
209
  kept_zero_boxes.sort(key = lambda x: x[0])
210
  kept_one_boxes.sort(key = lambda x: x[0])
211
  textual_description = "" #final output
212
- textual_description += "The key elements are interpreted via visual translator. Their coordinates are represented as outlined boxes (top-left, bottom-right). "
213
  #print(f"The key elements are interpreted via visual translator. Their coordinates are represented as outlined boxes (top-left, bottom-right)")
214
  if len(kept_zero_boxes) >= 1:
215
  left_most_zero_cor = kept_zero_boxes[0]
@@ -246,7 +246,8 @@ def drawWithAllBox_info(pil_image, box_info):
246
 
247
  def process_image(image):
248
  if image is None:
249
- return None, "", "", None
 
250
  pil_image = image.copy() if hasattr(image, 'copy') else Image.fromarray(image)
251
  roboflow_ret = RoboFlowGetOutlineBoxesPIL(pil_image)
252
  all_box_info = parse_roboflow_result(roboflow_ret, classes)
@@ -279,15 +280,13 @@ with gr.Blocks() as demo:
279
  text_out = gr.Textbox(label="Textual Description", lines=8)
280
  json_state = gr.State("")
281
  download_btn = gr.DownloadButton(
282
- label="Download Box Info as JSON"
 
283
  )
284
 
285
  # --- Backend Functions ---
286
- def _process(image):
287
- boxed_img, textual, json_str = process_image(image)
288
- return boxed_img, textual, json_str
289
-
290
- def create_and_download_json(json_str):
291
  if not json_str:
292
  return None
293
  with tempfile.NamedTemporaryFile(
@@ -297,6 +296,17 @@ with gr.Blocks() as demo:
297
  f.write(json_str)
298
  return f.name
299
 
 
 
 
 
 
 
 
 
 
 
 
300
  def check_token(token):
301
  # Securely check if the token is correct
302
  if ACCESS_TOKEN and token == ACCESS_TOKEN:
@@ -312,15 +322,13 @@ with gr.Blocks() as demo:
312
  )
313
 
314
  run_btn.click(
315
- _process,
316
  inputs=img_input,
317
- outputs=[img_out, text_out, json_state]
 
318
  )
319
 
320
- download_btn.click(
321
- create_and_download_json,
322
- inputs=json_state,
323
- outputs=download_btn
324
- )
325
 
326
  demo.launch()
 
209
  kept_zero_boxes.sort(key = lambda x: x[0])
210
  kept_one_boxes.sort(key = lambda x: x[0])
211
  textual_description = "" #final output
212
+ textual_description += "The key elements are interpreted via visual translator. Their coordinates are represented as outlined boxes (top-left, bottom-right)."
213
  #print(f"The key elements are interpreted via visual translator. Their coordinates are represented as outlined boxes (top-left, bottom-right)")
214
  if len(kept_zero_boxes) >= 1:
215
  left_most_zero_cor = kept_zero_boxes[0]
 
246
 
247
  def process_image(image):
248
  if image is None:
249
+ # Ensure we always return 3 values to prevent errors.
250
+ return None, "", ""
251
  pil_image = image.copy() if hasattr(image, 'copy') else Image.fromarray(image)
252
  roboflow_ret = RoboFlowGetOutlineBoxesPIL(pil_image)
253
  all_box_info = parse_roboflow_result(roboflow_ret, classes)
 
280
  text_out = gr.Textbox(label="Textual Description", lines=8)
281
  json_state = gr.State("")
282
  download_btn = gr.DownloadButton(
283
+ label="Download Box Info as JSON",
284
+ interactive=False # Start as disabled.
285
  )
286
 
287
  # --- Backend Functions ---
288
+ def create_json_file(json_str):
289
+ """Creates a temp file with JSON content and returns its path."""
 
 
 
290
  if not json_str:
291
  return None
292
  with tempfile.NamedTemporaryFile(
 
296
  f.write(json_str)
297
  return f.name
298
 
299
+ def _process_and_prepare_download(image):
300
+ """Processes the image, creates the JSON file, and updates the UI."""
301
+ boxed_img, textual, json_str = process_image(image)
302
+ filepath = create_json_file(json_str)
303
+
304
+ # Create an update object for the download button.
305
+ # This sets its value to the file path and makes it clickable.
306
+ download_update = gr.DownloadButton.update(value=filepath, interactive=True)
307
+
308
+ return boxed_img, textual, json_str, download_update
309
+
310
  def check_token(token):
311
  # Securely check if the token is correct
312
  if ACCESS_TOKEN and token == ACCESS_TOKEN:
 
322
  )
323
 
324
  run_btn.click(
325
+ _process_and_prepare_download,
326
  inputs=img_input,
327
+ # The output now includes the download button itself.
328
+ outputs=[img_out, text_out, json_state, download_btn]
329
  )
330
 
331
+ # The download_btn no longer needs its own click event.
332
+ # Clicking it after it has a 'value' will trigger the download.
 
 
 
333
 
334
  demo.launch()