dpanchali commited on
Commit
b2b9a52
·
verified ·
1 Parent(s): 092959d

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +43 -125
app.py CHANGED
@@ -1,53 +1,3 @@
1
- import streamlit as st
2
- import pandas as pd
3
- import joblib
4
- import os
5
- from huggingface_hub import hf_hub_download
6
-
7
- # ==========================================
8
- # 1. Page Configuration & Model Loading
9
- # ==========================================
10
- st.set_page_config(page_title="Engine Predictive Maintenance", layout="centered")
11
-
12
- # Configuration from previous training steps
13
- REPO_ID = "dpanchali/predictive_maintenance_model"
14
- FILENAME = "predictive_maintenance_model.joblib"
15
-
16
- @st.cache_resource
17
- def load_model():
18
- """Download and load the model from Hugging Face Hub."""
19
- try:
20
- # Download the model file from the repository
21
- model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
22
- model = joblib.load(model_path)
23
- return model
24
- except Exception as e:
25
- st.error(f"Error loading model from Hugging Face: {e}")
26
- return None
27
-
28
- # Load the model
29
- model = load_model()
30
-
31
- # ==========================================
32
- # 2. UI Layout
33
- # ==========================================
34
- st.title("Engine Condition Predictor")
35
- st.markdown("""
36
- This application uses a trained **XGBoost** model to predict the health of an engine
37
- based on real-time sensor data.
38
- """)
39
-
40
- st.header("Input Engine Sensor Data")
41
- st.info("Enter the current readings from the engine sensors below:")
42
-
43
- # Creating columns for a cleaner layout
44
- col1, col2 = st.columns(2)
45
-
46
- with col1:
47
- engine_rpm = st.number_input("Engine RPM", min_value=0, max_value=10000, value=700, step=10)
48
- lub_oil_pressure = st.number_input("Lub Oil Pressure (bar)", min_value=0.0, max_value=20.0, value=2.5, format="%.4f")
49
- fuel_pressure = st.number_input("Fuel Pressure (bar)", min_value=0.0, max_value=50.0, value=11.8, format="%.4f")
50
-
51
  %%writefile predictive_maintenance_project/deployment/app.py
52
  import streamlit as st
53
  import pandas as pd
@@ -60,7 +10,6 @@ from huggingface_hub import hf_hub_download
60
  # ==========================================
61
  st.set_page_config(page_title="Engine Predictive Maintenance", layout="centered")
62
 
63
- # Configuration from previous training steps
64
  REPO_ID = "dpanchali/predictive_maintenance_model"
65
  FILENAME = "predictive_maintenance_model.joblib"
66
 
@@ -68,120 +17,89 @@ FILENAME = "predictive_maintenance_model.joblib"
68
  def load_model():
69
  """Download and load the model from Hugging Face Hub."""
70
  try:
71
- # Download the model file from the repository
72
  model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
73
  model = joblib.load(model_path)
74
  return model
75
  except Exception as e:
76
- st.error(f"Error loading model from Hugging Face: {e}")
77
  return None
78
 
79
- # Load the model
80
  model = load_model()
81
 
82
  # ==========================================
83
  # 2. UI Layout
84
  # ==========================================
85
- st.title("Engine Condition Predictor")
86
  st.markdown("""
87
- This application uses a trained **XGBoost** model to predict the health of an engine
88
- based on real-time sensor data.
89
  """)
90
 
91
  st.header("Input Engine Sensor Data")
92
- st.info("Enter the current readings from the engine sensors below:")
93
 
94
- # Creating columns for a cleaner layout
95
  col1, col2 = st.columns(2)
96
 
97
  with col1:
98
- engine_rpm = st.number_input("Engine RPM", min_value=0, max_value=10000, value=700, step=10)
99
- lub_oil_pressure = st.number_input("Lub Oil Pressure (bar)", min_value=0.0, max_value=20.0, value=2.5, format="%.4f")
100
- fuel_pressure = st.number_input("Fuel Pressure (bar)", min_value=0.0, max_value=50.0, value=11.8, format="%.4f")
101
 
102
  with col2:
103
- coolant_pressure = st.number_input("Coolant Pressure (bar)", min_value=0.0, max_value=20.0, value=3.2, format="%.4f")
104
- lub_oil_temp = st.number_input("Lub Oil Temp (°C)", min_value=0.0, max_value=200.0, value=84.1, format="%.4f")
105
- coolant_temp = st.number_input("Coolant Temp (°C)", min_value=0.0, max_value=200.0, value=81.6, format="%.4f")
106
-
107
- load_index = engine_rpm * fuel_pressure / 100
108
- thermal_stress = coolant_temp - lub_oil_temp
109
 
110
  # ==========================================
111
  # 3. Prediction Logic
112
  # ==========================================
113
  if st.button("Predict Engine Condition", type="primary"):
114
  if model is not None:
115
- # Prepare input data in the same format as training
116
- input_data = pd.DataFrame([{
117
- 'Engine rpm': engine_rpm,
118
- 'Lub oil pressure': lub_oil_pressure,
119
- 'Fuel pressure': fuel_pressure,
120
- 'Coolant pressure': coolant_pressure,
121
- 'lub oil temp': lub_oil_temp,
122
- 'Coolant temp': coolant_temp,
123
- 'load_index' : load_index,
124
- 'thermal_stress' : thermal_stress
125
- }])
126
-
127
- # Perform prediction
128
- prediction = model.predict(input_data)[0]
129
- prediction_proba = model.predict_proba(input_data)[0]
130
-
131
- # Display results
132
- st.divider()
133
- st.subheader("Results")
134
-
135
- if prediction == 1:
136
- st.success("**Prediction: Engine is in Good Condition**")
137
- else:
138
- st.error("**Prediction: Maintenance Required (Potential Fault Detected)**")
139
-
140
- # Display confidence scores
141
- st.write(f"**Confidence Score:** {max(prediction_proba)*100:.2f}%")
142
-
143
- # Optional: Display gauge or progress bar for health
144
- health_score = prediction_proba[1] # Probability of class 1 (Good)
145
- st.progress(health_score)
146
- else:
147
- st.warning("Model is not loaded. Please check your Hugging Face credentials and Repo ID.")
148
-
149
- # ==========================================
150
- # 4. Footer
151
- # ==========================================
152
- st.sidebar.markdown("### Model Information")
153
- st.sidebar.text(f"Repo: {REPO_ID}")
154
- st.sidebar.text(f"File: {FILENAME}")
155
- st.sidebar.markdown("---")
156
- st.sidebar.write("This tool is intended for predictive maintenance scheduling based on sensor patterns.")
157
 
158
  # Perform prediction
159
  prediction = model.predict(input_data)[0]
160
  prediction_proba = model.predict_proba(input_data)[0]
161
 
162
- # Display results
163
  st.divider()
164
  st.subheader("Results")
165
 
166
- if prediction == 1:
167
- st.success("**Prediction: Engine is in Good Condition**")
168
  else:
169
- st.error("**Prediction: Maintenance Required (Potential Fault Detected)**")
170
 
171
- # Display confidence scores
 
172
  st.write(f"**Confidence Score:** {max(prediction_proba)*100:.2f}%")
173
-
174
- # Optional: Display gauge or progress bar for health
175
- health_score = prediction_proba[1] # Probability of class 1 (Good)
176
  st.progress(health_score)
 
177
  else:
178
- st.warning("Model is not loaded. Please check your Hugging Face credentials and Repo ID.")
179
 
180
  # ==========================================
181
- # 4. Footer
182
  # ==========================================
183
- st.sidebar.markdown("### Model Information")
184
- st.sidebar.text(f"Repo: {REPO_ID}")
185
- st.sidebar.text(f"File: {FILENAME}")
186
- st.sidebar.markdown("---")
187
- st.sidebar.write("This tool is intended for predictive maintenance scheduling based on sensor patterns.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  %%writefile predictive_maintenance_project/deployment/app.py
2
  import streamlit as st
3
  import pandas as pd
 
10
  # ==========================================
11
  st.set_page_config(page_title="Engine Predictive Maintenance", layout="centered")
12
 
 
13
  REPO_ID = "dpanchali/predictive_maintenance_model"
14
  FILENAME = "predictive_maintenance_model.joblib"
15
 
 
17
  def load_model():
18
  """Download and load the model from Hugging Face Hub."""
19
  try:
 
20
  model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
21
  model = joblib.load(model_path)
22
  return model
23
  except Exception as e:
24
+ st.error(f"Error loading model: {e}")
25
  return None
26
 
 
27
  model = load_model()
28
 
29
  # ==========================================
30
  # 2. UI Layout
31
  # ==========================================
32
+ st.title("🚢 Engine Condition Predictor")
33
  st.markdown("""
34
+ This application uses a trained **XGBoost** model to predict engine health
35
+ based on sensor patterns.
36
  """)
37
 
38
  st.header("Input Engine Sensor Data")
 
39
 
 
40
  col1, col2 = st.columns(2)
41
 
42
  with col1:
43
+ engine_rpm = st.number_input("Engine RPM", min_value=0, max_value=10000, value=700)
44
+ lub_oil_pressure = st.number_input("Lub Oil Pressure (bar)", min_value=0.0, value=2.5, format="%.4f")
45
+ fuel_pressure = st.number_input("Fuel Pressure (bar)", min_value=0.0, value=11.8, format="%.4f")
46
 
47
  with col2:
48
+ coolant_pressure = st.number_input("Coolant Pressure (bar)", min_value=0.0, value=3.2, format="%.4f")
49
+ lub_oil_temp = st.number_input("Lub Oil Temp (°C)", min_value=0.0, value=84.1, format="%.4f")
50
+ coolant_temp = st.number_input("Coolant Temp (°C)", min_value=0.0, value=81.6, format="%.4f")
 
 
 
51
 
52
  # ==========================================
53
  # 3. Prediction Logic
54
  # ==========================================
55
  if st.button("Predict Engine Condition", type="primary"):
56
  if model is not None:
57
+ # Calculate engineered features right before prediction
58
+ load_index = float(engine_rpm * fuel_pressure / 100)
59
+ thermal_stress = float(coolant_temp - lub_oil_temp)
60
+
61
+ # Create DataFrame with EXACT column names and order from your error message
62
+ input_data = pd.DataFrame([[
63
+ engine_rpm,
64
+ lub_oil_pressure,
65
+ fuel_pressure,
66
+ coolant_pressure,
67
+ lub_oil_temp,
68
+ coolant_temp,
69
+ load_index,
70
+ thermal_stress
71
+ ]], columns=[
72
+ 'Engine rpm', 'Lub oil pressure', 'Fuel pressure', 'Coolant pressure',
73
+ 'lub oil temp', 'Coolant temp', 'load_index', 'thermal_stress'
74
+ ])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  # Perform prediction
77
  prediction = model.predict(input_data)[0]
78
  prediction_proba = model.predict_proba(input_data)[0]
79
 
 
80
  st.divider()
81
  st.subheader("Results")
82
 
83
+ if prediction == 0: # Assuming 0 is Good based on typical maintenance datasets
84
+ st.success("**Status: Engine is in Good Condition**")
85
  else:
86
+ st.error("**Status: Maintenance Required (Potential Fault)**")
87
 
88
+ # Confidence Visual
89
+ health_score = prediction_proba[0] # Probability of "Healthy"
90
  st.write(f"**Confidence Score:** {max(prediction_proba)*100:.2f}%")
 
 
 
91
  st.progress(health_score)
92
+
93
  else:
94
+ st.error("Model could not be loaded. Check your Hugging Face Repo ID.")
95
 
96
  # ==========================================
97
+ # 4. Sidebar Information
98
  # ==========================================
99
+ with st.sidebar:
100
+ st.markdown("### Model Details")
101
+ st.text(f"Repo: {REPO_ID}")
102
+ st.markdown("---")
103
+ st.write("Calculated Features:")
104
+ st.caption(f"Load Index: {engine_rpm * fuel_pressure / 100:.2f}")
105
+ st.caption(f"Thermal Stress: {coolant_temp - lub_oil_temp:.2f}")