-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
62 lines (51 loc) · 1.89 KB
/
handler.py
File metadata and controls
62 lines (51 loc) · 1.89 KB
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
from collections.abc import Generator
from datetime import datetime
import runpod
from docketanalyzer_ocr import load_pdf, pdf_document
def handler(event: dict) -> Generator[dict, None, None]:
"""RunPod serverless handler for OCR processing.
This function processes PDF documents for OCR text extraction in a serverless
environment. It can handle PDFs provided either as binary data or via S3 keys.
Args:
event: The event dictionary containing:
- input: Dictionary with processing parameters:
- s3_key: Optional S3 key to load the PDF from
- file: Optional binary PDF data
- filename: Optional filename for the PDF
- batch_size: Optional batch size for processing (default: 1)
Yields:
dict: Per page results:
- page: Processed page data
- seconds_elapsed: Processing time so far
- progress: Processing progress (0-1)
"""
start = datetime.now()
inputs = event.pop("input")
filename = inputs.get("filename")
batch_size = inputs.get("batch_size", 1)
if inputs.get("s3_key"):
pdf_data, filename = load_pdf(
s3_key=inputs.pop("s3_key"), filename=filename
)
elif inputs.get("file"):
pdf_data, filename = load_pdf(
file=inputs.pop("file"),
filename=filename,
)
else:
raise ValueError("Neither 's3_key' nor 'file' provided in input")
doc = pdf_document(pdf_data, filename=filename)
for i, page in enumerate(doc.stream(batch_size=batch_size)):
duration = (datetime.now() - start).total_seconds()
yield {
"page": page.data,
"seconds_elapsed": duration,
"progress": i / len(doc),
}
doc.close()
runpod.serverless.start(
{
"handler": handler,
"return_aggregate_stream": False,
}
)