|
|
| |
| import joblib |
|
|
| |
| import pandas as pd |
|
|
| |
| from flask import Flask, request, jsonify |
|
|
| |
| from utils.validation import validate_and_prepare_input, InputValidationError |
|
|
| |
| pred_mainteanance_api = Flask ("Engine Maintenance Predictor") |
|
|
| |
| model = joblib.load ("best_eng_fail_pred_model.joblib") |
|
|
| |
| @pred_mainteanance_api.get ('/') |
| def home (): |
| return "Welcome to the Engine Maintenance Prediction!" |
|
|
| |
| @pred_mainteanance_api.post ('/v1/EngPredMaintenance') |
| def predict_need_maintenance (): |
| |
| engine_sensor_inputs = request.get_json () |
|
|
| |
| |
| |
| try: |
| input_json = request.get_json() |
| input_df = pd.DataFrame([input_json]) |
|
|
| validated_df = validate_and_prepare_input(input_df, model) |
|
|
| prediction = model.predict(validated_df)[0] |
| probability = model.predict_proba(validated_df)[0, 1] |
|
|
| return jsonify({ |
| "status": "success", |
| "prediction": int(prediction), |
| "probability": float(probability) |
| }) |
|
|
| except InputValidationError as e: |
| return jsonify({ |
| "status": "error", |
| "error_type": "validation_error", |
| "message": str(e) |
| }), 400 |
|
|
| except Exception as e: |
| return jsonify({ |
| "status": "error", |
| "error_type": "internal_error", |
| "message": "Unexpected server error" |
| }), 500 |
|
|
|
|
| |
| @pred_mainteanance_api.post ('/v1/EngPredMaintenanceForBatch') |
| def predict_need_maintenance_for_batch (): |
| |
| |
| |
| |
| try: |
| |
| file = request.files.get('file') |
|
|
| if file is None: |
| return jsonify({ |
| "status": "error", |
| "error_type": "input_error", |
| "message": "File not provided" |
| }), 400 |
| |
| if file.filename == "": |
| return jsonify({ |
| "status": "error", |
| "error_type": "input_error", |
| "message": "No file selected" |
| }), 400 |
| |
| |
| input_df = pd.read_csv (file) |
|
|
| if input_df.empty: |
| return jsonify({ |
| "status": "error", |
| "error_type": "input_error", |
| "message": "Uploaded file is empty" |
| }), 400 |
|
|
| |
| |
| |
| |
| |
| input_df.drop(columns=['Engine Condition'], inplace=True, errors='ignore') |
|
|
| |
| input_df.columns = input_df.columns.str.replace(' ', '_') |
|
|
| |
| int_columns = input_df.select_dtypes(include=['int64']).columns |
| input_df[int_columns] = input_df[int_columns].astype('float64') |
| |
| |
| validated_df = validate_and_prepare_input(input_df, model) |
|
|
| |
| predictions = model.predict(validated_df) |
| probabilities = model.predict_proba(validated_df)[:, 1] |
|
|
| |
| prediction_list = predictions.tolist() |
| probabilities_list = probabilities.tolist() |
|
|
| return jsonify({ |
| "status": "success", |
| "total_records": len(prediction_list), |
| "predictions": prediction_list, |
| "probabilities": probabilities_list, |
| }) |
|
|
| except InputValidationError as e: |
| return jsonify({ |
| "status": "error", |
| "error_type": "validation_error", |
| "message": str(e) |
| }), 400 |
|
|
| except Exception as e: |
| return jsonify({ |
| "status": "error", |
| "error_type": "internal_error", |
| "message": "Unexpected server error" |
| }), 500 |
|
|
|
|
| |
| if __name__ == "__main__": |
| import os |
| port = int (os.environ.get("PORT", 7860)) |
| pred_mainteanance_api.run(host="0.0.0.0", port=port) |
|
|