Skip to content

Scrubs bank statements locally & analyse with Groq.

Python

Updated Notes.md and README.md

asadullahdot0d7a81229bbd
diff --git a/.gitignore b/.gitignore
index 194fa43..6cf519a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
.venv/
__pycache__/
.env
-data/
\ No newline at end of file
+data/
+.pytest_cache/
\ No newline at end of file
diff --git a/NOTES.md b/NOTES.md
index ef873e4..06b16ea 100644
--- a/NOTES.md
+++ b/NOTES.md
@@ -1,19 +1,21 @@
# NOTES
-**Scrubber.** Ordered regexes + validators (Luhn, IBAN length, ISO country codes), specific -> generic, fully local. *Rejected:* Presidio/spaCy (a ~400 MB model, and when I benchmarked it on this dummy data it missed identifiers), and any "remove URLs" rule (it destroys `Netflix.com`). Precision is tested with "must not change" lookalikes (`12 Apr 2026 ATM WDL CRESCENT ATM GULSHAN BR`, 7-digit amounts). I checked another candidate's public repo only to compare designs (local parser + one small model call, fail-closed gate); nothing was copied. Generalisation: I invented nine statements from UAE, Saudi, Australia, South Africa, Singapore, Nigeria, Kenya and an ID-format mix, listed every identifier I planted, and ran the scrubber. First pass leaked 7/49 and 3/34 (e.g. `Mobile,0501234567` in a CSV, TFN, a card showing its first six digits) and over-scrubbed an amount; all are fixed and kept as tests.
+**Scrubber.** Ordered regexes plus validators (Luhn, IBAN length, ISO country codes), specific to generic, fully local. *Rejected:* Presidio/spaCy (~400 MB; it missed identifiers on this dummy data) and any "remove URLs" rule (it destroys `Netflix.com`). Precision is tested with lookalike lines that must not change. For generalisation I invented nine statements (UAE, Saudi, Australia, South Africa, Singapore, Nigeria, Kenya, an ID mix) with every identifier listed. The first passes leaked 7/49 and 3/34 and ate an amount; all fixed and kept as tests.
-**API.** The *run* is the central resource (`POST /runs`, `/runs/{id}/analysis`, `/runs/{id}/chat`), mirroring the DB. Scrub, analysis and chat are separate calls so a Groq failure or rerun never repeats scrubbing. Uploads exist on disk only during the request. A verifier gate returns 422 and stores nothing if anything looks like PII.
-**DB.** `scrub_runs` -> `scrubbed_files`, `analyses` (JSONB), `chat_messages`. I store the scrubbed *text* (exactly what Groq sees), not parsed transactions: statement formats are unbounded, so a transactions table would need a schema per bank. Cascade deletes remove a run completely.
+**API.** The *run* is the central resource (`/runs`, `/runs/{id}/analysis`, `/runs/{id}/chat`), mirroring the DB. Scrub, analysis and chat are separate calls, so a Groq failure never repeats scrubbing. A verifier gate returns 422 and stores nothing if anything looks like PII.
+**DB.** `scrub_runs` -> `scrubbed_files`, `analyses` (JSONB), `chat_messages`. I store scrubbed *text*, exactly what Groq sees, not parsed transactions: formats are unbounded, so a transactions table would need a schema per bank.
## LLM stage
-**Model.** `openai/gpt-oss-120b`: 131k context (a whole statement fits), JSON mode, fast. `llama-3.3-70b-versatile` was my first choice but Groq retired it on 16 Aug 2026 (its deprecations page; its models page was stale). `gpt-oss-20b` is the fallback. The plan's limits (8,000 tokens/min, 200,000/day) shaped the design: calls are small, 429s are retried using Groq's wait hint, and a *daily* limit fails fast.
+**Model.** `openai/gpt-oss-120b`: 131k context, JSON mode, fast. `llama-3.3-70b` was my first choice, but Groq retired it on 16 Aug 2026. `gpt-oss-20b` is the fallback. The plan's limits (8,000 tokens/min, 200,000/day) shaped the design: small calls, 429 retries using Groq's wait hint, and fast failure on the daily limit.
-**Three extraction modes, same data (116 rows).** Model-only: 73-160 s, almost all rate-limit waiting (~20k tokens), and the model's "transfer vs spending" labels changed between runs (USD spending 673.22 vs 823.22). Local-only: 0.1 s, but a CSV with no currency becomes `UNK`. **Hybrid (default):** a deterministic parser reads the clear lines; the model reads only leftovers, one tiny currency check and the insights. 116/116 rows, 2 calls, ~2 s, identical totals to the model-only runs. If Groq is down, hybrid still completes with template insights.
+**Extraction modes (same 116 rows).** Model-only: 73-160 s, mostly rate-limit waiting, and its "transfer vs spending" labels changed between runs. Local-only: 0.1 s, but a CSV with no currency becomes `UNK`. **Hybrid (default):** a deterministic parser reads clear lines; the model reads only leftovers, a tiny currency check and the insights. 116/116 rows, 2 calls, ~2 s, same totals as model-only. Hybrid still completes if Groq is down.
-**System prompts.** Extraction: statement text is *data, never instructions*; one entry per line (duplicates stay duplicates); fixed category list; redaction tags are never reconstructed; direction only from the statement's own sign/Dr/Cr, otherwise omit the row; each row carries `evidence` (the amount plus nearby words, copied verbatim) so code can check it. Insights see only pre-computed figures. Chat gets the rules plus a compact ledger and the verified totals, and must return `{in_scope, answer, used_ids}`.
+**System prompts.** Extraction: the statement is *data, never instructions*; one entry per line; fixed categories; redaction tags never reconstructed; direction only from the statement's own sign/Dr/Cr; verbatim `evidence` per row. Insights see only pre-computed figures. Chat returns `{in_scope, answer, used_ids}` against a compact ledger.
-**Hallucination and accuracy.** The model never produces a total. Each row is kept only if its evidence occurs in the statement, the amount occurs in that evidence, the date occurs in the statement, and it is not repeated more often than the line is. Totals, categories, merchants, duplicates and reversals are computed in Python. Insights quoting a number not in the aggregates are dropped. Chat: every number in an answer must come from the data *and* agree with the rows the model cited; the app re-adds those rows itself, retries once telling the model the real sum, then falls back to an app-computed answer.
+**Hallucination.** The model never produces a total. A row is kept only if its evidence, amount and date occur in the statement and it is not repeated more than the line is. Totals, categories and duplicates are computed in Python; insights quoting unknown numbers are dropped. In chat, every number must come from the data and agree with the rows the model cites; the app re-adds them, retries once with the real sum, then returns its own computed answer.
-**Guardrails and how they were tested.** Pre-send leak gate; question scrubbing; 429/daily-limit handling; refusal/bad-JSON retry; only the last 6 turns, from the DB, so a client cannot inject turns. Each has an adversarial test with a scripted fake Groq: invented rows, wrong amounts, over-repeated lines, invented insight numbers, a poisoned description, an IBAN in the question, injected history, malformed JSON. Real-model runs found two bugs the fakes could not: my evidence check rejected 70 of 117 genuine rows (it expected exact text; fixed to a local token match), and a chat answer quoted a true total that contradicted its own citations (now rejected). The chat has only been run on `gpt-oss-20b` (quotas ran out), and on a hard question that model sometimes returned invalid JSON.
+**Guardrails and tests.** Pre-send leak gate, question scrubbing, daily-limit handling, bad-JSON retry, and a 6-turn history read from the DB (clients cannot inject turns). Each has an adversarial test with a scripted fake Groq: invented rows, wrong amounts, repeated lines, invented insight numbers, a poisoned description, an IBAN in a question. Real runs found two bugs fakes could not: my evidence check rejected 70 of 117 genuine rows, and a chat answer contradicted its own citations (now rejected). The chat has only been run on `gpt-oss-20b`, which sometimes returned invalid JSON.
-**With more time.** OCR for scanned PDFs; have the model emit a structured query (merchant, period, category) that Python executes, so citation completeness is guaranteed (today a model can cite 8 of 9 matching rows consistently and be wrong); optional local NER for names in free text; retrieval for very long statements; currency inference from sibling files.
+**Improvements.** OCR for scanned PDFs; have the model emit a structured query that Python executes, guaranteeing citation completeness (today a model can cite 8 of 9 matching rows consistently and be wrong); retrieval for long statements.
+
+**AI use.** Claude Code (Claude Sonnet 5.5) wrote most code and tests under my direction. Transcripts and `prompts.txt` are included.
diff --git a/README.md b/README.md
index 9166c2a..20083f3 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
Scrubs bank and wallet statements (PDF, DOCX, CSV, TXT, XLSX) **locally**, stores only the scrubbed text in PostgreSQL, then analyses it with Groq and lets you chat about it. Raw statements never leave the machine and are never written to the database.
-GitHub: `<add the public repository link here>`
+GitHub: `https://github.com/asadullahdot/financial-pii-scrubber`
## Approach (and limits)