CoolFace
Apppublic

akshit1229/credit-statement-intelligence

sourceHugging Facemitupdated 8mo agoView on Hugging Face
0likes
App README

Credit Statement Intelligence Engine

A production-grade AI system for credit statement intelligence using RAG (Retrieval-Augmented Generation) and intent-based query routing.

๐ŸŽฏ Features

  • โ€”PDF Ingestion: Parse multiple credit card statement PDFs with different formats
  • โ€”Smart Data Extraction: Extract normalized transactions and statement metadata
  • โ€”Vector Store: Semantic search using ChromaDB and sentence transformers
  • โ€”Intent Classification: Route queries using 5 intent types (list, aggregate, compare, topn, generalexplanation)
  • โ€”Evidence-Backed Answers: No hallucinations - all responses include source evidence
  • โ€”Analytics & Reconciliation: Spend analysis, category breakdown, and data validation
  • โ€”REST API: FastAPI with interactive documentation

๐Ÿ“‹ System Requirements

  • โ€”Python 3.11+
  • โ€”2GB RAM minimum (4GB recommended for vector embeddings)
  • โ€”Groq API key (for LLM-based intent classification)

๐Ÿš€ Quick Start

1. Installation

bash
# Clone the repository
git clone <repository-url>
cd credit-statement-intelligence

# Create virtual environment
python -m venv .venv
.venv\Scripts\activate  # Windows
# source .venv/bin/activate  # Linux/Mac

# Install dependencies
pip install -r requirements.txt

2. Configuration

Create a .env file in the project root:

env
GROQ_API_KEY=your_groq_api_key_here

3. Run the Server

bash
python main.py

The server will start on http://localhost:8000

  • โ€”API Docs: http://localhost:8000/docs
  • โ€”ReDoc: http://localhost:8000/redoc

๐Ÿ“ก API Endpoints

EndpointMethodDescription
/GETAPI information
/healthGETHealth check with database stats
/ingestPOSTUpload and process PDF statements
/queryPOSTAsk natural language questions
/export/csvGETExport all transactions to CSV
/summaryGETGet analytics summary
/reconciliationGETGet reconciliation report

๐Ÿ’ก Usage Examples

Upload a PDF Statement

bash
curl -X POST "http://localhost:8000/ingest" \
  -F "file=@statement.pdf" \
  -F "statement_year=2023"

Ask Questions

bash
# Aggregate query
curl -X POST "http://localhost:8000/query" \
  -H "Content-Type: application/json" \
  -d '{"question": "How much did I spend on dining?"}'

# Top N query
curl -X POST "http://localhost:8000/query" \
  -H "Content-Type: application/json" \
  -d '{"question": "What are my top 5 expenses?"}'

# RAG query (general explanation)
curl -X POST "http://localhost:8000/query" \
  -H "Content-Type: application/json" \
  -d '{"question": "What fees are mentioned in my statement?"}'

Get Analytics

bash
# Summary with category breakdown, top merchants, time analysis
curl "http://localhost:8000/summary"

# Reconciliation report
curl "http://localhost:8000/reconciliation"

# Export to CSV
curl "http://localhost:8000/export/csv" -o transactions.csv

๐Ÿ—๏ธ Architecture

credit-statement-intelligence/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ ingestion/          # PDF parsing and data extraction
โ”‚   โ”‚   โ”œโ”€โ”€ parsers/        # PDF parser implementation
โ”‚   โ”‚   โ”œโ”€โ”€ normalizers/    # Transaction normalization
โ”‚   โ”‚   โ””โ”€โ”€ storage.py      # DuckDB storage layer
โ”‚   โ”œโ”€โ”€ vector_store/       # ChromaDB vector store
โ”‚   โ”œโ”€โ”€ query_router/       # Intent classification & query execution
โ”‚   โ”œโ”€โ”€ analytics/          # Spending insights
โ”‚   โ”œโ”€โ”€ reconciliation/     # Data validation
โ”‚   โ””โ”€โ”€ api/                # FastAPI routes
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ raw/                # Uploaded PDFs
โ”‚   โ”œโ”€โ”€ processed/          # Processed data
โ”‚   โ”œโ”€โ”€ vector_db/          # ChromaDB persistence
โ”‚   โ””โ”€โ”€ statements.db       # DuckDB database
โ”œโ”€โ”€ outputs/                # CSV exports and reports
โ””โ”€โ”€ tests/                  # Test suite

Technology Stack

  • โ€”Backend: FastAPI, Uvicorn
  • โ€”PDF Parsing: pdfplumber
  • โ€”Database: DuckDB (analytical queries)
  • โ€”Vector Store: ChromaDB + sentence-transformers
  • โ€”LLM: Groq (llama-3.3-70b-versatile)
  • โ€”Data Processing: pandas, numpy
  • โ€”Testing: pytest

๐Ÿงช Testing

Run Unit Tests

bash
pytest tests/test_query_router.py -v

Test Results: All 11 tests passed โœ…

  • โ€”Intent classification (aggregate, list, topn, compare, generalexplanation)
  • โ€”Query plan structure validation
  • โ€”Multiple query type tests

Manual API Testing

  1. 1.Start the server: python main.py
  2. 2.Open http://localhost:8000/docs
  3. 3.Test endpoints via Swagger UI

๐Ÿ” Intent Classification

The system classifies user questions into 5 intents:

  1. 1.list: "Show me all dining transactions"
  2. 2.aggregate: "How much did I spend on groceries?"
  3. 3.compare: "Compare January vs February spending"
  4. 4.top_n: "What are my top 5 expenses?"
  5. 5.general_explanation: "What is the annual fee?" (uses RAG)

Each query generates a structured query plan:

json
{
  "intent": "aggregate",
  "query_plan": {
    "filters": {
      "category": "dining",
      "txn_type": "debit"
    },
    "operation": "sum",
    "parameters": {}
  },
  "requires_rag": false,
  "confidence": 0.95
}

๐Ÿ“Š Analytics Features

  • โ€”Category Breakdown: Spending by category (dining, groceries, transportation, etc.)
  • โ€”Top Merchants: Highest spending merchants
  • โ€”Time Analysis: Monthly and weekly spending patterns
  • โ€”Unusual Transactions: Flagged high-value or duplicate transactions
  • โ€”Spending Patterns: Day-of-week and month-period analysis

๐Ÿ”’ Data Safety

  • โ€”No Hallucinations: All answers include evidence from actual data
  • โ€”Source Attribution: Retrieved document chunks with page references
  • โ€”Validation: Reconciliation against statement totals
  • โ€”Error Handling: Proper validation when no data is available

๐ŸŽ“ Design Decisions

Why DuckDB?

  • โ€”Analytical query performance for aggregations
  • โ€”SQL interface for complex filtering
  • โ€”Efficient columnar storage

Why ChromaDB?

  • โ€”Easy Python integration
  • โ€”Good performance for moderate datasets
  • โ€”Built-in persistence

Why Groq?

  • โ€”Fast inference for intent classification
  • โ€”Good structured output support
  • โ€”Cost-effective for production use

๐Ÿ“ Assumptions

  1. 1.PDF Format: Statements contain tabular transaction data
  2. 2.Date Format: Transactions have parseable dates (DD/MM/YYYY or similar)
  3. 3.Amount Format: Amounts are in decimal format with "Cr" suffix for credits
  4. 4.Statement Year: May need to be provided if not in PDF
  5. 5.Category Matching: Based on keyword matching in merchant names

๐Ÿ› Known Issues / Future Improvements

  • โ€”[ ] Support for more PDF formats (OCR for scanned PDFs)
  • โ€”[ ] Multi-currency support
  • โ€”[ ] User authentication and multi-tenant support
  • โ€”[ ] Export to multiple formats (Excel, JSON)
  • โ€”[ ] Advanced duplicate detection
  • โ€”[ ] Machine learning for category classification

๐Ÿ“– Sample Outputs

After ingestion, the system generates:

  • โ€”transactions.csv: All extracted transactions
  • โ€”summary.json: Analytics summary
  • โ€”reconciliation_report.json: Validation report

See the outputs/ directory for examples.

๐Ÿค Contributing

This is an assignment project. For production use, consider:

  • โ€”Adding authentication/authorization
  • โ€”Implementing rate limiting
  • โ€”Adding comprehensive logging
  • โ€”Database connection pooling
  • โ€”Caching for frequent queries

๐Ÿ“„ License

This is an educational project created as an assignment.

๐Ÿ™ Acknowledgments

Built using:

  • โ€”FastAPI framework
  • โ€”ChromaDB vector database
  • โ€”Groq LLM API
  • โ€”pdfplumber library