Luis J Camargo commited on
Commit
3141d0f
Β·
1 Parent(s): 7f05360

feat: Add a JSON output tab to display raw inference results for both Document and Visual Language tasks.

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -219,15 +219,16 @@ def run_inference(img_path, task_type="ocr", progress=gr.Progress()):
219
  logger.info(f"Processing file {fname}...")
220
  fpath = os.path.join(run_output_dir, fname)
221
  if fname.endswith(".md"):
 
222
  with open(fpath, 'r', encoding='utf-8') as f:
223
  content = f.read()
 
224
  if content not in md_content:
225
  md_content += content + "\n\n"
226
  elif fname.endswith(".json"):
227
  with open(fpath, 'r', encoding='utf-8') as f:
228
  content = f.read()
229
- if content not in json_content:
230
- json_content += content + "\n\n"
231
  elif fname.endswith((".png", ".jpg", ".jpeg")) and ("res" in fname or "vis" in fname):
232
  vis_src = image_to_base64_data_url(fpath)
233
  new_vis = f'<div style="margin-bottom:20px; border: 2px solid #10b981; border-radius: 12px; overflow: hidden; background:white;">'
@@ -303,20 +304,22 @@ with gr.Blocks() as demo:
303
  vis_image_doc = gr.HTML('<div style="text-align:center; color:#94a3b8; padding: 50px;">Results will appear here.</div>')
304
  with gr.Tab("πŸ“œ Raw Source"):
305
  md_raw_doc = gr.Code(language="markdown")
 
 
306
 
307
  def parse_doc_wrapper(fp, ch, uw, progress=gr.Progress()):
308
  if not fp:
309
- yield "⚠️ Please upload an image.", "", ""
310
  return
311
  # Initial yield to force loading indicators on all tabs
312
- yield "βŒ› Initializing...", gr.update(value="<p>βŒ› Processing...</p>"), "βŒ› Initializing..."
313
- for res_preview, res_raw, res_vis, _ in run_inference(fp, task_type="Document", progress=progress):
314
- yield res_preview, res_vis, res_raw
315
 
316
  btn_parse.click(
317
  parse_doc_wrapper,
318
  [file_doc, chart_switch, unwarp_switch],
319
- [md_preview_doc, vis_image_doc, md_raw_doc],
320
  show_progress="full"
321
  )
322
 
@@ -336,19 +339,22 @@ with gr.Blocks() as demo:
336
  md_preview_vl = gr.Markdown(latex_delimiters=LATEX_DELIMS, elem_classes="output-box")
337
  with gr.Tab("πŸ“œ Source"):
338
  md_raw_vl = gr.Code(language="markdown")
 
 
339
 
340
  def run_vl_wrapper(fp, prompt, progress=gr.Progress()):
341
  if not fp:
342
- yield "⚠️ Please upload an image.", ""
343
  return
344
- for res_preview, res_raw, _, _ in run_inference(fp, task_type=prompt, progress=progress):
345
- yield res_preview, res_raw
 
346
 
347
  for btn, prompt in [(btn_ocr, "Text"), (btn_formula, "Formula"), (btn_table, "Table")]:
348
  btn.click(
349
  run_vl_wrapper,
350
  [file_vl, gr.State(prompt)],
351
- [md_preview_vl, md_raw_vl],
352
  show_progress="full"
353
  )
354
 
 
219
  logger.info(f"Processing file {fname}...")
220
  fpath = os.path.join(run_output_dir, fname)
221
  if fname.endswith(".md"):
222
+ logger.info(f"Processing MD file {fname}...")
223
  with open(fpath, 'r', encoding='utf-8') as f:
224
  content = f.read()
225
+ logger.info(f"MD content: {content}")
226
  if content not in md_content:
227
  md_content += content + "\n\n"
228
  elif fname.endswith(".json"):
229
  with open(fpath, 'r', encoding='utf-8') as f:
230
  content = f.read()
231
+ json_content += content + "\n\n"
 
232
  elif fname.endswith((".png", ".jpg", ".jpeg")) and ("res" in fname or "vis" in fname):
233
  vis_src = image_to_base64_data_url(fpath)
234
  new_vis = f'<div style="margin-bottom:20px; border: 2px solid #10b981; border-radius: 12px; overflow: hidden; background:white;">'
 
304
  vis_image_doc = gr.HTML('<div style="text-align:center; color:#94a3b8; padding: 50px;">Results will appear here.</div>')
305
  with gr.Tab("πŸ“œ Raw Source"):
306
  md_raw_doc = gr.Code(language="markdown")
307
+ with gr.Tab("πŸ’Ύ JSON Feed"):
308
+ json_doc = gr.Code(language="json")
309
 
310
  def parse_doc_wrapper(fp, ch, uw, progress=gr.Progress()):
311
  if not fp:
312
+ yield "⚠️ Please upload an image.", "", "", ""
313
  return
314
  # Initial yield to force loading indicators on all tabs
315
+ yield "βŒ› Initializing...", gr.update(value="<p>βŒ› Processing...</p>"), "βŒ› Initializing...", "{}"
316
+ for res_preview, res_raw, res_vis, res_json in run_inference(fp, task_type="Document", progress=progress):
317
+ yield res_preview, res_vis, res_raw, res_json
318
 
319
  btn_parse.click(
320
  parse_doc_wrapper,
321
  [file_doc, chart_switch, unwarp_switch],
322
+ [md_preview_doc, vis_image_doc, md_raw_doc, json_doc],
323
  show_progress="full"
324
  )
325
 
 
339
  md_preview_vl = gr.Markdown(latex_delimiters=LATEX_DELIMS, elem_classes="output-box")
340
  with gr.Tab("πŸ“œ Source"):
341
  md_raw_vl = gr.Code(language="markdown")
342
+ with gr.Tab("πŸ’Ύ JSON Feed"):
343
+ json_vl = gr.Code(language="json")
344
 
345
  def run_vl_wrapper(fp, prompt, progress=gr.Progress()):
346
  if not fp:
347
+ yield "⚠️ Please upload an image.", "", ""
348
  return
349
+ yield "βŒ› Initializing...", "βŒ› Initializing...", "{}"
350
+ for res_preview, res_raw, _, res_json in run_inference(fp, task_type=prompt, progress=progress):
351
+ yield res_preview, res_raw, res_json
352
 
353
  for btn, prompt in [(btn_ocr, "Text"), (btn_formula, "Formula"), (btn_table, "Table")]:
354
  btn.click(
355
  run_vl_wrapper,
356
  [file_vl, gr.State(prompt)],
357
+ [md_preview_vl, md_raw_vl, json_vl],
358
  show_progress="full"
359
  )
360