charantejapolavarapu commited on
Commit
89d76a3
·
verified ·
1 Parent(s): 39e1f00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -42
app.py CHANGED
@@ -4,55 +4,110 @@ import joblib
4
  import numpy as np
5
  import plotly.graph_objects as go
6
  import os
 
7
 
8
- # Page Config
9
- st.set_page_config(page_title="Jet Engine AI", page_icon="✈️")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  @st.cache_resource
12
  def load_model():
13
  if os.path.exists('engine_model.pkl'):
14
- return joblib.load('engine_model.pkl')
15
- return None
 
 
 
 
 
16
 
 
17
  model = load_model()
18
 
19
- st.title("✈️ Predictive Maintenance Dashboard")
20
-
21
- if model is None:
22
- st.error("Model file 'engine_model.pkl' not found. Please upload it.")
23
- st.stop()
24
-
25
- # Sidebar for Inputs
26
- st.sidebar.header("Sensor Data Input")
27
- cycle = st.sidebar.slider("Operational Cycle", 1, 350, 100)
28
- s2 = st.sidebar.number_input("Sensor 2 (Temp)", value=642.0)
29
- s3 = st.sidebar.number_input("Sensor 3 (Temp)", value=1589.0)
30
- s4 = st.sidebar.number_input("Sensor 4 (Pressure)", value=1408.0)
31
- s7 = st.sidebar.number_input("Sensor 7 (HPC Press)", value=553.0)
32
- s11 = st.sidebar.number_input("Sensor 11 (Speed)", value=47.5)
33
-
34
- if st.button("Predict Engine Health"):
35
- # Create input array (matches the 15 features in training)
36
- # Filling remaining slots with defaults for simplicity
37
- inputs = np.array([[cycle, s2, s3, s4, s7, 550, s11, 2388, 521, 8.4, 392, 39, 23, 0.001, 0.0002]])
38
- prediction = model.predict(inputs)
39
- rul = int(max(0, prediction[0]))
40
-
41
- # Gauge Chart
42
- fig = go.Figure(go.Indicator(
43
- mode = "gauge+number",
44
- value = rul,
45
- title = {'text': "Remaining Useful Life (Cycles)"},
46
- gauge = {'axis': {'range': [0, 200]},
47
- 'steps' : [
48
- {'range': [0, 30], 'color': "red"},
49
- {'range': [30, 80], 'color': "orange"},
50
- {'range': [80, 200], 'color': "green"}]}))
51
- st.plotly_chart(fig)
52
 
53
- if rul < 30:
54
- st.error("🚨 CRITICAL: Maintenance Required Immediately!")
55
- elif rul < 80:
56
- st.warning("⚠️ CAUTION: Schedule Maintenance Soon.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  else:
58
- st.success(" HEALTHY: Engine is in good condition.")
 
 
 
 
4
  import numpy as np
5
  import plotly.graph_objects as go
6
  import os
7
+ import xgboost as xgb
8
 
9
+ # 1. PAGE SETUP
10
+ st.set_page_config(page_title="Jet Engine AI Predictor", page_icon="✈️", layout="wide")
11
+
12
+ # 2. AUTO-TRAINING LOGIC (Fixes the "Missing File" or "Corrupted File" issue)
13
+ def train_and_save_model():
14
+ with st.spinner("Training the AI model for the first time... please wait."):
15
+ # Load Data from NASA Repository
16
+ url = "https://raw.githubusercontent.com/datasets-machine-learning/nasa-turbofan-failure-prediction/master/data/train_FD001.txt"
17
+ cols = ['unit', 'cycles', 'os1', 'os2', 'os3'] + [f's{i}' for i in range(1, 22)]
18
+ df = pd.read_csv(url, sep='\s+', header=None, names=cols)
19
+
20
+ # Calculate RUL (Target)
21
+ max_cycles = df.groupby('unit')['cycles'].max().reset_index()
22
+ max_cycles.columns = ['unit', 'max_of_unit']
23
+ df = df.merge(max_cycles, on='unit', how='left')
24
+ df['RUL'] = df['max_of_unit'] - df['cycles']
25
+
26
+ # Select Features (15 columns)
27
+ features = ['cycles', 's2', 's3', 's4', 's7', 's8', 's11', 's12', 's13', 's15', 's17', 's20', 's21', 'os1', 'os2']
28
+ X = df[features]
29
+ y = df['RUL']
30
+
31
+ # Train XGBoost
32
+ model = xgb.XGBRegressor(n_estimators=100, learning_rate=0.1, max_depth=5)
33
+ model.fit(X, y)
34
+
35
+ # Save locally in the HF Space
36
+ joblib.dump(model, 'engine_model.pkl')
37
+ return model
38
 
39
  @st.cache_resource
40
  def load_model():
41
  if os.path.exists('engine_model.pkl'):
42
+ try:
43
+ return joblib.load('engine_model.pkl')
44
+ except:
45
+ # If the file is corrupted (like the mport error), re-train it
46
+ return train_and_save_model()
47
+ else:
48
+ return train_and_save_model()
49
 
50
+ # Load model on startup
51
  model = load_model()
52
 
53
+ # 3. UI LAYOUT
54
+ st.title("✈️ Jet Engine Predictive Maintenance System")
55
+ st.markdown("---")
56
+
57
+ col1, col2 = st.columns([1, 2])
58
+
59
+ with col1:
60
+ st.header("📥 Sensor Inputs")
61
+ st.write("Adjust sliders to simulate engine conditions:")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ # Inputs
64
+ cycle = st.slider("Current Cycles", 1, 350, 100)
65
+ s2 = st.slider("Temp (S2)", 640.0, 650.0, 642.5)
66
+ s3 = st.slider("Temp (S3)", 1580.0, 1600.0, 1589.0)
67
+ s4 = st.slider("Pressure (S4)", 1400.0, 1430.0, 1408.0)
68
+ s7 = st.slider("Pressure (S7)", 550.0, 560.0, 553.5)
69
+ s11 = st.slider("Speed (S11)", 47.0, 48.5, 47.5)
70
+
71
+ # Analyze Button
72
+ predict_btn = st.button("Predict Engine Health", type="primary")
73
+
74
+ with col2:
75
+ st.header("📊 Prediction Result")
76
+
77
+ if predict_btn:
78
+ # Prepare inputs (must be 15 values to match model training)
79
+ # Using some static defaults for the other 9 features to keep UI simple
80
+ sample_input = np.array([[cycle, s2, s3, s4, s7, 550, s11, 2388, 521, 8.4, 392, 39, 23, 0.001, 0.0002]])
81
+
82
+ # Make Prediction
83
+ prediction = model.predict(sample_input)
84
+ rul = int(max(0, prediction[0]))
85
+
86
+ # Gauge Chart
87
+ fig = go.Figure(go.Indicator(
88
+ mode = "gauge+number",
89
+ value = rul,
90
+ title = {'text': "Remaining Useful Life (Cycles)"},
91
+ gauge = {
92
+ 'axis': {'range': [0, 200]},
93
+ 'steps' : [
94
+ {'range': [0, 30], 'color': "red"},
95
+ {'range': [30, 80], 'color': "orange"},
96
+ {'range': [80, 200], 'color': "green"}],
97
+ 'bar': {'color': "black"}
98
+ }
99
+ ))
100
+ st.plotly_chart(fig)
101
+
102
+ # Status Message
103
+ if rul < 30:
104
+ st.error(f"🚨 CRITICAL: Engine failure likely in {rul} cycles!")
105
+ elif rul < 80:
106
+ st.warning(f"⚠️ CAUTION: Maintenance check required within {rul} cycles.")
107
+ else:
108
+ st.success(f"✅ HEALTHY: Engine is safe for {rul} more cycles.")
109
  else:
110
+ st.info("Adjust the sliders and click 'Predict' to see the results.")
111
+
112
+ st.markdown("---")
113
+ st.caption("B.Tech AI&DS Project | Dataset: NASA C-MAPSS")