Spaces:
Sleeping
Sleeping
Commit ยท
e551557
1
Parent(s): 1650930
Enhanced UX: tabs, demo files, step-by-step guide
Browse files
app.py
CHANGED
|
@@ -1,68 +1,255 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
rul = int(composite * 200)
|
| 13 |
|
|
|
|
| 14 |
if rul < 30:
|
| 15 |
-
urgency = "
|
|
|
|
| 16 |
elif rul < 100:
|
| 17 |
-
urgency = "
|
|
|
|
| 18 |
else:
|
| 19 |
-
urgency = "
|
|
|
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
recs = []
|
| 22 |
if health_index < 60:
|
| 23 |
-
recs.append("
|
| 24 |
if vibration > 0.4:
|
| 25 |
-
recs.append("
|
| 26 |
if heat_rate_delta > 6:
|
| 27 |
-
recs.append("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
if not recs:
|
| 29 |
-
recs.append("
|
| 30 |
|
| 31 |
-
return f"""# RUL Prediction
|
| 32 |
|
| 33 |
## Remaining Useful Life: **{rul} cycles**
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
| 39 |
-
|
|
| 40 |
-
|
|
| 41 |
-
|
|
| 42 |
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
|
| 68 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# =============================================================================
|
| 4 |
+
# PREDICTIVE AGENT - LSTM-Based RUL Prediction
|
| 5 |
+
# Author: David Fernandez | Industrial AI Engineer | LangGraph Contributor
|
| 6 |
+
# =============================================================================
|
| 7 |
+
|
| 8 |
+
# Demo CSV content
|
| 9 |
+
DEMO_CSV_HEALTHY = """cycle,health_index,vibration,heat_rate_delta,operating_hours,start_count
|
| 10 |
+
1,98.2,0.12,0.8,45000,850
|
| 11 |
+
2,97.8,0.13,0.9,46000,860
|
| 12 |
+
3,97.5,0.13,1.0,47000,870
|
| 13 |
+
4,97.1,0.14,1.1,48000,880
|
| 14 |
+
5,96.5,0.14,1.2,48500,920"""
|
| 15 |
+
|
| 16 |
+
DEMO_CSV_DEGRADED = """cycle,health_index,vibration,heat_rate_delta,operating_hours,start_count
|
| 17 |
+
1,65.2,0.32,5.2,62000,1100
|
| 18 |
+
2,58.4,0.38,6.1,64000,1120
|
| 19 |
+
3,52.1,0.42,7.0,66000,1140
|
| 20 |
+
4,47.8,0.45,7.8,67000,1160
|
| 21 |
+
5,42.3,0.48,8.5,68000,1180"""
|
| 22 |
+
|
| 23 |
+
def predict_rul(health_index, vibration, heat_rate_delta, operating_hours, start_count):
|
| 24 |
+
"""Predict Remaining Useful Life based on equipment health."""
|
| 25 |
+
|
| 26 |
+
# Normalize factors (0-1 scale, higher = healthier)
|
| 27 |
+
hi_factor = health_index / 100
|
| 28 |
+
vib_factor = 1 - min(vibration / 1.0, 1)
|
| 29 |
+
hr_factor = 1 - min(heat_rate_delta / 15, 1)
|
| 30 |
+
hours_factor = 1 - min(operating_hours / 80000, 1)
|
| 31 |
+
starts_factor = 1 - min(start_count / 1500, 1)
|
| 32 |
+
|
| 33 |
+
# Weighted composite (based on LSTM feature importance)
|
| 34 |
+
composite = (
|
| 35 |
+
hi_factor * 0.35 +
|
| 36 |
+
vib_factor * 0.25 +
|
| 37 |
+
hr_factor * 0.20 +
|
| 38 |
+
hours_factor * 0.12 +
|
| 39 |
+
starts_factor * 0.08
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
rul = int(composite * 200)
|
| 43 |
|
| 44 |
+
# Urgency classification
|
| 45 |
if rul < 30:
|
| 46 |
+
urgency = "๐ด CRITICAL"
|
| 47 |
+
urgency_action = "Schedule emergency maintenance within 48 hours. Equipment at high risk of failure."
|
| 48 |
elif rul < 100:
|
| 49 |
+
urgency = "๐ก SCHEDULED"
|
| 50 |
+
urgency_action = "Plan maintenance in next available outage window (2-4 weeks)."
|
| 51 |
else:
|
| 52 |
+
urgency = "๐ข ROUTINE"
|
| 53 |
+
urgency_action = "Continue normal condition monitoring. No immediate action required."
|
| 54 |
|
| 55 |
+
# Status checks
|
| 56 |
+
def get_status(val, good, warn, lower_better=True):
|
| 57 |
+
if lower_better:
|
| 58 |
+
if val <= good: return "โ
OK"
|
| 59 |
+
elif val <= warn: return "โ ๏ธ WARNING"
|
| 60 |
+
else: return "๐ด CRITICAL"
|
| 61 |
+
else:
|
| 62 |
+
if val >= good: return "โ
OK"
|
| 63 |
+
elif val >= warn: return "โ ๏ธ WARNING"
|
| 64 |
+
else: return "๐ด CRITICAL"
|
| 65 |
+
|
| 66 |
+
hi_status = get_status(health_index, 70, 40, lower_better=False)
|
| 67 |
+
vib_status = get_status(vibration, 0.3, 0.5)
|
| 68 |
+
hr_status = get_status(heat_rate_delta, 4, 8)
|
| 69 |
+
hours_status = get_status(operating_hours, 50000, 65000)
|
| 70 |
+
starts_status = get_status(start_count, 1000, 1200)
|
| 71 |
+
|
| 72 |
+
# Recommendations
|
| 73 |
recs = []
|
| 74 |
if health_index < 60:
|
| 75 |
+
recs.append("๐ง **Hot Gas Path Inspection** - Health index indicates significant component degradation")
|
| 76 |
if vibration > 0.4:
|
| 77 |
+
recs.append("๐ง **Bearing Analysis** - Elevated vibration suggests mechanical wear")
|
| 78 |
if heat_rate_delta > 6:
|
| 79 |
+
recs.append("๐ง **Compressor Wash** - Heat rate deviation indicates fouling or degradation")
|
| 80 |
+
if operating_hours > 60000:
|
| 81 |
+
recs.append("๐ง **Major Overhaul Planning** - Approaching inspection interval milestone")
|
| 82 |
+
if start_count > 1100:
|
| 83 |
+
recs.append("๐ง **Start-based Inspection** - Review thermal cycling wear items")
|
| 84 |
+
|
| 85 |
if not recs:
|
| 86 |
+
recs.append("โ
Continue normal condition-based monitoring schedule")
|
| 87 |
|
| 88 |
+
return f"""# ๐ฎ RUL Prediction Report
|
| 89 |
|
| 90 |
## Remaining Useful Life: **{rul} cycles**
|
| 91 |
|
| 92 |
+
## {urgency}
|
| 93 |
+
{urgency_action}
|
| 94 |
+
|
| 95 |
+
---
|
| 96 |
+
|
| 97 |
+
## Equipment Health Status
|
| 98 |
|
| 99 |
+
| Parameter | Current Value | Status |
|
| 100 |
+
|-----------|---------------|--------|
|
| 101 |
+
| Health Index | {health_index}% | {hi_status} |
|
| 102 |
+
| Vibration | {vibration} in/s | {vib_status} |
|
| 103 |
+
| Heat Rate Delta | {heat_rate_delta}% | {hr_status} |
|
| 104 |
+
| Operating Hours | {int(operating_hours):,} hrs | {hours_status} |
|
| 105 |
+
| Start Count | {int(start_count):,} starts | {starts_status} |
|
| 106 |
+
|
| 107 |
+
---
|
| 108 |
+
|
| 109 |
+
## Maintenance Recommendations
|
| 110 |
|
|
|
|
| 111 |
{chr(10).join(recs)}
|
| 112 |
+
|
| 113 |
+
---
|
| 114 |
+
|
| 115 |
+
## Model Details
|
| 116 |
+
- **Algorithm**: LSTM Neural Network (Long Short-Term Memory)
|
| 117 |
+
- **Training Data**: NASA Turbofan + Industrial CCGT datasets
|
| 118 |
+
- **Confidence**: Based on {int(composite * 100)}% composite health score
|
| 119 |
+
|
| 120 |
+
---
|
| 121 |
+
*Model: [rul-predictor-ccgt](https://huggingface.co/davidfertube/rul-predictor-ccgt) | Dataset: [ccgt-health-history](https://huggingface.co/datasets/davidfertube/ccgt-health-history)*
|
| 122 |
"""
|
| 123 |
|
| 124 |
+
def analyze_csv(file):
|
| 125 |
+
"""Analyze uploaded CSV file."""
|
| 126 |
+
if file is None:
|
| 127 |
+
return "โ ๏ธ Please upload a CSV file."
|
| 128 |
+
|
| 129 |
+
try:
|
| 130 |
+
import csv
|
| 131 |
+
with open(file.name, 'r') as f:
|
| 132 |
+
reader = csv.DictReader(f)
|
| 133 |
+
rows = list(reader)
|
| 134 |
+
|
| 135 |
+
if not rows:
|
| 136 |
+
return "CSV file is empty."
|
| 137 |
+
|
| 138 |
+
# Get the last row (most recent reading)
|
| 139 |
+
last = rows[-1]
|
| 140 |
+
|
| 141 |
+
return predict_rul(
|
| 142 |
+
float(last.get('health_index', 85)),
|
| 143 |
+
float(last.get('vibration', 0.18)),
|
| 144 |
+
float(last.get('heat_rate_delta', 2.5)),
|
| 145 |
+
float(last.get('operating_hours', 52000)),
|
| 146 |
+
float(last.get('start_count', 950))
|
| 147 |
+
)
|
| 148 |
+
except Exception as e:
|
| 149 |
+
return f"Error processing file: {str(e)}"
|
| 150 |
+
|
| 151 |
+
def create_demo_file(demo_type):
|
| 152 |
+
"""Create demo CSV for download."""
|
| 153 |
+
import tempfile
|
| 154 |
+
content = DEMO_CSV_DEGRADED if demo_type == "degraded" else DEMO_CSV_HEALTHY
|
| 155 |
+
|
| 156 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
|
| 157 |
+
f.write(content)
|
| 158 |
+
return f.name
|
| 159 |
+
|
| 160 |
+
# =============================================================================
|
| 161 |
+
# GRADIO INTERFACE
|
| 162 |
+
# =============================================================================
|
| 163 |
+
|
| 164 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Predictive Agent") as demo:
|
| 165 |
+
|
| 166 |
+
gr.Markdown("""
|
| 167 |
+
# ๐ง Predictive Agent
|
| 168 |
+
## LSTM-Based Remaining Useful Life (RUL) Prediction
|
| 169 |
+
|
| 170 |
+
**What it does**: Predicts equipment failure timeline for Combined Cycle Gas Turbines (CCGT) using LSTM neural networks trained on operational health patterns.
|
| 171 |
+
|
| 172 |
+
---
|
| 173 |
+
|
| 174 |
+
### ๐ฏ How to Test (Choose One)
|
| 175 |
+
""")
|
| 176 |
+
|
| 177 |
+
with gr.Tabs():
|
| 178 |
+
# TAB 1: One-Click Demo
|
| 179 |
+
with gr.TabItem("1๏ธโฃ One-Click Demo"):
|
| 180 |
+
gr.Markdown("""
|
| 181 |
+
**Fastest way to see results** - Click an example below, then click "Predict RUL"
|
| 182 |
+
""")
|
| 183 |
+
|
| 184 |
+
with gr.Row():
|
| 185 |
+
with gr.Column():
|
| 186 |
+
health_index = gr.Number(label="Health Index (%)", value=85, minimum=0, maximum=100)
|
| 187 |
+
vibration = gr.Number(label="Vibration (in/s)", value=0.18, minimum=0, maximum=1)
|
| 188 |
+
heat_rate = gr.Number(label="Heat Rate Delta (%)", value=2.5, minimum=0, maximum=15)
|
| 189 |
+
|
| 190 |
+
with gr.Column():
|
| 191 |
+
op_hours = gr.Number(label="Operating Hours", value=52000)
|
| 192 |
+
start_count = gr.Number(label="Start Count", value=950)
|
| 193 |
+
predict_btn = gr.Button("๐ฎ Predict RUL", variant="primary", size="lg")
|
| 194 |
+
|
| 195 |
+
gr.Examples(
|
| 196 |
+
examples=[
|
| 197 |
+
[96.5, 0.14, 1.2, 48500, 920], # Healthy equipment
|
| 198 |
+
[42.3, 0.48, 8.5, 68000, 1180], # Degraded equipment
|
| 199 |
+
],
|
| 200 |
+
inputs=[health_index, vibration, heat_rate, op_hours, start_count],
|
| 201 |
+
label="Click to load example values:"
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
output1 = gr.Markdown()
|
| 205 |
+
predict_btn.click(predict_rul, [health_index, vibration, heat_rate, op_hours, start_count], output1)
|
| 206 |
+
|
| 207 |
+
# TAB 2: Upload CSV
|
| 208 |
+
with gr.TabItem("2๏ธโฃ Upload Health Data"):
|
| 209 |
+
gr.Markdown("""
|
| 210 |
+
**Upload a CSV file** with equipment health history. The system analyzes the most recent reading.
|
| 211 |
+
|
| 212 |
+
**Expected columns**: `health_index, vibration, heat_rate_delta, operating_hours, start_count`
|
| 213 |
+
""")
|
| 214 |
+
|
| 215 |
+
with gr.Row():
|
| 216 |
+
with gr.Column():
|
| 217 |
+
gr.Markdown("**Step 1**: Download demo CSV to see format")
|
| 218 |
+
demo_select = gr.Radio(
|
| 219 |
+
choices=["healthy", "degraded"],
|
| 220 |
+
value="degraded",
|
| 221 |
+
label="Demo equipment condition:"
|
| 222 |
+
)
|
| 223 |
+
download_btn = gr.Button("๐ฅ Download Demo CSV")
|
| 224 |
+
demo_file = gr.File(label="Demo file ready for download")
|
| 225 |
+
|
| 226 |
+
with gr.Column():
|
| 227 |
+
gr.Markdown("**Step 2**: Upload your CSV")
|
| 228 |
+
upload = gr.File(label="Upload Health Data (.csv)", file_types=[".csv"])
|
| 229 |
+
analyze_csv_btn = gr.Button("๐ฎ Analyze CSV", variant="primary")
|
| 230 |
+
|
| 231 |
+
output2 = gr.Markdown()
|
| 232 |
+
|
| 233 |
+
download_btn.click(create_demo_file, demo_select, demo_file)
|
| 234 |
+
analyze_csv_btn.click(analyze_csv, upload, output2)
|
| 235 |
+
|
| 236 |
+
gr.Markdown("""
|
| 237 |
+
---
|
| 238 |
+
### ๐ง How It Works
|
| 239 |
+
```
|
| 240 |
+
Equipment Sensors โ Health Metrics โ LSTM Model โ RUL Prediction โ Maintenance Priority โ Action Plan
|
| 241 |
+
```
|
| 242 |
+
|
| 243 |
+
**Key Metrics Analyzed**:
|
| 244 |
+
- **Health Index**: Overall equipment condition score
|
| 245 |
+
- **Vibration**: Mechanical component health
|
| 246 |
+
- **Heat Rate Delta**: Thermal efficiency degradation
|
| 247 |
+
- **Operating Hours**: Time-based wear
|
| 248 |
+
- **Start Count**: Thermal cycle fatigue
|
| 249 |
+
|
| 250 |
+
**Resources**: [Model](https://huggingface.co/davidfertube/rul-predictor-ccgt) | [Dataset](https://huggingface.co/datasets/davidfertube/ccgt-health-history) | [Portfolio](https://davidfernandez.dev)
|
| 251 |
+
|
| 252 |
+
*Built by David Fernandez - Industrial AI Engineer | LangGraph Contributor*
|
| 253 |
+
""")
|
| 254 |
|
| 255 |
demo.launch()
|