shahed125238's picture
Update app.py
39a46ae verified
Raw History Blame Contribute Delete
3.76 kB
import gradio as gr
import pandas as pd
import pickle
# ------------------ Load Model ------------------
with open("logistic_regression_model.pkl", "rb") as f:
model = pickle.load(f)
with open("feature_columns.pkl", "rb") as f:
feature_columns = pickle.load(f)
# ------------------ Prediction Function ------------------
def predict_status(air_temp, proc_temp, speed, torque, tool_wear):
sample_input = {
"Air temperature [K]": air_temp,
"Process temperature [K]": proc_temp,
"Rotational speed [rpm]": speed,
"Torque [Nm]": torque,
"Tool wear [min]": tool_wear
}
df = pd.DataFrame([sample_input])
for col in feature_columns:
if col not in df.columns:
df[col] = 0
df = df[feature_columns]
prediction = model.predict(df)[0]
reasons = []
if air_temp < 150 or air_temp > 500:
reasons.append("Air temperature outside optimal range (150–500 K).")
if proc_temp < 150 or proc_temp > 500:
reasons.append("Process temperature not within safe limits.")
if speed < 500 or speed > 1500:
reasons.append("Rotational speed is outside recommended range.")
if torque < 100 or torque > 800:
reasons.append("Torque level may damage machine components.")
if tool_wear > 300:
reasons.append("Tool wear is high — maintenance required.")
if prediction == 0:
message = "❌ **Machine Status: NOT OPERATIONAL**\n\n"
message += "\n".join([f"- {r}" for r in reasons]) if reasons else "- No clear fault detected."
else:
message = "✅ **Machine Status: OPERATING NORMALLY**\n\nAll parameters are within acceptable ranges."
return message
# ------------------ Professional CSS ------------------
custom_css = """
body {
background-color: #0f172a;
}
.gradio-container {
max-width: 1100px;
margin: auto;
padding: 32px;
background: #020617;
border-radius: 16px;
box-shadow: 0 20px 40px rgba(0,0,0,0.4);
}
h1 {
text-align: center;
font-size: 32px;
color: #e5e7eb;
}
.subtitle {
text-align: center;
color: #94a3b8;
margin-bottom: 32px;
}
.panel {
background: #020617;
border: 1px solid #1e293b;
border-radius: 14px;
padding: 24px;
}
label {
color: #cbd5f5 !important;
font-weight: 600;
}
.output-box {
min-height: 240px;
font-size: 16px;
}
footer {
text-align: center;
color: #64748b;
font-size: 13px;
margin-top: 24px;
}
"""
# ------------------ Gradio Layout ------------------
with gr.Blocks(css=custom_css) as app:
gr.Markdown(
"<h1>Smart Machine Health Predictor</h1>"
"<div class='subtitle'>Industrial Machine Failure Prediction System</div>"
)
with gr.Row():
with gr.Column(scale=1):
with gr.Group(elem_classes="panel"):
air = gr.Slider(0, 1000, value=300, label="Air Temperature (K)")
proc = gr.Slider(0, 1000, value=300, label="Process Temperature (K)")
speed = gr.Slider(0, 2000, value=1000, label="Rotational Speed (rpm)")
torque = gr.Slider(0, 1000, value=500, label="Torque (Nm)")
wear = gr.Slider(0, 1000, value=100, label="Tool Wear (min)")
predict_btn = gr.Button("Run Prediction", variant="primary")
with gr.Column(scale=1):
output = gr.Markdown(elem_classes="panel output-box")
predict_btn.click(
predict_status,
inputs=[air, proc, speed, torque, wear],
outputs=output
)
gr.Markdown("<footer>Built with Gradio • Predictive Maintenance Dashboard</footer>")
# ------------------ Launch ------------------
if __name__ == "__main__":
app.launch()