How srungavruksham/bank-statement-extractor works
Purpose and Entry Points
This is a Python/Streamlit bank-statement extraction system. It converts PDFs from data/statements/ into structured customer, account, and transaction data; validates the result; captures lessons from failures; and supports human approval. The interactive entry point is app.py; orchestration is centered in pipeline.py.
UI and Human Review Boundary
app.py provides the Streamlit workflow: select an allowed statement, provide the bank name, run processing, inspect live stage results, and approve or reject output. Human decisions cross into durable workflow state through review.py, which writes approved records under data/approved/. Input access is intentionally bounded to data/statements/.
Paths: app.py, review.py, data/approved/ef66dfd6-3848-4339-8f4d-770cbbd2e79f.json
Pipeline Orchestration
pipeline.py coordinates stages and returns their combined result to the UI. Its principal flow is: safely read PDF text, retrieve bank-specific learned rules, run parallel extraction, merge fields, validate deterministic rules, and create/refine a reflection lesson on failure. This module is the control-plane boundary between UI, LLM agents, Python validation, memory, and telemetry.
Paths: pipeline.py, utils.py, validation.py
LLM Extraction and Merge
crewai_extraction.py implements the CrewAI/Gemini agent layer. Three parallel agents extract identity, account, and transaction data from statement text. A separate merge agent combines only smaller identity/account outputs; transaction rows are restored in Python rather than regenerated by the merge model, reducing truncation and transcription risk.
Prompt contracts are versioned and separated per role, with a registry resolving their active definitions.
Paths: crewai_extraction.py, prompts/identity.yaml, prompts/account.yaml, prompts/transactions.yaml, prompts/merge.yaml, prompts/registry.py
Deterministic Validation and Reflection
validation.py applies non-LLM checks such as required-field presence, balance arithmetic, and date consistency. This is a hard correctness boundary: model-produced structured values are checked by repeatable Python logic rather than another model call. Failures feed a reflection prompt that records the issue and avoidance guidance; known numeric strings such as "$1,234.56" remain a documented validation limitation.
Paths: validation.py, prompts/reflection.yaml, utils.py
Memory and Learning Loop
memory/store.py persists bank-scoped lessons across runs. Validation-originated lessons and human rejection feedback remain distinguishable by source. Repeated mistakes for a bank are promoted after two occurrences into trusted semantic rules, which are injected into future extraction/reflection context.
Paths: memory/store.py, pipeline.py, review.py
Observability and Runtime Dependencies
telemetry/tracker.py records per-agent LLM token use and cost, enabling the UI to expose execution evidence and spend. Runtime infrastructure is local Python storage plus filesystem directories; there is no indicated database or remote backend beyond Gemini access through CrewAI. Required external services/libraries include Streamlit, CrewAI with Google GenAI/Gemini, PyPDF2, dotenv-based environment configuration, and YAML prompt loading.
Paths: telemetry/tracker.py, requirements.txt, app.py
Main entry points: crewai_extraction.py CrewAI extraction · pipeline.py Pipeline orchestrator · app.py Streamlit app · store.py Bank lesson store
Components 16
UI & Review
- Streamlit app · Selects statements, runs processing, shows results, and collects decisions.
app.py - Statement PDFs · Allowed source documents.
- Review workflow · Persists approvals and records rejection feedback.
review.py - Approved records
data/approved/ef66dfd6-3848-4339-8f4d-770cbbd2e79f.json
Extraction Pipeline
- Pipeline orchestrator · Coordinates extraction, validation, learning, and returned stage results.
pipeline.py - Safe PDF text reader
utils.py - CrewAI extraction · Runs parallel identity, account, and transaction extraction plus merge.
crewai_extraction.py - Role extractors · Extract identity, account, and transaction data independently.
crewai_extraction.py - Merge and restore rows · Merges small identity/account outputs and restores transaction rows in Python.
crewai_extraction.py - Prompt contracts · Role-specific extraction, merge, and reflection contracts.
prompts/identity.yaml - Prompt registry
prompts/registry.py - Deterministic validation · Checks required fields, balances, and date consistency.
validation.py - Gemini via CrewAI
requirements.txt
Learning & Evidence
- Bank lesson store · Promotes repeated bank-specific mistakes into trusted rules after two occurrences.
memory/store.py - Reflection lesson · Captures validation issues and avoidance guidance.
prompts/reflection.yaml - LLM telemetry · Records per-agent token use and cost.
telemetry/tracker.py
Connections 21
- Statement PDFs → Streamlit app: select allowed PDF
- Streamlit app → Pipeline orchestrator: run processing
- Pipeline orchestrator → Safe PDF text reader: read safely
- Safe PDF text reader → CrewAI extraction: statement text
- Pipeline orchestrator → Bank lesson store: retrieve bank rules
- Bank lesson store → CrewAI extraction: learned context
- Prompt registry → Prompt contracts: resolve active prompts
- Prompt contracts → CrewAI extraction: agent contracts
- CrewAI extraction → Role extractors: parallel extraction
- Role extractors → Merge and restore rows: structured outputs
- Gemini via CrewAI → CrewAI extraction: LLM inference
- CrewAI extraction → LLM telemetry: token use and cost
- Merge and restore rows → Deterministic validation: combined record
- Deterministic validation → Pipeline orchestrator: validated result
- Deterministic validation → Reflection lesson: on failure
- Reflection lesson → Bank lesson store: validation lesson
- Pipeline orchestrator → Streamlit app: live stage results
- Streamlit app → Review workflow: approve or reject
- Review workflow → Approved records: approved record
- Review workflow → Bank lesson store: rejection feedback
- LLM telemetry → Streamlit app: execution evidence