Spaces:
Sleeping
Sleeping
Update ai_mapping.py
Browse files- ai_mapping.py +3 -8
ai_mapping.py
CHANGED
|
@@ -19,7 +19,8 @@ def extract_key_values_with_layoutlm(page_data: list, pdf_path: str) -> Dict[str
|
|
| 19 |
"""
|
| 20 |
Extract key-value pairs from PDF text using LayoutLMv3-base or fallback to regex.
|
| 21 |
Args:
|
| 22 |
-
page_data (list): List of dictionaries with 'text' (str), 'words' (list of str),
|
|
|
|
| 23 |
pdf_path (str): Path to the PDF file.
|
| 24 |
Returns:
|
| 25 |
dict: Key-value pairs extracted from the document.
|
|
@@ -46,7 +47,7 @@ def extract_key_values_with_layoutlm(page_data: list, pdf_path: str) -> Dict[str
|
|
| 46 |
pix.save(img_path)
|
| 47 |
image = Image.open(img_path).convert("RGB")
|
| 48 |
|
| 49 |
-
# Tokenize words with corresponding bounding boxes
|
| 50 |
words = page_info["words"]
|
| 51 |
bboxes = page_info["bbox"]
|
| 52 |
encoding = tokenizer(
|
|
@@ -65,12 +66,6 @@ def extract_key_values_with_layoutlm(page_data: list, pdf_path: str) -> Dict[str
|
|
| 65 |
image_encoding = feature_extractor(image, return_tensors="pt")
|
| 66 |
pixel_values = image_encoding["pixel_values"]
|
| 67 |
|
| 68 |
-
# Ensure bbox length matches input_ids
|
| 69 |
-
if len(bbox[0]) < len(input_ids[0]):
|
| 70 |
-
bbox = torch.cat([bbox, torch.zeros((1, len(input_ids[0]) - len(bbox[0]), 4), dtype=torch.int64)])
|
| 71 |
-
elif len(bbox[0]) > len(input_ids[0]):
|
| 72 |
-
bbox = bbox[:, :len(input_ids[0])]
|
| 73 |
-
|
| 74 |
# Pass inputs to the model
|
| 75 |
with torch.no_grad():
|
| 76 |
outputs = model(
|
|
|
|
| 19 |
"""
|
| 20 |
Extract key-value pairs from PDF text using LayoutLMv3-base or fallback to regex.
|
| 21 |
Args:
|
| 22 |
+
page_data (list): List of dictionaries with 'text' (str), 'words' (list of str),
|
| 23 |
+
'bbox' (list of [x0, y0, x1, y1] normalized to 0-1000), and 'image_dims' ([width, height]) per page.
|
| 24 |
pdf_path (str): Path to the PDF file.
|
| 25 |
Returns:
|
| 26 |
dict: Key-value pairs extracted from the document.
|
|
|
|
| 47 |
pix.save(img_path)
|
| 48 |
image = Image.open(img_path).convert("RGB")
|
| 49 |
|
| 50 |
+
# Tokenize words with corresponding normalized bounding boxes
|
| 51 |
words = page_info["words"]
|
| 52 |
bboxes = page_info["bbox"]
|
| 53 |
encoding = tokenizer(
|
|
|
|
| 66 |
image_encoding = feature_extractor(image, return_tensors="pt")
|
| 67 |
pixel_values = image_encoding["pixel_values"]
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# Pass inputs to the model
|
| 70 |
with torch.no_grad():
|
| 71 |
outputs = model(
|