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()