AI-Powered Fraud Detection & Investigation Platform
GNN (GraphSAGE/GAT) Β· Explainable AI (SHAP/GNNExplainer) Β· RAG Β· LLM Reports Β· LangGraph Agent Β· FastAPI Β· Docker Β· AWS
An end-to-end fraud detection and investigation system that combines Graph Neural Networks with LLM-powered investigation reports.
Bitcoin transactions form a network (graph) β money flows from one address to another. Fraudsters don't act in isolation; they create patterns of suspicious connections. A regular ML model looks at each transaction independently, but our GraphSAGE model looks at the entire neighborhood of a transaction to decide if it's fraud.
Traditional ML: "Is THIS transaction suspicious?" β looks at 166 features of ONE transaction
GNN (This model): "Is this suspicious GIVEN context?" β looks at the transaction AND its neighbors
This system: "WHY is it suspicious?" β explains, retrieves knowledge, generates report
The system goes beyond prediction β it provides a full investigation pipeline that an analyst can use to understand and act on fraud detections.
graph TD
A["1οΈβ£ Training Layer<br/>(GraphSAGE Model)"] --> B["2οΈβ£ API Layer<br/>(FastAPI Server)"]
B --> C["3οΈβ£ Docker Layer<br/>(Containerization)"]
C --> D["4οΈβ£ Deployment Layer<br/>(AWS EC2 Public Website)"]
style A fill:#4a9eff,color:white
style B fill:#10b981,color:white
style C fill:#8b5cf6,color:white
style D fill:#f59e0b,color:white
Trained on the Elliptic dataset using PyTorch Geometric. We processed the raw graph data into data/processed/elliptic_graph.pt and achieved a PR-AUC of 0.92 using GraphSAGE. The model analyzes not just the 166 features of a transaction, but features aggregated from its 1-hop and 2-hop neighborhood.
A FastAPI server that turns the trained PyTorch model into a usable service. Instead of requiring users to write Python scripts to load .pt files, any client (a website, mobile app, or bank backend) can simply send an HTTP POST request to get predictions and natural language investigation reports.
Bundles Python 3.11, PyTorch, PyG, FastAPI, the trained model, and the graph data into a single gnn-fraudnet image. This eliminates "works on my machine" issues and ensures that the API runs consistently on any OS without complicated setup.
The Docker container is deployed on an AWS EC2 instance. We use Nginx as a reverse proxy to route public HTTP traffic (port 80) securely to the internal Docker container (port 8000), making the interactive cyberpunk dashboard and API endpoints accessible to the world.
βββββββββββββββββββββββ
β Transaction β
β (Node ID) β
ββββββββββββ¬βββββββββββ
β
ββββββββββββΌβββββββββββ
β GraphSAGE / GAT β
β Fraud Prediction β
ββββββββββββ¬βββββββββββ
β
ββββββββββββΌβββββββββββ
β Investigation β
β Context Builder β
ββββββ¬βββββββββββββ¬ββββ
β β
ββββββββββββΌβββ βββββββΌβββββββββββ
βSHAP / GNN β βRAG Retrieval β
βExplainer β βFraud Knowledge β
ββββββββββββ¬βββ βββββββ¬βββββββββββ
β β
ββββββΌβββββββββββββΌββββ
β LLM API β
β (Groq / Gemini) β
ββββββββββββ¬βββββββββββ
β
ββββββββββββΌβββββββββββ
β Natural-Language β
β Investigation β
β Report β
βββββββββββββββββββββββ
The system employs a sophisticated pipeline orchestrated by LangGraph. Each step is a LangGraph node, accumulating state as the workflow progresses:
predict β explain β analyze_graph β retrieve_knowledge β generate_report
- Predict: Runs the GraphSAGE forward pass.
- Explain: Uses GNNExplainer to identify which local and neighborhood features contributed most to the prediction.
- Analyze Graph: Computes in/out degrees, 1-hop and 2-hop illicit neighborhood concentrations, and flags structural anomalies.
- Retrieve Knowledge: Uses ChromaDB and sentence-transformers to query a local RAG knowledge base of fraud typologies.
- Generate Report: Injects the structured context into a prompt for the Groq (or Gemini) LLM, generating a comprehensive markdown report for human analysts.
Elliptic Bitcoin Dataset β one of the few real-world labeled cryptocurrency fraud datasets.
| Property | Value |
|---|---|
| Nodes (transactions) | 203,769 |
| Edges (BTC flows) | 234,355 |
| Features per node | 166 |
| Illicit (fraud) | 4,545 |
| Licit (legit) | 42,019 |
| Unknown | 157,205 |
π₯ Download β Kaggle Place the 3 CSVs in
data/raw/
Test-set metrics from a 70/15/15 split of the labeled nodes:
| Model | F1 (Illicit) | PR-AUC | Accuracy | Uses Graph? |
|---|---|---|---|---|
| Logistic Regression | 0.602 | 0.757 | 0.880 | β |
| Random Forest | 0.950 | 0.983 | 0.991 | β |
| XGBoost | 0.954 | 0.987 | 0.991 | β |
| GraphSAGE | 0.739 | 0.917 | 0.936 | β |
| GAT | 0.449 | 0.615 | 0.779 | β |
Honest takeaway: Tree-based baselines (XGBoost, RF) beat GNNs on this specific dataset because the 166 engineered features already encode rich neighborhood information. However, GraphSAGE demonstrates strong graph-aware learning, clearly beating linear models, and excels at leveraging structural context dynamically.
- Python 3.11+
- Groq API key or Gemini API key (free β for LLM reports)
git clone https://github.com/Piyush314159/GNN-FraudNet.git
cd GNN-FraudNet
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
pip install python-dotenv chromadb sentence-transformers google-genai langgraph groqcp .env.example .env
# Edit .env and add your GROQ_API_KEY or GEMINI_API_KEY
# (Set LLM_PROVIDER=groq or gemini)uvicorn api.main:app --reload --port 8000Open http://localhost:8000 in your browser to view the interactive dashboard.
# Investigate a known illicit node using curl
curl -X POST http://localhost:8000/investigate/42 \
-H "Content-Type: application/json" \
-d '{"include_shap": false, "explanation_top_k": 10}'docker-compose up --build
# Or manually:
docker build -t gnn-fraudnet .
docker run -p 8000:8000 --env-file .env gnn-fraudnetThe final production service is hosted on AWS, exposing the interactive dashboard and API to the public internet using a secure Nginx reverse proxy architecture. We utilize Groq's fast LLM API (openai/gpt-oss-120b) to overcome Gemini's rate limits and generate reports in ~3.7 seconds.
Public User / Browser β HTTP (:80) β AWS Security Group β Nginx Reverse Proxy
β
AWS EC2 Instance (m7i-flex.large) β 127.0.0.1:8000 β Docker Container β FastAPI
β
Investigation Report β Groq LLM API β ChromaDB RAG β GraphSAGE β GNNExplainer
- Launch EC2 Instance: Use an
m7i-flex.large(2 vCPUs, 8 GiB RAM, 30GB EBS) running Ubuntu 24.04 LTS. - Install Dependencies:
sudo apt update && sudo apt install -y docker.io docker-compose nginx sudo usermod -aG docker ubuntu - Transfer Files to EC2:
# Make sure you transfer the large processed graph artifact scp -i ~/gnn-key.pem -r GNN-FraudNet/ ubuntu@<EC2_PUBLIC_IP>:~/
- Configure the App Environment:
cd ~/GNN-FraudNet cp .env.example .env # Edit .env to add your GROQ_API_KEY (Set LLM_PROVIDER=groq)
- Configure Nginx as a Reverse Proxy:
Create a configuration in
/etc/nginx/sites-available/gnn-fraudnet:Enable the site, disable the default, and reload Nginx:server { listen 80; server_name <EC2_PUBLIC_IP>; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
sudo ln -s /etc/nginx/sites-available/gnn-fraudnet /etc/nginx/sites-enabled/ sudo rm /etc/nginx/sites-enabled/default sudo systemctl reload nginx
- Run the Docker Container:
# Docker is securely bound to 127.0.0.1:8000 docker-compose up -d - AWS Security Group Configuration: Allow inbound HTTP on port 80 and SSH on port 22 (restricted to your IP address). Do NOT expose port 8000 directly to the internet.
During deployment, several engineering challenges were addressed:
- PyTorch Architecture: Docker cache issues resulted in ARM64 wheels on an x86_64 EC2 instance. This was solved by downloading specific CPU-only x86_64 PyTorch wheels (
--platform linux_x86_64) to avoid CUDA bloat. - Dependency Conflicts: Upgraded
transformersconflicted with pinned PyTorch 2.4.0. We fixed this by pinningsentence-transformers==3.3.1andtransformers==4.46.3. - LLM Rate Limits: Transitioned to the Groq API from Gemini because free-tier Gemini limits caused HTTP 429 Too Many Requests errors. Groq resolved the bottleneck, dropping investigation time from ~8.5s to ~3.7s.
| Method | Endpoint | Description |
|---|---|---|
GET |
/ |
Interactive fraud detection dashboard (AWS Website Entry) |
GET |
/health |
Health check with service availability status |
POST |
/predict |
Predict from 166 raw features |
GET |
/predict/{node_id} |
Predict for a known graph node |
POST |
/investigate/{node_id} |
Full investigation β prediction + explanation + RAG + LLM report |
GET |
/node/{node_id} |
Graph info β degree, neighbors, structural flags |
{
"node_id": 78642,
"prediction": {
"fraud_probability": 0.1628,
"label": "licit",
"confidence": "high",
"probabilities": [0.8372, 0.1628]
},
"explanation": {
"method": "gnn_explainer",
"top_features": [
{"feature_name": "feature_149", "importance": 0.8334, "direction": "increases_fraud", "rank": 1}
]
},
"graph_analysis": {
"in_degree": 1,
"out_degree": 1,
"total_degree": 2,
"neighbors_1hop": {"total": 1, "illicit": 0, "licit": 0, "unknown": 1},
"structural_flags": ["mostly_unknown_neighbors"]
},
"retrieved_context": [
{"text": "Mixing services are designed to obscure...", "source": "Fraud Typologies", "relevance_score": 0.82}
],
"investigation_report": "### 1. RISK ASSESSMENT\n- **Severity Level**: LOW\n...",
"metadata": {
"duration_seconds": 3.739,
"model_name": "GraphSAGE",
"explanation_method": "gnn_explainer"
}
}GNN-FraudNet/
β
βββ src/
β βββ config.py β centralized configuration
β βββ models/
β β βββ graphsage.py β GraphSAGE model
β β βββ gat.py β GAT model
β β βββ baseline.py β LogReg / RF / XGBoost
β βββ services/
β β βββ prediction.py β model inference service
β β βββ explainability.py β SHAP / GNNExplainer wrapper
β β βββ graph_analysis.py β neighbor & structural analysis
β β βββ investigation.py β investigation context builder
β βββ rag/
β β βββ knowledge_base.py β ChromaDB vector store
β β βββ retriever.py β semantic search
β β βββ documents/ β fraud knowledge markdown files
β βββ llm/
β β βββ prompts.py β anti-hallucination prompt templates
β β βββ report_generator.py β Groq/Gemini API integration
β βββ agents/
β β βββ investigation_agent.py β LangGraph workflow
β βββ data_loader.py β CSV β PyG Data
β βββ graph_builder.py β normalization & masks
β βββ train.py β training loop
β βββ evaluate.py β metrics & comparison
β βββ explain.py β GNNExplainer + SHAP
β
βββ api/
β βββ main.py β FastAPI app + all endpoints
β βββ schema.py β Pydantic request/response models
β βββ static/index.html β interactive dashboard UI
β
βββ tests/ β pytest test suite
βββ notebooks/ β research pipeline (4 notebooks)
βββ data/ β raw CSVs + processed graph
βββ results/ β trained models + metrics
β
βββ Dockerfile
βββ docker-compose.yml
βββ aws.json β sample AWS investigation response
βββ .github/workflows/ci.yml β GitHub Actions CI
βββ .env.example β environment template
βββ README.md
# Run all tests
python -m pytest tests/ -v
# Run specific test file
python -m pytest tests/test_prediction.py -v
# Run with coverage
python -m pytest tests/ --cov=src --cov=apiTests use synthetic 50-node graphs β no need for the real dataset to run unit tests.
| Layer | Tool |
|---|---|
| GNN Framework | PyTorch Geometric |
| Deep Learning | PyTorch 2.4.0 (CPU-Optimized for Inference) |
| Baseline Models | XGBoost Β· Random Forest Β· scikit-learn |
| Explainability | SHAP Β· GNNExplainer |
| RAG | ChromaDB Β· sentence-transformers |
| LLM | Groq (openai/gpt-oss-120b) / Google Gemini |
| Agentic Workflow | LangGraph |
| API | FastAPI Β· Uvicorn |
| Frontend | Vanilla HTML/CSS/JS (cyberpunk dashboard) |
| Cloud / Network | Nginx Reverse Proxy Β· AWS Security Groups |
| Deployment | Docker Β· Docker Compose Β· AWS EC2 (Ubuntu 24.04) |
| CI/CD | GitHub Actions |
| Testing | pytest |
The ML pipeline was built as four notebooks. Run them in order:
jupyter lab notebooks/| # | Notebook | What It Does |
|---|---|---|
| 1 | 01_eda.ipynb |
Dataset exploration and feature analysis |
| 2 | 02_graph_construction.ipynb |
Builds the PyG Data object and adjacency matrix |
| 3 | 03_baseline_models.ipynb |
Trains LogReg, Random Forest, and XGBoost baselines |
| 4 | 04_gnn_training.ipynb |
Trains GraphSAGE / GAT and generates explainability scores |
Tip: All notebooks have pre-rendered outputs β open them to see results without re-executing.
This project bridges particle physics and fintech. The GNN message-passing framework applied here originates from work on the Belle II experiment β where Graph Neural Networks reconstruct subatomic particle decay trees from detector hits. The fundamental logic is identical: particles leave connected traces in a detector, while fraudsters leave connected monetary traces in a blockchain. The same mathematical structural reasoning applies to both financial transaction graphs and particle physics collisions.
Made by Piyush Β· IIT Hyderabad