Spaces:
Runtime error
Runtime error
Upload 6 files
Browse files- README.md.txt +0 -0
- app.py +85 -0
- hf_model_files/label_encoders.joblib +3 -0
- hf_model_files/rf_fraud_model.joblib +3 -0
- hf_model_files/scaler.joblib +3 -0
- requirements.txt.txt +6 -0
README.md.txt
ADDED
|
File without changes
|
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
|
| 5 |
+
# =========================
|
| 6 |
+
# LOAD MODEL ARTIFACTS
|
| 7 |
+
# =========================
|
| 8 |
+
rf_model = joblib.load("hf_model_files/rf_fraud_model.joblib")
|
| 9 |
+
scaler = joblib.load("hf_model_files/scaler.joblib")
|
| 10 |
+
encoders = joblib.load("hf_model_files/label_encoders.joblib")
|
| 11 |
+
|
| 12 |
+
# =========================
|
| 13 |
+
# EXTRACT VALID CATEGORIES
|
| 14 |
+
# =========================
|
| 15 |
+
transaction_types = list(encoders['transaction_type'].classes_)
|
| 16 |
+
channels = list(encoders['channel'].classes_)
|
| 17 |
+
locations = list(encoders['location'].classes_)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# =========================
|
| 21 |
+
# PREDICTION FUNCTION
|
| 22 |
+
# =========================
|
| 23 |
+
def predict_fraud(transaction_type, channel, location, amount, old_balance, new_balance):
|
| 24 |
+
try:
|
| 25 |
+
# Create input dataframe
|
| 26 |
+
input_df = pd.DataFrame([{
|
| 27 |
+
"transaction_type": transaction_type,
|
| 28 |
+
"channel": channel,
|
| 29 |
+
"location": location,
|
| 30 |
+
"amount": float(amount),
|
| 31 |
+
"old_balance": float(old_balance),
|
| 32 |
+
"new_balance": float(new_balance)
|
| 33 |
+
}])
|
| 34 |
+
|
| 35 |
+
# Encode categorical features
|
| 36 |
+
for col in ['transaction_type', 'channel', 'location']:
|
| 37 |
+
input_df[col] = encoders[col].transform(input_df[col])
|
| 38 |
+
|
| 39 |
+
# Scale features
|
| 40 |
+
X_scaled = scaler.transform(input_df)
|
| 41 |
+
|
| 42 |
+
# Predict
|
| 43 |
+
pred = rf_model.predict(X_scaled)[0]
|
| 44 |
+
prob = rf_model.predict_proba(X_scaled)[0][1]
|
| 45 |
+
|
| 46 |
+
return (
|
| 47 |
+
"🚨 Fraud Detected" if pred == 1 else "✅ Not Fraud",
|
| 48 |
+
f"{prob:.4f}"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return "Error", str(e)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# =========================
|
| 56 |
+
# GRADIO INTERFACE
|
| 57 |
+
# =========================
|
| 58 |
+
iface = gr.Interface(
|
| 59 |
+
fn=predict_fraud,
|
| 60 |
+
inputs=[
|
| 61 |
+
gr.Dropdown(transaction_types, label="Transaction Type"),
|
| 62 |
+
gr.Dropdown(channels, label="Channel"),
|
| 63 |
+
gr.Dropdown(locations, label="Location"),
|
| 64 |
+
gr.Number(label="Amount"),
|
| 65 |
+
gr.Number(label="Old Balance"),
|
| 66 |
+
gr.Number(label="New Balance"),
|
| 67 |
+
],
|
| 68 |
+
outputs=[
|
| 69 |
+
gr.Textbox(label="Prediction"),
|
| 70 |
+
gr.Textbox(label="Fraud Probability")
|
| 71 |
+
],
|
| 72 |
+
title="💳 Fraud Detection System (South Africa)",
|
| 73 |
+
description="Select transaction details to predict whether a transaction is fraudulent.",
|
| 74 |
+
|
| 75 |
+
examples=[
|
| 76 |
+
[transaction_types[0], channels[0], locations[0], 5000, 10000, 5000],
|
| 77 |
+
[transaction_types[-1], channels[-1], locations[-1], 200, 500, 300],
|
| 78 |
+
]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# =========================
|
| 82 |
+
# RUN APP
|
| 83 |
+
# =========================
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
iface.launch()
|
hf_model_files/label_encoders.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b97f934e8112187d17faeba8d3459c0118165210eb7c7e6b6e420347cd00189b
|
| 3 |
+
size 1262
|
hf_model_files/rf_fraud_model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:99c9b8a2824d6e77c1c47ebdfe3e339e1318d0165852fd4ff3dace0b1ee01dd1
|
| 3 |
+
size 50247033
|
hf_model_files/scaler.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c14022688f5904a4f574541fb3c0e96badec120e2843e52b826f655689bf9792
|
| 3 |
+
size 1103
|
requirements.txt.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
numpy
|
| 3 |
+
scikit-learn
|
| 4 |
+
imbalanced-learn
|
| 5 |
+
joblib
|
| 6 |
+
gradio
|