SharleyK commited on
Commit
b033899
·
verified ·
1 Parent(s): 18495da

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +0 -3
  2. README.md +1 -3
  3. app.py +103 -20
Dockerfile CHANGED
@@ -1,10 +1,7 @@
1
  FROM python:3.10-slim
2
-
3
  WORKDIR /app
4
  COPY requirements.txt .
5
  RUN pip install --no-cache-dir -r requirements.txt
6
-
7
  COPY app.py .
8
-
9
  EXPOSE 7860
10
  CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0"]
 
1
  FROM python:3.10-slim
 
2
  WORKDIR /app
3
  COPY requirements.txt .
4
  RUN pip install --no-cache-dir -r requirements.txt
 
5
  COPY app.py .
 
6
  EXPOSE 7860
7
  CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0"]
README.md CHANGED
@@ -1,5 +1,4 @@
1
  ---
2
- ---
3
  title: Predictive Maintenance
4
  emoji: 🔧
5
  colorFrom: blue
@@ -12,7 +11,6 @@ pinned: false
12
 
13
  ## Predictive Maintenance – Engine Failure Prediction
14
 
15
- Streamlit app deployed on **Hugging Face Spaces** using Docker.
16
 
17
  Author: Sharley Kulkarni
18
-
 
1
  ---
 
2
  title: Predictive Maintenance
3
  emoji: 🔧
4
  colorFrom: blue
 
11
 
12
  ## Predictive Maintenance – Engine Failure Prediction
13
 
14
+ Streamlit app deployed on Hugging Face Spaces.
15
 
16
  Author: Sharley Kulkarni
 
app.py CHANGED
@@ -1,30 +1,112 @@
 
1
  import streamlit as st
2
  import pandas as pd
 
3
  import joblib
4
  from huggingface_hub import hf_hub_download
 
 
 
 
 
5
 
6
- st.set_page_config(page_title="Predictive Maintenance", layout="wide")
 
 
 
 
 
 
 
 
7
 
8
  @st.cache_resource
9
- def load_model():
10
- repo = "SharleyK/predictive-maintenance-model"
11
- model = joblib.load(hf_hub_download(repo, "best_model.pkl"))
12
- scaler = joblib.load(hf_hub_download(repo, "scaler.pkl"))
13
- return model, scaler
 
 
 
 
 
 
 
14
 
15
- model, scaler = load_model()
 
 
16
 
17
- st.title("Predictive Maintenance - Engine Failure Prediction")
 
18
 
19
- engine_rpm = st.slider("Engine RPM", 100, 2500, 800)
20
- lub_oil_pressure = st.slider("Lub Oil Pressure", 0.0, 8.0, 3.0)
21
- fuel_pressure = st.slider("Fuel Pressure", 0.0, 22.0, 6.0)
22
- coolant_pressure = st.slider("Coolant Pressure", 0.0, 8.0, 2.5)
23
- lub_oil_temp = st.slider("Lub Oil Temp", 70.0, 95.0, 80.0)
24
- coolant_temp = st.slider("Coolant Temp", 60.0, 200.0, 85.0)
25
 
26
- if st.button("Predict"):
27
- X = pd.DataFrame([{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  "engine_rpm": engine_rpm,
29
  "lub_oil_pressure": lub_oil_pressure,
30
  "fuel_pressure": fuel_pressure,
@@ -33,10 +115,11 @@ if st.button("Predict"):
33
  "coolant_temp": coolant_temp
34
  }])
35
 
36
- X_scaled = scaler.transform(X)
37
- pred = model.predict(X_scaled)[0]
38
 
39
  if pred == 1:
40
- st.error("Engine Failure Detected")
41
  else:
42
- st.success("Engine Operating Normally")
 
 
 
1
+
2
  import streamlit as st
3
  import pandas as pd
4
+ import numpy as np
5
  import joblib
6
  from huggingface_hub import hf_hub_download
7
+ import json
8
+
9
+ # =============================================================================
10
+ # STREAMLIT CONFIG
11
+ # =============================================================================
12
 
13
+ st.set_page_config(
14
+ page_title="Predictive Maintenance",
15
+ layout="wide",
16
+ page_icon="🛠️"
17
+ )
18
+
19
+ # =============================================================================
20
+ # LOAD MODEL
21
+ # =============================================================================
22
 
23
  @st.cache_resource
24
+ def load_artifacts():
25
+ repo_id = "SharleyK/PredictiveMaintenance"
26
+
27
+ model = joblib.load(hf_hub_download(repo_id, "best_model.pkl"))
28
+ scaler = joblib.load(hf_hub_download(repo_id, "scaler.pkl"))
29
+ metadata = json.load(open(hf_hub_download(repo_id, "metadata.json")))
30
+
31
+ return model, scaler, metadata
32
+
33
+
34
+ model, scaler, metadata = load_artifacts()
35
+ MODEL_NAME = metadata.get("model_name", "Unknown Model")
36
 
37
+ # =============================================================================
38
+ # FEATURE ENGINEERING
39
+ # =============================================================================
40
 
41
+ def engineer_features(df):
42
+ df = df.copy()
43
 
44
+ df["temp_pressure_ratio"] = df["coolant_temp"] / (df["coolant_pressure"] + 1e-6)
45
+ df["oil_temp_pressure_ratio"] = df["lub_oil_temp"] / (df["lub_oil_pressure"] + 1e-6)
 
 
 
 
46
 
47
+ df["pressure_diff_oil_coolant"] = df["lub_oil_pressure"] - df["coolant_pressure"]
48
+ df["pressure_diff_fuel_oil"] = df["fuel_pressure"] - df["lub_oil_pressure"]
49
+
50
+ df["temp_diff_oil_coolant"] = df["lub_oil_temp"] - df["coolant_temp"]
51
+
52
+ df["avg_temp"] = (df["lub_oil_temp"] + df["coolant_temp"]) / 2
53
+ df["avg_pressure"] = (
54
+ df["lub_oil_pressure"] +
55
+ df["fuel_pressure"] +
56
+ df["coolant_pressure"]
57
+ ) / 3
58
+
59
+ df["high_rpm"] = (df["engine_rpm"] > 934).astype(int)
60
+ df["high_temp"] = (df["coolant_temp"] > 82.9).astype(int)
61
+ df["operating_stress"] = df["high_rpm"] * df["high_temp"]
62
+ df["low_oil_pressure"] = (df["lub_oil_pressure"] < 2.52).astype(int)
63
+
64
+ return df
65
+
66
+ # =============================================================================
67
+ # PREDICTION
68
+ # =============================================================================
69
+
70
+ def predict(df):
71
+ df_eng = engineer_features(df)
72
+ X_scaled = scaler.transform(df_eng)
73
+ pred = model.predict(X_scaled)[0]
74
+
75
+ if hasattr(model, "predict_proba"):
76
+ confidence = model.predict_proba(X_scaled)[0][pred]
77
+ else:
78
+ confidence = 0.85
79
+
80
+ return pred, confidence
81
+
82
+ # =============================================================================
83
+ # UI
84
+ # =============================================================================
85
+
86
+ st.title("🛠️ Predictive Maintenance – Engine Failure Prediction")
87
+ st.caption("AI-powered real-time engine health assessment")
88
+
89
+ st.markdown(f"""
90
+ **Model:** {MODEL_NAME}
91
+ **Accuracy:** 66.68% | **Recall:** 87.13%
92
+ """)
93
+
94
+ st.divider()
95
+
96
+ col1, col2 = st.columns(2)
97
+
98
+ with col1:
99
+ engine_rpm = st.slider("Engine RPM", 50, 2500, 800, step=10)
100
+ lub_oil_pressure = st.slider("Lub Oil Pressure", 0.0, 8.0, 3.3)
101
+ fuel_pressure = st.slider("Fuel Pressure", 0.0, 22.0, 6.5)
102
+
103
+ with col2:
104
+ coolant_pressure = st.slider("Coolant Pressure", 0.0, 8.0, 2.3)
105
+ lub_oil_temp = st.slider("Lub Oil Temp (°C)", 70.0, 95.0, 77.0)
106
+ coolant_temp = st.slider("Coolant Temp (°C)", 60.0, 200.0, 78.0)
107
+
108
+ if st.button("Predict Engine Condition"):
109
+ df = pd.DataFrame([{
110
  "engine_rpm": engine_rpm,
111
  "lub_oil_pressure": lub_oil_pressure,
112
  "fuel_pressure": fuel_pressure,
 
115
  "coolant_temp": coolant_temp
116
  }])
117
 
118
+ pred, conf = predict(df)
 
119
 
120
  if pred == 1:
121
+ st.error("🚨 FAULTY ENGINE DETECTED")
122
  else:
123
+ st.success(" ENGINE OPERATING NORMALLY")
124
+
125
+ st.metric("Confidence", f"{conf*100:.1f}%")