Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FraudLens

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.

Overview

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

Key Features

Core Fraud Detection

  • 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

Explainability

  • 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

Advanced RAG System

  • 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

Human-AI Collaboration

  • 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

Active Learning

  • 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

Model Improvement

  • 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

External Data Integration (Mock/Demo)

  • 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.

Tech Stack

Machine Learning

  • 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

RAG & LLM

  • 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)

UI & Infrastructure

  • Streamlit 1.53.0: Interactive web dashboard
  • SQLite: Lightweight database for human feedback storage
  • Matplotlib 3.10.8: Visualization and plotting

System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         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      β”‚      β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Data Flow

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)  β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Project Structure

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

Installation

Setup Steps

  1. Clone the repository

    git clone https://github.com/pmr123/FraudLens.git
  2. Create and activate virtual environment

    python -m venv venv
    # On Windows:
    venv\Scripts\activate
    # On Linux/Mac:
    source venv/bin/activate
  3. Install dependencies

    pip install -r requirements.txt
  4. 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
  5. Download dataset

    • Download the Credit Card Fraud Detection dataset from Kaggle
    • Place creditcard.csv in data/raw/
  6. 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/
  7. Prepare RAG documents (optional)

    # Generate synthetic fraud cases
    python -m scripts.generate_synthetic_cases
    
    # Process documents for RAG
    python -m scripts.prepare_rag_data
  8. Start the application

    python -m streamlit run app/main_streamlit.py

    Note: Always use python -m streamlit run (not just streamlit run) to ensure the correct Python environment is used, especially with conda environments.

Usage

Streamlit Dashboard

The dashboard provides four main tabs:

  1. 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
  2. Review Queue

    • View transactions requiring human review
    • Approve or block transactions
    • Add reviewer notes and escalate cases
    • Track review history
  3. Active Learning

    • Generate prioritized queue of uncertain transactions
    • Review transactions selected by active learning
    • View uncertainty scores and statistics
    • Submit feedback for model improvement
  4. Analytics

    • Review statistics (total reviews, agreement rates)
    • Active learning analytics
    • Disagreement case analysis
    • Performance metrics

Model Retraining

After collecting human feedback, retrain the model:

python -m scripts.retrain_with_feedback \
    --mode incremental \
    --strategy combined \
    --min-reviews 50

Options:

  • --mode: incremental (add to existing) or full (retrain from scratch)
  • --strategy: equal, uncertainty, al_priority, time_decay, or combined
  • --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

Configuration

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

Features in Detail

Hybrid Search

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

Re-ranking

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

Multi-query Retrieval

Generates query variations to improve coverage:

  • LLM generates alternative phrasings
  • Retrieves documents for each variation
  • Aggregates results with deduplication

Active Learning

Selects most informative transactions for review:

  • Entropy-based: Prioritizes high uncertainty
  • Margin-based: Focuses on borderline cases
  • Diverse: Combines uncertainty with feature diversity

Model Retraining

Incorporates human feedback into model:

  • Multiple weighting strategies (uncertainty, time decay, AL priority)
  • Incremental or full retraining modes
  • Model versioning and performance tracking

Performance

  • 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)

References

About

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.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages