Spaces:
Build error
Build error
Ankur Goyal
commited on
Commit
·
d703b38
1
Parent(s):
3441721
Latest updates
Browse files
app.py
CHANGED
@@ -99,46 +99,48 @@ FIELDS = {
|
|
99 |
"Payment Terms": ["Payment Terms?"],
|
100 |
}
|
101 |
|
|
|
|
|
|
|
102 |
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
error = None
|
|
|
105 |
if path:
|
106 |
try:
|
107 |
document = load_document(path)
|
108 |
-
return (
|
109 |
-
document,
|
110 |
-
gr.update(visible=True, value=document.preview),
|
111 |
-
gr.update(visible=True),
|
112 |
-
gr.update(visible=False, value=None),
|
113 |
-
gr.update(visible=False, value=None),
|
114 |
-
None,
|
115 |
-
)
|
116 |
except Exception as e:
|
117 |
traceback.print_exc()
|
118 |
error = str(e)
|
119 |
-
return (
|
120 |
-
None,
|
121 |
-
gr.update(visible=False, value=None),
|
122 |
-
gr.update(visible=False),
|
123 |
-
gr.update(visible=False, value=None),
|
124 |
-
gr.update(visible=False, value=None),
|
125 |
-
gr.update(visible=True, value=error) if error is not None else None,
|
126 |
-
None,
|
127 |
-
)
|
128 |
|
|
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
else:
|
134 |
-
return (
|
135 |
-
None,
|
136 |
-
gr.update(visible=False, value=None),
|
137 |
-
gr.update(visible=False),
|
138 |
-
gr.update(visible=False, value=None),
|
139 |
-
gr.update(visible=False, value=None),
|
140 |
-
None,
|
141 |
-
)
|
142 |
|
143 |
|
144 |
colors = ["#64A087", "green", "black"]
|
@@ -212,16 +214,10 @@ def load_example_document(img, title, model):
|
|
212 |
document = load_document(QUESTION_FILES[title])
|
213 |
else:
|
214 |
document = ImageDocument(Image.fromarray(img), ocr_reader=get_ocr_reader())
|
215 |
-
preview, answer, table = process_fields(document, model)
|
216 |
-
return (
|
217 |
-
document,
|
218 |
-
preview,
|
219 |
-
gr.update(visible=True),
|
220 |
-
answer,
|
221 |
-
table,
|
222 |
-
)
|
223 |
else:
|
224 |
-
|
|
|
|
|
225 |
|
226 |
|
227 |
CSS = """
|
@@ -379,6 +375,7 @@ with gr.Blocks(css=CSS) as demo:
|
|
379 |
choices=list(CHECKPOINTS.keys()),
|
380 |
value=list(CHECKPOINTS.keys())[0],
|
381 |
label="Model",
|
|
|
382 |
)
|
383 |
|
384 |
with gr.Row():
|
@@ -388,10 +385,7 @@ with gr.Blocks(css=CSS) as demo:
|
|
388 |
)
|
389 |
with gr.Tabs():
|
390 |
with gr.TabItem("Table"):
|
391 |
-
output_table = gr.Dataframe(
|
392 |
-
headers=["Field", "Value"],
|
393 |
-
value=[[name, None] for name in FIELDS.keys()],
|
394 |
-
)
|
395 |
|
396 |
with gr.TabItem("JSON"):
|
397 |
output = gr.JSON(label="Output", visible=False)
|
@@ -399,10 +393,10 @@ with gr.Blocks(css=CSS) as demo:
|
|
399 |
for cb in [img_clear_button, clear_button]:
|
400 |
cb.click(
|
401 |
lambda _: (
|
402 |
-
gr.update(visible=False, value=None),
|
403 |
-
None,
|
404 |
-
gr.update(visible=False, value=None),
|
405 |
-
gr.update(
|
406 |
gr.update(visible=False),
|
407 |
None,
|
408 |
None,
|
@@ -427,13 +421,14 @@ with gr.Blocks(css=CSS) as demo:
|
|
427 |
|
428 |
upload.change(
|
429 |
fn=process_upload,
|
430 |
-
inputs=[upload],
|
431 |
-
outputs=[document, image, img_clear_button, output, output_table
|
432 |
)
|
|
|
433 |
submit.click(
|
434 |
fn=process_path,
|
435 |
-
inputs=[url],
|
436 |
-
outputs=[document, image, img_clear_button, output, output_table
|
437 |
)
|
438 |
|
439 |
question.submit(
|
@@ -457,7 +452,7 @@ with gr.Blocks(css=CSS) as demo:
|
|
457 |
example_image.change(
|
458 |
fn=load_example_document,
|
459 |
inputs=[example_image, example_question, model],
|
460 |
-
outputs=[document, image, img_clear_button, output, output_table],
|
461 |
)
|
462 |
|
463 |
if __name__ == "__main__":
|
|
|
99 |
"Payment Terms": ["Payment Terms?"],
|
100 |
}
|
101 |
|
102 |
+
EMPTY_TABLE = dict(
|
103 |
+
headers=["Field", "Value"], value=[[name, None] for name in FIELDS.keys()]
|
104 |
+
)
|
105 |
|
106 |
+
|
107 |
+
def process_document(document, model, error=None):
|
108 |
+
if document is not None and error is None:
|
109 |
+
preview, json_output, table = process_fields(document, model)
|
110 |
+
return (
|
111 |
+
document,
|
112 |
+
preview,
|
113 |
+
gr.update(visible=True),
|
114 |
+
gr.update(visible=False, value=None),
|
115 |
+
json_output,
|
116 |
+
table,
|
117 |
+
)
|
118 |
+
else:
|
119 |
+
return (
|
120 |
+
None,
|
121 |
+
None,
|
122 |
+
gr.update(visible=False),
|
123 |
+
gr.update(visible=True, value=error) if error is not None else None,
|
124 |
+
None,
|
125 |
+
None,
|
126 |
+
)
|
127 |
+
|
128 |
+
|
129 |
+
def process_path(path, model):
|
130 |
error = None
|
131 |
+
document = None
|
132 |
if path:
|
133 |
try:
|
134 |
document = load_document(path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
except Exception as e:
|
136 |
traceback.print_exc()
|
137 |
error = str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
+
return process_document(document, model, error)
|
140 |
|
141 |
+
|
142 |
+
def process_upload(file, model):
|
143 |
+
return process_path(file.name if file else None, model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
|
146 |
colors = ["#64A087", "green", "black"]
|
|
|
214 |
document = load_document(QUESTION_FILES[title])
|
215 |
else:
|
216 |
document = ImageDocument(Image.fromarray(img), ocr_reader=get_ocr_reader())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
217 |
else:
|
218 |
+
document = None
|
219 |
+
|
220 |
+
return process_document(document, model)
|
221 |
|
222 |
|
223 |
CSS = """
|
|
|
375 |
choices=list(CHECKPOINTS.keys()),
|
376 |
value=list(CHECKPOINTS.keys())[0],
|
377 |
label="Model",
|
378 |
+
visible=False,
|
379 |
)
|
380 |
|
381 |
with gr.Row():
|
|
|
385 |
)
|
386 |
with gr.Tabs():
|
387 |
with gr.TabItem("Table"):
|
388 |
+
output_table = gr.Dataframe(**EMPTY_TABLE)
|
|
|
|
|
|
|
389 |
|
390 |
with gr.TabItem("JSON"):
|
391 |
output = gr.JSON(label="Output", visible=False)
|
|
|
393 |
for cb in [img_clear_button, clear_button]:
|
394 |
cb.click(
|
395 |
lambda _: (
|
396 |
+
gr.update(visible=False, value=None), # image
|
397 |
+
None, # document
|
398 |
+
gr.update(visible=False, value=None), # output
|
399 |
+
gr.update(**EMPTY_TABLE), # output_table
|
400 |
gr.update(visible=False),
|
401 |
None,
|
402 |
None,
|
|
|
421 |
|
422 |
upload.change(
|
423 |
fn=process_upload,
|
424 |
+
inputs=[upload, model],
|
425 |
+
outputs=[document, image, img_clear_button, url_error, output, output_table],
|
426 |
)
|
427 |
+
|
428 |
submit.click(
|
429 |
fn=process_path,
|
430 |
+
inputs=[url, model],
|
431 |
+
outputs=[document, image, img_clear_button, url_error, output, output_table],
|
432 |
)
|
433 |
|
434 |
question.submit(
|
|
|
452 |
example_image.change(
|
453 |
fn=load_example_document,
|
454 |
inputs=[example_image, example_question, model],
|
455 |
+
outputs=[document, image, img_clear_button, url_error, output, output_table],
|
456 |
)
|
457 |
|
458 |
if __name__ == "__main__":
|