Update app.py
Browse files
app.py
CHANGED
|
@@ -52,6 +52,51 @@ def predict_need_maintenance ():
|
|
| 52 |
}), 500
|
| 53 |
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
# Run the Flask app
|
| 56 |
if __name__ == "__main__":
|
| 57 |
import os
|
|
|
|
| 52 |
}), 500
|
| 53 |
|
| 54 |
|
| 55 |
+
# Define an endpoint to predict sales for Super Kart
|
| 56 |
+
@pred_mainteanance_api.post ('/v1/EngPredMaintenanceForBatch')
|
| 57 |
+
def predict_need_maintenance_for_batch ():
|
| 58 |
+
# Get JSON data from the request
|
| 59 |
+
engine_sensor_inputs = request.get_json ()
|
| 60 |
+
|
| 61 |
+
# validate request (json)
|
| 62 |
+
# if input is valid - return prediction
|
| 63 |
+
# in case of error - return appropriate error
|
| 64 |
+
try:
|
| 65 |
+
input_json = request.get_json()
|
| 66 |
+
|
| 67 |
+
# Convert JSON list → DataFrame
|
| 68 |
+
input_df = pd.DataFrame([input_json])
|
| 69 |
+
|
| 70 |
+
# Validate entire batch
|
| 71 |
+
validated_df = validate_and_prepare_input(input_df, model)
|
| 72 |
+
|
| 73 |
+
# predict for given input
|
| 74 |
+
predictions = model.predict(validated_df)
|
| 75 |
+
|
| 76 |
+
# Convert numpy array → Python list
|
| 77 |
+
prediction_list = predictions.tolist()
|
| 78 |
+
|
| 79 |
+
return jsonify({
|
| 80 |
+
"status": "success", # overall batch status
|
| 81 |
+
"total_records": len(prediction_list),
|
| 82 |
+
"predictions": prediction_list, # simple list version
|
| 83 |
+
})
|
| 84 |
+
|
| 85 |
+
except InputValidationError as e:
|
| 86 |
+
return jsonify({
|
| 87 |
+
"status": "error",
|
| 88 |
+
"error_type": "validation_error",
|
| 89 |
+
"message": str(e)
|
| 90 |
+
}), 400
|
| 91 |
+
|
| 92 |
+
except Exception as e:
|
| 93 |
+
return jsonify({
|
| 94 |
+
"status": "error",
|
| 95 |
+
"error_type": "internal_error",
|
| 96 |
+
"message": "Unexpected server error"
|
| 97 |
+
}), 500
|
| 98 |
+
|
| 99 |
+
|
| 100 |
# Run the Flask app
|
| 101 |
if __name__ == "__main__":
|
| 102 |
import os
|