| import joblib |
| import pandas as pd |
| from huggingface_hub import hf_hub_download |
| from flask import Flask, request, jsonify |
|
|
| |
| |
| |
| def load_model(): |
| model_path = hf_hub_download( |
| repo_id="asvravi/asv-preventive-maintenance", |
| filename="preventive_maintenance_model_v1.joblib" |
| ) |
| return joblib.load(model_path) |
|
|
| model = load_model() |
|
|
| |
| |
| |
| predictive_maintenance_api = Flask("Predictive Maintenance") |
|
|
| |
| |
| |
| @predictive_maintenance_api.get("/") |
| def home(): |
| return jsonify({ |
| "message": "Engine Predictive Maintenance API is running." |
| }) |
|
|
| |
| |
| |
| @predictive_maintenance_api.post("/v1/PredictiveMaintenance") |
| def predict_engine_condition(): |
|
|
| try: |
| |
| sensor_data = request.get_json() |
|
|
| if not sensor_data: |
| return jsonify({"error": "No input data provided"}), 400 |
|
|
| |
| data_info = { |
| "engine_rpm": float(sensor_data.get("engine_rpm")), |
| "lub_oil_pressure": float(sensor_data.get("lub_oil_pressure")), |
| "fuel_pressure": float(sensor_data.get("fuel_pressure")), |
| "coolant_pressure": float(sensor_data.get("coolant_pressure")), |
| "lub_oil_temp": float(sensor_data.get("lub_oil_temp")), |
| "coolant_temp": float(sensor_data.get("coolant_temp")) |
| } |
|
|
| |
| input_df = pd.DataFrame([data_info]) |
|
|
| |
| fault_prob = model.predict_proba(input_df)[0][1] |
| |
| result = { |
| "fault_probability": round(float(fault_prob), 4) |
| } |
|
|
| return jsonify(result), 200 |
|
|
| except Exception as e: |
| return jsonify({"error": str(e)}), 500 |
|
|
| """ |
| We can expose another API to predict for a batch of inputs, if required |
| """ |
|
|