bambadij commited on
Commit
0dae986
·
verified ·
1 Parent(s): becb531
Files changed (1) hide show
  1. app.py +14 -20
app.py CHANGED
@@ -7,10 +7,16 @@ import pandas as pd
7
  # Initialize FastAPI app
8
  app = FastAPI()
9
 
10
-
11
- # Load the trained Gradient Boosting model
12
  model = joblib.load('gradient_boosting_model.pkl')
13
  encoder = joblib.load("encoder.pkl")
 
 
 
 
 
 
 
14
  # Define the input data schema
15
  class PredictionInput(BaseModel):
16
  age: int
@@ -30,19 +36,6 @@ class PredictionInput(BaseModel):
30
  previous: int
31
  poutcome: str
32
 
33
- # Define the mapping for categorical variables (if encoded)
34
- categorical_mapping = {
35
- "job": ['admin.', 'technician', 'blue-collar', 'management', 'retired', 'services', 'self-employed', 'entrepreneur', 'unemployed', 'housemaid', 'student', 'unknown'],
36
- "marital": ['married', 'single', 'divorced'],
37
- "education": ['secondary', 'tertiary', 'primary', 'unknown'],
38
- "default": ['no', 'yes'],
39
- "housing": ['no', 'yes'],
40
- "loan": ['no', 'yes'],
41
- "contact": ['unknown', 'telephone', 'cellular'],
42
- "month": ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'],
43
- "poutcome": ['unknown', 'other', 'failure', 'success']
44
- }
45
-
46
  # Utility function to preprocess the input data
47
  def preprocess_input(data: PredictionInput):
48
  # Convert input data to a DataFrame for compatibility
@@ -64,17 +57,17 @@ def preprocess_input(data: PredictionInput):
64
  "previous": [data.previous],
65
  "poutcome": [data.poutcome],
66
  }
67
-
68
  input_df = pd.DataFrame(input_df)
69
 
70
  # Apply OneHotEncoder to categorical columns
71
  encoded_features = encoder.transform(input_df[categorical_columns])
72
-
73
  # Combine encoded features with numerical features
74
  numerical_features = input_df.drop(columns=categorical_columns).values
75
  final_features = np.concatenate([numerical_features, encoded_features], axis=1)
76
 
77
  return final_features
 
78
  # Define the GET endpoint to show the structure
79
  @app.get("/structure")
80
  async def get_structure():
@@ -98,16 +91,17 @@ async def get_structure():
98
  "poutcome": "success"
99
  }
100
  return example_data
 
101
  # Define a POST endpoint for predictions
102
  @app.post("/predict")
103
  async def predict(data: PredictionInput):
104
  # Preprocess the input
105
  input_data = preprocess_input(data)
106
-
107
  # Make a prediction
108
  prediction = model.predict(input_data)
109
-
110
  # Convert prediction to "yes"/"no"
111
  response = "yes" if prediction[0] == 1 else "no"
112
-
113
  return {"prediction": response}
 
7
  # Initialize FastAPI app
8
  app = FastAPI()
9
 
10
+ # Load the trained Gradient Boosting model and encoder
 
11
  model = joblib.load('gradient_boosting_model.pkl')
12
  encoder = joblib.load("encoder.pkl")
13
+
14
+ # Define categorical columns
15
+ categorical_columns = [
16
+ "job", "marital", "education", "default", "housing",
17
+ "loan", "contact", "month", "poutcome"
18
+ ]
19
+
20
  # Define the input data schema
21
  class PredictionInput(BaseModel):
22
  age: int
 
36
  previous: int
37
  poutcome: str
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  # Utility function to preprocess the input data
40
  def preprocess_input(data: PredictionInput):
41
  # Convert input data to a DataFrame for compatibility
 
57
  "previous": [data.previous],
58
  "poutcome": [data.poutcome],
59
  }
 
60
  input_df = pd.DataFrame(input_df)
61
 
62
  # Apply OneHotEncoder to categorical columns
63
  encoded_features = encoder.transform(input_df[categorical_columns])
64
+
65
  # Combine encoded features with numerical features
66
  numerical_features = input_df.drop(columns=categorical_columns).values
67
  final_features = np.concatenate([numerical_features, encoded_features], axis=1)
68
 
69
  return final_features
70
+
71
  # Define the GET endpoint to show the structure
72
  @app.get("/structure")
73
  async def get_structure():
 
91
  "poutcome": "success"
92
  }
93
  return example_data
94
+
95
  # Define a POST endpoint for predictions
96
  @app.post("/predict")
97
  async def predict(data: PredictionInput):
98
  # Preprocess the input
99
  input_data = preprocess_input(data)
100
+
101
  # Make a prediction
102
  prediction = model.predict(input_data)
103
+
104
  # Convert prediction to "yes"/"no"
105
  response = "yes" if prediction[0] == 1 else "no"
106
+
107
  return {"prediction": response}