Ankur Goyal commited on
Commit
47c4130
1 Parent(s): 2af0878

Time to debug

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +79 -63
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🦉
4
  colorFrom: purple
5
  colorTo: purple
6
  sdk: gradio
7
- sdk_version: 3.1.7
8
  app_file: app.py
9
  pinned: true
10
  ---
 
4
  colorFrom: purple
5
  colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 3.2
8
  app_file: app.py
9
  pinned: true
10
  ---
app.py CHANGED
@@ -179,7 +179,9 @@ def process_question(question, document, model=list(CHECKPOINTS.keys())[0]):
179
 
180
  def process_fields(document, model=list(CHECKPOINTS.keys())[0]):
181
  pages = [x.copy().convert("RGB") for x in document.preview]
 
182
  ret = {}
 
183
 
184
  for (field_name, questions) in FIELDS.items():
185
  answers = [run_pipeline(model, q, document, top_k=1) for q in questions]
@@ -187,9 +189,12 @@ def process_fields(document, model=list(CHECKPOINTS.keys())[0]):
187
  top = answers[0]
188
  annotate_page(top, pages, document)
189
  ret[field_name] = top
 
 
190
  return (
191
  gr.update(visible=True, value=pages),
192
  gr.update(visible=True, value=ret),
 
193
  )
194
 
195
 
@@ -200,10 +205,16 @@ def load_example_document(img, title, model):
200
  document = load_document(QUESTION_FILES[title])
201
  else:
202
  document = ImageDocument(Image.fromarray(img), ocr_reader=get_ocr_reader())
203
- preview, answer = process_fields(document, model)
204
- return document, preview, gr.update(visible=True), answer
 
 
 
 
 
 
205
  else:
206
- return None, None, gr.update(visible=False), None
207
 
208
 
209
  CSS = """
@@ -369,73 +380,78 @@ with gr.Blocks(css=CSS) as demo:
369
  "Submit", variant="primary", elem_id="submit-button"
370
  )
371
  with gr.Column():
372
- output_text = gr.Textbox(
373
- label="Top Answer", visible=False, elem_id="answer"
 
 
374
  )
375
- output = gr.JSON(label="Output", visible=False)
376
 
377
- for cb in [img_clear_button, clear_button]:
378
- cb.click(
379
- lambda _: (
380
- gr.update(visible=False, value=None),
381
- None,
382
- gr.update(visible=False, value=None),
383
- gr.update(visible=False, value=None),
384
- gr.update(visible=False),
385
- None,
386
- None,
387
- None,
388
- gr.update(visible=False, value=None),
389
- None,
390
- ),
391
- inputs=clear_button,
392
- outputs=[
393
- image,
394
- document,
395
- output,
396
- output_text,
397
- img_clear_button,
398
- example_image,
399
- upload,
400
- url,
401
- url_error,
402
- question,
403
- ],
404
- )
405
-
406
- upload.change(
407
- fn=process_upload,
408
- inputs=[upload],
409
- outputs=[document, image, img_clear_button, output, output_text, url_error],
410
- )
411
- submit.click(
412
- fn=process_path,
413
- inputs=[url],
414
- outputs=[document, image, img_clear_button, output, output_text, url_error],
415
- )
416
-
417
- question.submit(
418
- fn=process_question,
419
- inputs=[question, document, model],
420
- outputs=[image, output, output_text],
421
- )
422
-
423
- submit_button.click(
424
- process_question,
425
- inputs=[question, document, model],
426
- outputs=[image, output, output_text],
427
- )
428
 
429
- model.change(
430
- process_question,
431
- inputs=[question, document, model],
432
- outputs=[image, output, output_text],
433
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
434
 
435
  example_image.change(
436
  fn=load_example_document,
437
  inputs=[example_image, example_question, model],
438
- outputs=[document, image, img_clear_button, output],
439
  )
440
 
441
  if __name__ == "__main__":
 
179
 
180
  def process_fields(document, model=list(CHECKPOINTS.keys())[0]):
181
  pages = [x.copy().convert("RGB") for x in document.preview]
182
+
183
  ret = {}
184
+ table = []
185
 
186
  for (field_name, questions) in FIELDS.items():
187
  answers = [run_pipeline(model, q, document, top_k=1) for q in questions]
 
189
  top = answers[0]
190
  annotate_page(top, pages, document)
191
  ret[field_name] = top
192
+ table.append([field_name, top.get("answer") if top is not None else None])
193
+
194
  return (
195
  gr.update(visible=True, value=pages),
196
  gr.update(visible=True, value=ret),
197
+ gr.update(visible=True, value=table),
198
  )
199
 
200
 
 
205
  document = load_document(QUESTION_FILES[title])
206
  else:
207
  document = ImageDocument(Image.fromarray(img), ocr_reader=get_ocr_reader())
208
+ preview, answer, table = process_fields(document, model)
209
+ return (
210
+ document,
211
+ preview,
212
+ gr.update(visible=True),
213
+ answer,
214
+ table,
215
+ )
216
  else:
217
+ return None, None, gr.update(visible=False), None, None
218
 
219
 
220
  CSS = """
 
380
  "Submit", variant="primary", elem_id="submit-button"
381
  )
382
  with gr.Column():
383
+ # with gr.Tab("Table"):
384
+ output_table = gr.Dataframe(
385
+ headers=["Field", "Value"],
386
+ value=[[name, None] for name in FIELDS.keys()],
387
  )
 
388
 
389
+ # with gr.Tab("JSON"):
390
+ # XXX RENAME
391
+ output = gr.JSON(label="Output", visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
392
 
393
+ # for cb in [img_clear_button, clear_button]:
394
+ # cb.click(
395
+ # lambda _: (
396
+ # gr.update(visible=False, value=None),
397
+ # None,
398
+ # gr.update(visible=False, value=None),
399
+ # gr.update(value=None),
400
+ # gr.update(visible=False),
401
+ # None,
402
+ # None,
403
+ # None,
404
+ # gr.update(visible=False, value=None),
405
+ # None,
406
+ # ),
407
+ # inputs=clear_button,
408
+ # outputs=[
409
+ # image,
410
+ # document,
411
+ # output,
412
+ # output_table,
413
+ # img_clear_button,
414
+ # example_image,
415
+ # upload,
416
+ # url,
417
+ # url_error,
418
+ # question,
419
+ # ],
420
+ # )
421
+ #
422
+ # upload.change(
423
+ # fn=process_upload,
424
+ # inputs=[upload],
425
+ # outputs=[document, image, img_clear_button, output, output_table, url_error],
426
+ # )
427
+ # submit.click(
428
+ # fn=process_path,
429
+ # inputs=[url],
430
+ # outputs=[document, image, img_clear_button, output, output_table, url_error],
431
+ # )
432
+ #
433
+ # question.submit(
434
+ # fn=process_question,
435
+ # inputs=[question, document, model],
436
+ # outputs=[image, output, output_table],
437
+ # )
438
+ #
439
+ # submit_button.click(
440
+ # process_question,
441
+ # inputs=[question, document, model],
442
+ # outputs=[image, output, output_table],
443
+ # )
444
+ #
445
+ # model.change(
446
+ # process_question,
447
+ # inputs=[question, document, model],
448
+ # outputs=[image, output, output_table],
449
+ # )
450
 
451
  example_image.change(
452
  fn=load_example_document,
453
  inputs=[example_image, example_question, model],
454
+ outputs=[document, image, img_clear_button, output, output_table],
455
  )
456
 
457
  if __name__ == "__main__":