Ankur Goyal commited on
Commit
98e826c
1 Parent(s): 253dc57

Fix plumbing, and keep field around, for now

Browse files
Files changed (1) hide show
  1. app.py +17 -19
app.py CHANGED
@@ -125,14 +125,13 @@ def process_document(document, fields, model, error=None):
125
  gr.update(visible=False),
126
  gr.update(visible=True, value=error) if error is not None else None,
127
  None,
128
- None,
129
  )
130
 
131
 
132
- def process_path(path, model):
133
  error = None
134
  document = None
135
- fields = {**FIELDS}
136
  if path:
137
  try:
138
  document = load_document(path)
@@ -143,8 +142,8 @@ def process_path(path, model):
143
  return process_document(document, fields, model, error)
144
 
145
 
146
- def process_upload(file, model):
147
- return process_path(file.name if file else None, model)
148
 
149
 
150
  colors = ["#64A087", "green", "black"]
@@ -175,9 +174,9 @@ def process_question(
175
  annotate_page(prediction, pages, document)
176
 
177
  field_name = question.rstrip("?")
178
- fields = {**FIELDS, field_name: [question]}
179
- output[field_name] = prediction
180
- table = output_table.values.tolist() + [[field_name, prediction.get("answer")]]
181
  return (
182
  gr.update(visible=True, value=pages),
183
  fields,
@@ -207,9 +206,8 @@ def process_fields(document, fields, model=list(CHECKPOINTS.keys())[0]):
207
  )
208
 
209
 
210
- def load_example_document(img, title, model):
211
  document = None
212
- fields = {**FIELDS}
213
  if img is not None:
214
  if title in QUESTION_FILES:
215
  document = load_document(QUESTION_FILES[title])
@@ -400,10 +398,10 @@ with gr.Blocks(css=CSS) as demo:
400
  lambda _: (
401
  gr.update(visible=False, value=None), # image
402
  None, # document
403
- {**FIELDS}, # fields
404
  gr.update(visible=False, value=None), # output
405
- gr.update(**empty_table(FIELDS)), # output_table
406
- gr.update(visible=False),
407
  None,
408
  None,
409
  None,
@@ -414,10 +412,10 @@ with gr.Blocks(css=CSS) as demo:
414
  outputs=[
415
  image,
416
  document,
417
- fields,
418
  output,
419
- output_table,
420
- img_clear_button,
421
  example_image,
422
  upload,
423
  url,
@@ -438,13 +436,13 @@ with gr.Blocks(css=CSS) as demo:
438
 
439
  upload.change(
440
  fn=process_upload,
441
- inputs=[upload, model],
442
  outputs=submit_outputs,
443
  )
444
 
445
  submit.click(
446
  fn=process_path,
447
- inputs=[url, model],
448
  outputs=submit_outputs,
449
  )
450
 
@@ -468,7 +466,7 @@ with gr.Blocks(css=CSS) as demo:
468
 
469
  example_image.change(
470
  fn=load_example_document,
471
- inputs=[example_image, example_question, model],
472
  outputs=submit_outputs,
473
  )
474
 
 
125
  gr.update(visible=False),
126
  gr.update(visible=True, value=error) if error is not None else None,
127
  None,
128
+ gr.update(**empty_table(fields)),
129
  )
130
 
131
 
132
+ def process_path(path, fields, model):
133
  error = None
134
  document = None
 
135
  if path:
136
  try:
137
  document = load_document(path)
 
142
  return process_document(document, fields, model, error)
143
 
144
 
145
+ def process_upload(file, fields, model):
146
+ return process_path(file.name if file else None, fields, model)
147
 
148
 
149
  colors = ["#64A087", "green", "black"]
 
174
  annotate_page(prediction, pages, document)
175
 
176
  field_name = question.rstrip("?")
177
+ fields = {field_name: [question], **fields}
178
+ output = {field_name: prediction, **output}
179
+ table = [[field_name, prediction.get("answer")]] + output_table.values.tolist()
180
  return (
181
  gr.update(visible=True, value=pages),
182
  fields,
 
206
  )
207
 
208
 
209
+ def load_example_document(img, title, fields, model):
210
  document = None
 
211
  if img is not None:
212
  if title in QUESTION_FILES:
213
  document = load_document(QUESTION_FILES[title])
 
398
  lambda _: (
399
  gr.update(visible=False, value=None), # image
400
  None, # document
401
+ # {**FIELDS}, # fields
402
  gr.update(visible=False, value=None), # output
403
+ # gr.update(**empty_table(FIELDS)), # output_table
404
+ # gr.update(visible=False),
405
  None,
406
  None,
407
  None,
 
412
  outputs=[
413
  image,
414
  document,
415
+ # fields,
416
  output,
417
+ # output_table,
418
+ # img_clear_button,
419
  example_image,
420
  upload,
421
  url,
 
436
 
437
  upload.change(
438
  fn=process_upload,
439
+ inputs=[upload, fields, model],
440
  outputs=submit_outputs,
441
  )
442
 
443
  submit.click(
444
  fn=process_path,
445
+ inputs=[url, fields, model],
446
  outputs=submit_outputs,
447
  )
448
 
 
466
 
467
  example_image.change(
468
  fn=load_example_document,
469
+ inputs=[example_image, example_question, fields, model],
470
  outputs=submit_outputs,
471
  )
472