Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
from flask import Flask, request, jsonify
|
| 5 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 6 |
+
import joblib
|
| 7 |
+
|
| 8 |
+
# Define the autoencoder model
|
| 9 |
+
class Autoencoder(nn.Module):
|
| 10 |
+
def __init__(self, input_dim):
|
| 11 |
+
super(Autoencoder, self).__init__()
|
| 12 |
+
self.encoder = nn.Sequential(
|
| 13 |
+
nn.Linear(input_dim, 64),
|
| 14 |
+
nn.BatchNorm1d(64),
|
| 15 |
+
nn.ReLU(),
|
| 16 |
+
nn.Dropout(0.2)
|
| 17 |
+
)
|
| 18 |
+
self.decoder = nn.Sequential(
|
| 19 |
+
nn.Linear(64, input_dim),
|
| 20 |
+
nn.BatchNorm1d(input_dim),
|
| 21 |
+
nn.Sigmoid()
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
def forward(self, x):
|
| 25 |
+
encoded = self.encoder(x)
|
| 26 |
+
decoded = self.decoder(encoded)
|
| 27 |
+
return decoded
|
| 28 |
+
|
| 29 |
+
# Load trained model
|
| 30 |
+
input_dim = 29 # Assuming 29 features (V1-V28 + Amount)
|
| 31 |
+
model = Autoencoder(input_dim)
|
| 32 |
+
model.load_state_dict(torch.load("trained_autoencoder.pth"))
|
| 33 |
+
model.eval()
|
| 34 |
+
|
| 35 |
+
# Load the MinMaxScaler
|
| 36 |
+
scaler = joblib.load("scaler.pkl")
|
| 37 |
+
|
| 38 |
+
# Create Flask app
|
| 39 |
+
app = Flask(__name__)
|
| 40 |
+
|
| 41 |
+
@app.route('/')
|
| 42 |
+
def home():
|
| 43 |
+
return "Credit Card Fraud Detection API is Running!"
|
| 44 |
+
|
| 45 |
+
@app.route('/predict', methods=['POST'])
|
| 46 |
+
def predict():
|
| 47 |
+
try:
|
| 48 |
+
# Get JSON input
|
| 49 |
+
data = request.get_json()
|
| 50 |
+
X_input = np.array(data['features']).reshape(1, -1) # Ensure it's in the right shape
|
| 51 |
+
|
| 52 |
+
# Scale input data
|
| 53 |
+
X_scaled = scaler.transform(X_input)
|
| 54 |
+
|
| 55 |
+
# Convert to PyTorch tensor
|
| 56 |
+
X_tensor = torch.tensor(X_scaled, dtype=torch.float32)
|
| 57 |
+
|
| 58 |
+
# Get reconstruction error
|
| 59 |
+
recon = model(X_tensor).detach().numpy()
|
| 60 |
+
recon_error = np.mean((recon - X_scaled) ** 2)
|
| 61 |
+
|
| 62 |
+
# Use threshold to classify as fraud (1) or normal (0)
|
| 63 |
+
threshold = 0.01 # Adjust this based on previous experiments
|
| 64 |
+
prediction = 1 if recon_error > threshold else 0
|
| 65 |
+
|
| 66 |
+
return jsonify({'fraud_probability': float(recon_error), 'is_fraud': prediction})
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return jsonify({'error': str(e)})
|
| 70 |
+
|
| 71 |
+
if __name__ == '__main__':
|
| 72 |
+
app.run(debug=True)
|