Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -102,33 +102,20 @@ def draw_boxes(image, boxes, predictions):
|
|
| 102 |
def process_image(image):
|
| 103 |
width, height = image.size
|
| 104 |
|
| 105 |
-
#
|
| 106 |
-
encoding = processor(
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
offset_mapping = encoding.pop("offset_mapping")
|
| 110 |
-
|
| 111 |
-
# forward pass
|
| 112 |
outputs = model(**encoding)
|
| 113 |
-
|
| 114 |
-
# get predictions
|
| 115 |
predictions = outputs.logits.argmax(-1).squeeze().tolist()
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
]
|
| 123 |
-
true_boxes = [
|
| 124 |
-
unnormalize_box(box, width, height)
|
| 125 |
-
for idx, box in enumerate(token_boxes)
|
| 126 |
-
if not is_subword[idx]
|
| 127 |
-
]
|
| 128 |
-
|
| 129 |
-
draw_boxes(image, true_boxes, true_predictions)
|
| 130 |
|
| 131 |
-
|
| 132 |
|
| 133 |
|
| 134 |
title = "OCR Document Parser : Information Extraction - Fine Tuned LiLT Language-independent Layout Transformer Model"
|
|
|
|
| 102 |
def process_image(image):
|
| 103 |
width, height = image.size
|
| 104 |
|
| 105 |
+
# create model input
|
| 106 |
+
encoding = processor(image, return_tensors="pt")
|
| 107 |
+
del encoding["pixel_values"]
|
| 108 |
+
# run inference
|
|
|
|
|
|
|
|
|
|
| 109 |
outputs = model(**encoding)
|
|
|
|
|
|
|
| 110 |
predictions = outputs.logits.argmax(-1).squeeze().tolist()
|
| 111 |
+
# get labels
|
| 112 |
+
labels = [model.config.id2label[prediction] for prediction in predictions]
|
| 113 |
+
if output_image:
|
| 114 |
+
return draw_boxes(image, encoding["bbox"][0], labels)
|
| 115 |
+
else:
|
| 116 |
+
return labels
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
|
| 119 |
|
| 120 |
|
| 121 |
title = "OCR Document Parser : Information Extraction - Fine Tuned LiLT Language-independent Layout Transformer Model"
|