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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from pathlib import Path
3
+
4
+ import gradio as gr
5
+ import pandas as pd
6
+ import torch
7
+ from sklearn.compose import ColumnTransformer
8
+ from sklearn.ensemble import RandomForestClassifier
9
+ from sklearn.metrics import (
10
+ accuracy_score,
11
+ average_precision_score,
12
+ confusion_matrix,
13
+ f1_score,
14
+ precision_score,
15
+ recall_score,
16
+ roc_auc_score,
17
+ )
18
+ from sklearn.model_selection import train_test_split
19
+ from sklearn.pipeline import Pipeline
20
+ from sklearn.preprocessing import OneHotEncoder
21
+ from transformers import AutoModelForCausalLM, AutoTokenizer
22
+
23
+ DATA_PATH = Path(__file__).parent / "ai4i2020.csv"
24
+
25
+ RANDOM_STATE = 42
26
+ DECISION_THRESHOLD = 0.40
27
+ LLM_MODEL_ID = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
28
+
29
+ FEATURES = [
30
+ "Type",
31
+ "Air temperature [K]",
32
+ "Process temperature [K]",
33
+ "Rotational speed [rpm]",
34
+ "Torque [Nm]",
35
+ "Tool wear [min]",
36
+ ]
37
+ TARGET = "Machine failure"
38
+
39
+
40
+ # -------------------------------------------------------------------
41
+ # Load dataset and train the predictive ML model
42
+ # -------------------------------------------------------------------
43
+ df = pd.read_csv(DATA_PATH)
44
+
45
+ X = df[FEATURES].copy()
46
+ y = df[TARGET].astype(int)
47
+
48
+ X_train, X_test, y_train, y_test = train_test_split(
49
+ X,
50
+ y,
51
+ test_size=0.20,
52
+ stratify=y,
53
+ random_state=RANDOM_STATE,
54
+ )
55
+
56
+ preprocessor = ColumnTransformer(
57
+ transformers=[
58
+ ("type", OneHotEncoder(handle_unknown="ignore"), ["Type"]),
59
+ ],
60
+ remainder="passthrough",
61
+ )
62
+
63
+ ml_model = Pipeline(
64
+ steps=[
65
+ ("preprocessor", preprocessor),
66
+ (
67
+ "classifier",
68
+ RandomForestClassifier(
69
+ n_estimators=250,
70
+ min_samples_leaf=2,
71
+ class_weight="balanced",
72
+ random_state=RANDOM_STATE,
73
+ n_jobs=-1,
74
+ ),
75
+ ),
76
+ ]
77
+ )
78
+
79
+ ml_model.fit(X_train, y_train)
80
+
81
+ test_probabilities = ml_model.predict_proba(X_test)[:, 1]
82
+ test_predictions = (test_probabilities >= DECISION_THRESHOLD).astype(int)
83
+
84
+ metrics = {
85
+ "Accuracy": accuracy_score(y_test, test_predictions),
86
+ "Precision": precision_score(y_test, test_predictions, zero_division=0),
87
+ "Recall": recall_score(y_test, test_predictions, zero_division=0),
88
+ "F1 Score": f1_score(y_test, test_predictions, zero_division=0),
89
+ "ROC-AUC": roc_auc_score(y_test, test_probabilities),
90
+ "PR-AUC": average_precision_score(y_test, test_probabilities),
91
+ }
92
+ cm = confusion_matrix(y_test, test_predictions)
93
+
94
+
95
+ # -------------------------------------------------------------------
96
+ # Load the local instruction-tuned language model
97
+ # -------------------------------------------------------------------
98
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
99
+ TORCH_DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
100
+
101
+ tokenizer = AutoTokenizer.from_pretrained(LLM_MODEL_ID)
102
+
103
+ llm_model = AutoModelForCausalLM.from_pretrained(
104
+ LLM_MODEL_ID,
105
+ torch_dtype=TORCH_DTYPE,
106
+ low_cpu_mem_usage=True,
107
+ )
108
+
109
+ llm_model.to(DEVICE)
110
+ llm_model.eval()
111
+
112
+
113
+ def validate_inputs(
114
+ product_type,
115
+ air_temperature,
116
+ process_temperature,
117
+ rotational_speed,
118
+ torque,
119
+ tool_wear,
120
+ ):
121
+ values = {
122
+ "Air temperature": air_temperature,
123
+ "Process temperature": process_temperature,
124
+ "Rotational speed": rotational_speed,
125
+ "Torque": torque,
126
+ "Tool wear": tool_wear,
127
+ }
128
+
129
+ for name, value in values.items():
130
+ if value is None:
131
+ raise gr.Error(f"{name} is required.")
132
+
133
+ if product_type not in {"L", "M", "H"}:
134
+ raise gr.Error("Product type must be L, M or H.")
135
+
136
+
137
+ def determine_risk_level(probability):
138
+ if probability >= 0.70:
139
+ return "High"
140
+ if probability >= DECISION_THRESHOLD:
141
+ return "Medium"
142
+ return "Low"
143
+
144
+
145
+ def build_reasoning_prompt(
146
+ product_type,
147
+ air_temperature,
148
+ process_temperature,
149
+ rotational_speed,
150
+ torque,
151
+ tool_wear,
152
+ probability,
153
+ risk_level,
154
+ ):
155
+ temperature_gap = process_temperature - air_temperature
156
+ predicted_state = (
157
+ "Potential machine failure"
158
+ if probability >= DECISION_THRESHOLD
159
+ else "Machine currently appears healthy"
160
+ )
161
+
162
+ system_message = """
163
+ You are an AI maintenance reasoning assistant for an educational engineering prototype.
164
+
165
+ Your role is to interpret a machine-learning prediction using only the supplied sensor readings.
166
+ Do not claim certainty.
167
+ Do not invent sensor values, standards, failure codes, manufacturer limits, or maintenance history.
168
+ Do not present your response as a substitute for a qualified engineer.
169
+ Do not recommend unsafe actions.
170
+ Any shutdown, repair, or maintenance action must be approved by a qualified engineer.
171
+
172
+ Return the response under exactly these headings:
173
+
174
+ ### Engineering Assessment
175
+ ### Evidence from Sensor Readings
176
+ ### Likely Engineering Concern
177
+ ### Recommended Immediate Action
178
+ ### Preventive Maintenance Recommendation
179
+ ### Maintenance Work Order
180
+ ### Human Approval Required
181
+ """.strip()
182
+
183
+ user_message = f"""
184
+ Analyse the following industrial machine data.
185
+
186
+ Product type: {product_type}
187
+ Air temperature: {air_temperature:.1f} K
188
+ Process temperature: {process_temperature:.1f} K
189
+ Temperature difference: {temperature_gap:.1f} K
190
+ Rotational speed: {rotational_speed:.0f} rpm
191
+ Torque: {torque:.1f} Nm
192
+ Tool wear: {tool_wear:.0f} minutes
193
+
194
+ Machine-learning result:
195
+ Predicted state: {predicted_state}
196
+ Failure probability: {probability:.1%}
197
+ Risk level: {risk_level}
198
+ Decision threshold: {DECISION_THRESHOLD:.0%}
199
+
200
+ Explain the result in clear language suitable for seventh-semester engineering students.
201
+ Keep the response practical and concise.
202
+ """.strip()
203
+
204
+ return [
205
+ {"role": "system", "content": system_message},
206
+ {"role": "user", "content": user_message},
207
+ ]
208
+
209
+
210
+ def generate_llm_reasoning(messages):
211
+ prompt_text = tokenizer.apply_chat_template(
212
+ messages,
213
+ tokenize=False,
214
+ add_generation_prompt=True,
215
+ )
216
+
217
+ inputs = tokenizer(
218
+ prompt_text,
219
+ return_tensors="pt",
220
+ truncation=True,
221
+ max_length=2048,
222
+ ).to(DEVICE)
223
+
224
+ with torch.inference_mode():
225
+ output_ids = llm_model.generate(
226
+ **inputs,
227
+ max_new_tokens=500,
228
+ do_sample=True,
229
+ temperature=0.35,
230
+ top_p=0.90,
231
+ repetition_penalty=1.08,
232
+ pad_token_id=tokenizer.eos_token_id,
233
+ )
234
+
235
+ generated_tokens = output_ids[0][inputs["input_ids"].shape[1]:]
236
+ response = tokenizer.decode(
237
+ generated_tokens,
238
+ skip_special_tokens=True,
239
+ ).strip()
240
+
241
+ return response
242
+
243
+
244
+ def create_structured_work_order(
245
+ probability,
246
+ risk_level,
247
+ llm_response,
248
+ ):
249
+ if risk_level == "High":
250
+ priority = "P1 - Immediate Engineering Review"
251
+ status = "Inspection Required"
252
+ elif risk_level == "Medium":
253
+ priority = "P2 - Urgent Monitoring and Inspection"
254
+ status = "Maintenance Review Required"
255
+ else:
256
+ priority = "P3 - Continue Monitoring"
257
+ status = "Monitor"
258
+
259
+ return {
260
+ "work_order_type": "AI-Assisted Predictive Maintenance",
261
+ "priority": priority,
262
+ "asset_status": status,
263
+ "predicted_failure_probability": f"{probability:.1%}",
264
+ "assigned_team": "Mechanical / Reliability Engineering",
265
+ "approval_status": "Pending Human Engineer Approval",
266
+ "llm_generated_engineering_note": llm_response,
267
+ }
268
+
269
+
270
+ def run_collaborative_ai(
271
+ product_type,
272
+ air_temperature,
273
+ process_temperature,
274
+ rotational_speed,
275
+ torque,
276
+ tool_wear,
277
+ ):
278
+ validate_inputs(
279
+ product_type,
280
+ air_temperature,
281
+ process_temperature,
282
+ rotational_speed,
283
+ torque,
284
+ tool_wear,
285
+ )
286
+
287
+ input_record = pd.DataFrame(
288
+ [
289
+ {
290
+ "Type": product_type,
291
+ "Air temperature [K]": air_temperature,
292
+ "Process temperature [K]": process_temperature,
293
+ "Rotational speed [rpm]": rotational_speed,
294
+ "Torque [Nm]": torque,
295
+ "Tool wear [min]": tool_wear,
296
+ }
297
+ ]
298
+ )
299
+
300
+ probability = float(ml_model.predict_proba(input_record)[0, 1])
301
+ prediction = int(probability >= DECISION_THRESHOLD)
302
+ risk_level = determine_risk_level(probability)
303
+
304
+ prediction_status = (
305
+ "FAILURE RISK DETECTED"
306
+ if prediction == 1
307
+ else "MACHINE APPEARS HEALTHY"
308
+ )
309
+
310
+ prediction_markdown = f"""
311
+ ## {prediction_status}
312
+
313
+ **Failure probability:** {probability:.1%}
314
+ **Risk level:** {risk_level}
315
+ **Decision threshold:** {DECISION_THRESHOLD:.0%}
316
+
317
+ The Random Forest model analysed the six machine operating inputs and estimated the probability of failure.
318
+ """
319
+
320
+ messages = build_reasoning_prompt(
321
+ product_type,
322
+ air_temperature,
323
+ process_temperature,
324
+ rotational_speed,
325
+ torque,
326
+ tool_wear,
327
+ probability,
328
+ risk_level,
329
+ )
330
+
331
+ llm_response = generate_llm_reasoning(messages)
332
+
333
+ agent_report = f"""
334
+ ## Local Agentic AI Reasoning
335
+
336
+ **Reasoning model:** `{LLM_MODEL_ID}`
337
+ **Execution device:** `{DEVICE}`
338
+
339
+ {llm_response}
340
+
341
+ ---
342
+
343
+ The language model interprets the ML output. It does not independently predict machine failure and must not override engineering judgement.
344
+ """
345
+
346
+ work_order = create_structured_work_order(
347
+ probability,
348
+ risk_level,
349
+ llm_response,
350
+ )
351
+
352
+ return prediction_markdown, agent_report, work_order
353
+
354
+
355
+ def model_summary():
356
+ failure_count = int(df[TARGET].sum())
357
+ healthy_count = int((df[TARGET] == 0).sum())
358
+
359
+ metrics_df = pd.DataFrame(
360
+ {
361
+ "Metric": list(metrics.keys()),
362
+ "Value": [round(value, 4) for value in metrics.values()],
363
+ }
364
+ )
365
+
366
+ confusion_df = pd.DataFrame(
367
+ cm,
368
+ index=["Actual Healthy", "Actual Failure"],
369
+ columns=["Predicted Healthy", "Predicted Failure"],
370
+ )
371
+
372
+ dataset_markdown = f"""
373
+ ## Dataset Summary
374
+
375
+ - **Rows:** {len(df):,}
376
+ - **Healthy records:** {healthy_count:,}
377
+ - **Failure records:** {failure_count:,}
378
+ - **Failure rate:** {failure_count / len(df):.2%}
379
+ - **Input features:** {len(FEATURES)}
380
+ - **Target:** Machine failure
381
+ - **Problem type:** Binary classification
382
+ - **Train/test split:** 80% / 20%, stratified
383
+ - **Decision threshold:** {DECISION_THRESHOLD:.0%}
384
+
385
+ The columns `TWF`, `HDF`, `PWF`, `OSF`, and `RNF` are excluded from the model inputs because they reveal the failure outcome and would create target leakage.
386
+ """
387
+ return dataset_markdown, metrics_df, confusion_df
388
+
389
+
390
+ with gr.Blocks(title="Engineering AI: Predictive Maintenance") as demo:
391
+ gr.Markdown(
392
+ """
393
+ # Engineering AI in Action
394
+ ### IoT Predictive Maintenance + Local Agentic AI Reasoning
395
+
396
+ This prototype combines:
397
+
398
+ 1. A **Random Forest machine-learning model** for failure prediction.
399
+ 2. A local **SmolLM2 instruction model** for engineering interpretation.
400
+ 3. An automated **maintenance work-order generator**.
401
+ 4. A mandatory **human-in-the-loop approval step**.
402
+ """
403
+ )
404
+
405
+ with gr.Tab("Live Engineering Demo"):
406
+ with gr.Row():
407
+ with gr.Column():
408
+ product_type = gr.Dropdown(
409
+ choices=["L", "M", "H"],
410
+ value="M",
411
+ label="Product Type",
412
+ )
413
+ air_temperature = gr.Slider(
414
+ minimum=295.0,
415
+ maximum=305.0,
416
+ value=300.0,
417
+ step=0.1,
418
+ label="Air Temperature [K]",
419
+ )
420
+ process_temperature = gr.Slider(
421
+ minimum=305.0,
422
+ maximum=315.0,
423
+ value=310.0,
424
+ step=0.1,
425
+ label="Process Temperature [K]",
426
+ )
427
+ rotational_speed = gr.Slider(
428
+ minimum=1100,
429
+ maximum=2900,
430
+ value=1450,
431
+ step=1,
432
+ label="Rotational Speed [rpm]",
433
+ )
434
+ torque = gr.Slider(
435
+ minimum=3.0,
436
+ maximum=80.0,
437
+ value=50.0,
438
+ step=0.1,
439
+ label="Torque [Nm]",
440
+ )
441
+ tool_wear = gr.Slider(
442
+ minimum=0,
443
+ maximum=253,
444
+ value=180,
445
+ step=1,
446
+ label="Tool Wear [min]",
447
+ )
448
+
449
+ run_button = gr.Button(
450
+ "Run ML + Local Agentic AI",
451
+ variant="primary",
452
+ )
453
+
454
+ with gr.Column():
455
+ prediction_output = gr.Markdown()
456
+ work_order_output = gr.JSON(
457
+ label="Generated Maintenance Work Order"
458
+ )
459
+
460
+ reasoning_output = gr.Markdown()
461
+
462
+ gr.Examples(
463
+ examples=[
464
+ ["M", 298.1, 308.6, 1551, 42.8, 0],
465
+ ["L", 300.7, 310.9, 1348, 58.7, 210],
466
+ ["H", 302.0, 313.5, 1280, 70.0, 225],
467
+ ],
468
+ inputs=[
469
+ product_type,
470
+ air_temperature,
471
+ process_temperature,
472
+ rotational_speed,
473
+ torque,
474
+ tool_wear,
475
+ ],
476
+ )
477
+
478
+ run_button.click(
479
+ fn=run_collaborative_ai,
480
+ inputs=[
481
+ product_type,
482
+ air_temperature,
483
+ process_temperature,
484
+ rotational_speed,
485
+ torque,
486
+ tool_wear,
487
+ ],
488
+ outputs=[
489
+ prediction_output,
490
+ reasoning_output,
491
+ work_order_output,
492
+ ],
493
+ )
494
+
495
+ with gr.Tab("Model & Dataset"):
496
+ dataset_output = gr.Markdown()
497
+ metrics_output = gr.Dataframe(
498
+ label="Test-set Performance",
499
+ interactive=False,
500
+ )
501
+ confusion_output = gr.Dataframe(
502
+ label="Confusion Matrix",
503
+ interactive=False,
504
+ )
505
+
506
+ demo.load(
507
+ fn=model_summary,
508
+ outputs=[
509
+ dataset_output,
510
+ metrics_output,
511
+ confusion_output,
512
+ ],
513
+ )
514
+
515
+ with gr.Tab("How the Collaboration Works"):
516
+ gr.Markdown(
517
+ f"""
518
+ ## Architecture
519
+
520
+ **IoT Sensor Inputs**
521
+ → **Random Forest Model** predicts failure probability
522
+ → **SmolLM2-1.7B-Instruct** interprets the readings and ML result
523
+ → **Action Layer** creates a maintenance work order
524
+ → **Human Engineer** validates and approves the recommendation
525
+
526
+ ## Models Used
527
+
528
+ - Predictive ML model: `RandomForestClassifier`
529
+ - Local reasoning model: `{LLM_MODEL_ID}`
530
+
531
+ ## Important Design Principle
532
+
533
+ The language model does not replace the predictive model. The Random Forest produces the numerical prediction. The language model converts that prediction and the sensor readings into an understandable engineering assessment.
534
+
535
+ This is an educational prototype and must not be used for real industrial safety decisions.
536
+ """
537
+ )
538
+
539
+
540
+ if __name__ == "__main__":
541
+ demo.launch()