riyadahmadov commited on
Commit
9ce6f17
1 Parent(s): bc5528a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -33
app.py CHANGED
@@ -17,12 +17,7 @@ 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()]
@@ -32,37 +27,40 @@ 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],
37
- 'country': [country],
38
- 'customer_age': [customer_age],
39
- 'card_type': [card_type],
40
- 'purchase_category': [purchase_category]
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+']])
58
- data['pca1'] = X_pca[:, 0]
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 = [
@@ -79,5 +77,5 @@ inputs = [
79
  gr.Dropdown(choices=country_choices, label="Country")
80
  ]
81
 
82
- # Define Gradio interfacefg
83
  gr.Interface(fn=predict, inputs=inputs, outputs="text").launch()
 
17
  pca = pickle.load(file)
18
 
19
  # Dummy data for generating choices
20
+ df = pd.read_csv('/content/synthetic_financial_data.csv')
 
 
 
 
 
21
 
22
  # Choices for dropdown menus
23
  card_type_choices = [(val, val) for val in df['card_type'].unique()]
 
27
 
28
  # Function to predict
29
  def predict(transaction_id, customer_id, merchant_id, amount, transaction_time, card_type, location, purchase_category, customer_age, transaction_description, country):
30
+ try:
31
+ data = pd.DataFrame({
32
+ 'amount': [amount],
33
+ 'country': [country],
34
+ 'customer_age': [customer_age],
35
+ 'card_type': [card_type],
36
+ 'purchase_category': [purchase_category]
37
+ })
38
+ data = pd.get_dummies(data, columns=['card_type', 'purchase_category'])
39
+ data['country'] = data['country'].map(fraud_mean_by_country_dict)
40
+ data['Age_group'] = data['customer_age'].apply(lambda x: group(x))
41
+ data = pd.get_dummies(data, columns=['Age_group'], drop_first=True, dtype=float)
42
 
43
+ columns_to_add = ['customer_age','card_type_MasterCard','purchase_category_Online Shopping' ,'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+']
44
 
45
+ # Add missing columns with default value of 0
46
+ for column in columns_to_add:
47
+ if column not in data.columns:
48
+ data[column] = 0
49
 
50
+ for i in data.columns:
51
+ data[i] = data[i].astype(int)
52
 
53
+ # Perform PCA
54
+ X_pca = pca.transform(data[['purchase_category_Groceries', 'purchase_category_Retail', 'purchase_category_Travel', 'Age_group_33-60', 'Age_group_60+']])
55
+ data['pca1'] = X_pca[:, 0]
56
+ data['pca2'] = X_pca[:, 1]
57
 
58
+ 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)
59
+ # Make predictions
60
+ prediction = clf.predict(data)
61
+ return "Fraudulent" if prediction[0] == 1 else "Not Fraudulent"
62
+ except Exception as e:
63
+ return str(e)
64
 
65
  # Define Gradio interface
66
  inputs = [
 
77
  gr.Dropdown(choices=country_choices, label="Country")
78
  ]
79
 
80
+ # Define Gradio interface
81
  gr.Interface(fn=predict, inputs=inputs, outputs="text").launch()