Skip to content

About

Confidence-routed invoice extraction pipeline with math validation and human-in-the-loop review (FastAPI, PostgreSQL, Next.js, Clairic).

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

Confidence-Routed Invoice Pipeline

Single-user receipt extraction pipeline that auto-approves trustworthy invoices and routes uncertain ones to a human review queue.

Architecture

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.

Routing rules

  • Confidence check: for required fields vendor, date, and total, route to NEEDS_REVIEW when confidence < 0.85 or null_prob > 0.20. Exactly 0.85 and exactly 0.20 pass.
  • Null/date check: a null or blank required field, or a date that is not strictly YYYY-MM-DD, also routes to NEEDS_REVIEW.
  • Math check: when subtotal, tax, and total are all present, route to NEEDS_REVIEW if abs((subtotal + tax) - total) > 0.05. A difference of exactly 0.05 passes. The flag added is math_mismatch.
  • Flags: all rules are evaluated, then deduplicated and emitted as ["vendor", "date", "total", "math_mismatch"].
  • Audit: raw_extraction is never overwritten. Reviewing saves edited final_data, sets APPROVED, and sets was_edited=True only when submitted data differs from the original raw-derived data.

Line-item math is non-blocking in V1.

Local quickstart

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 8000

In a second terminal:

# 3. Start the frontend
cd frontend
cp .env.example .env.local
bun install
bun run dev

Open 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 lint

Offline demo

Keep 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.

About

Confidence-routed invoice extraction pipeline with math validation and human-in-the-loop review (FastAPI, PostgreSQL, Next.js, Clairic).

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages