-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
267 lines (222 loc) · 7.76 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""Spaceship Titanic Prediction API."""
import logging
import traceback
from typing import Dict, Any
from flask import Flask, request, jsonify, render_template
from pydantic import BaseModel, Field
import h2o
import pandas as pd
import numpy as np
app = Flask(__name__)
logging.basicConfig(level=logging.DEBUG)
# Initialize MODEL at the module level
MODEL = None
class SpaceshipPassenger(BaseModel):
"""Represents a passenger on the Spaceship Titanic."""
home_planet: str = Field(..., description="Passenger's home planet")
cryo_sleep: bool = Field(
..., description="Whether the passenger was in cryo sleep"
)
cabin: str = Field(..., description="Passenger's cabin")
destination: str = Field(..., description="Passenger's destination")
age: int = Field(..., description="Passenger's age")
vip: bool = Field(..., description="Whether the passenger is a VIP")
room_service: int = Field(0, description="Amount spent on room service")
food_court: int = Field(0, description="Amount spent at food court")
shopping_mall: int = Field(0, description="Amount spent at shopping mall")
spa: int = Field(0, description="Amount spent at spa")
vr_deck: int = Field(0, description="Amount spent on VR deck")
name: str = Field(None, description="Passenger's name")
def detect_anomalies_iqr(df: pd.DataFrame, features: list) -> pd.DataFrame:
"""Detect anomalies using the Interquartile Range method."""
anomalies_list = []
for feature in features:
if feature not in df.columns:
logging.warning("Feature '%s' not found in DataFrame.", feature)
continue
if not np.issubdtype(df[feature].dtype, np.number):
logging.warning(
"Feature '%s' is not numerical and will be skipped.", feature
)
continue
q1 = df[feature].quantile(0.25)
q3 = df[feature].quantile(0.75)
iqr = q3 - q1
lower_bound = q1 - 1.5 * iqr
upper_bound = q3 + 1.5 * iqr
feature_anomalies = df[
(df[feature] < lower_bound) | (df[feature] > upper_bound)
]
if not feature_anomalies.empty:
logging.info(
"Anomalies detected in feature '%s': %s",
feature,
feature_anomalies,
)
else:
logging.info("No anomalies detected in feature '%s'.", feature)
anomalies_list.append(feature_anomalies)
if anomalies_list:
anomalies = (
pd.concat(anomalies_list).drop_duplicates().reset_index(drop=True)
)
return anomalies[features]
return pd.DataFrame(columns=features)
def engineer_spaceship_features(df: pd.DataFrame) -> pd.DataFrame:
"""Engineer features for the Spaceship Titanic dataset."""
df["TotalSpending"] = (
df["RoomService"]
+ df["FoodCourt"]
+ df["ShoppingMall"]
+ df["Spa"]
+ df["VRDeck"]
)
df["CabinDeck"] = df["Cabin"].str[0]
df["CabinNumber"] = df["Cabin"].str.split("/").str[1].astype(float)
df["CabinSide"] = df["Cabin"].str[-1]
df["GroupSize"] = (
1 # Since we're dealing with a single passenger, set GroupSize to 1
)
df["AgeGroup"] = pd.cut(
df["Age"],
bins=[0, 18, 65, float("inf")],
labels=["Child", "Adult", "Senior"],
)
df["HomePlanetCryoSleep"] = (
df["HomePlanet"] + "_" + df["CryoSleep"].astype(str)
)
return df
def preprocess_data(passenger: SpaceshipPassenger) -> pd.DataFrame:
"""Preprocess passenger data for prediction."""
data = pd.DataFrame([passenger.dict()])
# Rename columns to match feature names expected in the data engineering
column_mapping = {
"home_planet": "HomePlanet",
"cryo_sleep": "CryoSleep",
"destination": "Destination",
"age": "Age",
"vip": "VIP",
"room_service": "RoomService",
"food_court": "FoodCourt",
"shopping_mall": "ShoppingMall",
"spa": "Spa",
"vr_deck": "VRDeck",
"cabin": "Cabin",
"name": "Name",
}
data.rename(columns=column_mapping, inplace=True)
# Perform feature engineering
data = engineer_spaceship_features(data)
# Detect anomalies
numerical_features = [
"Age",
"RoomService",
"FoodCourt",
"ShoppingMall",
"Spa",
"VRDeck",
"TotalSpending",
"CabinNumber",
]
anomalies = detect_anomalies_iqr(data, numerical_features)
data["IsAnomaly"] = data.index.isin(anomalies.index).astype(int)
return data
def predict(passenger: SpaceshipPassenger) -> Dict[str, Any]:
"""Make a prediction for a passenger."""
preprocessed_data = preprocess_data(passenger)
logging.debug(
"Preprocessed data: %s", preprocessed_data.to_dict(orient="records")
)
# Convert to H2OFrame
h2o_frame = h2o.H2OFrame(preprocessed_data)
# Ensure all columns from the training data are present
model_columns = [
"PassengerId",
"HomePlanet",
"CryoSleep",
"Cabin",
"Destination",
"Age",
"VIP",
"RoomService",
"FoodCourt",
"ShoppingMall",
"Spa",
"VRDeck",
"Name",
"IsAnomaly",
"TotalSpending",
"CabinDeck",
"CabinNumber",
"CabinSide",
"GroupSize",
"AgeGroup",
"HomePlanetCryoSleep",
]
for col in model_columns:
if col not in h2o_frame.columns:
h2o_frame[col] = None
# Convert categorical columns to enum type
categorical_columns = [
"HomePlanet",
"CryoSleep",
"Cabin",
"Destination",
"VIP",
"IsAnomaly",
"CabinDeck",
"CabinSide",
"AgeGroup",
"HomePlanetCryoSleep",
]
for col in categorical_columns:
h2o_frame[col] = h2o_frame[col].asfactor()
predictions = MODEL.predict(h2o_frame)
logging.debug("Prediction columns: %s", predictions.columns)
if "predict" in predictions.columns and "True" in predictions.columns:
transported = bool(predictions["predict"][0, 0])
probability = float(
predictions["True"][0, 0]
) # Probability of being transported
else:
raise ValueError("Unexpected prediction structure")
return {"transported": transported, "transported_probability": probability}
@app.route("/")
def home():
"""Render the home page."""
return render_template("index.html")
@app.route("/predict", methods=["POST"])
def make_prediction():
"""Make a prediction based on input data."""
try:
logging.debug("Received data: %s", request.json)
passenger = SpaceshipPassenger(**request.json)
result = predict(passenger)
logging.debug("Prediction result: %s", result)
return jsonify(result)
except (ValueError, KeyError, TypeError) as error:
logging.error("Error during prediction: %s", str(error), exc_info=True)
return (
jsonify({"error": str(error), "details": traceback.format_exc()}),
400,
)
@app.route("/model-info", methods=["GET"])
def model_info():
"""Get information about the model."""
try:
model_columns = MODEL.model_json["output"]["names"]
return jsonify({"expected_columns": model_columns})
except (AttributeError, KeyError) as error:
logging.error(
"Error fetching model info: %s", str(error), exc_info=True
)
return (
jsonify({"error": str(error), "details": traceback.format_exc()}),
400,
)
if __name__ == "__main__":
h2o.init()
MODEL = h2o.load_model(
"/app/models/StackedEnsemble_Best1000_1_AutoML_1_20240811_214618"
)
app.run(host="0.0.0.0", port=8080, debug=True)