Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
|
| 4 |
+
# Load the model and scaler
|
| 5 |
+
model_artifacts = joblib.load("knn_model_with_scaler.pkl")
|
| 6 |
+
knn = model_artifacts["model"]
|
| 7 |
+
scaler = model_artifacts["scaler"]
|
| 8 |
+
|
| 9 |
+
# Define the prediction function
|
| 10 |
+
def classify_instance(account_age, freq_credential_change, return_to_order, vpn_temp_mail, credit_score):
|
| 11 |
+
# Create a new instance from inputs
|
| 12 |
+
new_instance = [account_age, freq_credential_change, return_to_order, vpn_temp_mail, credit_score]
|
| 13 |
+
new_instance_scaled = scaler.transform([new_instance]) # Scale the input
|
| 14 |
+
prediction = knn.predict(new_instance_scaled) # Predict using the KNN model
|
| 15 |
+
return "Fraud" if prediction[0] == 1 else "Not Fraud"
|
| 16 |
+
|
| 17 |
+
# Define the Gradio interface
|
| 18 |
+
inputs = [
|
| 19 |
+
gr.Number(label="Account Age (months)"),
|
| 20 |
+
gr.Number(label="Frequency of Credential Change (per year)"),
|
| 21 |
+
gr.Number(label="Return to Order Ratio"),
|
| 22 |
+
gr.Radio(["No", "Yes"], label="VPN/Temp Mail?", type="index"),
|
| 23 |
+
gr.Number(label="Credit Score")
|
| 24 |
+
]
|
| 25 |
+
outputs = gr.Textbox(label="Prediction")
|
| 26 |
+
|
| 27 |
+
gr.Interface(
|
| 28 |
+
fn=classify_instance,
|
| 29 |
+
inputs=inputs,
|
| 30 |
+
outputs=outputs,
|
| 31 |
+
title="Fraud Detection KNN Model",
|
| 32 |
+
description="Enter the attributes of a transaction to predict if it's fraud or not."
|
| 33 |
+
).launch()
|