Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
934 lines (745 loc) · 20.4 KB
/
Copy pathapp.py
File metadata and controls
934 lines (745 loc) · 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
import os
import numpy as np
import pandas as pd
import streamlit as st
from typing import TypedDict
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import (
precision_score,
recall_score,
fbeta_score,
average_precision_score,
confusion_matrix,
)
from langgraph.graph import StateGraph, START, END
from langchain_groq import ChatGroq
from dotenv import load_dotenv
# ============================================================
# 0. PAGE + DATA CONFIG
# ============================================================
st.set_page_config(
page_title="VANTAGE | MineOps Copilot",
layout="wide",
)
DATA_URL = (
"https://archive.ics.uci.edu/ml/"
"machine-learning-databases/00601/ai4i2020.csv"
)
FEATURES = [
"Type",
"Air_Temp_K",
"Process_Temp_K",
"Rotational_Speed_rpm",
"Torque_Nm",
"Tool_Wear_min",
]
NUMERIC_FEATURES = [
"Air_Temp_K",
"Process_Temp_K",
"Rotational_Speed_rpm",
"Torque_Nm",
"Tool_Wear_min",
]
TARGET = "Machine_Failure"
COLUMN_MAP = {
"UDI": "UDI",
"Product ID": "Product_ID",
"Type": "Type",
"Air temperature [K]": "Air_Temp_K",
"Process temperature [K]": "Process_Temp_K",
"Rotational speed [rpm]": "Rotational_Speed_rpm",
"Torque [Nm]": "Torque_Nm",
"Tool wear [min]": "Tool_Wear_min",
"Machine failure": "Machine_Failure",
"TWF": "TWF",
"HDF": "HDF",
"PWF": "PWF",
"OSF": "OSF",
"RNF": "RNF",
}
DISPLAY_NAMES = {
"Air_Temp_K": "Air temperature",
"Process_Temp_K": "Process temperature",
"Rotational_Speed_rpm": "Rotational speed",
"Torque_Nm": "Torque",
"Tool_Wear_min": "Tool wear",
}
UNITS = {
"Air_Temp_K": "K",
"Process_Temp_K": "K",
"Rotational_Speed_rpm": "rpm",
"Torque_Nm": "Nm",
"Tool_Wear_min": "min",
}
# ============================================================
# 1. DATA
# ============================================================
@st.cache_data
def load_full_dataset():
"""
Prefer a local copy for demo reliability.
"""
df = None
for local_path in ["ai4i2020.csv", "ai4i2020_full.csv"]:
if os.path.exists(local_path):
df = pd.read_csv(local_path)
break
if df is None:
df = pd.read_csv(DATA_URL)
df = df.rename(columns=COLUMN_MAP)
required = FEATURES + [TARGET, "UDI"]
missing = [c for c in required if c not in df.columns]
if missing:
raise ValueError(f"Missing required columns: {missing}")
return df
# ============================================================
# 2. MODEL
# ============================================================
@st.cache_resource
def train_failure_model(df):
"""
Random Forest model:
- learns relationships from labelled data
- avoids arbitrary hand-set feature weights
Threshold:
- chosen on validation data using F2
- F2 weights recall more heavily than precision
"""
X = df[FEATURES].copy()
y = df[TARGET].astype(int).copy()
X_train, X_temp, y_train, y_temp = train_test_split(
X,
y,
test_size=0.40,
stratify=y,
random_state=42,
)
X_val, X_test, y_val, y_test = train_test_split(
X_temp,
y_temp,
test_size=0.50,
stratify=y_temp,
random_state=42,
)
preprocessor = ColumnTransformer(
[
(
"numeric",
StandardScaler(),
NUMERIC_FEATURES,
),
(
"categorical",
OneHotEncoder(handle_unknown="ignore"),
["Type"],
),
]
)
classifier = RandomForestClassifier(
n_estimators=350,
max_depth=10,
min_samples_leaf=3,
class_weight="balanced",
random_state=42,
n_jobs=-1,
)
model = Pipeline(
[
("preprocess", preprocessor),
("classifier", classifier),
]
)
model.fit(X_train, y_train)
val_probability = model.predict_proba(X_val)[:, 1]
threshold_rows = []
for threshold in np.arange(0.05, 0.96, 0.01):
val_prediction = (
val_probability >= threshold
).astype(int)
threshold_rows.append(
{
"threshold": threshold,
"precision": precision_score(
y_val,
val_prediction,
zero_division=0,
),
"recall": recall_score(
y_val,
val_prediction,
zero_division=0,
),
"f2": fbeta_score(
y_val,
val_prediction,
beta=2,
zero_division=0,
),
}
)
threshold_table = pd.DataFrame(threshold_rows)
best_row = threshold_table.loc[
threshold_table["f2"].idxmax()
]
alert_threshold = float(best_row["threshold"])
test_probability = model.predict_proba(X_test)[:, 1]
test_prediction = (
test_probability >= alert_threshold
).astype(int)
tn, fp, fn, tp = confusion_matrix(
y_test,
test_prediction,
labels=[0, 1],
).ravel()
metrics = {
"alert_threshold": alert_threshold,
"precision": precision_score(
y_test,
test_prediction,
zero_division=0,
),
"recall": recall_score(
y_test,
test_prediction,
zero_division=0,
),
"f2": fbeta_score(
y_test,
test_prediction,
beta=2,
zero_division=0,
),
"average_precision": average_precision_score(
y_test,
test_probability,
),
"true_positive": int(tp),
"false_positive": int(fp),
"false_negative": int(fn),
"true_negative": int(tn),
}
test_records = X_test.copy()
test_records[TARGET] = y_test.values
test_records["Model_Risk"] = test_probability
test_records["UDI"] = df.loc[
test_records.index,
"UDI",
].values
return model, metrics, threshold_table, test_records
# ============================================================
# 3. DEMO FLEET
# ============================================================
@st.cache_data
def build_demo_fleet(test_records):
"""
Build a 100-record demonstration fleet.
Historical failures are deliberately oversampled for demo visibility.
"""
failed = test_records[
test_records[TARGET] == 1
]
healthy = test_records[
test_records[TARGET] == 0
]
n_failed = min(15, len(failed))
n_healthy = 100 - n_failed
demo = pd.concat(
[
failed.sample(
n=n_failed,
random_state=42,
),
healthy.sample(
n=n_healthy,
random_state=42,
),
]
)
return (
demo.sample(frac=1, random_state=42)
.reset_index(drop=True)
)
# ============================================================
# 4. TELEMETRY CONTEXT
# ============================================================
def percentile_rank(
series: pd.Series,
value: float,
) -> float:
"""
Percentage of reference observations at or below current value.
"""
return float(
(series <= value).mean() * 100
)
def relative_band(percentile: float) -> str:
"""
Simple attention cue only.
"""
if percentile >= 90 or percentile <= 10:
return "Review signal"
return "Typical"
def relative_position_text(
percentile: float,
) -> str:
"""
Plain-language comparison against the reference dataset.
"""
if percentile >= 95:
return "Well above typical range"
if percentile >= 90:
return "Above typical range"
if percentile <= 5:
return "Well below typical range"
if percentile <= 10:
return "Below typical range"
return "Within typical range"
def build_telemetry_context(
full_df: pd.DataFrame,
selected_row: pd.Series,
) -> pd.DataFrame:
rows = []
for feature in NUMERIC_FEATURES:
value = float(
selected_row[feature]
)
percentile = percentile_rank(
full_df[feature],
value,
)
q10 = float(
full_df[feature].quantile(0.10)
)
q90 = float(
full_df[feature].quantile(0.90)
)
rows.append(
{
"Signal":
DISPLAY_NAMES[feature],
"Current":
f"{value:.1f} {UNITS[feature]}",
"Typical range":
f"{q10:.1f}–{q90:.1f} "
f"{UNITS[feature]}",
"Position":
relative_position_text(
percentile
),
"Flag":
relative_band(
percentile
),
}
)
return pd.DataFrame(rows)
# ============================================================
# 5. GENERATIVE-AI EXPLANATION LAYER
# ============================================================
load_dotenv()
GROQ_MODEL_CANDIDATES = [
"llama-3.3-70b-versatile",
"llama-3.1-8b-instant",
"openai/gpt-oss-20b",
]
def invoke_llm_with_fallback(
prompt: str,
):
"""
Optional explanation layer.
The quantitative dashboard keeps working if Groq fails.
"""
key = os.getenv("GROQ_API_KEY")
if not key:
return (
None,
"No GROQ_API_KEY configured",
)
last_error = None
for model_name in GROQ_MODEL_CANDIDATES:
try:
llm = ChatGroq(
model=model_name,
temperature=0.1,
groq_api_key=key,
)
response = llm.invoke(
prompt
)
return (
response.content,
model_name,
)
except Exception as exc:
last_error = (
f"{type(exc).__name__}: {exc}"
)
return None, last_error
class AgentState(TypedDict):
evidence: str
explanation: str
handover: str
def pass_evidence(
state: AgentState,
):
return {
"evidence":
state["evidence"]
}
def explain_risk(
state: AgentState,
):
prompt = f"""
You are supporting a reliability engineering team.
IMPORTANT BOUNDARIES:
- A quantitative ML model has already created the model risk score.
- Do NOT independently diagnose a mechanical failure.
- Do NOT invent safety limits, engineering thresholds, repair costs,
isolation procedures or financial savings.
- Statistical reference ranges are not engineering safety limits.
- Final maintenance decisions belong to authorised reliability /
maintenance personnel.
MODEL EVIDENCE:
{state['evidence']}
Write for an operational user.
OUTPUT:
WHY THIS ASSET WAS FLAGGED:
Explain the model status in plain language.
NOTABLE TELEMETRY:
Mention only the readings that stand out most strongly relative to the
reference data.
WHAT HAPPENS NEXT:
Recommend either continued monitoring or reliability review, based on
the model status.
"""
content, error = (
invoke_llm_with_fallback(
prompt
)
)
if content is not None:
return {
"explanation": content
}
return {
"explanation":
"The generative-AI explanation layer is unavailable. "
"The quantitative model remains operational. "
f"Technical note: {error}"
}
def draft_handover(
state: AgentState,
):
prompt = f"""
Convert this explanation into a concise shift-handover note.
BOUNDARIES:
- Do NOT invent safety procedures.
- Do NOT order equipment isolation.
- Do NOT invent financial values.
- Final action belongs to authorised reliability / maintenance staff.
EXPLANATION:
{state['explanation']}
FORMAT:
STATUS:
WHY:
NEXT STEP:
DECISION OWNER:
"""
content, _ = (
invoke_llm_with_fallback(
prompt
)
)
if content is not None:
return {
"handover": content
}
return {
"handover":
"STATUS: Review selected asset status.\n"
"WHY: Refer to model score and current telemetry context.\n"
"NEXT STEP: Follow the dashboard recommendation and approved "
"site procedures.\n"
"DECISION OWNER: Authorised reliability / maintenance team."
}
workflow = StateGraph(
AgentState
)
workflow.add_node(
"evidence",
pass_evidence,
)
workflow.add_node(
"explanation",
explain_risk,
)
workflow.add_node(
"handover",
draft_handover,
)
workflow.add_edge(
START,
"evidence",
)
workflow.add_edge(
"evidence",
"explanation",
)
workflow.add_edge(
"explanation",
"handover",
)
workflow.add_edge(
"handover",
END,
)
app_engine = workflow.compile()
# ============================================================
# 6. PREPARE MODEL + DEMO DATA
# ============================================================
full_df = load_full_dataset()
(
model,
metrics,
threshold_table,
test_records,
) = train_failure_model(
full_df
)
demo_df = build_demo_fleet(
test_records
)
demo_df["Risk_Priority_Score"] = (
demo_df["Model_Risk"] * 100
).round(1)
demo_df["Status"] = np.where(
demo_df["Model_Risk"]
>= metrics["alert_threshold"],
"Review Required",
"Monitor",
)
demo_df = demo_df.sort_values(
by="Model_Risk",
ascending=False,
).reset_index(drop=True)
# ============================================================
# APP
# ============================================================
st.title("VANTAGE | MineOps Copilot")
operations_tab, reliability_tab = st.tabs(
["Operations Dashboard", "Shift Handover"]
)
review_df = demo_df[
demo_df["Status"] == "Review Required"
].copy()
monitor_df = demo_df[
demo_df["Status"] == "Monitor"
].copy()
with operations_tab:
st.subheader("Fleet Overview")
c1, c2, c3 = st.columns(3)
c1.metric(
"Assets monitored",
len(demo_df),
)
c2.metric(
"Require review",
len(review_df),
)
c3.metric(
"Continue monitoring",
len(monitor_df),
)
if len(review_df) > 0:
st.warning(
f"{len(review_df)} assets require reliability review."
)
else:
st.success(
"No assets currently require reliability review."
)
st.subheader("Priority Assets")
priority_table = (
demo_df[
[
"UDI",
"Risk_Priority_Score",
"Status",
]
]
.head(10)
.copy()
)
priority_table.columns = [
"Asset ID",
"Risk priority score (%)",
"Status",
]
st.dataframe(
priority_table,
hide_index=True,
use_container_width=True,
)
st.divider()
st.subheader("Asset Status")
asset_options = demo_df.index.tolist()
selected_index = st.selectbox(
"Select asset:",
options=asset_options,
format_func=lambda i: str(
demo_df.loc[i, "UDI"]
),
key="dashboard_asset_index",
)
selected_row = demo_df.loc[
selected_index
].copy()
selected_udi = selected_row["UDI"]
risk_score = float(
selected_row["Risk_Priority_Score"]
)
threshold_percent = (
metrics["alert_threshold"] * 100
)
status = selected_row["Status"]
r1, r2, r3 = st.columns(3)
r1.metric(
"Risk priority score",
f"{risk_score:.1f}%",
)
r2.metric(
"Review threshold",
f"{threshold_percent:.1f}%",
)
r3.metric(
"Status",
status,
)
if status == "Review Required":
st.warning(
"Reliability review required."
)
else:
st.success("Monitor")
st.markdown("### Telemetry")
st.caption(f"Asset {selected_udi}")
telemetry_context = build_telemetry_context(
full_df=full_df,
selected_row=selected_row,
)
st.table(
telemetry_context.set_index("Signal")
)
with st.popover(
"What does 'Typical range' mean?"
):
st.write(
"This shows where most readings in the reference dataset usually fall. "
"A reading outside this range is unusual and may deserve attention, "
"but it does not automatically mean the equipment is unsafe or failing."
)
with reliability_tab:
review_df = demo_df[
demo_df["Status"] == "Review Required"
].copy()
if len(review_df) == 0:
st.success(
"No assets currently require reliability review."
)
else:
st.subheader("Shift Handover")
st.caption(
"Summary and recommended focus for the incoming reliability / maintenance shift."
)
if st.button(
"Generate Shift Handover",
type="primary",
key="generate_next_shift_brief",
):
fleet_rows = []
for _, row in review_df.sort_values(
"Risk_Priority_Score",
ascending=False,
).iterrows():
telemetry = build_telemetry_context(
full_df=full_df,
selected_row=row,
)
notable = telemetry[
telemetry["Flag"] == "Review signal"
]
notable_text = (
", ".join(
notable["Signal"].tolist()
)
if len(notable) > 0
else (
"no single telemetry signal "
"outside the typical range"
)
)
fleet_rows.append(
f"Asset {row['UDI']}: "
f"risk priority score "
f"{row['Risk_Priority_Score']:.1f}%; "
f"notable signals: {notable_text}."
)
fleet_evidence = "\n".join(
fleet_rows
)
fleet_prompt = f"""
You are preparing a concise shift handover for an incoming mining reliability / maintenance team.
CURRENT REVIEW QUEUE:
{fleet_evidence}
BOUNDARIES:
- Do not invent failures, safety incidents, repair costs, work orders or engineering limits.
- Do not authorise maintenance or isolation.
- Prioritise the highest-risk assets first.
- Do not describe any failure as imminent unless explicitly supported by the evidence.
- Do not name an individual decision owner.
Write exactly:
SHIFT SUMMARY:
Two concise sentences describing the current review workload.
TOP PRIORITY ASSETS:
List the top three assets in priority order, with one short reason each.
RECOMMENDED NEXT-SHIFT ACTIONS:
Give 2-4 concise, evidence-based actions for the incoming shift. These may include reviewing the highest-risk assets first, verifying abnormal telemetry, trending selected signals, or escalating an asset for reliability review.
DECISION OWNER:
Reliability / maintenance team.
"""
fleet_summary, error = invoke_llm_with_fallback(
fleet_prompt
)
if fleet_summary is not None:
st.session_state[
"next_shift_brief"
] = fleet_summary
st.session_state[
"next_shift_brief_error"
] = None
else:
st.session_state[
"next_shift_brief"
] = None
st.session_state[
"next_shift_brief_error"
] = str(error)
if st.session_state.get("next_shift_brief"):
st.success(
st.session_state[
"next_shift_brief"
]
)
if (
st.session_state.get("next_shift_brief") is None
and st.session_state.get(
"next_shift_brief_error"
)
):
st.error(
"Shift handover service unavailable."
)