Datasets:

ArXiv:
License:
File size: 9,989 Bytes
b837da3
24702c6
b837da3
 
24702c6
b837da3
24702c6
b837da3
24702c6
b837da3
24702c6
b837da3
24702c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b837da3
24702c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d585e51
b837da3
24702c6
 
 
 
 
 
b837da3
 
24702c6
 
 
 
b837da3
24702c6
 
 
 
 
 
 
 
 
 
 
b837da3
 
24702c6
 
 
 
 
 
 
 
 
 
 
 
d585e51
24702c6
 
 
 
b837da3
24702c6
 
 
 
 
 
 
 
 
b837da3
 
24702c6
 
 
 
b837da3
24702c6
 
 
 
 
 
 
 
 
 
 
b837da3
 
24702c6
b837da3
24702c6
 
 
 
 
 
 
d585e51
24702c6
 
 
 
 
b837da3
24702c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b837da3
 
24702c6
b837da3
24702c6
 
 
 
 
 
 
 
d585e51
24702c6
 
 
 
 
b837da3
24702c6
 
 
 
 
 
 
 
b837da3
 
24702c6
 
 
 
 
 
b837da3
24702c6
 
 
 
 
 
b837da3
 
24702c6
 
 
 
 
d585e51
24702c6
 
 
 
 
b837da3
24702c6
 
 
 
 
 
 
b837da3
 
24702c6
 
 
 
 
 
 
 
 
 
b837da3
 
24702c6
 
 
 
 
 
 
b837da3
 
24702c6
d585e51
24702c6
 
 
 
b837da3
24702c6
 
 
 
 
 
b837da3
 
24702c6
b837da3
24702c6
b837da3
24702c6
 
 
 
b837da3
 
 
 
 
 
 
24702c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b837da3
24702c6
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# Document Parsing Models - Inference Guide

## Overview

The scripts in this folder allow users to extract structured data from unstructured documents using different document parsing services and libraries. Each service follows a standard installation procedure and provides an `infer_*` script to perform inference on PDF/Image samples.

You can choose from document parsing products such as **Upstage DP**, **AWS Textract**, **Google Document AI**, **Microsoft Azure Form Recognizer**, **LlamaParse**, or **Unstructured**. Most of these services require an API key for access. Make sure to follow specific setup instructions for each product to properly configure the environment.

Each service generates a JSON output file in a consistent format with `time_sec` field for performance measurement.

---

## Quick Start

**Run a single inference script:**
```bash
python scripts/infer_upstage.py \
    --data-path <path to dataset> \
    --save-path <output.json> \
    [--concurrent 4] [--sampling-rate 0.5] [--request-timeout 600]
```

---

## Common CLI Arguments

All `infer_*` scripts share these arguments:

| Argument | Description | Default |
|----------|-------------|---------|
| `--data-path` | Path to documents directory | Required |
| `--save-path` | Output JSON file path | Required |
| `--input-formats` | File extensions to process | `.pdf .jpg .jpeg .png .bmp .tiff .heic` |
| `--concurrent` | Enable async mode with N concurrent requests | None (sync mode) |
| `--sampling-rate` | Fraction of files to process (0.0-1.0) | 1.0 |
| `--request-timeout` | API timeout in seconds | 600 |
| `--random-seed` | Random seed for reproducible sampling | None (random) |

---

## Common Features

All inference scripts share the following features:

- **Time Measurement**: Automatically measures API latency and stores `time_sec` in each result
- **Interim Results**: Saves individual API results to avoid redundant API calls on re-runs
- **Error Handling**: Continues execution even if some files fail
- **Progress Tracking**: Shows progress and completion status for each document
- **Cost Optimization**: Skips already processed files to avoid unnecessary API costs
- **Concurrency**: Optional async mode with semaphore-based rate limiting
- **Sampling**: Optional random sampling with reproducible seeds

### How Interim Results Work

Each inference script creates an interim directory (named after the output file) where individual API results are stored:

```
predictions/
├── upstage_infer.json              # Final merged results
└── upstage_infer/                  # Interim directory
    ├── document1.pdf.json
    ├── document2.pdf.json
    └── document3.pdf.json
```

Benefits:
1. **Crash Recovery**: If the script crashes, already processed files are preserved
2. **Incremental Processing**: Re-running the script only processes new files
3. **Cost Savings**: Avoids redundant API calls for successful results

### Sampling and Reproducible Results

All inference scripts support random sampling of input files using the `--sampling-rate` parameter (0.0-1.0). For reproducible results across multiple runs, use the `--random-seed` parameter:

```bash
# Sample 50% of files with reproducible selection
python scripts/infer_upstage.py \
    --data-path ./documents \
    --save-path results.json \
    --sampling-rate 0.5 \
    --random-seed 42
```

**Benefits:**
- **Reproducible Experiments**: Same seed + same sampling rate = identical file selection
- **Performance Testing**: Compare different services on the exact same documents
- **Cost Control**: Test on smaller datasets while maintaining representative samples

**Note**: Without `--random-seed`, sampling will be different each run (standard random behavior).

---

## Upstage

Follow the [official Upstage DP Documentation](https://developers.upstage.ai/docs/apis/document-parse) to set up Upstage for Document Parsing.

### Environment Variables
```bash
export UPSTAGE_API_KEY="your-api-key"
export UPSTAGE_ENDPOINT="https://api.upstage.ai/v1/document-ai/document-parse"
```

### Inference
```bash
python scripts/infer_upstage.py \
    --data-path <path to dataset> \
    --save-path <output.json> \
    [--model-name document-parse-nightly] \
    [--mode standard|enhanced] \
    [--output-formats text html markdown]
```

**Service-specific arguments:**
- `--model-name`: Model version (default: `document-parse-nightly`)
- `--mode`: Parsing mode - `standard` or `enhanced`
- `--output-formats`: Output formats to request

---

## AWS Textract

### Installation
```bash
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws configure
pip install boto3
```

Refer to the [AWS Textract Documentation](https://docs.aws.amazon.com/en_us/textract/latest/dg/getting-started.html) for detailed instructions.

### Environment Variables
```bash
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
export AWS_REGION="your-region"
export AWS_S3_BUCKET_NAME="your-bucket"  # Required for PDF processing
```

### Inference
```bash
python scripts/infer_aws.py \
    --data-path <path to dataset> \
    --save-path <output.json>
```

**Note:** PDFs use async Textract jobs (S3 upload + polling); images use direct analysis.

---

## Google Document AI

### Installation
```bash
apt-get install apt-transport-https ca-certificates gnupg curl
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | gpg --dearmor -o /usr/share/keyrings/cloud.google.gpg
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
apt-get update && apt-get install google-cloud-cli
gcloud init
pip install google-cloud-documentai
```

More information in the [Google Document AI Documentation](https://console.cloud.google.com/ai/document-ai).

### Environment Variables
```bash
export GOOGLE_PROJECT_ID="your-project-id"
export GOOGLE_PROCESSOR_ID="your-processor-id"
export GOOGLE_LOCATION="us"
export GOOGLE_ENDPOINT="us-documentai.googleapis.com"
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/credentials.json"
```

### Inference
```bash
python scripts/infer_google.py \
    --data-path <path to dataset> \
    --save-path <output.json>
```

---

## Microsoft Azure Document Intelligence

### Installation
```bash
pip install azure-ai-formrecognizer==3.3.0
```

See the [Microsoft Azure Form Recognizer Documentation](https://learn.microsoft.com/en-us/azure/ai-services/document-intelligence/quickstarts/get-started-sdks-rest-api) for additional details.

### Environment Variables
```bash
export MICROSOFT_API_KEY="your-api-key"
export MICROSOFT_ENDPOINT="https://your-resource.cognitiveservices.azure.com/"
```

### Inference
```bash
python scripts/infer_microsoft.py \
    --data-path <path to dataset> \
    --save-path <output.json>
```

---

## LlamaParse

Refer to the [official LlamaParse Documentation](https://docs.cloud.llamaindex.ai/category/API/parsing) to set up LlamaParse.

### Environment Variables
```bash
export LLAMAPARSE_API_KEY="your-api-key"
export LLAMAPARSE_POST_URL="https://api.cloud.llamaindex.ai/api/v1/parsing/upload"
export LLAMAPARSE_GET_URL="https://api.cloud.llamaindex.ai/api/v1/parsing/job"
```

### Inference
```bash
python scripts/infer_llamaparse.py \
    --data-path <path to dataset> \
    --save-path <output.json> \
    [--mode cost-effective|agentic|agentic-plus]
```

**Service-specific arguments:**
- `--mode`: Parsing mode
  - `cost-effective`: Fast, standard documents (default)
  - `agentic`: Balanced quality/cost
  - `agentic-plus`: Highest quality

**Note:** Time measurement includes polling time for async API calls.

---

## Unstructured

### Installation
```bash
pip install "unstructured[all-docs]"
pip install poppler-utils

apt install tesseract-ocr libtesseract-dev
apt install tesseract-ocr-[lang]  # Use appropriate language code
```

Detailed installation instructions at [Unstructured Documentation](https://unstructured-io.github.io/unstructured/installing.html). Use [Tesseract Language Codes](https://tesseract-ocr.github.io/tessdoc/Data-Files-in-different-versions.html) for OCR support in different languages.

### Environment Variables
```bash
export UNSTRUCTURED_API_KEY="your-api-key"
export UNSTRUCTURED_URL="https://api.unstructured.io/general/v0/general"
```

### Inference
```bash
python scripts/infer_unstructured.py \
    --data-path <path to dataset> \
    --save-path <output.json>
```

---

## Category Mapping

Within each `infer_*` script, a `CATEGORY_MAP` is defined to standardize the mapping of layout elements across different products. This ensures uniform evaluation by mapping the extracted document layout classes to standardized categories.

Example from LlamaParse:
```python
CATEGORY_MAP = {
    "text": "paragraph",
    "heading": "heading1",
    "table": "table"
}
```

Modify the `CATEGORY_MAP` in inference scripts according to your document layout categories for accurate results.

---

## Utils Module

The `utils.py` module provides shared functionality:

- `read_file_paths()` - Find files with supported formats
- `validate_json_save_path()` - Validate output file path
- `load_json_file()` - Safely load existing JSON results
- `get_interim_dir_path()` - Get interim directory path
- `save_interim_result()` - Save individual API result
- `load_interim_result()` - Load existing interim result
- `collect_all_interim_results()` - Merge all interim results

---

## Base Classes (for developers)

The `base.py` module provides inheritance hierarchy:

- **`BaseInference`**: Core class with sync/async orchestration, interim result handling, performance metrics
- **`HttpClientInference`**: For HTTP-based APIs (Upstage, LlamaParse) - manages `httpx.AsyncClient`

Use `create_argument_parser()` from `base.py` to get standard CLI arguments when creating new inference scripts.