arnavarpit commited on
Commit
838be9a
ยท
verified ยท
1 Parent(s): 5651e13

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +120 -46
app.py CHANGED
@@ -4,73 +4,147 @@ import streamlit as st
4
  from huggingface_hub import hf_hub_download
5
  import numpy as np
6
 
7
- st.set_page_config(page_title="Engine Predictive Maintenance", page_icon="๐Ÿ› ๏ธ")
8
- st.title("Engine Predictive Maintenance โ€“ Failure Risk Prediction")
9
-
10
- HF_MODEL_REPO = os.getenv("HF_MODEL_REPO")
11
- if not HF_MODEL_REPO:
12
- st.error("HF_MODEL_REPO environment variable is not set. Please configure it in your Space settings.")
13
- st.stop()
14
 
 
 
15
  MODEL_FILE = "model.joblib"
16
 
17
  @st.cache_resource
18
  def load_model():
19
- local_path = hf_hub_download(repo_id=HF_MODEL_REPO, filename=MODEL_FILE)
20
- return joblib.load(local_path)
21
-
 
 
 
 
 
 
 
 
 
 
 
22
  model = load_model()
23
 
24
- st.sidebar.header("Engine Sensor Readings")
 
 
 
 
 
 
25
 
 
 
26
  col1, col2 = st.columns(2)
27
- engine_rpm = col1.number_input("Engine RPM", value=800.0, step=10.0, help="Engine rotations per minute")
28
- lub_oil_pressure = col1.number_input("Lube Oil Pressure (bar)", value=3.0, step=0.1, help="Lubrication oil pressure")
29
- fuel_pressure = col1.number_input("Fuel Pressure (bar)", value=6.0, step=0.1, help="Fuel system pressure")
30
- coolant_pressure = col1.number_input("Coolant Pressure (bar)", value=2.0, step=0.1, help="Cooling system pressure")
31
 
32
- lub_oil_temperature = col2.number_input("Lube Oil Temp (ยฐC)", value=80.0, step=0.5, help="Lubrication oil temperature")
33
- coolant_temperature = col2.number_input("Coolant Temp (ยฐC)", value=80.0, step=0.5, help="Coolant temperature")
 
 
 
 
 
 
 
 
 
34
 
35
  st.markdown("---")
36
 
37
- col_left, col_right = st.columns([2, 1])
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- with col_left:
40
- st.subheader("Current Sensor Readings")
41
- st.write(f"**Engine RPM:** {engine_rpm}")
42
- st.write(f"**Lube Oil Pressure:** {lub_oil_pressure} bar")
43
- st.write(f"**Fuel Pressure:** {fuel_pressure} bar")
44
- st.write(f"**Coolant Pressure:** {coolant_pressure} bar")
45
- st.write(f"**Lube Oil Temperature:** {lub_oil_temperature} ยฐC")
46
- st.write(f"**Coolant Temperature:** {coolant_temperature} ยฐC")
47
 
48
- with col_right:
49
- if st.button("Predict Maintenance Need", type="primary"):
 
 
 
 
 
50
  X = np.array([[engine_rpm, lub_oil_pressure, fuel_pressure, coolant_pressure,
51
  lub_oil_temperature, coolant_temperature]])
52
 
53
  try:
54
- proba = model.predict_proba(X)[:, 1][0]
55
- pred = model.predict(X)[0]
56
-
57
- if pred == 1:
58
- st.error("โš ๏ธ **MAINTENANCE REQUIRED**")
59
- st.metric("Failure Risk Score", f"{proba:.1%}")
60
- else:
61
- st.success("โœ… **NORMAL OPERATION**")
62
- st.metric("Failure Risk Score", f"{proba:.1%}")
63
-
64
- # Risk level indicator
65
- if proba < 0.3:
66
- st.info("๐ŸŸข Low Risk")
67
- elif proba < 0.7:
68
- st.warning("๐ŸŸก Medium Risk")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  else:
70
- st.error("๐Ÿ”ด High Risk")
 
 
 
 
 
71
 
72
  except Exception as e:
73
- st.error(f"Prediction error: {e}")
 
74
 
75
  st.markdown("---")
76
- st.caption("This app loads the latest model directly from the Hugging Face Model Hub.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  from huggingface_hub import hf_hub_download
5
  import numpy as np
6
 
7
+ st.set_page_config(page_title="Engine Predictive Maintenance", page_icon="๐Ÿ› ๏ธ", layout="wide")
8
+ st.title("๐Ÿš— Engine Predictive Maintenance โ€“ Failure Risk Prediction")
 
 
 
 
 
9
 
10
+ # Get model repo from environment or use default
11
+ HF_MODEL_REPO = os.getenv("HF_MODEL_REPO", "arnavarpit/engine-predictive-maintenance-sklearn")
12
  MODEL_FILE = "model.joblib"
13
 
14
  @st.cache_resource
15
  def load_model():
16
+ """Load the trained model from Hugging Face Hub"""
17
+ try:
18
+ with st.spinner("๐Ÿ”„ Loading model from Hugging Face Hub..."):
19
+ local_path = hf_hub_download(repo_id=HF_MODEL_REPO, filename=MODEL_FILE)
20
+ model = joblib.load(local_path)
21
+ st.success("โœ… Model loaded successfully!")
22
+ return model
23
+ except Exception as e:
24
+ st.error(f"โŒ Error loading model: {e}")
25
+ st.info(f"Attempting to load from: {HF_MODEL_REPO}")
26
+ st.stop()
27
+ return None
28
+
29
+ # Load model
30
  model = load_model()
31
 
32
+ # Show model info
33
+ with st.expander("โ„น๏ธ Model Information"):
34
+ st.write(f"**Model Repository:** {HF_MODEL_REPO}")
35
+ st.write(f"**Model File:** {MODEL_FILE}")
36
+ if model:
37
+ st.write(f"**Model Type:** {type(model).__name__}")
38
+
39
 
40
+ # Input Section
41
+ st.markdown("### ๐Ÿ“Š Enter Engine Sensor Readings")
42
  col1, col2 = st.columns(2)
 
 
 
 
43
 
44
+ with col1:
45
+ st.markdown("**Pressure & RPM Readings**")
46
+ engine_rpm = st.number_input("Engine RPM", value=800.0, min_value=0.0, max_value=5000.0, step=10.0, help="Engine rotations per minute")
47
+ lub_oil_pressure = st.number_input("Lube Oil Pressure (bar)", value=3.0, min_value=0.0, max_value=20.0, step=0.1, help="Lubrication oil pressure")
48
+ fuel_pressure = st.number_input("Fuel Pressure (bar)", value=6.0, min_value=0.0, max_value=20.0, step=0.1, help="Fuel system pressure")
49
+ coolant_pressure = st.number_input("Coolant Pressure (bar)", value=2.0, min_value=0.0, max_value=20.0, step=0.1, help="Cooling system pressure")
50
+
51
+ with col2:
52
+ st.markdown("**Temperature Readings**")
53
+ lub_oil_temperature = st.number_input("Lube Oil Temp (ยฐC)", value=80.0, min_value=-50.0, max_value=200.0, step=0.5, help="Lubrication oil temperature")
54
+ coolant_temperature = st.number_input("Coolant Temp (ยฐC)", value=80.0, min_value=-50.0, max_value=200.0, step=0.5, help="Coolant temperature")
55
 
56
  st.markdown("---")
57
 
58
+ # Display current readings
59
+ st.markdown("### ๐Ÿ“‹ Current Sensor Summary")
60
+ col_a, col_b, col_c = st.columns(3)
61
+ with col_a:
62
+ st.metric("Engine RPM", f"{engine_rpm:.0f}")
63
+ st.metric("Lube Oil Pressure", f"{lub_oil_pressure:.1f} bar")
64
+ with col_b:
65
+ st.metric("Fuel Pressure", f"{fuel_pressure:.1f} bar")
66
+ st.metric("Coolant Pressure", f"{coolant_pressure:.1f} bar")
67
+ with col_c:
68
+ st.metric("Lube Oil Temp", f"{lub_oil_temperature:.1f} ยฐC")
69
+ st.metric("Coolant Temp", f"{coolant_temperature:.1f} ยฐC")
70
 
71
+ st.markdown("---")
 
 
 
 
 
 
 
72
 
73
+ # Prediction Section
74
+ st.markdown("### ๐Ÿ”ฎ Maintenance Prediction")
75
+ if st.button("๐Ÿ” Predict Maintenance Need", type="primary", use_container_width=True):
76
+ if model is None:
77
+ st.error("โŒ Model not loaded. Cannot make predictions.")
78
+ else:
79
+ # Prepare input data
80
  X = np.array([[engine_rpm, lub_oil_pressure, fuel_pressure, coolant_pressure,
81
  lub_oil_temperature, coolant_temperature]])
82
 
83
  try:
84
+ with st.spinner("Analyzing sensor data..."):
85
+ proba = model.predict_proba(X)[:, 1][0]
86
+ pred = model.predict(X)[0]
87
+
88
+ # Display results
89
+ result_col1, result_col2 = st.columns([1, 1])
90
+
91
+ with result_col1:
92
+ if pred == 1:
93
+ st.error("### โš ๏ธ MAINTENANCE REQUIRED")
94
+ st.warning("The engine shows signs of potential failure. Schedule maintenance immediately.")
95
+ else:
96
+ st.success("### โœ… NORMAL OPERATION")
97
+ st.info("The engine is operating within normal parameters.")
98
+
99
+ with result_col2:
100
+ st.metric("Failure Risk Score", f"{proba:.1%}", delta=None)
101
+
102
+ # Risk level indicator with color coding
103
+ if proba < 0.3:
104
+ st.success("๐ŸŸข **Low Risk** - Continue normal operations")
105
+ elif proba < 0.7:
106
+ st.warning("๐ŸŸก **Medium Risk** - Monitor closely")
107
+ else:
108
+ st.error("๐Ÿ”ด **High Risk** - Immediate attention required")
109
+
110
+ # Additional insights
111
+ st.markdown("---")
112
+ st.markdown("#### ๐Ÿ“ˆ Risk Analysis")
113
+ st.progress(proba)
114
+
115
+ if proba > 0.5:
116
+ st.markdown("""
117
+ **Recommended Actions:**
118
+ - Schedule comprehensive engine inspection
119
+ - Check lubrication and cooling systems
120
+ - Review sensor readings for anomalies
121
+ - Prepare maintenance resources
122
+ """)
123
  else:
124
+ st.markdown("""
125
+ **Current Status:**
126
+ - All systems operating normally
127
+ - Continue regular monitoring
128
+ - Next scheduled maintenance as per routine
129
+ """)
130
 
131
  except Exception as e:
132
+ st.error(f"โŒ Prediction error: {e}")
133
+ st.exception(e)
134
 
135
  st.markdown("---")
136
+ st.markdown("### ๐Ÿ’ก About This Application")
137
+ st.info("""
138
+ This application uses machine learning to predict engine maintenance needs based on real-time sensor data.
139
+ The model analyzes six key engine parameters to assess failure risk and provide maintenance recommendations.
140
+
141
+ **Features:**
142
+ - Real-time failure risk prediction
143
+ - Interactive sensor input controls
144
+ - Visual risk level indicators
145
+ - Maintenance recommendations
146
+
147
+ **Model Source:** The trained model is loaded directly from the Hugging Face Model Hub.
148
+ """)
149
+
150
+ st.caption(f"๐Ÿค— Model: `{HF_MODEL_REPO}` | Built with Streamlit")