Spaces:
Sleeping
Sleeping
add app file
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import joblib
|
4 |
+
import pandas as pd
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
# Load model bundle
|
8 |
+
bundle = joblib.load("rf_model_bundle.pkl")
|
9 |
+
model = bundle["model"]
|
10 |
+
threshold = bundle["threshold"]
|
11 |
+
|
12 |
+
# Prediction function
|
13 |
+
def predict_fraud(step, amount, oldbalanceOrig, newbalanceOrig,
|
14 |
+
oldbalanceDest, newbalanceDest,
|
15 |
+
nameDest_freq, nameDest_fraud_rate,
|
16 |
+
type, OrigEmptyBefore, OrigEmptyAfter, DestEmptyBefore, DestEmptyAfter):
|
17 |
+
|
18 |
+
balanceChangeOrig = oldbalanceOrig - newbalanceOrig
|
19 |
+
balanceChangeDest = newbalanceDest - oldbalanceDest
|
20 |
+
|
21 |
+
X = pd.DataFrame([{
|
22 |
+
"step": step,
|
23 |
+
"amount": amount,
|
24 |
+
"oldbalanceOrig": oldbalanceOrig,
|
25 |
+
"newbalanceOrig": newbalanceOrig,
|
26 |
+
"oldbalanceDest": oldbalanceDest,
|
27 |
+
"newbalanceDest": newbalanceDest,
|
28 |
+
"balanceChangeOrig": balanceChangeOrig,
|
29 |
+
"balanceChangeDest": balanceChangeDest,
|
30 |
+
"nameDest_freq": nameDest_freq,
|
31 |
+
"nameDest_fraud_rate": nameDest_fraud_rate,
|
32 |
+
"type": type,
|
33 |
+
"OrigEmptyBefore": OrigEmptyBefore,
|
34 |
+
"OrigEmptyAfter": OrigEmptyAfter,
|
35 |
+
"DestEmptyBefore": DestEmptyBefore,
|
36 |
+
"DestEmptyAfter": DestEmptyAfter
|
37 |
+
}])
|
38 |
+
|
39 |
+
prob = model.predict_proba(X)[0][1]
|
40 |
+
pred = int(prob >= threshold)
|
41 |
+
return f"{'🚨 Fraud' if pred else '✅ Not Fraud'} (Probability: {prob:.2f})"
|
42 |
+
|
43 |
+
# Gradio UI
|
44 |
+
demo = gr.Interface(
|
45 |
+
fn=predict_fraud,
|
46 |
+
inputs=[
|
47 |
+
gr.Number(label="Transaction Step", info="Time unit since system start"),
|
48 |
+
gr.Number(label="Transaction Amount ($)", info="Total amount of the transaction"),
|
49 |
+
gr.Number(label="Sender's Balance Before", info="Balance before transaction"),
|
50 |
+
gr.Number(label="Sender's Balance After", info="Balance after transaction"),
|
51 |
+
gr.Number(label="Recipient's Balance Before", info="Recipient balance before transaction"),
|
52 |
+
gr.Number(label="Recipient's Balance After", info="Recipient balance after transaction"),
|
53 |
+
gr.Number(label="Recipient Account Frequency", info="Number of prior transactions to recipient"),
|
54 |
+
gr.Number(label="Recipient Fraud Rate", info="Historical fraud rate for recipient (0–1)"),
|
55 |
+
gr.Radio(["CASH_OUT", "TRANSFER", "PAYMENT", "CASH_IN"], label="Transaction Type", info="CASH_OUT & TRANSFER are riskier"),
|
56 |
+
gr.Radio([0, 1], label="Sender Balance Empty Before?", info="1 = Yes, 0 = No"),
|
57 |
+
gr.Radio([0, 1], label="Sender Balance Empty After?", info="1 = Yes, 0 = No"),
|
58 |
+
gr.Radio([0, 1], label="Recipient Balance Empty Before?", info="1 = Yes, 0 = No"),
|
59 |
+
gr.Radio([0, 1], label="Recipient Balance Empty After?", info="1 = Yes, 0 = No"),
|
60 |
+
],
|
61 |
+
outputs="text",
|
62 |
+
title="💸 Fraud Detection App (Random Forest)",
|
63 |
+
description="Enter transaction data to predict the likelihood of fraud using a trained ML model.",
|
64 |
+
)
|
65 |
+
|
66 |
+
demo.launch()
|