Update app.py
Browse files
app.py
CHANGED
|
@@ -1,87 +1,87 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import pickle
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
-
|
| 7 |
-
# Load pre-trained models and encoders
|
| 8 |
-
with open('is_fraud.pkl', 'rb') as f:
|
| 9 |
-
is_fraud_model = pickle.load(f)
|
| 10 |
-
|
| 11 |
-
with open('fraud_type.pkl', 'rb') as f:
|
| 12 |
-
fraud_type_model = pickle.load(f)
|
| 13 |
-
|
| 14 |
-
# Load label encoders for features
|
| 15 |
-
encoders = {}
|
| 16 |
-
for feature in ['card_type', 'location', 'purchase_category', 'time_of_day']:
|
| 17 |
-
with open(f'{feature}.pkl', 'rb') as f:
|
| 18 |
-
encoders[feature] = pickle.load(f)
|
| 19 |
-
|
| 20 |
-
with open('fraud_type_le.pkl', 'rb') as f:
|
| 21 |
-
fraud_type_encoder = pickle.load(f)
|
| 22 |
-
|
| 23 |
-
# Define the prediction function
|
| 24 |
-
def predict_fraud(amount, card_type, location, purchase_category, customer_age, time_of_day):
|
| 25 |
-
# Preprocess input
|
| 26 |
-
input_data = pd.DataFrame({
|
| 27 |
-
'amount': [amount],
|
| 28 |
-
'card_type': [card_type],
|
| 29 |
-
'location': [location],
|
| 30 |
-
'purchase_category': [purchase_category],
|
| 31 |
-
'customer_age': [customer_age],
|
| 32 |
-
'time_of_day': [time_of_day]
|
| 33 |
-
})
|
| 34 |
-
|
| 35 |
-
# Label encode the inputs
|
| 36 |
-
for feature in ['card_type', 'location', 'purchase_category', 'time_of_day']:
|
| 37 |
-
input_data[feature] = encoders[feature].transform(input_data[feature])
|
| 38 |
-
|
| 39 |
-
# Fraud Detection (Binary Classification)
|
| 40 |
-
is_fraud_pred_prob = is_fraud_model.predict_proba(input_data)[0]
|
| 41 |
-
is_fraud_class = np.argmax(is_fraud_pred_prob)
|
| 42 |
-
|
| 43 |
-
# Fraud Type Classification (Multiclass Classification)
|
| 44 |
-
fraud_type_pred_prob = fraud_type_model.predict_proba(input_data)[0]
|
| 45 |
-
fraud_type_class = fraud_type_encoder.inverse_transform([np.argmax(fraud_type_pred_prob)])[0]
|
| 46 |
-
|
| 47 |
-
# Bar charts for probabilities
|
| 48 |
-
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
|
| 49 |
-
|
| 50 |
-
# Plot Fraud Detection Probabilities
|
| 51 |
-
axes[0].bar(['Not Fraudulent', 'Fraudulent'], is_fraud_pred_prob)
|
| 52 |
-
axes[0].set_title('Fraud Detection Probability')
|
| 53 |
-
axes[0].set_ylabel('Probability')
|
| 54 |
-
|
| 55 |
-
# Plot Fraud Type Probabilities
|
| 56 |
-
fraud_types = fraud_type_encoder.classes_
|
| 57 |
-
axes[1].bar(fraud_types, fraud_type_pred_prob)
|
| 58 |
-
axes[1].set_title('Fraud Type Probability')
|
| 59 |
-
axes[1].set_ylabel('Probability')
|
| 60 |
-
plt.xticks(rotation=45, ha="right")
|
| 61 |
-
|
| 62 |
-
plt.tight_layout()
|
| 63 |
-
|
| 64 |
-
return f"Fraud Detection{max(is_fraud_pred_prob)*100}: {'Fraudulent' if is_fraud_class == 1 else 'Not Fraudulent'}", f"Predicted Fraud Type: {fraud_type_class}", fig
|
| 65 |
-
|
| 66 |
-
# Gradio Interface Setup
|
| 67 |
-
interface = gr.Interface(
|
| 68 |
-
fn=predict_fraud,
|
| 69 |
-
inputs=[
|
| 70 |
-
gr.Number(label="Transaction Amount"),
|
| 71 |
-
gr.Dropdown(choices=['MasterCard', 'Visa' ,'Rupay'],label="Card Type"),
|
| 72 |
-
gr.Dropdown(choices=['Surat' ,'Hyderabad' ,'Kolkata' ,'Mumbai' ,'Delhi' ,'Chennai', 'Jaipur',
|
| 73 |
-
'Ahmedabad', 'Bangalore' ,'Pune'],label="Location"),
|
| 74 |
-
gr.Dropdown(choices=['POS' ,'Digital'],label="Purchase Category"),
|
| 75 |
-
gr.Number(label="Customer Age"),
|
| 76 |
-
gr.Dropdown(choices=['night', 'morning', 'afternoon', 'evening'],label="Time of Day")
|
| 77 |
-
],
|
| 78 |
-
outputs=[
|
| 79 |
-
gr.Markdown(label="Fraud Detection Result"),
|
| 80 |
-
gr.Markdown(label="Fraud Type Result"),
|
| 81 |
-
gr.Plot(label="Probabilities Bar Chart")
|
| 82 |
-
],
|
| 83 |
-
title="Fraud Detection & Fraud Type Prediction",
|
| 84 |
-
description="
|
| 85 |
-
|
| 86 |
-
# Launch Gradio Interface
|
| 87 |
-
interface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import pickle
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
# Load pre-trained models and encoders
|
| 8 |
+
with open('is_fraud.pkl', 'rb') as f:
|
| 9 |
+
is_fraud_model = pickle.load(f)
|
| 10 |
+
|
| 11 |
+
with open('fraud_type.pkl', 'rb') as f:
|
| 12 |
+
fraud_type_model = pickle.load(f)
|
| 13 |
+
|
| 14 |
+
# Load label encoders for features
|
| 15 |
+
encoders = {}
|
| 16 |
+
for feature in ['card_type', 'location', 'purchase_category', 'time_of_day']:
|
| 17 |
+
with open(f'{feature}.pkl', 'rb') as f:
|
| 18 |
+
encoders[feature] = pickle.load(f)
|
| 19 |
+
|
| 20 |
+
with open('fraud_type_le.pkl', 'rb') as f:
|
| 21 |
+
fraud_type_encoder = pickle.load(f)
|
| 22 |
+
|
| 23 |
+
# Define the prediction function
|
| 24 |
+
def predict_fraud(amount, card_type, location, purchase_category, customer_age, time_of_day):
|
| 25 |
+
# Preprocess input
|
| 26 |
+
input_data = pd.DataFrame({
|
| 27 |
+
'amount': [amount],
|
| 28 |
+
'card_type': [card_type],
|
| 29 |
+
'location': [location],
|
| 30 |
+
'purchase_category': [purchase_category],
|
| 31 |
+
'customer_age': [customer_age],
|
| 32 |
+
'time_of_day': [time_of_day]
|
| 33 |
+
})
|
| 34 |
+
|
| 35 |
+
# Label encode the inputs
|
| 36 |
+
for feature in ['card_type', 'location', 'purchase_category', 'time_of_day']:
|
| 37 |
+
input_data[feature] = encoders[feature].transform(input_data[feature])
|
| 38 |
+
|
| 39 |
+
# Fraud Detection (Binary Classification)
|
| 40 |
+
is_fraud_pred_prob = is_fraud_model.predict_proba(input_data)[0]
|
| 41 |
+
is_fraud_class = np.argmax(is_fraud_pred_prob)
|
| 42 |
+
|
| 43 |
+
# Fraud Type Classification (Multiclass Classification)
|
| 44 |
+
fraud_type_pred_prob = fraud_type_model.predict_proba(input_data)[0]
|
| 45 |
+
fraud_type_class = fraud_type_encoder.inverse_transform([np.argmax(fraud_type_pred_prob)])[0]
|
| 46 |
+
|
| 47 |
+
# Bar charts for probabilities
|
| 48 |
+
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
|
| 49 |
+
|
| 50 |
+
# Plot Fraud Detection Probabilities
|
| 51 |
+
axes[0].bar(['Not Fraudulent', 'Fraudulent'], is_fraud_pred_prob)
|
| 52 |
+
axes[0].set_title('Fraud Detection Probability')
|
| 53 |
+
axes[0].set_ylabel('Probability')
|
| 54 |
+
|
| 55 |
+
# Plot Fraud Type Probabilities
|
| 56 |
+
fraud_types = fraud_type_encoder.classes_
|
| 57 |
+
axes[1].bar(fraud_types, fraud_type_pred_prob)
|
| 58 |
+
axes[1].set_title('Fraud Type Probability')
|
| 59 |
+
axes[1].set_ylabel('Probability')
|
| 60 |
+
plt.xticks(rotation=45, ha="right")
|
| 61 |
+
|
| 62 |
+
plt.tight_layout()
|
| 63 |
+
|
| 64 |
+
return f"Fraud Detection{max(is_fraud_pred_prob)*100}: {'Fraudulent' if is_fraud_class == 1 else 'Not Fraudulent'}", f"Predicted Fraud Type: {fraud_type_class}", fig
|
| 65 |
+
|
| 66 |
+
# Gradio Interface Setup
|
| 67 |
+
interface = gr.Interface(
|
| 68 |
+
fn=predict_fraud,
|
| 69 |
+
inputs=[
|
| 70 |
+
gr.Number(label="Transaction Amount"),
|
| 71 |
+
gr.Dropdown(choices=['MasterCard', 'Visa' ,'Rupay'],label="Card Type"),
|
| 72 |
+
gr.Dropdown(choices=['Surat' ,'Hyderabad' ,'Kolkata' ,'Mumbai' ,'Delhi' ,'Chennai', 'Jaipur',
|
| 73 |
+
'Ahmedabad', 'Bangalore' ,'Pune'],label="Location"),
|
| 74 |
+
gr.Dropdown(choices=['POS' ,'Digital'],label="Purchase Category"),
|
| 75 |
+
gr.Number(label="Customer Age"),
|
| 76 |
+
gr.Dropdown(choices=['night', 'morning', 'afternoon', 'evening'],label="Time of Day")
|
| 77 |
+
],
|
| 78 |
+
outputs=[
|
| 79 |
+
gr.Markdown(label="Fraud Detection Result"),
|
| 80 |
+
gr.Markdown(label="Fraud Type Result"),
|
| 81 |
+
gr.Plot(label="Probabilities Bar Chart")
|
| 82 |
+
],
|
| 83 |
+
title="Fraud Detection & Fraud Type Prediction",
|
| 84 |
+
description="By detecting fraudulent transactions, this model helps protect users and businesses in the Indian digital landscape, fostering a safer and more trustworthy online environment.",theme=gr.themes.Soft())
|
| 85 |
+
|
| 86 |
+
# Launch Gradio Interface
|
| 87 |
+
interface.launch()
|