| | |
| | import numpy as np |
| | import joblib |
| | import pandas as pd |
| | from flask import Flask, request, jsonify |
| | import os |
| | import logging |
| |
|
| | |
| | logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
| |
|
| | |
| | predictive_maintenance_api = Flask("Predictive Maintenance API") |
| |
|
| | |
| | |
| | |
| | MODEL_PATH = "Predictive_Maintenance_XGB_Tuned_model_v1_0.joblib" |
| |
|
| | |
| | model = None |
| | try: |
| | model = joblib.load(MODEL_PATH) |
| | logging.info(f"Model loaded successfully from {MODEL_PATH}") |
| | except Exception as e: |
| | logging.error(f"Error loading model: {e}") |
| |
|
| | |
| | @predictive_maintenance_api.get('/') |
| | def home(): |
| | """ |
| | This function handles GET requests to the root URL ('/') of the API. |
| | It returns a simple welcome message. |
| | """ |
| | logging.info("Home page accessed.") |
| | return "Welcome to the Predictive Maintenance API for Engine Health!" |
| |
|
| | |
| | @predictive_maintenance_api.post('/v1/engine_condition_prediction') |
| | def predict_engine_condition(): |
| | """ |
| | This function handles POST requests to the '/v1/engine_condition_prediction' endpoint. |
| | It expects a JSON payload containing engine sensor data and returns |
| | the predicted engine condition (0 for Normal, 1 for Faulty) as a JSON response. |
| | """ |
| | logging.info("Prediction request received.") |
| | if model is None: |
| | logging.error("Model not loaded when prediction request came. Returning 500.") |
| | return jsonify({"error": "Model not loaded. Please check server logs."}), 500 |
| |
|
| | |
| | engine_data = request.get_json() |
| | logging.info(f"Received engine data: {engine_data}") |
| |
|
| | |
| | |
| | try: |
| | sample_data = { |
| | 'Engine_RPM': [engine_data['Engine_RPM']], |
| | 'Lub_Oil_Pressure': [engine_data['Lub_Oil_Pressure']], |
| | 'Fuel_Pressure': [engine_data['Fuel_Pressure']], |
| | 'Coolant_Pressure': [engine_data['Coolant_Pressure']], |
| | 'Lub_Oil_Temperature': [engine_data['Lub_Oil_Temperature']], |
| | 'Coolant_Temperature': [engine_data['Coolant_Temperature']] |
| | } |
| | logging.debug(f"Extracted sample data: {sample_data}") |
| | except KeyError as e: |
| | logging.error(f"Missing data for feature: {e}. Returning 400.") |
| | return jsonify({"error": f"Missing data for feature: {e}. Please provide all required sensor readings."}), 400 |
| | except Exception as e: |
| | logging.error(f"Unexpected error during data extraction: {e}. Returning 400.") |
| | return jsonify({"error": f"An unexpected error occurred during data processing: {e}"}), 400 |
| |
|
| |
|
| | |
| | |
| | input_df = pd.DataFrame(sample_data) |
| |
|
| | |
| | try: |
| | prediction_proba = model.predict_proba(input_df) |
| | predicted_class = model.predict(input_df)[0] |
| | logging.info(f"Prediction made: class={predicted_class}, probabilities={prediction_proba}") |
| | except Exception as e: |
| | logging.error(f"Error during model prediction: {e}. Returning 500.") |
| | return jsonify({"error": f"Error during model prediction: {e}"}), 500 |
| |
|
| | |
| | condition_map = {0: "Normal", 1: "Faulty"} |
| | predicted_condition_str = condition_map.get(predicted_class, "Unknown") |
| |
|
| | |
| | return jsonify({ |
| | "predicted_engine_condition_class": int(predicted_class), |
| | "predicted_engine_condition_label": predicted_condition_str, |
| | "probability_normal": round(float(prediction_proba[0][0]), 4), |
| | "probability_faulty": round(float(prediction_proba[0][1]), 4) |
| | }) |
| |
|
| | |
| | if __name__ == '__main__': |
| | |
| | port = int(os.environ.get('PORT', 5000)) |
| | predictive_maintenance_api.run(host='0.0.0.0', port=port, debug=True) |
| |
|