Metadata-Version: 2.4
Name: griiist
Version: 0.3
Summary: Harvest IIIF collections into ML-ready datasets (HuggingFace, WebDataset, parquet).
Author: Roger Howard
License-Expression: Apache-2.0
Keywords: iiif,machine-learning,dataset,huggingface,webdataset,cultural-heritage,rag
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: iiisight>=0.6
Provides-Extra: hf
Requires-Dist: datasets[vision]>=2; extra == "hf"
Provides-Extra: webdataset
Requires-Dist: webdataset>=0.2; extra == "webdataset"
Provides-Extra: parquet
Requires-Dist: pyarrow>=16; extra == "parquet"
Provides-Extra: all
Requires-Dist: griiist[hf,parquet,webdataset]; extra == "all"
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
Requires-Dist: pytest-cov>=5; extra == "dev"
Requires-Dist: ruff==0.17.2; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Dynamic: license-file

<p align="center">
  <img alt="griiist" src="https://gitlab.com/iiiris-org/griiist/-/raw/main/logos/griiist-auto.svg" width="320">
</p>

# griiist

**IIIF-to-dataset tooling for ML/RAG.** Millions of openly licensed, richly described images sit behind IIIF endpoints in the world's galleries, libraries, archives, and museums — and there is no polite, resumable, rate-limited harvester that crawls whole *collections*, resolves rights, downloads at a chosen resolution, and emits ML-ready datasets with metadata intact. Existing IIIF downloaders are single-manifest CLI tools. griiist connects the cultural-heritage world to the ML world in a way neither community has built.

## The idea in one line

Point griiist at an IIIF Collection; get back a HuggingFace dataset / WebDataset shards / embedding-ready parquet of only the images you're licensed to use, with their metadata and provenance intact.

## How it's structured: catalog, then materialize

The central design choice is to **separate the cheap metadata crawl from the expensive image download.** At millions-of-images scale these are different operations and must not be conflated.

1. **Catalog** — a metadata-only crawl. Walk IIIF Collections → Manifests → Canvases and build a queryable local index of *everything available*: image identifiers, `info.json` capabilities, resolved rights, and flattened metadata. No image bytes. Fast, polite, laptop-sized even for large collections.
2. **Materialize** — take a *filter over the catalog* and produce a dataset: download images at the chosen resolution, package into the target format with metadata and provenance intact.

Crawl an institution once, then produce many filtered datasets — "only CC0 paintings > 2000px", "a 10k random sample for a prototype" — without re-hitting their servers. The rights filter is inspectable *before* anything is downloaded, and the expensive half is trivially resumable because its work-list is a static, checkpointed table.

## Pipeline

Four decoupled, individually checkpointed stages:

```
discover  →  filter/select  →  fetch  →  package
(catalog) ────────────────┘   └──── (materialize) ────┘
```

Decoupling `fetch` from `discover` is what lets a harvest be interrupted and resumed without re-crawling or re-downloading.

Materialize writes its output as numbered shards inside the `--out` directory and records each image's progress in the catalog. Rerun the same command to resume an interrupted run; images that failed are recorded rather than aborting the run, and `--retry-failed` retries them.

## What "correct" means here

These are load-bearing requirements, not nice-to-haves:

- **Level-0 aware.** IIIF Image API compliance varies. Many institutional servers are level 0 and serve only pre-generated sizes listed in `info.json`, not arbitrary `!w,h`. griiist reads `info.json` and negotiates to the closest *available* size rather than assuming it can request anything.
- **Conservative rights allowlist.** RightsStatements.org includes "In Copyright" statements, and even its "No Copyright" family carries non-commercial, contractual and other legal restrictions (`NoC-NC`, `NoC-CR`, `NoC-OKLR`); richly described ≠ openly licensed. Only `NoC-US` and `NKC` count as open. griiist normalizes CC and RightsStatements.org URIs (and the Presentation 2.x `license`/`attribution` vs 3.0 `rights`/`requiredStatement` split) and, by default, emits only affirmatively-open items. Unresolvable rights → excluded, not included.
- **Lossless metadata.** IIIF `metadata` is heterogeneous, localized, free-form. Every record carries both a normalized subset (image_id, manifest_uri, canvas_id, label, dimensions, rights, attribution, provider) *and* the raw manifest JSON.
- **Provenance as output.** Every record carries its source URI, the exact size parameters requested, harvest timestamp, and tool version — so a dataset is reproducible and its licensing is auditable.
- **Polite and resumable.** Per-host rate limiting with backoff and `Retry-After` handling; a conservative global default with per-institution overrides. Any harvest is interruptible and resumes without lost or duplicated work. A manifest or sub-collection that can't be loaded is reported and skipped; the rest of the collection is still cataloged.

## Politeness

Every request (catalog crawl and image download alike) goes through one paced client. By default griiist allows **1 request per second and 2 in flight per host**, which is safe for a small institution's server. It honors `Retry-After` on 429/503 by pausing all requests to that host, and retries transient failures with exponential backoff. Requests identify themselves with a `griiist/<version>` User-Agent. Different hosts proceed independently, so one slow server doesn't hold up the rest.

For a host you know can take more, raise its limit:

```bash
griiist materialize --out ds --rate images.example.org=10 --concurrency 16
```

`--rate HOST=RPS` is repeatable. `--default-rate` and `--host-concurrency` change the defaults for every other host, and work on `griiist catalog` too.

## Built on iiisight

griiist is a Python application built on [**iiisight**](https://pypi.org/project/iiisight/), the tolerant, async IIIF read client. iiisight handles the IIIF side — collection/manifest/canvas traversal, Presentation v2→v3 normalization, and `info.json`-aware Image API requests — so griiist can focus on what makes it a *harvester*: rights filtering, polite resumable crawling at scale, cataloging, and dataset packaging. The split is deliberate: iiisight is the reusable read substrate; griiist is one application built on it.

## Outputs

- **HuggingFace `datasets`** — parquet-backed, ready to `load_dataset` / push to the Hub.
- **WebDataset** — tar shards for large-scale streaming training.
- **Parquet** — clean, well-schematized, ready for a downstream embedding pipeline.

All three are written as numbered shards inside the `--out` directory, never as a single file: `shard-NNNNNN.tar` (WebDataset), `part-NNNNNN.parquet` (parquet; read the directory with `pyarrow.parquet.read_table(out)`), or `train-NNNNNN.parquet` (HuggingFace; load with `load_dataset("parquet", data_dir=out)`, not `load_from_disk`).

## Non-goals

Not a IIIF viewer. Not a general web scraper — it uses the structured IIIF APIs only. Not an embedding/inference engine ("embedding-ready" means the parquet is clean and schematized, not that griiist runs models). No image editing beyond resolution selection.

## Name

"griiist" ≈ *grist* — grain brought to the mill for grinding; *grist for the mill* is raw material to be processed. The tool turns IIIF collections into grist for the training mill. The extra `i`s echo IIIF.
