Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Everything runs through the `tetrak-ocr` console script. See
tetrak-ocr backends # what is installed, and what each needs
tetrak-ocr ocr <path> --backend tesseract-auto
tetrak-ocr batch --backend auto-local # workspace/: scans/ → processed/, failures to triage/
tetrak-ocr triage --serve # what triage/ holds, as a page on 127.0.0.1 only
tetrak-ocr evaluate --all --save # → evaluation/ocr/benchmark.{csv,md} + runs/
python -m evaluation.ocr.reading_order --save # → evaluation/ocr/reading_order.{csv,md}: column-aware order vs a positioned layer's

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Processing: kar-mi-troupe-poster.jpg
quality=0.0618
→ TRIAGE: kar-mi-troupe-poster.jpg
→ TRIAGE: kar-mi-troupe-poster.md
→ TRIAGE: kar-mi-troupe-poster.json (+ 4 full transcript(s))

Triaged (1): kar-mi-troupe-poster.jpg
Done. 0 file(s) processed, 1 sent to triage.
Expand All @@ -117,6 +118,11 @@ it. The triage queue is what stops bad transcripts quietly entering a catalogue
looking like good ones, and turns "which ones did it get wrong?" into a folder
you can actually work through.

Beside the manifest, every candidate's full transcript is kept and a `.json`
record says what scored what and why the file was held. `tetrak-ocr triage`
lists the queue, and `tetrak-ocr triage --serve` shows it as a page on your
own machine only.

### Output formats

By default each transcript is written as Markdown. `--format` asks for others,
Expand Down
42 changes: 37 additions & 5 deletions docs/guide/command-line.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Using the command line

Installing the package provides one command, `tetrak-ocr`, with four
Installing the package provides one command, `tetrak-ocr`, with five
subcommands:

| Subcommand | What it does |
|---|---|
| `backends` | List every backend and whether this environment can run it |
| `ocr` | Transcribe a single file, to stdout or to files beside it |
| `batch` | Transcribe everything in `workspace/scans/` |
| `triage` | List what `batch` held back, or serve it as a local page |
| `evaluate` | Benchmark backends against the evaluation corpus (needs a checkout) |

`tetrak-ocr --help` and `tetrak-ocr <subcommand> --help` list every flag; the
Expand Down Expand Up @@ -103,10 +104,17 @@ do not stop the batch: the file is reported and the run carries on.

`auto-local` refuses to return a transcript whose quality score falls below
the floor (0.10, `MIN_QUALITY_THRESHOLD`). When that happens the batch
pipeline diverts the file to `workspace/triage/` along with a manifest,
`workspace/triage/<stem>.md`, recording which backends were tried, their
scores, and the first 500 characters of each transcript, so you can decide
whether to rescan, send it to Claude, or transcribe by hand.
pipeline diverts the file to `workspace/triage/` along with three kinds of
file, so you can decide whether to rescan, send it to Claude, or transcribe
by hand:

| File | What it holds |
|---|---|
| `<stem>.md` | A manifest for reading: which backends were tried, their scores, and the first 500 characters of each transcript |
| `<stem>.json` | A record for software: every candidate's score, the reason the file was held, when, and where each full transcript is |
| `<stem>.<backend>.txt` | Each candidate's transcript, whole |

[`tetrak-ocr triage`](#tetrak-ocr-triage) lists the queue from the records.

`--quality-gate` applies the same threshold to any single named backend:

Expand Down Expand Up @@ -353,6 +361,30 @@ Processing: king-of-kings-souvenir-1927.pdf
the batch pipeline moves into the same directory, and one would silently
overwrite the other. Non-PDF sources keep the plain `<stem>.pdf`.

## `tetrak-ocr triage`

What `batch` held back, from the records in `workspace/triage/`: each file,
its best score, the engine that produced it and the reason it was held.

```bash
tetrak-ocr triage # a table in the terminal
tetrak-ocr triage --serve # the same as a page, at http://127.0.0.1:8321/
tetrak-ocr triage --serve --port 9000
tetrak-ocr triage --dir ../other/triage
```

`--serve` binds to the loopback address and nothing else, and there is no
flag to change that. The page reads a local directory for the person at the
machine, so there is nothing to authenticate and nothing remote, and it
answers one address, `/`, with GET; it serves no files. It is read-only:
triage stays passive, and the page re-reads the directory on every request,
so it follows a batch that is still running. Stop it with Ctrl-C.

A directory written by a release before 5.19 holds manifests without
records. Those files are still listed, by name, with a note to read the
manifest; an empty or missing directory is reported as empty rather than as
an error.

## `tetrak-ocr evaluate`

Benchmark backends against the evaluation corpus: every fixture is
Expand Down
25 changes: 20 additions & 5 deletions src/tetrak_ocr/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
if TYPE_CHECKING: # pragma: no cover
from .qa_score import LowQualityError

from . import outputs, telemetry
from . import outputs, telemetry, triage
from .imaging import page_documents
from .layout import TextSpan, spans_of
from .manifest import DEFAULT_NAME as MANIFEST_NAME
Expand Down Expand Up @@ -138,15 +138,20 @@ def supported_extensions(backend: str) -> set[str]:
return _registry_extensions(backend)


def _send_to_triage(image_path: Path, error: "LowQualityError") -> None:
"""Move a file to the triage queue and write its manifest.
def _send_to_triage(image_path: Path, error: "LowQualityError", backend: str | None = None) -> None:
"""Move a file to the triage queue and write its manifest and record.

The manifest (workspace/triage/<stem>.md) contains a score table and a short
excerpt from each backend's transcript for human review.
excerpt from each backend's transcript for human review, and is written
exactly as it always has been. Beside it, :mod:`tetrak_ocr.triage` writes
the record (<stem>.json: the scores, the reason, where each transcript
is) and every candidate's full transcript, which the manifest's 500
character excerpts used to discard.

Args:
image_path: Original file path (inside workspace/scans/).
error: LowQualityError carrying .scores and .transcripts.
backend: The backend the run asked for, recorded with the item.
"""
TRIAGE_DIR.mkdir(parents=True, exist_ok=True)

Expand All @@ -169,11 +174,21 @@ def _send_to_triage(image_path: Path, error: "LowQualityError") -> None:
manifest_path = TRIAGE_DIR / f"{image_path.stem}.md"
manifest_path.write_text("\n".join(lines), encoding="utf-8")

record, *transcripts = triage.hold(
TRIAGE_DIR,
image_path,
error.scores,
error.transcripts,
reason=str(error),
backend=backend,
)

dest = TRIAGE_DIR / image_path.name
shutil.move(str(image_path), dest)

print(f" → TRIAGE: {dest.name}")
print(f" → TRIAGE: {manifest_path.name}")
print(f" → TRIAGE: {record.name} (+ {len(transcripts)} full transcript(s))")


def _relative(path: Path) -> str:
Expand Down Expand Up @@ -286,7 +301,7 @@ def _record(status: str, **fields) -> None:
)

def _triage(exc: "LowQualityError") -> None:
_send_to_triage(image_path, exc)
_send_to_triage(image_path, exc, backend)
if triaged is not None:
triaged.append(image_path.name)
reading["quality_gate"] = "failed"
Expand Down
40 changes: 39 additions & 1 deletion src/tetrak_ocr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import sys
from pathlib import Path

from . import outputs, telemetry
from . import outputs, telemetry, triage
from .errors import OcrPipelineError
from .layout import spans_of
from .registry import BACKENDS, available
Expand Down Expand Up @@ -168,6 +168,19 @@ def _cmd_batch(args: argparse.Namespace) -> int:
return batch.main(args=forwarded)


def _cmd_triage(args: argparse.Namespace) -> int:
"""List the triage queue, or serve it as a page on the loopback address."""
from .batch import TRIAGE_DIR

# The same rule as `batch`: the directory is the user's, resolved against
# the working directory, unless they point somewhere else.
directory = Path(args.dir) if args.dir else TRIAGE_DIR
if args.serve:
return triage.serve(directory, port=args.port)
print(triage.describe(triage.queue(directory), directory))
return 0


def _cmd_evaluate(args: argparse.Namespace) -> int:
# The harness lives outside the package, next to the corpus it reads, so
# that evaluation data and evaluation code stay together. That also means
Expand Down Expand Up @@ -330,6 +343,31 @@ def add_pdf_flags(parser: argparse.ArgumentParser) -> None:
add_pdf_flags(p)
p.set_defaults(func=_cmd_batch)

p = sub.add_parser(
"triage",
help="list what is held in workspace/triage/, or serve it as a local page",
)
p.add_argument(
"--serve",
action="store_true",
help=f"serve the queue as a page on http://{triage.HOST}:{triage.DEFAULT_PORT}/ "
"until Ctrl-C. Loopback only: nothing remote, nothing to authenticate",
)
p.add_argument(
"--port",
type=int,
default=triage.DEFAULT_PORT,
metavar="PORT",
help=f"port for --serve (default: {triage.DEFAULT_PORT})",
)
p.add_argument(
"--dir",
metavar="PATH",
help="the triage directory to read (default: workspace/triage/ under the "
"working directory)",
)
p.set_defaults(func=_cmd_triage)

# `evaluate` forwards its flags verbatim to the harness, which owns its own
# argument parser. REMAINDER cannot do this — argparse rejects a leading
# `--flag` before it reaches the positional — so main() uses
Expand Down
Loading
Loading