directory-pipeline

Turn a public digital archive URL into a structured, browsable CSV — no manual transcription, no custom code per collection type.

Give it a URL from the Library of Congress, Internet Archive, or any institution that publishes a public IIIF manifest (CONTENTdm repositories included). It downloads the scans, OCRs them, and extracts entries into a structured CSV. Every row links back to its page in the original scan and, with the enrichment steps, to the exact line.

Built for digitized historical directories — city directories, gazetteers, trade directories — but works on just about any historical document with regular entry-like structure.

Interactive data explorer with facet filters, field distribution charts, and a detail panel with IIIF source thumbnail

The auto-generated data explorer. Categorical facets generated from the data, full-text search, IIIF page thumbnails, and a “View in source document” deep link for every row.


Quick start

Requires Python 3.11+ and uv.

uv sync                      # creates .venv/ and installs the `pipeline` command
source .venv/bin/activate    # puts `pipeline` on your PATH (or prefix each command with `uv run`)

# One-time calibration for a new collection type — generates OCR and NER
# prompts that will work for any item with the same entry structure
pipeline ingest    https://archive.org/details/ldpd_11290437_000/
pipeline calibrate https://archive.org/details/ldpd_11290437_000/

# Automated run — produces the entries CSV + browsable HTML explorer
pipeline run       https://archive.org/details/ldpd_11290437_000/

# Or the human-in-the-loop version of the same run: adds page scoping,
# #xywh= bounding boxes, and interactive alignment review (needs GPU or Apple Silicon)
pipeline guided    https://archive.org/details/ldpd_11290437_000/

Calibrate once, run many. pipeline calibrate is a one-time step per collection type: it opens a browser UI where you pick 4–10 representative pages, then has Gemini analyze them and write tailored OCR and extraction prompts. For any additional volume in the same series, point to the first volume’s NER prompt and skip calibration entirely:

pipeline run https://archive.org/details/ldpd_11290437_001/ \
  --ner-prompt output/ldpd_11290437_000/ner_prompt.md

Requires GEMINI_API_KEY. Can run on the free tier — no billing required for collections up to ~150 pages. Flex inference (~50% cheaper API calls, 1–15 min latency per request, best-effort availability) is on by default; pass --no-flex for time-sensitive runs.


What you get

A CSV where every row is one extracted entry. Field names are driven entirely by your NER prompt — no code changes necessary for a new document or volume of the same type. An example entry from a travel guide:

name address city state category canvas_fragment
Mrs. Simmons Tourist Home 418 Johnson St Augusta GA Tourist Home https://...#xywh=142,890,1240,68

The canvas_fragment column is a IIIF URI pointing back to the source scan. With the precision upgrade, it includes a #xywh= bounding box pinpointing the exact line. The data explorer and map use this to link directly to the highlighted entry in the original document.

Alongside the extracted data CSV, pipeline run also generates a self-contained HTML data explorer (shown above). With geocoding enabled on materials with address fields, you also get:

Leaflet map with clustered markers, category filter sidebar, and a popup with IIIF source thumbnail

Markers clustered and color-coded by category. Popups include a IIIF thumbnail fetched directly from the source institution’s image server.


Examples

Two published collections built with this pipeline:

Tulsa 1921 city directory explorer: entry detail with a crop of the source scan highlighting the matched line Green Book explorer: faceted entry list with a detail panel showing cross-year listings and a location map

Entry detail views from the published explorers. Left: each Tulsa entry renders the exact line from the source scan via its canvas_fragment URI. Right: a Green Book establishment with its listing history across volumes.

Both explorers received additional front-end design work beyond what the pipeline generates. The pipeline produces the entry CSVs, IIIF manifests, and a baseline HTML explorer; these published sites build on that output.


Installation

uv sync                  # core: Gemini OCR + entry extraction
uv sync --extra gpu      # add Surya OCR (GPU or Apple Silicon recommended)
uv sync --extra geo      # add geocoding + map generation
uv sync --all-extras     # everything

uv sync creates and manages the project’s virtual environment in .venv/ and installs the pipeline command into it — there is no environment to create yourself, and no conda. Either activate it once (source .venv/bin/activate) or prefix each command with uv run. Everything below is written in the bare form, and the same applies to the underlying python main.py … interface.

Set your API keys (or copy .env.template to .env):

export GEMINI_API_KEY=your_key_here
export GOOGLE_MAPS_API_KEY=your_key_here   # optional; enables address-level geocoding
Already have Anaconda? This project is uv-native — there is no conda environment to install. Let uv manage its own interpreter and leave conda out of the way: ```bash uv python install 3.12 uv sync --extra gpu ``` Two things bite conda users in particular: **An Intel Python on an Apple Silicon Mac.** macOS runs x86_64 interpreters under Rosetta silently, and Anaconda's Intel installer is easy to grab by accident. PyTorch has shipped no macOS x86_64 wheel since 2.2.2, so `uv sync --extra gpu` fails to resolve Surya — and a torch installed from conda-forge (which still builds `osx-64` from source) will run emulated and CPU-only on a machine with a perfectly good GPU. `main.py` warns when it detects this. Check yours with: ```bash python -c "import platform; print(platform.machine())" # want: arm64 ``` **Notebooks running on the wrong kernel.** `uv sync` installs into `.venv/`, which is not the conda kernel Jupyter starts with — so imports fail even though the packages are installed. Register the project venv as a kernel and select it from the Kernel menu: ```bash uv run python -m ipykernel install --user --name directory-pipeline ```

Commands

Run pipeline --help for all subcommands, and pipeline <subcommand> --help for each one’s options:

pipeline run    <URL>          # automated: download → OCR → extract → explore
pipeline guided <URL>          # human-in-loop: page selection + alignment review
pipeline ingest <URL>          # download only
pipeline calibrate <URL|DIR>   # select sample pages + generate prompts (once per collection type)
pipeline ocr    <DIR>          # Surya OCR + Gemini OCR + align bboxes
pipeline review  <DIR>         # interactive alignment review (browser UI)
pipeline extract <DIR>         # NER extraction + explorer
pipeline geo     <DIR>         # geocode entries + build map (needs address fields)
pipeline postprocess <DIR>     # fix + combine volumes + rebuild explorer

Each subcommand wraps the underlying python main.py <URL> [flags] stage interface — see docs/pipeline-stages.md for the flag-level reference and every artifact each stage produces.

Beyond the subcommands:

Goal Command
Improve accuracy on complex layout-dependent materials pipeline extract output/<vol>/ --mode multimodal → details
Review entries that could not be placed on a line python tools/collate_review_queue.py output/<vol>/
See what a run actually cost python tools/report_cost.py output/<vol>/
Export ALTO XML (Solr, IIIF Content Search) python main.py output/<vol>/ --export-alto
Export W3C/IIIF annotations python -m pipeline.iiif.export_annotations output/<vol>/
Extract one section of a huge volume without downloading it all python tools/slice_manifest.py <MANIFEST> --from-id … --to-id … --slug <slug> → details
Use pipeline pieces from a notebook or script from pipeline.api import iter_canvases, … → details

Precision upgrade

The core pipeline run path gives you a canvas URI per row. Adding Surya OCR and alignment upgrades each entry’s canvas_fragment to a #xywh= bounding box on its exact line, usable by any IIIF viewer. An entry the pipeline cannot place confidently keeps its page link and goes on the review list rather than getting a guessed box.

pipeline ocr    output/<vol>/    # Surya bboxes + alignment (requires GPU or Apple Silicon)
pipeline review output/<vol>/    # optional: fix unmatched lines interactively

NW alignment result drawn on a source page — orange bounding boxes on matched lines, unmatched Gemini lines listed in the margin in red

Multimodal extraction

By default extraction sends the OCR text to Gemini. --mode multimodal also sends the page image, so the model can see section headings, column boundaries and layout cues that OCR text loses. It helps most where headings fall mid-page or reading order is ambiguous — on Green Book volumes it eliminated mid-page geographic attribution errors — and adds about $0.00006 per page (costs).

pipeline extract output/<vol>/ --mode multimodal

Costs

A single ~80-page volume costs roughly $0.30–$0.40 in Gemini API charges with --flex, about twice that at standard rates, and fits within the free tier’s daily quota (the 15 RPM limit stretches the API stages to 15–20 minutes). python tools/report_cost.py output/<vol>/ reports what a run actually spent, from the token usage each stage records. See docs/costs.md for the full breakdown, including platform costs for Surya OCR (Mac, Colab, and GPU).


Docs

Key design decisions

See docs/key-design-decisions.md for full technical notes.


Prior work and inspirations

See docs/prior-work.md for these and other annotated citations, including the Green Books digitization the category taxonomy comes from.