Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
|
| 7 |
+
from sklearn.model_selection import train_test_split
|
| 8 |
+
from sklearn.preprocessing import StandardScaler
|
| 9 |
+
from sklearn.linear_model import LogisticRegression
|
| 10 |
+
from sklearn.ensemble import RandomForestClassifier, IsolationForest
|
| 11 |
+
from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score, roc_curve
|
| 12 |
+
|
| 13 |
+
# -------------------------
|
| 14 |
+
# Load Dataset (auto-download from GitHub mirror if not present)
|
| 15 |
+
# -------------------------
|
| 16 |
+
import os, requests, zipfile
|
| 17 |
+
|
| 18 |
+
DATA_URL = "https://storage.googleapis.com/download.tensorflow.org/data/creditcard.csv"
|
| 19 |
+
DATA_PATH = "creditcard.csv"
|
| 20 |
+
|
| 21 |
+
if not os.path.exists(DATA_PATH):
|
| 22 |
+
print("Downloading dataset...")
|
| 23 |
+
r = requests.get(DATA_URL)
|
| 24 |
+
with open(DATA_PATH, "wb") as f:
|
| 25 |
+
f.write(r.content)
|
| 26 |
+
|
| 27 |
+
df = pd.read_csv(DATA_PATH)
|
| 28 |
+
|
| 29 |
+
# -------------------------
|
| 30 |
+
# Preprocess
|
| 31 |
+
# -------------------------
|
| 32 |
+
X = df.drop("Class", axis=1)
|
| 33 |
+
y = df["Class"]
|
| 34 |
+
|
| 35 |
+
scaler = StandardScaler()
|
| 36 |
+
X["Amount"] = scaler.fit_transform(X["Amount"].values.reshape(-1, 1))
|
| 37 |
+
|
| 38 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 39 |
+
X, y, test_size=0.3, random_state=42, stratify=y
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# -------------------------
|
| 43 |
+
# Train Models
|
| 44 |
+
# -------------------------
|
| 45 |
+
log_reg = LogisticRegression(max_iter=5000, class_weight="balanced", random_state=42)
|
| 46 |
+
log_reg.fit(X_train, y_train)
|
| 47 |
+
|
| 48 |
+
rf = RandomForestClassifier(n_estimators=200, class_weight="balanced", random_state=42)
|
| 49 |
+
rf.fit(X_train, y_train)
|
| 50 |
+
|
| 51 |
+
iso_forest = IsolationForest(
|
| 52 |
+
n_estimators=200, contamination=0.0017, random_state=42
|
| 53 |
+
)
|
| 54 |
+
iso_forest.fit(X_train)
|
| 55 |
+
|
| 56 |
+
# -------------------------
|
| 57 |
+
# Evaluation
|
| 58 |
+
# -------------------------
|
| 59 |
+
def evaluate_models():
|
| 60 |
+
results = {}
|
| 61 |
+
|
| 62 |
+
# Logistic Regression
|
| 63 |
+
y_pred_lr = log_reg.predict(X_test)
|
| 64 |
+
y_prob_lr = log_reg.predict_proba(X_test)[:, 1]
|
| 65 |
+
results["Logistic Regression"] = classification_report(y_test, y_pred_lr, digits=4)
|
| 66 |
+
|
| 67 |
+
# Random Forest
|
| 68 |
+
y_pred_rf = rf.predict(X_test)
|
| 69 |
+
y_prob_rf = rf.predict_proba(X_test)[:, 1]
|
| 70 |
+
results["Random Forest"] = classification_report(y_test, y_pred_rf, digits=4)
|
| 71 |
+
|
| 72 |
+
# Isolation Forest
|
| 73 |
+
y_pred_if = iso_forest.predict(X_test)
|
| 74 |
+
y_pred_if = np.where(y_pred_if == -1, 1, 0)
|
| 75 |
+
results["Isolation Forest"] = classification_report(y_test, y_pred_if, digits=4)
|
| 76 |
+
|
| 77 |
+
return results
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# -------------------------
|
| 81 |
+
# Fraud Prediction Function
|
| 82 |
+
# -------------------------
|
| 83 |
+
def predict_transaction(amount, time, v_features):
|
| 84 |
+
# Build feature vector
|
| 85 |
+
data = np.array([time] + v_features + [amount]).reshape(1, -1)
|
| 86 |
+
data[:, -1] = scaler.transform(data[:, -1].reshape(-1, 1)) # scale amount
|
| 87 |
+
|
| 88 |
+
pred_lr = log_reg.predict(data)[0]
|
| 89 |
+
prob_lr = log_reg.predict_proba(data)[0][1]
|
| 90 |
+
|
| 91 |
+
pred_rf = rf.predict(data)[0]
|
| 92 |
+
prob_rf = rf.predict_proba(data)[0][1]
|
| 93 |
+
|
| 94 |
+
pred_if = iso_forest.predict(data)[0]
|
| 95 |
+
pred_if = 1 if pred_if == -1 else 0
|
| 96 |
+
|
| 97 |
+
return {
|
| 98 |
+
"Logistic Regression": f"Fraud={pred_lr} (Prob={prob_lr:.3f})",
|
| 99 |
+
"Random Forest": f"Fraud={pred_rf} (Prob={prob_rf:.3f})",
|
| 100 |
+
"Isolation Forest": f"Fraud={pred_if}",
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
# -------------------------
|
| 105 |
+
# Gradio UI
|
| 106 |
+
# -------------------------
|
| 107 |
+
def ui_transaction(amount, time, *v_features):
|
| 108 |
+
v_features = list(v_features)
|
| 109 |
+
return predict_transaction(amount, time, v_features)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
with gr.Blocks() as demo:
|
| 113 |
+
gr.Markdown("# 💳 Credit Card Fraud Detection\nCompare Logistic Regression, Random Forest & Isolation Forest")
|
| 114 |
+
|
| 115 |
+
with gr.Tab("Evaluate Models"):
|
| 116 |
+
btn = gr.Button("Run Evaluation")
|
| 117 |
+
out = gr.Textbox(lines=15, label="Results")
|
| 118 |
+
|
| 119 |
+
def run_eval():
|
| 120 |
+
res = evaluate_models()
|
| 121 |
+
return "\n\n".join([f"{k}:\n{v}" for k, v in res.items()])
|
| 122 |
+
|
| 123 |
+
btn.click(run_eval, outputs=out)
|
| 124 |
+
|
| 125 |
+
with gr.Tab("Predict a Transaction"):
|
| 126 |
+
amount = gr.Number(label="Transaction Amount")
|
| 127 |
+
time = gr.Number(label="Time (seconds since first transaction)")
|
| 128 |
+
v_inputs = [gr.Number(label=f"V{i}") for i in range(1, 29)]
|
| 129 |
+
|
| 130 |
+
btn_pred = gr.Button("Predict Fraud")
|
| 131 |
+
out_pred = gr.JSON(label="Predictions")
|
| 132 |
+
|
| 133 |
+
btn_pred.click(ui_transaction, inputs=[amount, time] + v_inputs, outputs=out_pred)
|
| 134 |
+
|
| 135 |
+
demo.launch()
|