The Problem: Small businesses spend 15-20 hours/week manually entering data from unstructured, messy PDFs.
The Solution: This app automatically parses PDFs, forces a Large Language Model (via OpenRouter) to extract the data into a strict JSON schema, and mathematically validates the totals before exporting to CSV.
- Structured AI Output: Uses
pydanticand theopenaiSDK to force the LLM to return clean, predictable JSON data (no conversational hallucinations). - The "Hallucination Moat": A dedicated validator checks the AI's math (
Subtotal + Tax == Total). If the math fails, the invoice is flagged for human review. - Separation of Concerns: Modular backend architecture (
parser.py,ai_extract.py,validator.py) separating the dumb text extraction from the smart business logic. - Safety Guardrails: Hard limits on PDF page counts to prevent API token limit crashes and cost overruns.
- Streamlit Dashboard: A clean, drag-and-drop web UI for end-users to process batches of invoices and download CSV reports.
extractor/
├── parser.py # Uses pdfplumber to rip raw text
├── ai_extract.py # Pydantic schema + OpenRouter(OpenAI SDK) API call
├── validator.py # Business logic & math validation
└── exporter.py # Converts extracted JSON to Pandas DataFrame -> CSV
- Clone the repository
- Create a virtual environment:
python3 -m venv .venv - Activate the environment:
source .venv/bin/activate - Install dependencies:
pip install -r requirements.txt - Add your OpenRouter API key to a
.envfile (OPENROUTER_API_KEY=your_key) - Run the app:
streamlit run app.py
- Async Processing: Implement
asyncioto batch process 50+ invoices concurrently. - Network Retries: Implement
tenacityfor exponential backoff if the AI API times out. - Structured Logging: Add server-side logging for production monitoring.