This project is a modular fraud detection platform that uses:
- 🧠 Traditional Machine Learning models
- 🤖 LLMs (Large Language Models) for human-style explanations
- 🔁 Reinforcement Learning-style feedback loops
- 📈 Drift detection to retrain models when fraud patterns change
- Real-time fraud prediction based on transaction data
- Transparent LLM-based explanations of decisions
- Feedback loop for manual corrections and re-training
- Modular pipeline for drift detection and re-training
- API endpoints for inserting transactions and getting predictions
anti-fraud/
│
├── database/
│ └── insert_transaction.py # Insert transaction into PostgreSQL
│
├── model/
│ ├── train_model.py # ML training logic
│ └── detect_drift.py # Detects drift and retrains if needed
│
├── llm/
│ └── explain_with_llm.py # LLM-based fraud reasoning
│
├── api/
│ └── feedback.py # Reinforcement feedback system
│
├── ui/
│ └── app.py # FastAPI app with endpoints
│
├── .env # Environment variables (not committed)
└── requirements.txt
python -m venv venv
source venv/bin/activate
pip install -r requirements.txtCreate a .env file at the root with the following variables:
DATABASE_URL=postgresql://username:password@localhost:5432/yourdb
OPENAI_API_KEY=sk-...
Insert sample transactions into the database:
python database/insert_transaction.pyThis will populate the transactions table with mock data, including random is_fraud labels.
python model/train_model.pyThis trains a basic fraud detection model on available transactions.
uvicorn ui.app:app --reloadThis exposes API routes at http://127.0.0.1:8000.
You can update the feedback column in the DB or use the /api/feedback endpoint to submit corrections (i.e., the user flags a false positive).
python model/detect_drift.pyThis compares current predictions vs actual feedback and retrains the model if drift exceeds a threshold.
Use the /api/transaction POST endpoint to submit a transaction. Example payload:
{
"user_id": 5,
"amount": 149.17,
"timestamp": "2025-06-20 15:30:00",
"location": "New York",
"device_type": "mobile"
}Call /api/explain with the transaction details, including optional user history, to receive a natural language explanation generated by an LLM.
Endpoint
POST http://127.0.0.1:8000/api/explain
Request Example
{
"features": {
"user_id": 5,
"amount": 149.17,
"timestamp": "2025-06-20 15:30:00",
"location": "New York",
"device_type": "mobile",
"user_history": [
{
"user_id": 5,
"amount": 1.17,
"timestamp": "2025-06-20 15:30:00",
"location": "New York",
"device_type": "mobile"
},
{
"user_id": 5,
"amount": 14.9,
"timestamp": "2025-06-20 15:30:00",
"location": "New York",
"device_type": "mobile"
}
]
},
"prediction": 1,
"probability": 0.75
}Response Example
{
"explanation": "Based on the provided transaction details and user history, the fraud detection system predicted that the transaction is fraudulent with a probability of 75.0%. This prediction makes sense based on the following reasons:\n\n1. Unusual Behavior: The transaction amount of $149.17 is significantly higher compared to the user's previous transaction amounts of $1.17 and $14.9. This sudden increase in transaction amount could indicate suspicious activity.\n\n2. Location and Device Consistency: While the transaction took place in New York and used a mobile device, which matches the user's historical data, the large difference in transaction amount raises a red flag. Fraudsters may try to mimic legitimate user behavior in terms of location and device to avoid detection.\n\n3. Probability Score: The high probability score of 75.0% indicates that the fraud detection system has identified multiple factors that suggest potential fraud, leading to a confident prediction.\n\nOverall, the prediction of fraud in this case is based on the deviation from the user's typical transaction behavior, combined with consistency in location and device usage."
}- Add frontend dashboard for analysts
- Store explanations in the DB for auditing
- Automate model versioning
- Add notification system for suspicious transactions
Built with ❤️ by SuperML.dev