File size: 6,777 Bytes
24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 cb40a1c b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 24702c6 b837da3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | """
Microsoft Azure Document Intelligence layout inference.
Uses Azure Document Intelligence (Form Recognizer) API for document analysis.
"""
import asyncio
import os
from azure.ai.formrecognizer import DocumentAnalysisClient
from azure.core.credentials import AzureKeyCredential
from base import BaseInference, create_argument_parser, parse_args_with_extra
CATEGORY_MAP = {
"Title": "heading1",
"SectionHeading": "heading1",
"footnote": "footnote",
"PageHeader": "header",
"PageFooter": "footer",
"Paragraph": "paragraph",
"Subheading": "heading1",
"SectionMarks": "paragraph",
"PageNumber": "paragraph"
}
class MicrosoftInference(BaseInference):
"""Microsoft Azure Document Intelligence layout inference."""
def __init__(
self,
save_path,
input_formats=None,
concurrent_limit=None,
sampling_rate=1.0,
request_timeout=600,
random_seed=None,
group_by_document=False,
file_ext_mapping=None
):
"""Initialize the MicrosoftInference class.
Args:
save_path (str): the json path to save the results
input_formats (list, optional): the supported file formats.
concurrent_limit (int, optional): maximum number of concurrent API requests
sampling_rate (float, optional): fraction of files to process (0.0-1.0)
request_timeout (float, optional): timeout in seconds for API requests
random_seed (int, optional): random seed for reproducible sampling
group_by_document (bool, optional): group per-page results into document-level
file_ext_mapping (str or dict, optional): file extension mapping for grouping
"""
super().__init__(
save_path,
input_formats,
concurrent_limit,
sampling_rate,
request_timeout,
random_seed,
group_by_document,
file_ext_mapping
)
MICROSOFT_API_KEY = os.getenv("MICROSOFT_API_KEY") or ""
MICROSOFT_ENDPOINT = os.getenv("MICROSOFT_ENDPOINT") or ""
if not all([MICROSOFT_API_KEY, MICROSOFT_ENDPOINT]):
raise ValueError("Please set the environment variables for Microsoft")
self.document_analysis_client = DocumentAnalysisClient(
endpoint=MICROSOFT_ENDPOINT,
credential=AzureKeyCredential(MICROSOFT_API_KEY)
)
def post_process(self, data):
"""Post-process Microsoft Document Intelligence API response to standard format."""
processed_dict = {}
for input_key in data.keys():
output_data = data[input_key]
processed_dict[input_key] = {"elements": []}
id_counter = 0
for par_elem in output_data["paragraphs"]:
category = CATEGORY_MAP.get(par_elem["role"], "paragraph")
transcription = par_elem["content"]
coord = [[pt["x"], pt["y"]] for pt in par_elem["bounding_regions"][0]["polygon"]]
xy_coord = [{"x": x, "y": y} for x, y in coord]
data_dict = {
"coordinates": xy_coord,
"category": category,
"id": id_counter,
"content": {"text": transcription, "html": "", "markdown": ""}
}
processed_dict[input_key]["elements"].append(data_dict)
id_counter += 1
html_transcription = ""
for table_elem in output_data["tables"]:
coord = [[pt["x"], pt["y"]] for pt in table_elem["bounding_regions"][0]["polygon"]]
xy_coord = [{"x": x, "y": y} for x, y in coord]
html_transcription += "<table>"
table_matrix = [
["" for _ in range(table_elem["column_count"])]
for _ in range(table_elem["row_count"])
]
for cell in table_elem["cells"]:
row = cell["row_index"]
col = cell["column_index"]
rowspan = cell.get("row_span", 1)
colspan = cell.get("column_span", 1)
content = cell["content"]
for r in range(row, row + rowspan):
for c in range(col, col + colspan):
if r == row and c == col:
table_matrix[r][c] = f"<td rowspan='{rowspan}' colspan='{colspan}'>{content}</td>"
else:
table_matrix[r][c] = None
for row in table_matrix:
html_transcription += "<tr>"
for cell in row:
if cell is not None:
html_transcription += cell
html_transcription += "</tr>"
html_transcription += "</table>"
data_dict = {
"coordinates": xy_coord,
"category": "table",
"id": id_counter,
"content": {"text": "", "html": html_transcription, "markdown": ""}
}
processed_dict[input_key]["elements"].append(data_dict)
id_counter += 1
return self._merge_processed_data(processed_dict)
def _analyze_document(self, filepath):
"""Analyze document using Azure Document Intelligence."""
with open(filepath, "rb") as input_data:
poller = self.document_analysis_client.begin_analyze_document(
"prebuilt-layout", document=input_data
)
result = poller.result()
return result.to_dict()
async def _call_api_async(self, filepath, *args, **kwargs):
"""Make the actual async API call for a file."""
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, self._analyze_document, filepath)
def _call_api_sync(self, filepath, *args, **kwargs):
"""Make the actual sync API call for a file."""
return self._analyze_document(filepath)
if __name__ == "__main__":
parser = create_argument_parser("Microsoft Azure Document Intelligence layout inference")
args = parse_args_with_extra(parser)
microsoft_inference = MicrosoftInference(
args.save_path,
input_formats=args.input_formats,
concurrent_limit=args.concurrent,
sampling_rate=args.sampling_rate,
request_timeout=args.request_timeout,
random_seed=args.random_seed,
group_by_document=args.group_by_document,
file_ext_mapping=args.file_ext_mapping
)
microsoft_inference.infer(args.data_path)
|