RoAr777 commited on
Commit
1ddaadd
·
verified ·
1 Parent(s): 17ecaca

Upload 12 files

Browse files
__results___files/__results___16_1.png ADDED
__results___files/__results___24_1.png ADDED
__results___files/__results___7_1.png ADDED
__results___files/__results___8_1.png ADDED
app.py ADDED
@@ -0,0 +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="Enter transaction details to predict whether it is fraudulent and the type of fraud.",theme=gr.themes.Soft())
85
+
86
+ # Launch Gradio Interface
87
+ interface.launch()
card_type.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1ff2b4f5e14406dbd6cb47c425241363e0e4ed4bbb79531559f4e1d06d655bcb
3
+ size 270
fraud_type.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1a65cf52537fa3ecbb1ef33fda5019ac91e91e72b9bc0e4e89ea9e730bab8e05
3
+ size 63175
fraud_type_le.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8ec57dc1c843288dbcf84da684832520133bbe179fb56fc345c3cd95d1eca405
3
+ size 308
is_fraud.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:854025fd806b08c16b0bca1c9086e1f92c5b1bece7e7297b4ebd8a0695b5fb8d
3
+ size 25783
location.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ea694c94debf38073dccfe683ad081c2b0d045d6022ae2781f07315f8aa48f5e
3
+ size 339
purchase_category.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:61987256eda305442de3f3251c1193e7fc508459e2f90d4fa956c053d668c07b
3
+ size 258
time_of_day.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:036b2ad5a116b419e2ead409493db14d9a2c5da9b8976aa8156c0ba335d2578a
3
+ size 282