File size: 5,467 Bytes
065b33f
 
284d73b
065b33f
6f45e05
284d73b
 
065b33f
284d73b
 
065b33f
2e5b50e
284d73b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
065b33f
284d73b
 
3c1b999
82d2432
 
 
2e5b50e
82d2432
 
 
 
 
 
 
 
2e5b50e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82d2432
 
3c1b999
2e5b50e
 
 
 
 
284d73b
2e5b50e
82d2432
 
284d73b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
065b33f
284d73b
 
 
 
 
065b33f
284d73b
200ed5e
284d73b
 
065b33f
 
284d73b
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
150
151
152
153
154
155
156
157
158
159
160
import gradio as gr
import pandas as pd
import pickle

# Load the saved model and feature columns
with open('logistic_regression_model.pkl', 'rb') as model_file:
    model = pickle.load(model_file)

with open('feature_columns.pkl', 'rb') as col_file:
    feature_columns = pickle.load(col_file)

def predict_status(air_temp, proc_temp, speed, torque, tool_wear):
    # Prepare the input data
    sample_input = {
        "Air temperature [K]": air_temp,
        "Process temperature [K]": proc_temp,
        "Rotational speed [rpm]": speed,
        "Torque [Nm]": torque,
        "Tool wear [min]": tool_wear
    }
    
    # Convert the sample input to a DataFrame
    sample_df = pd.DataFrame([sample_input])
    
    # Ensure all columns from training data are present in the sample DataFrame
    for col in feature_columns:
        if col not in sample_df.columns:
            sample_df[col] = 0  # Add missing columns and fill with 0
    
    # Reorder columns to match the order in the training data
    sample_df = sample_df[feature_columns]

    # Make a prediction
    prediction = model.predict(sample_df)[0]

    # Initialize the reason list
    reasons = []

    # Define acceptable ranges for each feature with user-friendly feedback
    acceptable_ranges = {
        "Air temperature [K]": (150, 500),
        "Process temperature [K]": (150, 500),
        "Rotational speed [rpm]": (500, 1500),
        "Torque [Nm]": (100, 800),
        "Tool wear [min]": (50, 300)
    }

    # User-friendly reasons for each feature
    if air_temp < 150:
        reasons.append("• The air temperature is too low, which might be causing the machine to underperform.")
    elif air_temp > 500:
        reasons.append("• The air temperature is too high, which could be causing the machine to overheat.")

    if proc_temp < 150:
        reasons.append("• The process temperature is below the normal range, possibly affecting machine efficiency.")
    elif proc_temp > 500:
        reasons.append("• The process temperature is too high, which might lead to overheating.")

    if speed < 500:
        reasons.append("• The rotational speed is too low, causing the machine to operate inefficiently.")
    elif speed > 1500:
        reasons.append("• The rotational speed is too high, which might be causing excessive strain on the machine.")

    if torque < 100:
        reasons.append("• The torque is too low, which may indicate that the machine is not operating at its full capacity.")
    elif torque > 800:
        reasons.append("• The torque is too high, possibly putting excessive stress on the machine's components.")

    if tool_wear < 50:
        reasons.append("• The tool wear is minimal, but routine checks are still recommended to prevent future issues.")
    elif tool_wear > 300:
        reasons.append("• The tool wear is high, and the tool may need to be replaced soon to maintain optimal performance.")

    # Generate the final result message
    if prediction == 0:
        result_message = "🔴 Machine is NOT working.\n"  # First line for machine status
        if reasons:
            result_message += "\n".join(reasons)  # Second row onward for reasons with bullet points
        else:
            result_message += "No specific reason identified."  # In case no issues found
    else:
        result_message = "🟢 Machine is working fine."

    return result_message

# Define custom CSS for professional UI/UX
custom_css = """
    body {
        background-color: #f0f2f5;
        font-family: Arial, sans-serif;
    }
    .gradio-container {
        max-width: 800px;
        margin: auto;
        padding: 40px;
        border-radius: 12px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        background-color: #fff;
        border: 1px solid #ddd;
    }
    .gradio-title {
        font-size: 28px;
        font-weight: bold;
        color: #333;
        text-align: center;
        margin-bottom: 20px;
    }
    .gradio-description {
        font-size: 16px;
        color: #555;
        text-align: center;
        margin-bottom: 40px;
    }
    .gradio-footer {
        font-size: 14px;
        color: #777;
        text-align: center;
        margin-top: 20px;
    }
    .gradio-input label {
        font-weight: bold;
        color: #444;
    }
    footer {
        font-size: 12px;
        color: #555;
        text-align: center;
        padding: 10px;
        border-top: 1px solid #e0e0e0;
        margin-top: 40px;
    }
    footer a {
        color: #555;
        text-decoration: none;
    }
    footer a:hover {
        text-decoration: underline;
    }
"""

# Define the Gradio interface
iface = gr.Interface(
    fn=predict_status,
    inputs=[
        gr.Slider(label="Air Temperature (K)", minimum=0, maximum=1000, step=0.01, value=300),
        gr.Slider(label="Process Temperature (K)", minimum=0, maximum=1000, step=0.01, value=300),
        gr.Slider(label="Rotational Speed (rpm)", minimum=0, maximum=2000, step=1, value=1000),
        gr.Slider(label="Torque (Nm)", minimum=0, maximum=1000, step=0.1, value=500),
        gr.Slider(label="Tool Wear (min)", minimum=0, maximum=1000, step=1, value=100),
    ],
    outputs="text",
    title="Smart Machine Health Predictor",
    description="Provide machine parameters to predict whether the machine is working or not.",
    css=custom_css,  # Apply custom CSS
)

# Launch the Gradio app
if __name__ == "__main__":
    iface.launch()