Rakksk-30185 commited on
Commit
3ff469d
1 Parent(s): 913030f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -15
app.py CHANGED
@@ -10,10 +10,8 @@ import joblib
10
  import numpy as np
11
  import gradio as gr
12
 
13
- # Load the XGBoost model
14
  xgboost_model = joblib.load('xgboost_model_new.pkl')
15
-
16
- # Load the StandardScaler
17
  scaler = joblib.load('scaler.pkl')
18
 
19
  month_to_number = {
@@ -58,7 +56,7 @@ options = [
58
  'Travel',
59
  'Entertainment'
60
  ]
61
- # Define category options
62
  category_options = [
63
  'Food/Dining',
64
  'Gas/Transport',
@@ -76,28 +74,18 @@ category_options = [
76
  ]
77
 
78
  def predict_credit_card_fraud(amount, city_pop, month, hour, age, gender, category):
79
- # Map the input month name to its corresponding number
80
- month = month_to_number[month]
81
 
 
82
  time_of_day = time_of_dayy(hour)
83
-
84
- # Prepare input data with dummy variables for category
85
  input_data = np.array([[amount, city_pop, month, hour, age, int(gender == 'M'),
86
  int(time_of_day == 'Night'), int(time_of_day == 'Evening'), int(time_of_day == 'Morning')] +
87
  [int(category == cat) for cat in category_options]])
88
 
89
- # Scale the input data using the loaded StandardScaler
90
  input_data[:, 0:2] = scaler.transform(input_data[:, 0:2])
91
-
92
- # Use predict_proba to get probability scores for class 1
93
  probability = xgboost_model.predict_proba(input_data)[:, 1]
94
-
95
- # Make predictions using the XGBoost model
96
  prediction = xgboost_model.predict(input_data)
97
 
98
  label = "Fraudulent" if prediction[0] == 1 else "Non-Fraudulent"
99
-
100
- # Return the probability score
101
  return round(probability[0], 2), label
102
 
103
  gender_options = ["M", "F"]
 
10
  import numpy as np
11
  import gradio as gr
12
 
13
+ # Load Model and Scaler:
14
  xgboost_model = joblib.load('xgboost_model_new.pkl')
 
 
15
  scaler = joblib.load('scaler.pkl')
16
 
17
  month_to_number = {
 
56
  'Travel',
57
  'Entertainment'
58
  ]
59
+
60
  category_options = [
61
  'Food/Dining',
62
  'Gas/Transport',
 
74
  ]
75
 
76
  def predict_credit_card_fraud(amount, city_pop, month, hour, age, gender, category):
 
 
77
 
78
+ month = month_to_number[month]
79
  time_of_day = time_of_dayy(hour)
 
 
80
  input_data = np.array([[amount, city_pop, month, hour, age, int(gender == 'M'),
81
  int(time_of_day == 'Night'), int(time_of_day == 'Evening'), int(time_of_day == 'Morning')] +
82
  [int(category == cat) for cat in category_options]])
83
 
 
84
  input_data[:, 0:2] = scaler.transform(input_data[:, 0:2])
 
 
85
  probability = xgboost_model.predict_proba(input_data)[:, 1]
 
 
86
  prediction = xgboost_model.predict(input_data)
87
 
88
  label = "Fraudulent" if prediction[0] == 1 else "Non-Fraudulent"
 
 
89
  return round(probability[0], 2), label
90
 
91
  gender_options = ["M", "F"]