| from fastapi import FastAPI
|
| import pandas as pd
|
| import sys
|
| import os
|
| from dotenv import load_dotenv
|
|
|
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
| load_dotenv()
|
|
|
| from src.pipeline.predict_pipeline import PredictPipeline
|
| from src.monitoring.db import save_to_db, init_db
|
|
|
|
|
| init_db()
|
|
|
| app = FastAPI(title="Fraud Detection API")
|
|
|
|
|
| pipeline = PredictPipeline()
|
|
|
| @app.get("/")
|
| def home():
|
| return {"message": "Fraud Detection API is running 🚀"}
|
|
|
| @app.post("/predict")
|
| def predict(data: dict):
|
|
|
| expected_columns = ["Time"] + [f"V{i}" for i in range(1, 29)] + ["Amount"]
|
|
|
|
|
| df = pd.DataFrame([data], columns=expected_columns)
|
|
|
|
|
| pred, prob = pipeline.predict(df)
|
|
|
|
|
| save_to_db(data, int(pred), float(prob))
|
|
|
|
|
| if prob > 0.8:
|
| action = "🚫 Block Transaction"
|
| elif prob > 0.4:
|
| action = "⚠️ Flag for Review"
|
| else:
|
| action = "✅ Allow Transaction"
|
|
|
| return {
|
| "fraud_prediction": int(pred),
|
| "fraud_probability": float(prob),
|
| "recommended_action": action
|
| } |