riyadahmadov
commited on
Commit
•
9ce6f17
1
Parent(s):
bc5528a
Update app.py
Browse files
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.
|
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 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
46 |
|
47 |
-
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
52 |
|
53 |
-
|
54 |
-
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
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
|
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()
|