shahed125238 commited on
Commit
1a089b7
·
verified ·
1 Parent(s): dd88bf3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -131
app.py CHANGED
@@ -2,15 +2,14 @@ import gradio as gr
2
  import pandas as pd
3
  import pickle
4
 
5
- # Load the saved model and feature columns
6
- with open('logistic_regression_model.pkl', 'rb') as model_file:
7
- model = pickle.load(model_file)
8
 
9
- with open('feature_columns.pkl', 'rb') as col_file:
10
- feature_columns = pickle.load(col_file)
11
 
12
  def predict_status(air_temp, proc_temp, speed, torque, tool_wear):
13
- # Prepare the input data
14
  sample_input = {
15
  "Air temperature [K]": air_temp,
16
  "Process temperature [K]": proc_temp,
@@ -18,142 +17,125 @@ def predict_status(air_temp, proc_temp, speed, torque, tool_wear):
18
  "Torque [Nm]": torque,
19
  "Tool wear [min]": tool_wear
20
  }
21
-
22
- # Convert the sample input to a DataFrame
23
- sample_df = pd.DataFrame([sample_input])
24
-
25
- # Ensure all columns from training data are present in the sample DataFrame
26
  for col in feature_columns:
27
- if col not in sample_df.columns:
28
- sample_df[col] = 0 # Add missing columns and fill with 0
29
-
30
- # Reorder columns to match the order in the training data
31
- sample_df = sample_df[feature_columns]
32
 
33
- # Make a prediction
34
- prediction = model.predict(sample_df)[0]
35
 
36
- # Initialize the reason list
37
  reasons = []
38
 
39
- # Define acceptable ranges for each feature with user-friendly feedback
40
- acceptable_ranges = {
41
- "Air temperature [K]": (150, 500),
42
- "Process temperature [K]": (150, 500),
43
- "Rotational speed [rpm]": (500, 1500),
44
- "Torque [Nm]": (100, 800),
45
- "Tool wear [min]": (50, 300)
46
- }
 
 
47
 
48
- # User-friendly reasons for each feature
49
- if air_temp < 150:
50
- reasons.append("• The air temperature is too low, which might be causing the machine to underperform.")
51
- elif air_temp > 500:
52
- reasons.append("• The air temperature is too high, which could be causing the machine to overheat.")
53
-
54
- if proc_temp < 150:
55
- reasons.append("• The process temperature is below the normal range, possibly affecting machine efficiency.")
56
- elif proc_temp > 500:
57
- reasons.append("• The process temperature is too high, which might lead to overheating.")
58
-
59
- if speed < 500:
60
- reasons.append("• The rotational speed is too low, causing the machine to operate inefficiently.")
61
- elif speed > 1500:
62
- reasons.append("• The rotational speed is too high, which might be causing excessive strain on the machine.")
63
-
64
- if torque < 100:
65
- reasons.append("• The torque is too low, which may indicate that the machine is not operating at its full capacity.")
66
- elif torque > 800:
67
- reasons.append("• The torque is too high, possibly putting excessive stress on the machine's components.")
68
-
69
- if tool_wear < 50:
70
- reasons.append("• The tool wear is minimal, but routine checks are still recommended to prevent future issues.")
71
- elif tool_wear > 300:
72
- reasons.append("• The tool wear is high, and the tool may need to be replaced soon to maintain optimal performance.")
73
-
74
- # Generate the final result message
75
  if prediction == 0:
76
- result_message = "🔴 Machine is NOT working.\n" # First line for machine status
77
- if reasons:
78
- result_message += "\n".join(reasons) # Second row onward for reasons with bullet points
79
- else:
80
- result_message += "No specific reason identified." # In case no issues found
81
  else:
82
- result_message = "🟢 Machine is working fine."
 
 
83
 
84
- return result_message
85
 
86
- # Define custom CSS for professional UI/UX
87
  custom_css = """
88
- body {
89
- background-color: #f0f2f5;
90
- font-family: Arial, sans-serif;
91
- }
92
- .gradio-container {
93
- max-width: 800px;
94
- margin: auto;
95
- padding: 40px;
96
- border-radius: 12px;
97
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
98
- background-color: #fff;
99
- border: 1px solid #ddd;
100
- }
101
- .gradio-title {
102
- font-size: 28px;
103
- font-weight: bold;
104
- color: #333;
105
- text-align: center;
106
- margin-bottom: 20px;
107
- }
108
- .gradio-description {
109
- font-size: 16px;
110
- color: #555;
111
- text-align: center;
112
- margin-bottom: 40px;
113
- }
114
- .gradio-footer {
115
- font-size: 14px;
116
- color: #777;
117
- text-align: center;
118
- margin-top: 20px;
119
- }
120
- .gradio-input label {
121
- font-weight: bold;
122
- color: #444;
123
- }
124
- footer {
125
- font-size: 12px;
126
- color: #555;
127
- text-align: center;
128
- padding: 10px;
129
- border-top: 1px solid #e0e0e0;
130
- margin-top: 40px;
131
- }
132
- footer a {
133
- color: #555;
134
- text-decoration: none;
135
- }
136
- footer a:hover {
137
- text-decoration: underline;
138
- }
 
 
139
  """
140
 
141
- # Define the Gradio interface
142
- iface = gr.Interface(
143
- fn=predict_status,
144
- inputs=[
145
- gr.Slider(label="Air Temperature (K)", minimum=0, maximum=1000, step=0.01, value=300),
146
- gr.Slider(label="Process Temperature (K)", minimum=0, maximum=1000, step=0.01, value=300),
147
- gr.Slider(label="Rotational Speed (rpm)", minimum=0, maximum=2000, step=1, value=1000),
148
- gr.Slider(label="Torque (Nm)", minimum=0, maximum=1000, step=0.1, value=500),
149
- gr.Slider(label="Tool Wear (min)", minimum=0, maximum=1000, step=1, value=100),
150
- ],
151
- outputs="text",
152
- title="Smart Machine Health Predictor",
153
- description="Provide machine parameters to predict whether the machine is working or not.",
154
- css=custom_css, # Apply custom CSS
155
- )
156
-
157
- # Launch the Gradio app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  if __name__ == "__main__":
159
- iface.launch()
 
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,
 
17
  "Torque [Nm]": torque,
18
  "Tool wear [min]": tool_wear
19
  }
20
+
21
+ df = pd.DataFrame([sample_input])
22
+
 
 
23
  for col in feature_columns:
24
+ if col not in df.columns:
25
+ df[col] = 0
 
 
 
26
 
27
+ df = df[feature_columns]
28
+ prediction = model.predict(df)[0]
29
 
 
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;
56
+ }
57
+
58
+ .gradio-container {
59
+ max-width: 1100px;
60
+ margin: auto;
61
+ padding: 32px;
62
+ background: #020617;
63
+ border-radius: 16px;
64
+ box-shadow: 0 20px 40px rgba(0,0,0,0.4);
65
+ }
66
+
67
+ h1 {
68
+ text-align: center;
69
+ font-size: 32px;
70
+ color: #e5e7eb;
71
+ margin-bottom: 8px;
72
+ }
73
+
74
+ .subtitle {
75
+ text-align: center;
76
+ color: #94a3b8;
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;
104
+ font-size: 13px;
105
+ margin-top: 24px;
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()