riyadahmadov commited on
Commit
0c8cb7b
·
verified ·
1 Parent(s): 34d60bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -20
app.py CHANGED
@@ -1,21 +1,36 @@
1
  import pandas as pd
2
- from sklearn.tree import DecisionTreeClassifier
3
- from sklearn.model_selection import train_test_split
4
- from sklearn.metrics import accuracy_score
5
- from sklearn.model_selection import cross_val_score
6
  import gradio as gr
7
- from sklearn.decomposition import PCA
8
  import json
9
  import pickle
 
10
 
11
- # Open and read the JSON file
12
  with open('fraud_mean_by_country.json', 'r') as json_file:
13
  fraud_mean_by_country_dict = json.load(json_file)
14
 
 
15
  with open("decision_tree_model.pkl", "rb") as file:
16
  clf = pickle.load(file)
17
 
18
- # Predict using the decision tree classifier
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def predict(transaction_id, customer_id, merchant_id, amount, transaction_time, card_type, location, purchase_category, customer_age, transaction_description, country):
20
  data = pd.DataFrame({
21
  'amount': [amount],
@@ -26,18 +41,17 @@ def predict(transaction_id, customer_id, merchant_id, amount, transaction_time,
26
  })
27
  data = pd.get_dummies(data, columns=['card_type', 'purchase_category'])
28
  data['country'] = data['country'].map(fraud_mean_by_country_dict)
29
- data['Age_group'] = data['customer_age'].apply(lambda x: group(x))
30
  data = pd.get_dummies(data, columns=['Age_group'], drop_first=True, dtype=float)
31
 
32
  columns_to_add = ['customer_age', 'card_type_Discover', 'card_type_Visa', 'purchase_category_Groceries', 'purchase_category_Restaurant','purchase_category_Retail', 'purchase_category_Travel', 'Age_group_33-60', 'Age_group_60+']
33
 
34
- # Add missing columns with default value of 0
35
  for column in columns_to_add:
36
  if column not in data.columns:
37
  data[column] = 0
38
 
39
  for i in data.columns:
40
- data[i] = data[i].astype(int)
41
 
42
  # Perform PCA
43
  X_pca = pca.transform(data[['purchase_category_Groceries', 'purchase_category_Retail', 'purchase_category_Travel', 'Age_group_33-60', 'Age_group_60+']])
@@ -45,16 +59,11 @@ def predict(transaction_id, customer_id, merchant_id, amount, transaction_time,
45
  data['pca2'] = X_pca[:, 1]
46
 
47
  data.drop(columns=['customer_age', 'card_type_Discover', 'card_type_Visa', 'purchase_category_Groceries', 'purchase_category_Retail', 'purchase_category_Travel', 'Age_group_33-60', 'Age_group_60+'], inplace=True)
 
48
  # Make predictions
49
  prediction = clf.predict(data)
50
  return "Fraudulent" if prediction[0] == 1 else "Not Fraudulent"
51
 
52
-
53
- card_type_choices = [(val, val) for val in df['card_type'].unique()]
54
- location_choices = [(val, val) for val in df['location'].unique()]
55
- purchase_category_choices = [(val, val) for val in df['purchase_category'].unique()]
56
- country_choices = [(val, val) for val in df['country'].unique()]
57
-
58
  # Define Gradio interface
59
  inputs = [
60
  gr.Textbox(label="Transaction ID"),
@@ -62,12 +71,12 @@ inputs = [
62
  gr.Textbox(label="Merchant ID"),
63
  gr.Number(label="Amount"),
64
  gr.Textbox(label="Transaction Time"),
65
- gr.Dropdown(choices= card_type_choices, label="Card Type"),
66
- gr.Dropdown(choices= location_choices, label="Location"),
67
- gr.Dropdown(choices= purchase_category_choices, label="Purchase Category"),
68
  gr.Number(label="Customer Age"),
69
  gr.Textbox(label="Transaction Description"),
70
- gr.Dropdown(choices= country_choices, label="Country")
71
  ]
72
 
73
  # Define Gradio interface
 
1
  import pandas as pd
 
 
 
 
2
  import gradio as gr
 
3
  import json
4
  import pickle
5
+ from sklearn.decomposition import PCA
6
 
7
+ # Load the JSON file
8
  with open('fraud_mean_by_country.json', 'r') as json_file:
9
  fraud_mean_by_country_dict = json.load(json_file)
10
 
11
+ # Load the decision tree model
12
  with open("decision_tree_model.pkl", "rb") as file:
13
  clf = pickle.load(file)
14
 
15
+ # Load PCA model
16
+ with open("pca_model.pkl", "rb") as file:
17
+ pca = pickle.load(file)
18
+
19
+ # Dummy data for generating choices
20
+ df = pd.DataFrame({
21
+ 'card_type': ['Visa', 'MasterCard', 'Discover'],
22
+ 'location': ['Location1', 'Location2'],
23
+ 'purchase_category': ['Groceries', 'Travel', 'Retail'],
24
+ 'country': ['USA', 'Canada']
25
+ })
26
+
27
+ # Choices for dropdown menus
28
+ card_type_choices = [(val, val) for val in df['card_type'].unique()]
29
+ location_choices = [(val, val) for val in df['location'].unique()]
30
+ purchase_category_choices = [(val, val) for val in df['purchase_category'].unique()]
31
+ country_choices = [(val, val) for val in df['country'].unique()]
32
+
33
+ # Function to predict
34
  def predict(transaction_id, customer_id, merchant_id, amount, transaction_time, card_type, location, purchase_category, customer_age, transaction_description, country):
35
  data = pd.DataFrame({
36
  'amount': [amount],
 
41
  })
42
  data = pd.get_dummies(data, columns=['card_type', 'purchase_category'])
43
  data['country'] = data['country'].map(fraud_mean_by_country_dict)
44
+ data['Age_group'] = data['customer_age'].apply(lambda x: 'Age_group_60+' if x > 60 else ('Age_group_33-60' if x >= 33 else 'Age_group_<33'))
45
  data = pd.get_dummies(data, columns=['Age_group'], drop_first=True, dtype=float)
46
 
47
  columns_to_add = ['customer_age', 'card_type_Discover', 'card_type_Visa', 'purchase_category_Groceries', 'purchase_category_Restaurant','purchase_category_Retail', 'purchase_category_Travel', 'Age_group_33-60', 'Age_group_60+']
48
 
 
49
  for column in columns_to_add:
50
  if column not in data.columns:
51
  data[column] = 0
52
 
53
  for i in data.columns:
54
+ data[i] = data[i].astype(int)
55
 
56
  # Perform PCA
57
  X_pca = pca.transform(data[['purchase_category_Groceries', 'purchase_category_Retail', 'purchase_category_Travel', 'Age_group_33-60', 'Age_group_60+']])
 
59
  data['pca2'] = X_pca[:, 1]
60
 
61
  data.drop(columns=['customer_age', 'card_type_Discover', 'card_type_Visa', 'purchase_category_Groceries', 'purchase_category_Retail', 'purchase_category_Travel', 'Age_group_33-60', 'Age_group_60+'], inplace=True)
62
+
63
  # Make predictions
64
  prediction = clf.predict(data)
65
  return "Fraudulent" if prediction[0] == 1 else "Not Fraudulent"
66
 
 
 
 
 
 
 
67
  # Define Gradio interface
68
  inputs = [
69
  gr.Textbox(label="Transaction ID"),
 
71
  gr.Textbox(label="Merchant ID"),
72
  gr.Number(label="Amount"),
73
  gr.Textbox(label="Transaction Time"),
74
+ gr.Dropdown(choices=card_type_choices, label="Card Type"),
75
+ gr.Dropdown(choices=location_choices, label="Location"),
76
+ gr.Dropdown(choices=purchase_category_choices, label="Purchase Category"),
77
  gr.Number(label="Customer Age"),
78
  gr.Textbox(label="Transaction Description"),
79
+ gr.Dropdown(choices=country_choices, label="Country")
80
  ]
81
 
82
  # Define Gradio interface