Single-user receipt extraction pipeline that auto-approves trustworthy invoices and routes uncertain ones to a human review queue.
Upload → FastAPI Worker → Clairic/Mock → Confidence & Math Router → Postgres → Next.js Review/Ledger
.jpg / .jpeg / .png
│
▼
POST /api/invoices/upload
│
BackgroundTask worker
│
EXTRACTOR_MODE=mock|clairic
│
RawExtractionPayload
│
confidence · null · date · math
│
AUTO_APPROVED / NEEDS_REVIEW / FAILED
│
invoices table (JSONB raw/final)
│
Dashboard · Review · Ledger
The FastAPI worker stores the immutable raw_extraction, derives final_data, and records flags, minimum confidence, and processing time. The Next.js app polls active uploads, lets a human correct flagged invoices, and shows a finalized ledger.
- Confidence check: for required fields
vendor,date, andtotal, route toNEEDS_REVIEWwhenconfidence < 0.85ornull_prob > 0.20. Exactly0.85and exactly0.20pass. - Null/date check: a null or blank required field, or a
datethat is not strictlyYYYY-MM-DD, also routes toNEEDS_REVIEW. - Math check: when
subtotal,tax, andtotalare all present, route toNEEDS_REVIEWifabs((subtotal + tax) - total) > 0.05. A difference of exactly0.05passes. The flag added ismath_mismatch. - Flags: all rules are evaluated, then deduplicated and emitted as
["vendor", "date", "total", "math_mismatch"]. - Audit:
raw_extractionis never overwritten. Reviewing saves editedfinal_data, setsAPPROVED, and setswas_edited=Trueonly when submitted data differs from the original raw-derived data.
Line-item math is non-blocking in V1.
Prerequisites: Docker, uv, and Bun.
# 1. Start PostgreSQL
docker compose up -d
# 2. Start the backend
cd backend
cp .env.example .env
uv sync
uv run --env-file .env uvicorn app.main:app --reload --port 8000In a second terminal:
# 3. Start the frontend
cd frontend
cp .env.example .env.local
bun install
bun run devOpen http://localhost:3000. The backend health check is at http://localhost:8000/api/health.
Useful checks:
cd backend && uv run pytest -q
cd frontend && bun run build && bun run lintKeep EXTRACTOR_MODE=mock in backend/.env. The mock extractor ignores receipt contents and routes solely on the stored filename. Sanitization preserves low_conf and math_err substrings, so use these names when uploading any small .png or .jpg:
| Filename contains | Behavior |
|---|---|
| nothing | High-confidence Blue Bottle Coffee receipt → AUTO_APPROVED |
low_conf |
Low vendor/total confidence → NEEDS_REVIEW, flags vendor, total |
math_err |
subtotal + tax != total → NEEDS_REVIEW, flag math_mismatch |
Examples: receipt.png, low_conf_receipt.png, math_err.jpg.
To try real extraction later, set EXTRACTOR_MODE=clairic and optionally provide HF_TOKEN in backend/.env for a higher ZeroGPU quota. The adapter uploads a downscaled copy of each image because the Space's clairic_model._request re-encodes uploads to RGBA PNG and Clairic rejects decoded payloads above 4 MiB. If the default Space reports GPU task aborted or No GPU was available, set CLAIRIC_SPACE=hugging-apps/clairic-flash in backend/.env.
Free ZeroGPU quota only covers a few live receipts per day. clairic-flash is faster but can produce noisy line items (skipped rows, repeated descriptions, or trailing blanks). V1 routing flags low-confidence scalar fields (vendor, date, total) and total math mismatches; line items are non-blocking, so that noise is reviewed whenever an invoice is flagged rather than triggering review on its own. For unlimited local demos, keep EXTRACTOR_MODE=mock.