-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
132 lines (120 loc) · 4.46 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from fastapi import FastAPI, APIRouter
from pydantic import BaseModel
import joblib
import numpy as np
# Load the trained model
model = joblib.load("model/fraud_model.pkl")
# Initialise FastAPI instance with metadata
app = FastAPI(
title="Fraud Detection API",
version="0.1.0",
description=(
"**This API provides endpoints for detecting potential credit card fraud using a machine learning model trained with supervised learning techniques. "
"Given a set of transaction features, the model predicts whether a transaction is likely fraudulent and provides the probability of fraud. "
"The API aims to assist financial institutions in identifying fraudulent transactions swiftly and accurately, ultimately helping to mitigate losses and enhance security measures. "
"By integrating this API into their systems, businesses can leverage advanced analytics to make informed decisions and improve their fraud detection capabilities.**"
),
contact={
"name": "Nafisa Lawal Idris",
"url": "https://nafisalawalidris.github.io/13/"
},
license_info={
"name": "MIT License",
"url": "https://opensource.org/licenses/MIT"
},
)
# Define the data schema for transaction input
class Transaction(BaseModel):
"""
Schema for input data. Each transaction record includes
28 V-features, the transaction amount, and timestamp.
"""
Time: float
V1: float
V2: float
V3: float
V4: float
V5: float
V6: float
V7: float
V8: float
V9: float
V10: float
V11: float
V12: float
V13: float
V14: float
V15: float
V16: float
V17: float
V18: float
V19: float
V20: float
V21: float
V22: float
V23: float
V24: float
V25: float
V26: float
V27: float
V28: float
Amount: float
# Root Endpoint
@app.get("/", summary="Root Endpoint")
def root():
"""
Welcome message and API overview.
"""
return {
"message": "Welcome to the Credit Card Fraud Detection API",
"details": "Visit '/docs' for a comprehensive overview of available endpoints and to test the API."
}
# Root Details
@app.get("/api/v0.1.0/root/", tags=["Root"], summary="Root Details")
def read_root_details():
"""
Detailed root information, including API overview and key endpoints.
"""
return {
"overview": "This API offers endpoints for fraud prediction in credit card transactions.",
"endpoints": [
{
"path": "/",
"description": "Root endpoint providing welcome message and high-level overview."
},
{
"path": "/api/v0.1.0/predict",
"description": "Predicts fraud likelihood and provides the probability for a given transaction."
},
]
}
# Create a router for fraud detection
fraud_detection_router = APIRouter()
@fraud_detection_router.post("/predict", summary="Predict Fraud", tags=["Fraud Detection"])
def predict(transaction: Transaction):
"""
Predicts whether a transaction is likely fraudulent.
- **transaction**: Transaction data, including 28 V-features, transaction amount, and timestamp.
- Returns:
- **fraud_prediction** (bool): True if fraud is predicted, False otherwise.
- **fraud_probability** (float): Probability of fraud (between 0 and 1).
"""
# Prepare the input data for model prediction
data = np.array([[
transaction.Time, transaction.V1, transaction.V2, transaction.V3, transaction.V4,
transaction.V5, transaction.V6, transaction.V7, transaction.V8, transaction.V9,
transaction.V10, transaction.V11, transaction.V12, transaction.V13, transaction.V14,
transaction.V15, transaction.V16, transaction.V17, transaction.V18, transaction.V19,
transaction.V20, transaction.V21, transaction.V22, transaction.V23, transaction.V24,
transaction.V25, transaction.V26, transaction.V27, transaction.V28, transaction.Amount
]])
# Make a prediction using the trained model
prediction = model.predict(data)
fraud_probability = model.predict_proba(data)[0][1] # Probability of fraud
# Return the prediction result
return {
"fraud_prediction": bool(prediction[0]), # True for fraud, False otherwise
"fraud_probability": fraud_probability
}
# Include the fraud detection router
app.include_router(fraud_detection_router, prefix="/api/v0.1.0", tags=["Fraud Detection"])