Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,14 +19,15 @@ with open("pca_model.pkl", "rb") as file:
|
|
| 19 |
# Dummy data for generating choices
|
| 20 |
df = pd.read_csv('synthetic_financial_data.csv')
|
| 21 |
|
| 22 |
-
#
|
| 23 |
def group(x):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
# Choices for dropdown menus
|
| 32 |
card_type_choices = [(val, val) for val in df['card_type'].unique()]
|
|
@@ -49,11 +50,7 @@ def predict(transaction_id, customer_id, merchant_id, amount, transaction_time,
|
|
| 49 |
data['Age_group'] = data['customer_age'].apply(lambda x: group(x))
|
| 50 |
data = pd.get_dummies(data, columns=['Age_group'], drop_first=True, dtype=float)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
columns_to_add = ['customer_age',
|
| 54 |
-
'card_type_American Express', 'card_type_Discover', 'card_type_MasterCard', 'card_type_Visa',
|
| 55 |
-
'purchase_category_Gas Station', 'purchase_category_Groceries', 'purchase_category_Online Shopping', 'purchase_category_Restaurant', 'purchase_category_Retail', 'purchase_category_Travel',
|
| 56 |
-
'Age_group_33-60', 'Age_group_60+']
|
| 57 |
|
| 58 |
# Add missing columns with default value of 0
|
| 59 |
for column in columns_to_add:
|
|
@@ -61,31 +58,27 @@ def predict(transaction_id, customer_id, merchant_id, amount, transaction_time,
|
|
| 61 |
data[column] = 0
|
| 62 |
|
| 63 |
for i in data.columns:
|
| 64 |
-
|
| 65 |
|
| 66 |
# Perform PCA
|
| 67 |
X_pca = pca.transform(data[['purchase_category_Groceries', 'purchase_category_Retail', 'purchase_category_Travel', 'Age_group_33-60', 'Age_group_60+']])
|
| 68 |
data['pca1'] = X_pca[:, 0]
|
| 69 |
data['pca2'] = X_pca[:, 1]
|
| 70 |
-
|
| 71 |
-
data.drop(columns=['customer_age'], inplace=True) # Remove 'customer_age' as it's already included in Age_group
|
| 72 |
|
|
|
|
| 73 |
# Make predictions
|
| 74 |
prediction = clf.predict(data)
|
| 75 |
-
|
| 76 |
-
# Custom output for specific IDs
|
| 77 |
-
return f'Customer with ID {customer_id} and Transaction ID {transaction_id}, which happened at {transaction_time}, is {result}'
|
| 78 |
except Exception as e:
|
| 79 |
return str(e)
|
| 80 |
|
| 81 |
-
|
| 82 |
# Define Gradio interface
|
| 83 |
inputs = [
|
| 84 |
gr.Textbox(label="Transaction ID"),
|
| 85 |
gr.Textbox(label="Customer ID"),
|
| 86 |
gr.Textbox(label="Merchant ID"),
|
| 87 |
gr.Number(label="Amount"),
|
| 88 |
-
gr.Textbox(label="Transaction
|
| 89 |
gr.Dropdown(choices=card_type_choices, label="Card Type"),
|
| 90 |
gr.Dropdown(choices=location_choices, label="Location"),
|
| 91 |
gr.Dropdown(choices=purchase_category_choices, label="Purchase Category"),
|
|
|
|
| 19 |
# Dummy data for generating choices
|
| 20 |
df = pd.read_csv('synthetic_financial_data.csv')
|
| 21 |
|
| 22 |
+
# Let's create new column from age column
|
| 23 |
def group(x):
|
| 24 |
+
if x < 33:
|
| 25 |
+
a = '18-32'
|
| 26 |
+
elif x < 60:
|
| 27 |
+
a = '33-60'
|
| 28 |
+
elif x >= 60:
|
| 29 |
+
a = '60+'
|
| 30 |
+
return a
|
| 31 |
|
| 32 |
# Choices for dropdown menus
|
| 33 |
card_type_choices = [(val, val) for val in df['card_type'].unique()]
|
|
|
|
| 50 |
data['Age_group'] = data['customer_age'].apply(lambda x: group(x))
|
| 51 |
data = pd.get_dummies(data, columns=['Age_group'], drop_first=True, dtype=float)
|
| 52 |
|
| 53 |
+
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+']
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# Add missing columns with default value of 0
|
| 56 |
for column in columns_to_add:
|
|
|
|
| 58 |
data[column] = 0
|
| 59 |
|
| 60 |
for i in data.columns:
|
| 61 |
+
data[i] = data[i].astype(int)
|
| 62 |
|
| 63 |
# Perform PCA
|
| 64 |
X_pca = pca.transform(data[['purchase_category_Groceries', 'purchase_category_Retail', 'purchase_category_Travel', 'Age_group_33-60', 'Age_group_60+']])
|
| 65 |
data['pca1'] = X_pca[:, 0]
|
| 66 |
data['pca2'] = X_pca[:, 1]
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
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)
|
| 69 |
# Make predictions
|
| 70 |
prediction = clf.predict(data)
|
| 71 |
+
return "Fraudulent" if prediction[0] == 1 else "Not Fraudulent"
|
|
|
|
|
|
|
| 72 |
except Exception as e:
|
| 73 |
return str(e)
|
| 74 |
|
|
|
|
| 75 |
# Define Gradio interface
|
| 76 |
inputs = [
|
| 77 |
gr.Textbox(label="Transaction ID"),
|
| 78 |
gr.Textbox(label="Customer ID"),
|
| 79 |
gr.Textbox(label="Merchant ID"),
|
| 80 |
gr.Number(label="Amount"),
|
| 81 |
+
gr.Textbox(label="Transaction Time"),
|
| 82 |
gr.Dropdown(choices=card_type_choices, label="Card Type"),
|
| 83 |
gr.Dropdown(choices=location_choices, label="Location"),
|
| 84 |
gr.Dropdown(choices=purchase_category_choices, label="Purchase Category"),
|