Skip to content

superMLdev/anti-fraud

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🛡️ ANTI-FRAUD: Agentic AI-Powered Fraud Detection System

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

🚀 Features

  • 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

🧩 Project Structure

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

⚙️ Setup Instructions

1. 📦 Install Dependencies

python -m venv venv
source venv/bin/activate
pip install -r requirements.txt

2. 🛠️ Set Up .env File

Create a .env file at the root with the following variables:

DATABASE_URL=postgresql://username:password@localhost:5432/yourdb
OPENAI_API_KEY=sk-...

🔄 Initial Workflow

✅ Step 1: Create Dummy Data

Insert sample transactions into the database:

python database/insert_transaction.py

This will populate the transactions table with mock data, including random is_fraud labels.

🧠 Step 2: Train the Initial ML Model

python model/train_model.py

This trains a basic fraud detection model on available transactions.

🪄 Step 3: Start the FastAPI Server

uvicorn ui.app:app --reload

This exposes API routes at http://127.0.0.1:8000.


🔁 Feedback Loop & Drift Training

📝 Step 4: Send Manual Feedback

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

📊 Step 5: Drift Detection & Retraining

python model/detect_drift.py

This compares current predictions vs actual feedback and retrains the model if drift exceeds a threshold.


🔐 Real-Time Prediction & Explanation

➕ Step 6: Insert a New Transaction

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"
}

🧾 Step 7: Get LLM Explanation

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."
}

🛠️ TODO / Enhancements

  • Add frontend dashboard for analysts
  • Store explanations in the DB for auditing
  • Automate model versioning
  • Add notification system for suspicious transactions

👨‍💻 Authors

Built with ❤️ by SuperML.dev


About

Anti Fraud detection in card transaction using XGBoost Model with zero data initially.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages