Commit
·
c144ca3
1
Parent(s):
e812064
Update app.py
Browse files
app.py
CHANGED
@@ -95,17 +95,17 @@ def app_outputs_by_model(uploaded_pdf, model_id, model, tokenizer, max_length, i
|
|
95 |
num_images = len(images)
|
96 |
|
97 |
if not msg.startswith("Error with the PDF"):
|
98 |
-
|
99 |
# Extraction of image data (text and bounding boxes)
|
100 |
dataset, texts_lines, texts_pars, texts_lines_par, row_indexes, par_boxes, line_boxes, lines_par_boxes = extraction_data_from_image(images)
|
101 |
# prepare our data in the format of the model
|
102 |
-
prepare_inference_features_partial = partial(
|
103 |
encoded_dataset = dataset.map(prepare_inference_features_partial, batched=True, batch_size=64, remove_columns=dataset.column_names)
|
104 |
custom_encoded_dataset = CustomDataset(encoded_dataset, tokenizer)
|
105 |
# Get predictions (token level)
|
106 |
outputs, images_ids_list, chunk_ids, input_ids, bboxes = predictions_token_level(images, custom_encoded_dataset, model_id, model)
|
107 |
-
# Get predictions (
|
108 |
-
probs_bbox, bboxes_list_dict, input_ids_dict_dict, probs_dict_dict, df =
|
109 |
# Get labeled images with lines bounding boxes
|
110 |
images = get_labeled_images(id2label, dataset, images_ids_list, bboxes_list_dict, probs_dict_dict)
|
111 |
|
@@ -145,7 +145,7 @@ def app_outputs_by_model(uploaded_pdf, model_id, model, tokenizer, max_length, i
|
|
145 |
df, df_empty = dict(), pd.DataFrame()
|
146 |
df[0], df[1] = df_empty.to_csv(csv_file, encoding="utf-8", index=False), df_empty.to_csv(csv_file, encoding="utf-8", index=False)
|
147 |
|
148 |
-
return msg, img_files[0],
|
149 |
|
150 |
def app_outputs(uploaded_pdf):
|
151 |
msg_lilt, img_files_lilt, images_lilt, csv_files_lilt, df_lilt = app_outputs_by_model(uploaded_pdf,
|
@@ -157,13 +157,13 @@ def app_outputs(uploaded_pdf):
|
|
157 |
max_length=max_length_layoutxlm, id2label=id2label_layoutxlm, cls_box=cls_box, sep_box=sep_box_layoutxlm)
|
158 |
|
159 |
return msg_lilt, msg_layoutxlm, img_files_lilt, img_files_layoutxlm, images_lilt, images_layoutxlm, csv_files_lilt, csv_files_layoutxlm, df_lilt, df_layoutxlm
|
160 |
-
|
161 |
# gradio APP
|
162 |
-
with gr.Blocks(title="Inference APP for Document Understanding at
|
163 |
gr.HTML("""
|
164 |
-
<div style="font-family:'Times New Roman', 'Serif'; font-size:26pt; font-weight:bold; text-align:center;"><h1>Inference APP for Document Understanding at
|
165 |
-
<div style="margin-top: 40px"><p>(04/01/2023) This Inference APP compares - only on the first PDF page - 2 Document Understanding models finetuned on the dataset <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://huggingface.co/datasets/pierreguillou/DocLayNet-base" target="_blank">DocLayNet base</a> at
|
166 |
-
<div><p>To test these 2 models separately, use their corresponding APP on Hugging Face Spaces: <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://huggingface.co/spaces/pierreguillou/Inference-APP-Document-Understanding-at-
|
167 |
""")
|
168 |
with gr.Row():
|
169 |
pdf_file = gr.File(label="PDF")
|
@@ -197,10 +197,10 @@ with gr.Blocks(title="Inference APP for Document Understanding at line level (v1
|
|
197 |
with gr.Row():
|
198 |
csvboxes = []
|
199 |
with gr.Column():
|
200 |
-
csv = gr.File(visible=True, label=f"LiLT csv file at
|
201 |
csvboxes.append(csv)
|
202 |
with gr.Column():
|
203 |
-
csv = gr.File(visible=True, label=f"LayoutXLM csv file at
|
204 |
csvboxes.append(csv)
|
205 |
with gr.Row():
|
206 |
dfboxes = []
|
|
|
95 |
num_images = len(images)
|
96 |
|
97 |
if not msg.startswith("Error with the PDF"):
|
98 |
+
|
99 |
# Extraction of image data (text and bounding boxes)
|
100 |
dataset, texts_lines, texts_pars, texts_lines_par, row_indexes, par_boxes, line_boxes, lines_par_boxes = extraction_data_from_image(images)
|
101 |
# prepare our data in the format of the model
|
102 |
+
prepare_inference_features_partial = partial(prepare_inference_features_paragraph, tokenizer=tokenizer, max_length=max_length, cls_box=cls_box, sep_box=sep_box)
|
103 |
encoded_dataset = dataset.map(prepare_inference_features_partial, batched=True, batch_size=64, remove_columns=dataset.column_names)
|
104 |
custom_encoded_dataset = CustomDataset(encoded_dataset, tokenizer)
|
105 |
# Get predictions (token level)
|
106 |
outputs, images_ids_list, chunk_ids, input_ids, bboxes = predictions_token_level(images, custom_encoded_dataset, model_id, model)
|
107 |
+
# Get predictions (line level)
|
108 |
+
probs_bbox, bboxes_list_dict, input_ids_dict_dict, probs_dict_dict, df = predictions_paragraph_level(max_length, tokenizer, id2label, dataset, outputs, images_ids_list, chunk_ids, input_ids, bboxes, cls_box, sep_box)
|
109 |
# Get labeled images with lines bounding boxes
|
110 |
images = get_labeled_images(id2label, dataset, images_ids_list, bboxes_list_dict, probs_dict_dict)
|
111 |
|
|
|
145 |
df, df_empty = dict(), pd.DataFrame()
|
146 |
df[0], df[1] = df_empty.to_csv(csv_file, encoding="utf-8", index=False), df_empty.to_csv(csv_file, encoding="utf-8", index=False)
|
147 |
|
148 |
+
return msg, img_files[0], images[0], csv_files[0], df[0]
|
149 |
|
150 |
def app_outputs(uploaded_pdf):
|
151 |
msg_lilt, img_files_lilt, images_lilt, csv_files_lilt, df_lilt = app_outputs_by_model(uploaded_pdf,
|
|
|
157 |
max_length=max_length_layoutxlm, id2label=id2label_layoutxlm, cls_box=cls_box, sep_box=sep_box_layoutxlm)
|
158 |
|
159 |
return msg_lilt, msg_layoutxlm, img_files_lilt, img_files_layoutxlm, images_lilt, images_layoutxlm, csv_files_lilt, csv_files_layoutxlm, df_lilt, df_layoutxlm
|
160 |
+
|
161 |
# gradio APP
|
162 |
+
with gr.Blocks(title="Inference APP for Document Understanding at paragraph level (v1 - LiLT base vs LayoutXLM base)", css=".gradio-container") as demo:
|
163 |
gr.HTML("""
|
164 |
+
<div style="font-family:'Times New Roman', 'Serif'; font-size:26pt; font-weight:bold; text-align:center;"><h1>Inference APP for Document Understanding at paragraph level (v1 - LiLT base vs LayoutXLM base)</h1></div>
|
165 |
+
<div style="margin-top: 40px"><p>(04/01/2023) This Inference APP compares - only on the first PDF page - 2 Document Understanding models finetuned on the dataset <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://huggingface.co/datasets/pierreguillou/DocLayNet-base" target="_blank">DocLayNet base</a> at paragraph level (chunk size of 512 tokens): <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://huggingface.co/pierreguillou/lilt-xlm-roberta-base-finetuned-with-DocLayNet-base-at-paragraphlevel-ml512" target="_blank">LiLT base combined with XLM-RoBERTa base</a> and <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://huggingface.co/pierreguillou/layout-xlm-base-finetuned-with-DocLayNet-base-at-paragraphlevel-ml512" target="_blank">LayoutXLM base combined with XLM-RoBERTa base</a>.</p></div>
|
166 |
+
<div><p>To test these 2 models separately, use their corresponding APP on Hugging Face Spaces: <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://huggingface.co/spaces/pierreguillou/Inference-APP-Document-Understanding-at-paragraphlevel-v1" target="_blank">LiLT base APP (v1 - paragraph level)</a> and <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://huggingface.co/spaces/pierreguillou/Inference-APP-Document-Understanding-at-paragraphlevel-v2" target="_blank">LayoutXLM base APP (v2 - paragraph level)</a>.</p></div><div style="margin-top: 20px"><p>Links to Document Understanding APPs:</p><ul><li>Line level: <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://huggingface.co/spaces/pierreguillou/Inference-APP-Document-Understanding-at-linelevel-v1" target="_blank">v1 (LiLT base)</a> | <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://huggingface.co/spaces/pierreguillou/Inference-APP-Document-Understanding-at-linelevel-v2" target="_blank">v2 (LayoutXLM base)</a></li><li>Paragraph level: <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://huggingface.co/spaces/pierreguillou/Inference-APP-Document-Understanding-at-paragraphlevel-v1" target="_blank">v1 (LiLT base)</a> | <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://huggingface.co/spaces/pierreguillou/Inference-APP-Document-Understanding-at-paragraphlevel-v2" target="_blank">v2 (LayoutXLM base)</a></li></ul></div><div style="margin-top: 20px"><p>More information about the DocLayNet datasets, the finetuning of the model and this APP in the following blog posts:</p><ul><li>(03/31/2023) <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://medium.com/@pierre_guillou/document-ai-inference-app-and-fine-tuning-notebook-for-document-understanding-at-paragraph-level-3507af80573d" target="_blank">Document AI | Inference APP and fine-tuning notebook for Document Understanding at paragraph level with LayoutXLM base</a></li><li>(03/25/2023) <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://medium.com/@pierre_guillou/document-ai-app-to-compare-the-document-understanding-lilt-and-layoutxlm-base-models-at-line-1c53eb481a15" target="_blank">Document AI | APP to compare the Document Understanding LiLT and LayoutXLM (base) models at line level</a></li><li>(03/05/2023) <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://medium.com/@pierre_guillou/document-ai-inference-app-and-fine-tuning-notebook-for-document-understanding-at-line-level-with-b08fdca5f4dc" target="_blank">Document AI | Inference APP and fine-tuning notebook for Document Understanding at line level with LayoutXLM base</a></li><li>(02/14/2023) <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://medium.com/@pierre_guillou/document-ai-inference-app-for-document-understanding-at-line-level-a35bbfa98893" target="_blank">Document AI | Inference APP for Document Understanding at line level</a></li><li>(02/10/2023) <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://medium.com/@pierre_guillou/document-ai-document-understanding-model-at-line-level-with-lilt-tesseract-and-doclaynet-dataset-347107a643b8" target="_blank">Document AI | Document Understanding model at line level with LiLT, Tesseract and DocLayNet dataset</a></li><li>(01/31/2023) <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://medium.com/@pierre_guillou/document-ai-doclaynet-image-viewer-app-3ac54c19956" target="_blank">Document AI | DocLayNet image viewer APP</a></li><li>(01/27/2023) <a style="text-decoration: none; border-bottom: #64b5f6 0.125em solid; color: #64b5f6" href="https://medium.com/@pierre_guillou/document-ai-processing-of-doclaynet-dataset-to-be-used-by-layout-models-of-the-hugging-face-hub-308d8bd81cdb" target="_blank">Document AI | Processing of DocLayNet dataset to be used by layout models of the Hugging Face hub (finetuning, inference)</a></li></ul></div>
|
167 |
""")
|
168 |
with gr.Row():
|
169 |
pdf_file = gr.File(label="PDF")
|
|
|
197 |
with gr.Row():
|
198 |
csvboxes = []
|
199 |
with gr.Column():
|
200 |
+
csv = gr.File(visible=True, label=f"LiLT csv file at paragraph level")
|
201 |
csvboxes.append(csv)
|
202 |
with gr.Column():
|
203 |
+
csv = gr.File(visible=True, label=f"LayoutXLM csv file at paragraph level")
|
204 |
csvboxes.append(csv)
|
205 |
with gr.Row():
|
206 |
dfboxes = []
|