FraudLens is an end-to-end fraud detection system that combines machine learning, explainable AI, and retrieval-augmented generation (RAG) to assist human reviewers in making informed fraud decisions.
This system provides an intelligent fraud review workflow that:
- Detects fraudulent transactions using XGBoost with calibrated probabilities
- Explains model decisions using SHAP values
- Retrieves relevant context (similar cases, policies) using advanced RAG techniques
- Enables human reviewers to override model decisions and provide feedback
- Continuously improves through active learning and model retraining
- XGBoost-based fraud detection with 86% precision and 86% recall
- Risk bucket classification (LOW/MEDIUM/HIGH) for automated decision routing
- Optimal threshold optimization balancing precision and recall
- Probability calibration using isotonic regression or Platt scaling for reliable confidence scores
- SHAP (SHapley Additive exPlanations) for transaction-level feature importance
- Global feature importance analysis across the dataset
- Interactive visualizations showing which features drive each prediction
- Zero-value filtering for cleaner, more interpretable explanations
- Hybrid search combining semantic (vector) and keyword (BM25) retrieval
- Re-ranking using cross-encoder models for improved document relevance
- Multi-query retrieval generating query variations for better coverage
- Intelligent summarization using LangChain and local LLMs (Ollama)
- Context-aware retrieval of similar fraud cases and relevant policies
- Review queue for transactions requiring human judgment
- Override capability allowing reviewers to disagree with model predictions
- Feedback collection storing reviewer decisions and notes in SQLite database
- Analytics dashboard tracking agreement rates, escalation patterns, and review statistics
- Uncertainty-based selection prioritizing transactions where the model is least confident
- Diversity-based selection ensuring selected transactions cover diverse patterns
- Analytics integration tracking the value of active learning reviews
- Queue management for efficient review workflow
- Confidence calibration adjusting probabilities to match observed frequencies
- Uncertainty quantification measuring model confidence using entropy or margin
- Model retraining incorporating human feedback with multiple weighting strategies
- Version management tracking model iterations and performance
- MCP (Model Context Protocol) client infrastructure for external data enrichment
- Mock customer history simulation (demonstrates integration pattern)
- Mock merchant risk indicators (demonstrates external API pattern)
- Mock geographic risk assessment (demonstrates location-based enrichment)
- Note: Currently uses mock data for demo purposes. The infrastructure is in place to connect to real MCP servers or external APIs.
- XGBoost 3.1.3: Gradient boosting for fraud detection
- scikit-learn 1.8.0: Model calibration, metrics, and utilities
- SHAP 0.50.0: Model explainability
- NumPy 2.2.4, Pandas 2.3.3: Data manipulation
- ChromaDB 1.4.0: Vector database for document storage and retrieval
- sentence-transformers 5.2.0: Embeddings for semantic search
- LangChain 1.2.0: LLM orchestration and prompt management
- langchain-ollama 1.0.1: Local LLM integration via Ollama
- rank-bm25 0.2.2: Keyword-based search (BM25)
- Streamlit 1.53.0: Interactive web dashboard
- SQLite: Lightweight database for human feedback storage
- Matplotlib 3.10.8: Visualization and plotting
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β User Interface β
β (Streamlit Dashboard) β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Transaction Analysis Tab β
β β’ Load test data / Upload transactions β
β β’ View predictions, probabilities, risk buckets β
β β’ SHAP explanations β
β β’ RAG context retrieval β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Core ML Pipeline β
β β
β ββββββββββββββββ ββββββββββββββββ βββββββββββββββββ β
β β XGBoost βββββΆβ Calibration βββββΆβ Uncertainty β β
β β Model β β Module β β Quantificationβ β
β ββββββββββββββββ ββββββββββββββββ βββββββββββββββββ β
β β β β β
β βββββββββββββββββββββ΄ββββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββ β
β β Risk Bucket β β
β β Classificationβ β
β βββββββββββββββββ β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Explainability Layer β
β β
β ββββββββββββββββ ββββββββββββββββ βββββββββββββββββ β
β β SHAP βββββΆβ Feature βββββΆβ Visualization β β
β β Values β β Importance β β (Plots) β β
β ββββββββββββββββ ββββββββββββββββ βββββββββββββββββ β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β RAG System β
β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Hybrid βββββΆβ Re-ranking βββββΆβ Multi-query β β
β β Search β β (Cross-enc) β β Retrieval β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β β β β
β βββββββββββββββββββββ΄ββββββββββββββββββββββ β
β β β
β βΌ β
β ββββββββββββββββ β
β β LangChain β β
β β Summarizationβ β
β ββββββββββββββββ β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Human Review Workflow β
β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Review βββββΆβ Feedback βββββΆβ Analytics β β
β β Queue β β Collection β β Dashboard β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β β β β
β βββββββββββββββββββββ΄ββββββββββββββββββββββ β
β β β
β βΌ β
β ββββββββββββββββ β
β β Active β β
β β Learning β β
β ββββββββββββββββ β
ββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Model Improvement β
β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Feedback βββββΆβ Weighted βββββΆβ Retraining β β
β β Database β β Preparation β β Script β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Transaction Input
β
βΌ
ββββββββββββββββββββ
β Feature Extract β
ββββββββββ¬ββββββββββ
β
βΌ
βββββββββββββββββββ ββββββββββββββββ
β XGBoost Model ββββββΆβ Probability β
ββββββββββ¬βββββββββ ββββββββ¬ββββββββ
β β
β βΌ
β βββββββββββββββββββ
β β Risk Bucket β
β β Classification β
β ββββββββββ¬βββββββββ
β β
βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ
β SHAP Explain β β Decision Logic β
β (Why this?) β β (Auto/Review) β
ββββββββββ¬βββββββββ ββββββββββ¬βββββββββ
β β
β βΌ
β βββββββββββββββββββ
β β RAG Context β
β β (Similar cases)β
β ββββββββββ¬βββββββββ
β β
βββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββ
β Human Reviewer β
β (Override/Approve)β
ββββββββββ¬ββββββββββββ
β
βΌ
βββββββββββββββββββ
β Feedback DB β
β (SQLite) β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β Model Retrain β
β (Incremental) β
βββββββββββββββββββ
FinRAG/
βββ app/ # Main application package
β βββ main_streamlit.py # Streamlit dashboard (main entry point)
β βββ main_streamlit_al_tab.py # Active Learning tab
β βββ data_pipeline.py # Data loading and preprocessing
β βββ model.py # Model loading and scoring
β βββ explainability.py # SHAP explanations
β βββ calibration.py # Probability calibration
β βββ uncertainty.py # Uncertainty quantification
β βββ active_learning.py # Active learning selection
β βββ retraining.py # Model retraining utilities
β βββ rag.py # Core RAG functionality
β βββ rag_hybrid.py # Hybrid search (semantic + keyword)
β βββ rag_rerank.py # Document re-ranking
β βββ rag_multiquery.py # Multi-query retrieval
β βββ langchain_rag.py # LangChain integration
β βββ mcp_client.py # MCP client for external data
β βββ human_feedback.py # Human feedback database management
β βββ config.py # Centralized configuration
β
βββ scripts/ # Utility scripts
β βββ train_model.py # Train XGBoost model
β βββ retrain_with_feedback.py # Retrain with human feedback
β βββ generate_synthetic_cases.py # Generate fraud cases for RAG
β βββ prepare_rag_data.py # Process documents for RAG
β
βββ data/ # Data directory
β βββ raw/ # Raw datasets (creditcard.csv)
β βββ processed/ # Processed data
β βββ human_feedback.db # SQLite database for reviews
β
βββ models/ # Trained models
β βββ fraud_xgb.joblib # Base XGBoost model
β βββ fraud_xgb_calibrated.joblib # Calibrated model
β βββ optimal_threshold.json # Optimal decision threshold
β βββ calibration_info.json # Calibration metadata
β
βββ rag_docs/ # RAG knowledge base
β βββ raw/ # Original documents
β βββ processed/ # Processed and chunked documents
β βββ *.txt # Processed text files
β βββ chroma_db/ # ChromaDB vector store
β
βββ requirements.txt # Python dependencies
βββREADME.md # This file
-
Clone the repository
git clone https://github.com/pmr123/FraudLens.git
-
Create and activate virtual environment
python -m venv venv # On Windows: venv\Scripts\activate # On Linux/Mac: source venv/bin/activate
-
Install dependencies
pip install -r requirements.txt
-
Install and configure Ollama (for LLM features)
# Download from https://ollama.ai # Pull a small model (fits in 8GB GPU): ollama pull llama3.2:3b
-
Download dataset
- Download the Credit Card Fraud Detection dataset from Kaggle
- Place
creditcard.csvindata/raw/
-
Train the model
python -m scripts.train_model
This will:
- Train the XGBoost model
- Find optimal threshold
- Save calibrated model (if enabled)
- Save model artifacts to
models/
-
Prepare RAG documents (optional)
# Generate synthetic fraud cases python -m scripts.generate_synthetic_cases # Process documents for RAG python -m scripts.prepare_rag_data
-
Start the application
python -m streamlit run app/main_streamlit.py
Note: Always use
python -m streamlit run(not juststreamlit run) to ensure the correct Python environment is used, especially with conda environments.
The dashboard provides four main tabs:
-
Transaction Analysis
- Load test data or upload CSV files
- View model predictions with probabilities and risk buckets
- Explore SHAP explanations for individual transactions
- Retrieve RAG context (similar cases and policies)
- Generate AI-powered review reports
-
Review Queue
- View transactions requiring human review
- Approve or block transactions
- Add reviewer notes and escalate cases
- Track review history
-
Active Learning
- Generate prioritized queue of uncertain transactions
- Review transactions selected by active learning
- View uncertainty scores and statistics
- Submit feedback for model improvement
-
Analytics
- Review statistics (total reviews, agreement rates)
- Active learning analytics
- Disagreement case analysis
- Performance metrics
After collecting human feedback, retrain the model:
python -m scripts.retrain_with_feedback \
--mode incremental \
--strategy combined \
--min-reviews 50Options:
--mode:incremental(add to existing) orfull(retrain from scratch)--strategy:equal,uncertainty,al_priority,time_decay, orcombined--min-reviews: Minimum number of reviews required--only-disagreements: Only use transactions where human disagreed with model--only-al: Only use transactions selected by active learning
Key configuration options in app/config.py:
- Model: Model path, threshold settings
- RAG: Search method (semantic/keyword/hybrid), re-ranking, multi-query
- Calibration: Enable/disable, method (isotonic/platt)
- Uncertainty: Enable/disable, method (entropy/margin)
- Active Learning: Enable/disable, selection method, number of transactions
- LLM: Ollama base URL, model name, temperature
Combines semantic (vector) and keyword (BM25) search for better retrieval:
- Semantic search finds conceptually similar documents
- Keyword search finds exact term matches
- Weighted combination or Reciprocal Rank Fusion (RRF) for merging results
Improves document relevance by re-scoring retrieved documents:
- Cross-encoder: Most accurate, processes query+document together
- LLM-based: Uses Ollama to score relevance
- Feature-based: Fast metadata-based re-ranking
Generates query variations to improve coverage:
- LLM generates alternative phrasings
- Retrieves documents for each variation
- Aggregates results with deduplication
Selects most informative transactions for review:
- Entropy-based: Prioritizes high uncertainty
- Margin-based: Focuses on borderline cases
- Diverse: Combines uncertainty with feature diversity
Incorporates human feedback into model:
- Multiple weighting strategies (uncertainty, time decay, AL priority)
- Incremental or full retraining modes
- Model versioning and performance tracking
- Model Performance: 86% precision, 86% recall on test set
- Inference Speed: <10ms per transaction (XGBoost)
- RAG Retrieval: <500ms for hybrid search + re-ranking
- SHAP Computation: <1s per transaction (TreeExplainer)