shahed125238 commited on
Commit
39a46ae
·
verified ·
1 Parent(s): fdad5d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -32
app.py CHANGED
@@ -2,14 +2,16 @@ import gradio as gr
2
  import pandas as pd
3
  import pickle
4
 
5
- # Load model and columns
6
  with open("logistic_regression_model.pkl", "rb") as f:
7
  model = pickle.load(f)
8
 
9
  with open("feature_columns.pkl", "rb") as f:
10
  feature_columns = pickle.load(f)
11
 
 
12
  def predict_status(air_temp, proc_temp, speed, torque, tool_wear):
 
13
  sample_input = {
14
  "Air temperature [K]": air_temp,
15
  "Process temperature [K]": proc_temp,
@@ -30,26 +32,26 @@ def predict_status(air_temp, proc_temp, speed, torque, tool_wear):
30
  reasons = []
31
 
32
  if air_temp < 150 or air_temp > 500:
33
- reasons.append("Air temperature out of optimal range.")
34
  if proc_temp < 150 or proc_temp > 500:
35
  reasons.append("Process temperature not within safe limits.")
36
  if speed < 500 or speed > 1500:
37
- reasons.append("Rotational speed outside recommended range.")
38
  if torque < 100 or torque > 800:
39
- reasons.append("Torque level may stress machine components.")
40
  if tool_wear > 300:
41
- reasons.append("Tool wear is high — maintenance recommended.")
42
 
43
  if prediction == 0:
44
- msg = "❌ **Machine Status: NOT OPERATIONAL**\n\n"
45
- msg += "\n".join([f"- {r}" for r in reasons]) if reasons else "- Cause not clearly identified."
46
  else:
47
- msg = "✅ **Machine Status: OPERATING NORMALLY**\n\nAll parameters are within acceptable ranges."
48
 
49
- return msg
50
 
51
 
52
- # ---------- Professional CSS ----------
53
  custom_css = """
54
  body {
55
  background-color: #0f172a;
@@ -68,7 +70,6 @@ h1 {
68
  text-align: center;
69
  font-size: 32px;
70
  color: #e5e7eb;
71
- margin-bottom: 8px;
72
  }
73
 
74
  .subtitle {
@@ -77,27 +78,23 @@ h1 {
77
  margin-bottom: 32px;
78
  }
79
 
80
- .block {
81
  background: #020617;
82
  border: 1px solid #1e293b;
83
  border-radius: 14px;
84
  padding: 24px;
85
  }
86
 
87
- .output-box {
88
- background: #020617;
89
- border: 1px solid #1e293b;
90
- border-radius: 14px;
91
- padding: 24px;
92
- min-height: 240px;
93
- font-size: 16px;
94
- }
95
-
96
  label {
97
  color: #cbd5f5 !important;
98
  font-weight: 600;
99
  }
100
 
 
 
 
 
 
101
  footer {
102
  text-align: center;
103
  color: #64748b;
@@ -106,36 +103,36 @@ footer {
106
  }
107
  """
108
 
109
- # ---------- Layout ----------
110
- with gr.Blocks(css=custom_css) as demo:
 
111
  gr.Markdown(
112
  "<h1>Smart Machine Health Predictor</h1>"
113
- "<div class='subtitle'>Predict operational health using real-time machine parameters</div>"
114
  )
115
 
116
  with gr.Row():
117
  with gr.Column(scale=1):
118
- with gr.Group(elem_classes="block"):
119
  air = gr.Slider(0, 1000, value=300, label="Air Temperature (K)")
120
  proc = gr.Slider(0, 1000, value=300, label="Process Temperature (K)")
121
  speed = gr.Slider(0, 2000, value=1000, label="Rotational Speed (rpm)")
122
  torque = gr.Slider(0, 1000, value=500, label="Torque (Nm)")
123
  wear = gr.Slider(0, 1000, value=100, label="Tool Wear (min)")
124
 
125
- submit = gr.Button("Run Prediction", variant="primary")
126
 
127
  with gr.Column(scale=1):
128
- output = gr.Markdown(elem_classes="output-box")
129
 
130
- submit.click(
131
  predict_status,
132
  inputs=[air, proc, speed, torque, wear],
133
  outputs=output
134
  )
135
 
136
- gr.Markdown(
137
- "<footer>Built with Gradio • Machine Health Monitoring System</footer>"
138
- )
139
 
 
140
  if __name__ == "__main__":
141
- demo.launch()
 
2
  import pandas as pd
3
  import pickle
4
 
5
+ # ------------------ Load Model ------------------
6
  with open("logistic_regression_model.pkl", "rb") as f:
7
  model = pickle.load(f)
8
 
9
  with open("feature_columns.pkl", "rb") as f:
10
  feature_columns = pickle.load(f)
11
 
12
+ # ------------------ Prediction Function ------------------
13
  def predict_status(air_temp, proc_temp, speed, torque, tool_wear):
14
+
15
  sample_input = {
16
  "Air temperature [K]": air_temp,
17
  "Process temperature [K]": proc_temp,
 
32
  reasons = []
33
 
34
  if air_temp < 150 or air_temp > 500:
35
+ reasons.append("Air temperature outside optimal range (150–500 K).")
36
  if proc_temp < 150 or proc_temp > 500:
37
  reasons.append("Process temperature not within safe limits.")
38
  if speed < 500 or speed > 1500:
39
+ reasons.append("Rotational speed is outside recommended range.")
40
  if torque < 100 or torque > 800:
41
+ reasons.append("Torque level may damage machine components.")
42
  if tool_wear > 300:
43
+ reasons.append("Tool wear is high — maintenance required.")
44
 
45
  if prediction == 0:
46
+ message = "❌ **Machine Status: NOT OPERATIONAL**\n\n"
47
+ message += "\n".join([f"- {r}" for r in reasons]) if reasons else "- No clear fault detected."
48
  else:
49
+ message = "✅ **Machine Status: OPERATING NORMALLY**\n\nAll parameters are within acceptable ranges."
50
 
51
+ return message
52
 
53
 
54
+ # ------------------ Professional CSS ------------------
55
  custom_css = """
56
  body {
57
  background-color: #0f172a;
 
70
  text-align: center;
71
  font-size: 32px;
72
  color: #e5e7eb;
 
73
  }
74
 
75
  .subtitle {
 
78
  margin-bottom: 32px;
79
  }
80
 
81
+ .panel {
82
  background: #020617;
83
  border: 1px solid #1e293b;
84
  border-radius: 14px;
85
  padding: 24px;
86
  }
87
 
 
 
 
 
 
 
 
 
 
88
  label {
89
  color: #cbd5f5 !important;
90
  font-weight: 600;
91
  }
92
 
93
+ .output-box {
94
+ min-height: 240px;
95
+ font-size: 16px;
96
+ }
97
+
98
  footer {
99
  text-align: center;
100
  color: #64748b;
 
103
  }
104
  """
105
 
106
+ # ------------------ Gradio Layout ------------------
107
+ with gr.Blocks(css=custom_css) as app:
108
+
109
  gr.Markdown(
110
  "<h1>Smart Machine Health Predictor</h1>"
111
+ "<div class='subtitle'>Industrial Machine Failure Prediction System</div>"
112
  )
113
 
114
  with gr.Row():
115
  with gr.Column(scale=1):
116
+ with gr.Group(elem_classes="panel"):
117
  air = gr.Slider(0, 1000, value=300, label="Air Temperature (K)")
118
  proc = gr.Slider(0, 1000, value=300, label="Process Temperature (K)")
119
  speed = gr.Slider(0, 2000, value=1000, label="Rotational Speed (rpm)")
120
  torque = gr.Slider(0, 1000, value=500, label="Torque (Nm)")
121
  wear = gr.Slider(0, 1000, value=100, label="Tool Wear (min)")
122
 
123
+ predict_btn = gr.Button("Run Prediction", variant="primary")
124
 
125
  with gr.Column(scale=1):
126
+ output = gr.Markdown(elem_classes="panel output-box")
127
 
128
+ predict_btn.click(
129
  predict_status,
130
  inputs=[air, proc, speed, torque, wear],
131
  outputs=output
132
  )
133
 
134
+ gr.Markdown("<footer>Built with Gradio • Predictive Maintenance Dashboard</footer>")
 
 
135
 
136
+ # ------------------ Launch ------------------
137
  if __name__ == "__main__":
138
+ app.launch()