omiran/predictive_maintenance
0
1# app.py2 3from flask import Flask, render_template, request4import pandas as pd5import torch6#from model import predict7import numpy as np8import torch.nn as nn9import torch.optim as optim10from torch.utils.data import DataLoader, TensorDataset11 12from flask import Flask, render_template, request, jsonify13import pandas as pd14import numpy as np15import torch16from sklearn.preprocessing import StandardScaler17if torch.cuda.is_available():18 device = torch.device("cuda") # Use GPU19else:20 device = torch.device("cpu") # Use CPU21 22app = Flask(__name__)23 24# Define the neural network architecture25class MultiOutputNN(nn.Module):26 def __init__(self, input_dim, output_dims):27 super(MultiOutputNN, self).__init__()28 self.shared_hidden_layer = nn.Sequential(29 nn.Linear(input_dim, 64),30 nn.ReLU()31 )32 self.output_layers = nn.ModuleList([33 nn.Linear(64, out_dim) for out_dim in output_dims34 ])35 36 def forward(self, x):37 shared_output = self.shared_hidden_layer(x)38 print(f'shared_output shape: {shared_output.shape}')39 40 # Before matrix multiplication41 print(f'input shape: {x.shape}')42 43 outputs = [output_layer(shared_output) for output_layer in self.output_layers]44 for i, output in enumerate(outputs):45 print(f'output {i} shape: {output.shape}')46 return outputs47 48 49 50# Define the upload folder51app.config['UPLOAD_FOLDER'] = 'uploads'52 53# Dummy preprocessing function54def preprocess_input(vibration_file, gas_file):55 vibration_data = pd.read_csv(vibration_file)56 gas_data = pd.read_csv(gas_file, sep=';')57 58 # Perform any necessary preprocessing here59 60 gas_data.drop(['Unnamed: 15', 'Unnamed: 16', 'Date', 'Time', 'NMHC(GT)'], axis=1, inplace=True)61 cleaned_gas_data = gas_data.dropna()62 print(cleaned_gas_data.info())63 def replace_comma_with_period_in_columns(df, columns):64 for column in columns:65 df[column] = df[column].str.replace(',', '.', regex=False)66 return df67 68 comma_col = ['CO(GT)', 'C6H6(GT)', 'T', 'RH', 'AH']69 cleaned_gas_data = replace_comma_with_period_in_columns(cleaned_gas_data, comma_col)70 71 for i in comma_col:72 cleaned_gas_data[i] = pd.to_numeric(cleaned_gas_data[i], errors='coerce')73 74 75 # Concatenate the data76 processed_data = np.hstack((vibration_data.iloc[:9357, :], cleaned_gas_data))77 78 # Standardize the data79 scaler = StandardScaler()80 processed_data_scaled = pd.DataFrame(scaler.fit_transform(processed_data))81 processed_data_scaled = processed_data_scaled.values82 83 # Convert processed data to tensor84 X_data_tensor = torch.Tensor(processed_data_scaled ).to(device)85 86 return X_data_tensor87 88 89# this function sends email to representatives if the abnormality is detected 90# in the machine from the vibration sensor or if a gas is detected in environment 91# from the gas sensor92 93import smtplib94from email.mime.text import MIMEText95from email.mime.multipart import MIMEMultipart96 97def send_email(subject, body):98 sender_email = 'bidehassan@gmail.com' 99 sender_password = 'rmih ytdp znow dgjw'100 recipient_email = 'bidehassan@gmail.com'101 102 message = MIMEMultipart()103 message['From'] = sender_email104 message['To'] = recipient_email105 message['Subject'] = subject106 107 message.attach(MIMEText(body, 'plain'))108 109 try:110 server = smtplib.SMTP('smtp.gmail.com', 587)111 server.starttls()112 server.login(sender_email, sender_password)113 server.sendmail(sender_email, recipient_email, message.as_string())114 server.quit()115 print("Email sent successfully")116 except Exception as e:117 print(f"Failed to send email. Error: {e}")118 119# Example usage:120# send_email("Anomaly Detected", "Anomalies have been detected in both gas and vibration sensors.")121 122 123# gas sensor detection function124def detect_gas_anomaly(gas_sensor):125 thresholds = {126 'CO(GT)': gas_sensor['CO(GT)'].mean() - 2 * gas_sensor['CO(GT)'].std(),127 'PT08.S1(CO)': gas_sensor['PT08.S1(CO)'].mean() - 2 * gas_sensor['PT08.S1(CO)'].std(),128 'C6H6(GT)': gas_sensor['C6H6(GT)'].mean() - 2 * gas_sensor['C6H6(GT)'].std(),129 'PT08.S2(NMHC)': gas_sensor['PT08.S2(NMHC)'].mean() - 2 * gas_sensor['PT08.S2(NMHC)'].std(),130 'NOx(GT)': gas_sensor['NOx(GT)'].mean() - 2 * gas_sensor['NOx(GT)'].std(),131 'PT08.S3(NOx)': gas_sensor['PT08.S3(NOx)'].mean() - 2 * gas_sensor['PT08.S3(NOx)'].std(),132 'NO2(GT)': gas_sensor['NO2(GT)'].mean() - 2 * gas_sensor['NO2(GT)'].std(),133 'PT08.S4(NO2)': gas_sensor['PT08.S4(NO2)'].mean() - 2 * gas_sensor['PT08.S4(NO2)'].std(),134 'PT08.S5(O3)': gas_sensor['PT08.S5(O3)'].mean() - 2 * gas_sensor['PT08.S5(O3)'].std(),135 'T': gas_sensor['T'].mean() - 2 * gas_sensor['T'].std(),136 'RH': gas_sensor['RH'].mean() - 2 * gas_sensor['RH'].std(),137 'AH': gas_sensor['AH'].mean() - 2 * gas_sensor['AH'].std()138}139 140 # # Create a DataFrame to store anomaly flags141 # anomalies = pd.DataFrame(index=gas_data.index)142 143 # for parameter in thresholds.keys():144 # # Detect anomalies for each parameter145 # is_anomaly = gas_data[parameter] < thresholds[parameter]146 # anomalies[f'{parameter}_Anomaly'] = is_anomaly.astype(int)147 148 # return anomalies149 anomalies = []150 151 for _, data_point in gas_sensor.iterrows():152 data_point_anomaly = {}153 for parameter, threshold in thresholds.items():154 data_point_anomaly[f'{parameter}_Anomaly'] = 1 if data_point[parameter] < threshold else 0155 anomalies.append(data_point_anomaly)156 157 return anomalies158 159# vibration sensor abnormality detection function160# def detect_vibration_anomaly(vibration_data):161# thresholds = {162# 'Vibration_1': 1.226e-1,163# 'Vibration_2': 2.413e-1,164# 'Vibration_3': 1.187e-1165# }166 167# # Create a DataFrame to store anomaly flags168# anomalies = pd.DataFrame(index=range(len(vibration_data))) # Assuming list of lists169 170# for i, sensor_readings in enumerate(vibration_data):171# for sensor, threshold in thresholds.items():172# # Detect anomalies for each sensor173# is_anomaly = sensor_readings[i] > threshold174# anomalies[f'{sensor}_Anomaly'] = is_anomaly.astype(int)175 176# return anomalies177 178# def detect_vibration_anomaly(vibration_data):179# thresholds = {180# 'Vibration_1': 1.226e-1,181# 'Vibration_2': 2.413e-1,182# 'Vibration_3': 1.187e-1183# }184 185 # Create a list to store anomaly flags186 anomalies = []187 188 # for sensor_readings in vibration_data:189 # sensor_anomalies = {} # Store anomalies for each sensor190 # for i, (sensor, threshold) in enumerate(thresholds.items()):191 # # Detect anomalies for each sensor192 # is_anomaly = sensor_readings[i+2] > threshold # Assuming sensor data starts from index 2193 # sensor_anomalies[f'{sensor}_Anomaly'] = int(is_anomaly)194 # anomalies.append(sensor_anomalies)195 196 # return anomalies197 198 # for data_point in vibration_data:199 # data_point_anomaly = {}200 # for i, (sensor, threshold) in enumerate(thresholds.items()):201 # data_point_anomaly[f'{sensor}_Anomaly'] = 1 if data_point[i+2] > threshold else 0202 # anomalies.append(data_point_anomaly)203 204 # return anomalies205 206# def detect_vibration_anomaly(predictions):207# threshold = -9.8 # Set your threshold value208 209# # Create a list to store anomaly flags210# anomalies = []211 212# for prediction in predictions:213# data_point_anomaly = {}214# data_point_anomaly['Vibration_1_Anomaly'] = 1 if prediction < threshold else 0215# data_point_anomaly['Vibration_2_Anomaly'] = 1 if prediction < threshold else 0216# data_point_anomaly['Vibration_3_Anomaly'] = 1 if prediction < threshold else 0217# anomalies.append(data_point_anomaly)218 219# return anomalies220def detect_vibration_anomaly(vibration_data):221 predicted_values = vibration_data # Replace with your actual predicted values222 223 # Calculate mean and standard deviation224 mean_value = np.mean(predicted_values)225 std_dev = np.std(predicted_values)226 227 # Define a multiplier (e.g., 2 for 2 standard deviations)228 multiplier = 2229 230 # Calculate threshold231 threshold = mean_value - (multiplier * std_dev)232 233 # Create a list to store anomaly flags234 anomalies = [1 if value < threshold else 0 for value in predicted_values]235 236 return anomalies237 238 239 240 # Use the model to make predictions241def predict(input_data):242 # Load the model243 model = MultiOutputNN(input_dim=17, output_dims=[1, 11])244 model.load_state_dict(torch.load('multi_output_model.pth'))245 246 model.eval()247 with torch.no_grad():248 outputs = model(input_data)249 print(outputs)250 regression_prediction = outputs[0].cpu().numpy() #item() # Assuming first output is regression251 classification_prediction = outputs[1].cpu().numpy() #item() # Assuming second output is classification252 253 return regression_prediction, classification_prediction254 255 256# Route for the home page257@app.route('/')258def home():259 return render_template('index.html')260 261# Route to handle file uploads262@app.route('/upload', methods=['POST'])263def upload_files():264 vibration_file = request.files['vibration_data']265 gas_file = request.files['gas_data']266 267 vibration_path = f"{app.config['UPLOAD_FOLDER']}/vibration.csv"268 print(vibration_path)269 gas_path = f"{app.config['UPLOAD_FOLDER']}/gas.csv"270 271 vibration_file.save(vibration_path)272 gas_file.save(gas_path)273 274 # Preprocess the uploaded files275 processed_input = preprocess_input(vibration_path, gas_path)276 277 # Use the `processed_input` in your predict function278 prediction = predict(processed_input) 279 regression_prediction, classification_prediction = predict(processed_input) # Include regression prediction280 281 # Read and process the gas and vibration data282 gas_data = pd.read_csv(gas_path, sep=';')283 gas_data.drop(['Unnamed: 15', 'Unnamed: 16', 'Date', 'Time', 'NMHC(GT)'], axis=1, inplace=True)284 285 def replace_comma_with_period_in_columns(df, columns):286 for column in columns:287 df[column] = df[column].str.replace(',', '.', regex=False)288 return df289 290 comma_col = ['CO(GT)', 'C6H6(GT)', 'T', 'RH', 'AH']291 gas_data = replace_comma_with_period_in_columns(gas_data, comma_col)292 293 for i in comma_col:294 gas_data[i] = pd.to_numeric(gas_data[i], errors='coerce')295 296 gas_anomaly = gas_data.copy() # have a copy of the dataframe before converted tp list297 gas_data = gas_data.values.tolist()298 299 vibration_data = pd.read_csv(vibration_path).values[:9357, :].tolist()300 vib_data = pd.read_csv(vibration_path).values[:9357, :]301 302 # Detect anomalies in vibration data303 anomalies_vibration = detect_vibration_anomaly(prediction[0])304 305 # Detect anomalies in gas data306 gas_sensor = pd.DataFrame(gas_data, columns=['CO(GT)', 'PT08.S1(CO)', 'C6H6(GT)', 'PT08.S2(NMHC)', 'NOx(GT)', 'PT08.S3(NOx)', 'NO2(GT)', 'PT08.S4(NO2)', 'PT08.S5(O3)', 'T', 'RH', 'AH'])307 anomalies_gas = detect_gas_anomaly(gas_sensor)308 309 # Send email if anomalies are detected310 if anomalies_vibration or anomalies_gas:311 send_email("Anomaly Detected", "Anomaly detected in the system!")312 313 return render_template('result.html', prediction=prediction, gas_data=gas_data, vibration_data=vibration_data, anomalies_vibration=anomalies_vibration, anomalies_gas=anomalies_gas)314 315if __name__ == '__main__':316 app.run(debug=True)317 