Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 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")
|
|
@@ -20,32 +21,34 @@ def predict_need_maintenance ():
|
|
| 20 |
# Get JSON data from the request
|
| 21 |
engine_sensor_inputs = request.get_json ()
|
| 22 |
|
| 23 |
-
import datetime
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
data_info = {
|
| 29 |
-
'Engine_rpm' : engine_sensor_inputs ['Engine_rpm'],
|
| 30 |
-
'Lub_oil_pressure' : engine_sensor_inputs ['Lub_oil_pressure'],
|
| 31 |
-
'Fuel_pressure' : engine_sensor_inputs ['Fuel_pressure'],
|
| 32 |
-
'Coolant_pressure' : engine_sensor_inputs ['Coolant_pressure'],
|
| 33 |
-
'lub_oil_temp' : engine_sensor_inputs ['lub_oil_temp'],
|
| 34 |
-
'Coolant_temp' : engine_sensor_inputs ['Coolant_temp']
|
| 35 |
-
}
|
| 36 |
|
| 37 |
-
|
| 38 |
-
input_data = pd.DataFrame ([data_info])
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Run the Flask app
|
| 51 |
if __name__ == "__main__":
|
|
|
|
| 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")
|
|
|
|
| 21 |
# Get JSON data from the request
|
| 22 |
engine_sensor_inputs = request.get_json ()
|
| 23 |
|
|
|
|
| 24 |
|
| 25 |
+
try:
|
| 26 |
+
input_json = request.get_json()
|
| 27 |
+
input_df = pd.DataFrame([input_json])
|
| 28 |
|
| 29 |
+
validated_df = validate_and_prepare_input(input_df, model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
prediction = model.predict(validated_df)[0]
|
|
|
|
| 32 |
|
| 33 |
+
return jsonify({
|
| 34 |
+
"status": "success",
|
| 35 |
+
"prediction": int(prediction)
|
| 36 |
+
})
|
| 37 |
|
| 38 |
+
except InputValidationError as e:
|
| 39 |
+
return jsonify({
|
| 40 |
+
"status": "error",
|
| 41 |
+
"error_type": "validation_error",
|
| 42 |
+
"message": str(e)
|
| 43 |
+
}), 400
|
| 44 |
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return jsonify({
|
| 47 |
+
"status": "error",
|
| 48 |
+
"error_type": "internal_error",
|
| 49 |
+
"message": "Unexpected server error"
|
| 50 |
+
}), 500
|
| 51 |
+
|
| 52 |
|
| 53 |
# Run the Flask app
|
| 54 |
if __name__ == "__main__":
|