Skip to content

Latest commit

 

History

History
141 lines (102 loc) · 8.38 KB

README.md

File metadata and controls

141 lines (102 loc) · 8.38 KB

Batch OCR on Render Workflows

Read the text out of many scanned PDFs with Tesseract and Poppler, one task run per document, on a Docker-based workflow service.

What it shows

  • A workflow service that builds from a Dockerfile, because Tesseract and Poppler are system binaries
  • A small orchestrator task on flex and a CPU-heavy OCR task on 2c-4g
  • Passing documents by reference (a URL or a file name), not as bytes
  • Classifying errors: bad input is returned as a failed result, a killed process or a network error is raised so Render retries it
  • Fanning out in chunks with ctx.run and asyncio.gather, without failing the batch on one document
  • Triggering the tasks from another service with the Render SDK client

Task tree

ocr_batch            (flex, fans out one chunk at a time, no retries)
  └── ocr_document   (2c-4g, one per document, retries twice)
        ├── fetch the PDF        ← https:// URL, or a file in samples/
        ├── pdfinfo              ← page count
        ├── pdftoppm             ← one PNG per page
        └── tesseract            ← text of each page, two pages at a time

How ocr_document handles each failure:

What happened Example What the task does
The input is bad Corrupt PDF, a tool exits with code > 0, HTTP 404, too many pages Returns {"doc_ref", "error"}. No retry
The tool isn't installed tesseract missing when you run outside Docker Returns {"doc_ref", "error"}. No retry
A tool was killed by a signal The kernel's out-of-memory killer sends SIGKILL to tesseract Raises. Render retries the run
The run ran out of memory The plan's memory is used up and Render ends the attempt with Task terminated by OOM killer Nothing to catch: the attempt is gone. Render retries the run
The network failed DNS error, connection reset, timeout, HTTP 429 or 5xx Raises. Render retries the run

A task has no "don't retry this error" option: any exception it raises uses up an attempt. So errors a retry can't fix are returned as results instead.

Prerequisites

  • Python 3.11+
  • The Render CLI
  • Tesseract and Poppler, to run locally (brew install tesseract poppler, or apt-get install tesseract-ocr poppler-utils). On Render, the Dockerfile installs them.
  • No API keys

Run locally

Render's local task server doesn't build Dockerfiles, so run the service with the tools installed on your machine:

brew install tesseract poppler
pip install -r requirements.txt
render workflows dev -- python main.py

In another terminal, OCR one document. Task input is a JSON array of the task's arguments, and a document is a file name in samples/ or an https:// URL:

render workflows tasks runs start ocr_document --local --input='["invoice-1042.pdf"]'

OCR four of the samples at once. corrupt.pdf isn't a valid PDF, so it comes back under failed after one attempt:

render workflows tasks runs start ocr_batch --local \
  --input='[["invoice-1042.pdf", "delivery-receipt-2207.pdf", "plan-amendment.pdf", "corrupt.pdf"]]'

To see a retry, OCR a URL that can't resolve. The run raises a network error on all three attempts, then fails:

render workflows tasks runs start ocr_document --local --input='["https://scans.example.invalid/invoice.pdf"]'

Optional settings, with their defaults, are in .env.example. Copy it to .env to change them.

Deploy to Render

Docker-based workflow services can only be created in the Render Dashboard, not with the CLI or API. They build from a Dockerfile in your repository; they can't pull a prebuilt image from a registry.

  1. Push this folder to a Git repository Render can access.

  2. In the Render Dashboard, click New > Workflow and connect the repository.

  3. Set these fields:

    Field Value
    Language Docker
    Root Directory batch-ocr (the folder that holds the Dockerfile). Leave it empty if this folder is the repository root.
    Dockerfile Path ./Dockerfile
    Start Command python main.py (required for Docker-based workflows, even though the Dockerfile has a CMD)
    Region Any
    Environment variables None required. Optionally the settings in .env.example

    There's no Build Command for Docker-based workflows: the Dockerfile does the build.

  4. Create the workflow. Once the first version is ready, run ocr_batch from the Dashboard with [["invoice-1042.pdf", "delivery-receipt-2207.pdf", "plan-amendment.pdf", "corrupt.pdf"]].

  5. To see an out-of-memory retry, run ocr_document with ["site-plan-oversized.pdf"]. That PDF is 3 KB, but its two pages are 60 inches square: at 300 DPI each one is an 18,000 × 18,000 pixel image, and Tesseract needs about 3.5 GB to read it. Two pages at once don't fit in 2c-4g's 4 GB. On Render, the attempt ends with Task terminated by OOM killer and Render retries the run; every attempt runs out of memory the same way, so the run fails after three attempts. With OCR_PAGE_WORKERS=1, the same document fits and returns its text.

ocr_document runs on the 2c-4g plan, which bills per second of each run. See compute plans.

Trigger from another service

client.py triggers both tasks with the Render SDK:

export RENDER_API_KEY=your-render-api-key
export WORKFLOW_SLUG=your-workflow-slug   # defaults to batch-ocr
python client.py

The core of it:

from render import Render

render = Render()  # reads RENDER_API_KEY

run = render.workflows.run_task(f"{SLUG}/ocr_batch", [["invoice-1042.pdf", "https://..."]])
if run.status == "completed":
    batch = run.results[0]  # results is a list holding the return value
    print(batch["succeeded"], batch["failed"])

In your own app, upload each scan to object storage and pass a presigned https:// URL. run_task waits for the run to finish. To start a run and check on it later, use start_task instead.

Limits and gotchas

  • Pass references, not files. The combined arguments of a task run can't exceed 4 MB, so pass URLs or keys, not PDF bytes. See limits.
  • Results stay on Render for 30 days. ocr_document returns the text of every page, and ocr_batch returns all of it. For large documents, write the text to your own storage and return its key. See state retention.
  • Chained runs are rate limited. A workspace can start 1,000 chained runs per minute. Over that, ctx.run fails instead of queueing. ocr_batch starts BATCH_CHUNK_SIZE runs (default 20), waits for them, then starts the next chunk. A document whose run couldn't start is listed under failed. See rate limits.
  • Timeouts apply per attempt. Each attempt of ocr_document gets 600 seconds and each retry gets a fresh 600. ocr_batch gets 3,600 seconds for the whole batch: every chunk lasts as long as its slowest document, retries included. Raise it for bigger batches. See timeouts.
  • A retry reruns the whole document. Retries start over on a new instance, so a 50-page PDF that fails on page 49 is OCRed again from page 1. Reading and OCR have no side effects, so that's safe. See retries.
  • Retries only help when the next attempt can pass. An out-of-memory kill caused by huge pages happens again on the same plan, as site-plan-oversized.pdf shows. If a document fails on every attempt with Task terminated by OOM killer or killed by signal 9, lower OCR_DPI, lower OCR_PAGE_WORKERS or move ocr_document to a plan with more memory.
  • OCR isn't exact. Tesseract misreads some characters, especially on noisy scans. Check the fields you depend on.

Links