Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,15 +2,14 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import pickle
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
with open(
|
| 7 |
-
model = pickle.load(
|
| 8 |
|
| 9 |
-
with open(
|
| 10 |
-
feature_columns = pickle.load(
|
| 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 |
-
|
| 23 |
-
|
| 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
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
# Reorder columns to match the order in the training data
|
| 31 |
-
sample_df = sample_df[feature_columns]
|
| 32 |
|
| 33 |
-
|
| 34 |
-
prediction = model.predict(
|
| 35 |
|
| 36 |
-
# Initialize the reason list
|
| 37 |
reasons = []
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
"Process temperature
|
| 43 |
-
|
| 44 |
-
"
|
| 45 |
-
|
| 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 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
return result_message
|
| 85 |
|
| 86 |
-
#
|
| 87 |
custom_css = """
|
| 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 |
-
gr.
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
)
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
if __name__ == "__main__":
|
| 159 |
-
|
|
|
|
| 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()
|