Real-time transaction fraud detection system with ML pipeline, REST API, and Cloudflare Workers deployment
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
app = FastAPI()
model = joblib.load("models/fraud_model.pkl")
class Transaction(BaseModel):
amount: float
merchant_id: str
merchant_category: str
@app.post("/predict")
async def predict_fraud(tx: Transaction):
features = extract_features(tx)
prediction = model.predict_proba([features])
return {
"fraud_probability": float(prediction[0][1]),
"is_fraud": bool(prediction[0][1] > 0.5),
"risk_level": "high" if prediction[0][1] > 0.8 else "low"
}