Upload folder using huggingface_hub
Browse files- app.py +31 -26
- utils/__init__.py +1 -0
- utils/validation.py +6 -5
app.py
CHANGED
|
@@ -2,12 +2,13 @@
|
|
| 2 |
import joblib
|
| 3 |
import pandas as pd
|
| 4 |
from flask import Flask, request, jsonify
|
|
|
|
| 5 |
|
| 6 |
# Initialize Flask app with a name
|
| 7 |
pred_mainteanance_api = Flask ("Engine Maintenance Predictor")
|
| 8 |
|
| 9 |
# Load the trained churn prediction model
|
| 10 |
-
model = joblib.load ("best_eng_fail_pred_model.joblib
|
| 11 |
|
| 12 |
# Define a route for the home page
|
| 13 |
@pred_mainteanance_api.get ('/')
|
|
@@ -20,31 +21,35 @@ def predict_need_maintenance ():
|
|
| 20 |
# Get JSON data from the request
|
| 21 |
engine_sensor_inputs = request.get_json ()
|
| 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 |
# Run the Flask app
|
|
|
|
| 2 |
import joblib
|
| 3 |
import pandas as pd
|
| 4 |
from flask import Flask, request, jsonify
|
| 5 |
+
from utils.validation import validate_and_prepare_input, InputValidationError
|
| 6 |
|
| 7 |
# Initialize Flask app with a name
|
| 8 |
pred_mainteanance_api = Flask ("Engine Maintenance Predictor")
|
| 9 |
|
| 10 |
# Load the trained churn prediction model
|
| 11 |
+
model = joblib.load ("best_eng_fail_pred_model.joblib")
|
| 12 |
|
| 13 |
# Define a route for the home page
|
| 14 |
@pred_mainteanance_api.get ('/')
|
|
|
|
| 21 |
# Get JSON data from the request
|
| 22 |
engine_sensor_inputs = request.get_json ()
|
| 23 |
|
| 24 |
+
# validate request (json)
|
| 25 |
+
# if input is valid - return prediction
|
| 26 |
+
# in case of error - return appropriate error
|
| 27 |
+
try:
|
| 28 |
+
input_json = request.get_json()
|
| 29 |
+
input_df = pd.DataFrame([input_json])
|
| 30 |
+
|
| 31 |
+
validated_df = validate_and_prepare_input(input_df, model)
|
| 32 |
+
|
| 33 |
+
prediction = model.predict(validated_df)[0]
|
| 34 |
+
|
| 35 |
+
return jsonify({
|
| 36 |
+
"status": "success",
|
| 37 |
+
"prediction": int(prediction)
|
| 38 |
+
})
|
| 39 |
+
|
| 40 |
+
except InputValidationError as e:
|
| 41 |
+
return jsonify({
|
| 42 |
+
"status": "error",
|
| 43 |
+
"error_type": "validation_error",
|
| 44 |
+
"message": str(e)
|
| 45 |
+
}), 400
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return jsonify({
|
| 49 |
+
"status": "error",
|
| 50 |
+
"error_type": "internal_error",
|
| 51 |
+
"message": "Unexpected server error"
|
| 52 |
+
}), 500
|
| 53 |
|
| 54 |
|
| 55 |
# Run the Flask app
|
utils/__init__.py
CHANGED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
|
utils/validation.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
|
| 3 |
|
|
@@ -22,7 +23,7 @@ def validate_and_prepare_input(input_df: pd.DataFrame, model):
|
|
| 22 |
raise InputValidationError("Unable to retrieve model feature names.")
|
| 23 |
|
| 24 |
# -------------------------
|
| 25 |
-
# 1
|
| 26 |
# -------------------------
|
| 27 |
missing_cols = set(expected_features) - set(input_df.columns)
|
| 28 |
if missing_cols:
|
|
@@ -31,7 +32,7 @@ def validate_and_prepare_input(input_df: pd.DataFrame, model):
|
|
| 31 |
)
|
| 32 |
|
| 33 |
# -------------------------
|
| 34 |
-
# 2
|
| 35 |
# -------------------------
|
| 36 |
extra_cols = set(input_df.columns) - set(expected_features)
|
| 37 |
if extra_cols:
|
|
@@ -40,7 +41,7 @@ def validate_and_prepare_input(input_df: pd.DataFrame, model):
|
|
| 40 |
)
|
| 41 |
|
| 42 |
# -------------------------
|
| 43 |
-
# 3
|
| 44 |
# -------------------------
|
| 45 |
for col in expected_features:
|
| 46 |
if not pd.api.types.is_numeric_dtype(input_df[col]):
|
|
@@ -49,8 +50,8 @@ def validate_and_prepare_input(input_df: pd.DataFrame, model):
|
|
| 49 |
)
|
| 50 |
|
| 51 |
# -------------------------
|
| 52 |
-
# 4
|
| 53 |
# -------------------------
|
| 54 |
input_df = input_df[expected_features]
|
| 55 |
|
| 56 |
-
return input_df
|
|
|
|
| 1 |
+
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
|
|
|
|
| 23 |
raise InputValidationError("Unable to retrieve model feature names.")
|
| 24 |
|
| 25 |
# -------------------------
|
| 26 |
+
# 1 Check missing columns
|
| 27 |
# -------------------------
|
| 28 |
missing_cols = set(expected_features) - set(input_df.columns)
|
| 29 |
if missing_cols:
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
# -------------------------
|
| 35 |
+
# 2 Check extra columns
|
| 36 |
# -------------------------
|
| 37 |
extra_cols = set(input_df.columns) - set(expected_features)
|
| 38 |
if extra_cols:
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
# -------------------------
|
| 44 |
+
# 3 Enforce numeric types
|
| 45 |
# -------------------------
|
| 46 |
for col in expected_features:
|
| 47 |
if not pd.api.types.is_numeric_dtype(input_df[col]):
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
# -------------------------
|
| 53 |
+
# 4 Reorder columns safely
|
| 54 |
# -------------------------
|
| 55 |
input_df = input_df[expected_features]
|
| 56 |
|
| 57 |
+
return input_df
|