KNN_Fraud / app.py
NiharMandahas's picture
Create app.py
d13e384 verified
import gradio as gr
import joblib
# Load the model and scaler
model_artifacts = joblib.load("knn_model_with_scaler.pkl")
knn = model_artifacts["model"]
scaler = model_artifacts["scaler"]
# Define the prediction function
def classify_instance(account_age, freq_credential_change, return_to_order, vpn_temp_mail, credit_score):
# Create a new instance from inputs
new_instance = [account_age, freq_credential_change, return_to_order, vpn_temp_mail, credit_score]
new_instance_scaled = scaler.transform([new_instance]) # Scale the input
prediction = knn.predict(new_instance_scaled) # Predict using the KNN model
return "Fraud" if prediction[0] == 1 else "Not Fraud"
# Define the Gradio interface
inputs = [
gr.Number(label="Account Age (months)"),
gr.Number(label="Frequency of Credential Change (per year)"),
gr.Number(label="Return to Order Ratio"),
gr.Radio(["No", "Yes"], label="VPN/Temp Mail?", type="index"),
gr.Number(label="Credit Score")
]
outputs = gr.Textbox(label="Prediction")
gr.Interface(
fn=classify_instance,
inputs=inputs,
outputs=outputs,
title="Fraud Detection KNN Model",
description="Enter the attributes of a transaction to predict if it's fraud or not."
).launch()