VeerendraManikonda commited on
Commit
9c4488e
·
verified ·
1 Parent(s): 672d634

Upload folder using huggingface_hub

Browse files
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+
3
+ # Set the working directory inside the container
4
+ WORKDIR /app
5
+
6
+ # Copy all files from the current directory to the container's working directory
7
+ COPY . .
8
+
9
+ # Install dependencies from the requirements file without using cache to reduce image size
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ # Define the command to start the application using Gunicorn with 4 worker processes
13
+ # - `-w 4`: Uses 4 worker processes for handling requests
14
+ # - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
15
+ # - `app:app`: Runs the Flask app (assuming `app.py` contains the Flask instance named `app`)
16
+ CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:revenue_predictor_api"]
Predictive_Maintenance_XGB_Tuned_model_v1_0.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:437d5fd38664410edd7b466678b492057351a252e789b174a1f17256b0595e71
3
+ size 759980
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import numpy as np
3
+ import joblib # For loading the serialized model
4
+ import pandas as pd # For data manipulation
5
+ from flask import Flask, request, jsonify # For creating the Flask API
6
+ import os # For accessing environment variables if needed
7
+
8
+ # Initialize the Flask application
9
+ predictive_maintenance_api = Flask("Predictive Maintenance API")
10
+
11
+ # Define the model path
12
+ # Use the correct path as defined in the notebook
13
+ MODEL_PATH = "backend_files/Predictive_Maintenance_XGB_Tuned_model_v1_0.joblib"
14
+
15
+ # Load the trained machine learning model
16
+ try:
17
+ model = joblib.load(MODEL_PATH)
18
+ print(f"Model loaded successfully from {MODEL_PATH}")
19
+ except Exception as e:
20
+ print(f"Error loading model: {e}")
21
+ model = None # Set model to None if loading fails
22
+
23
+ # Define a route for the home page (GET request)
24
+ @predictive_maintenance_api.get('/')
25
+ def home():
26
+ """
27
+ This function handles GET requests to the root URL ('/') of the API.
28
+ It returns a simple welcome message.
29
+ """
30
+ return "Welcome to the Predictive Maintenance API for Engine Health!"
31
+
32
+ # Define an endpoint for engine condition prediction (POST request)
33
+ @predictive_maintenance_api.post('/v1/engine_condition_prediction')
34
+ def predict_engine_condition():
35
+ """
36
+ This function handles POST requests to the '/v1/engine_condition_prediction' endpoint.
37
+ It expects a JSON payload containing engine sensor data and returns
38
+ the predicted engine condition (0 for Normal, 1 for Faulty) as a JSON response.
39
+ """
40
+ if model is None:
41
+ return jsonify({"error": "Model not loaded. Please check server logs."}), 500
42
+
43
+ # Get the JSON data from the request body
44
+ engine_data = request.get_json()
45
+
46
+ # Extract relevant features from the JSON data
47
+ # Features: 'Engine_RPM', 'Lub_Oil_Pressure', 'Fuel_Pressure', 'Coolant_Pressure', 'Lub_Oil_Temperature', 'Coolant_Temperature'
48
+ try:
49
+ sample_data = {
50
+ 'Engine_RPM': [engine_data['Engine_RPM']],
51
+ 'Lub_Oil_Pressure': [engine_data['Lub_Oil_Pressure']],
52
+ 'Fuel_Pressure': [engine_data['Fuel_Pressure']],
53
+ 'Coolant_Pressure': [engine_data['Coolant_Pressure']],
54
+ 'Lub_Oil_Temperature': [engine_data['Lub_Oil_Temperature']],
55
+ 'Coolant_Temperature': [engine_data['Coolant_Temperature']]
56
+ }
57
+ except KeyError as e:
58
+ return jsonify({"error": f"Missing data for feature: {e}. Please provide all required sensor readings."}), 400
59
+
60
+ # Convert the extracted data into a Pandas DataFrame
61
+ # Ensure the order of columns matches the training data
62
+ input_df = pd.DataFrame(sample_data)
63
+
64
+ # Make prediction
65
+ # model.predict_proba will give probabilities for both classes [prob_class_0, prob_class_1]
66
+ # model.predict will give the predicted class (0 or 1)
67
+ prediction_proba = model.predict_proba(input_df)
68
+ predicted_class = model.predict(input_df)[0]
69
+
70
+ # Map the predicted class to a readable string
71
+ condition_map = {0: "Normal", 1: "Faulty"}
72
+ predicted_condition_str = condition_map.get(predicted_class, "Unknown")
73
+
74
+ # Return the prediction as a JSON response
75
+ return jsonify({
76
+ "predicted_engine_condition_class": int(predicted_class), # Ensure it's a standard Python int
77
+ "predicted_engine_condition_label": predicted_condition_str,
78
+ "probability_normal": round(float(prediction_proba[0][0]), 4),
79
+ "probability_faulty": round(float(prediction_proba[0][1]), 4)
80
+ })
81
+
82
+ # To run the Flask app (for local testing)
83
+ if __name__ == '__main__':
84
+ # You might want to get the port from an environment variable for deployment
85
+ port = int(os.environ.get('PORT', 5000))
86
+ predictive_maintenance_api.run(host='0.0.0.0', port=port, debug=True)
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pandas==2.2.2
2
+ numpy==2.0.2
3
+ scikit-learn==1.6.1
4
+ xgboost==2.1.4
5
+ joblib==1.4.2
6
+ Werkzeug==2.2.2
7
+ flask==2.2.2
8
+ gunicorn==20.1.0
9
+ requests==2.28.1
10
+ uvicorn[standard]
11
+ streamlit==1.43.2