Create fraud_detection_pipeline.py
Browse files- fraud_detection_pipeline.py +58 -0
fraud_detection_pipeline.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import Pipeline
|
| 2 |
+
import numpy as np
|
| 3 |
+
import joblib
|
| 4 |
+
from typing import Dict, List, Union
|
| 5 |
+
|
| 6 |
+
class FraudDetectionPipeline(Pipeline):
|
| 7 |
+
def __init__(self):
|
| 8 |
+
super().__init__()
|
| 9 |
+
# Load the model and scaler
|
| 10 |
+
self.model = joblib.load("random_forest_model.joblib")
|
| 11 |
+
self.scaler = joblib.load("rf_scaler.joblib")
|
| 12 |
+
|
| 13 |
+
def preprocess(self, features: Dict[str, Union[int, float]]) -> np.ndarray:
|
| 14 |
+
"""
|
| 15 |
+
Preprocess the input features
|
| 16 |
+
Expected features:
|
| 17 |
+
- account_age: int (months)
|
| 18 |
+
- cred_changes_freq: float (per year)
|
| 19 |
+
- return_order_ratio: float
|
| 20 |
+
- vpn_usage: int (0 or 1)
|
| 21 |
+
- credit_score: int
|
| 22 |
+
"""
|
| 23 |
+
# Convert input to correct format
|
| 24 |
+
input_data = np.array([[
|
| 25 |
+
features['account_age'],
|
| 26 |
+
features['cred_changes_freq'],
|
| 27 |
+
features['return_order_ratio'],
|
| 28 |
+
features['vpn_usage'],
|
| 29 |
+
features['credit_score']
|
| 30 |
+
]])
|
| 31 |
+
|
| 32 |
+
# Scale the features
|
| 33 |
+
scaled_input = self.scaler.transform(input_data)
|
| 34 |
+
return scaled_input
|
| 35 |
+
|
| 36 |
+
def _forward(self, features: Dict[str, Union[int, float]]) -> Dict[str, Union[str, float]]:
|
| 37 |
+
"""
|
| 38 |
+
Make prediction using the model
|
| 39 |
+
"""
|
| 40 |
+
# Preprocess
|
| 41 |
+
scaled_input = self.preprocess(features)
|
| 42 |
+
|
| 43 |
+
# Get prediction and probability
|
| 44 |
+
prediction = self.model.predict(scaled_input)[0]
|
| 45 |
+
probabilities = self.model.predict_proba(scaled_input)[0]
|
| 46 |
+
|
| 47 |
+
# Return prediction and confidence
|
| 48 |
+
return {
|
| 49 |
+
"prediction": "Fraud" if prediction == 1 else "Not Fraud",
|
| 50 |
+
"confidence": float(probabilities[prediction]),
|
| 51 |
+
"fraud_probability": float(probabilities[1])
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
def postprocess(self, model_outputs):
|
| 55 |
+
return model_outputs
|
| 56 |
+
|
| 57 |
+
def load_pipeline():
|
| 58 |
+
return FraudDetectionPipeline()
|