shahed125238 commited on
Commit
065b33f
·
verified ·
1 Parent(s): cf89eb5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+ import pandas as pd
4
+ from sklearn.preprocessing import StandardScaler, OneHotEncoder
5
+
6
+ # Load the trained model
7
+ model = joblib.load('logistic_regression_model.pkl')
8
+
9
+ # Load the scaler and encoder used during training (if applicable)
10
+ scaler = StandardScaler() # Make sure to use the same scaler as during training
11
+ encoder = OneHotEncoder(sparse=False, drop='first') # Make sure to use the same encoder as during training
12
+
13
+ # Define the input feature names
14
+ input_features = [
15
+ "UDI",
16
+ "Air temperature [K]",
17
+ "Process temperature [K]",
18
+ "Rotational speed [rpm]",
19
+ "Torque [Nm]",
20
+ "Tool wear [min]"
21
+ ]
22
+
23
+ def predict(UDI, air_temp, process_temp, rotational_speed, torque, tool_wear):
24
+ # Prepare input data
25
+ input_data = pd.DataFrame([[UDI, air_temp, process_temp, rotational_speed, torque, tool_wear]],
26
+ columns=input_features)
27
+
28
+ # Handle categorical features (if any) - Assuming 'UDI' might be categorical
29
+ non_numeric_columns = input_data.select_dtypes(exclude=['number']).columns
30
+ if len(non_numeric_columns) > 0:
31
+ input_data_encoded = encoder.transform(input_data[non_numeric_columns])
32
+ input_data_encoded = pd.DataFrame(input_data_encoded, index=input_data.index)
33
+ input_data = pd.concat([input_data.drop(non_numeric_columns, axis=1), input_data_encoded], axis=1)
34
+
35
+ # Scale the features
36
+ input_data = scaler.transform(input_data)
37
+
38
+ # Predict using the loaded model
39
+ prediction = model.predict(input_data)
40
+ return "Failure" if prediction[0] == 1 else "No Failure"
41
+
42
+ # Create the Gradio interface
43
+ interface = gr.Interface(
44
+ fn=predict,
45
+ inputs=[
46
+ gr.inputs.Number(label="UDI"),
47
+ gr.inputs.Number(label="Air temperature [K]"),
48
+ gr.inputs.Number(label="Process temperature [K]"),
49
+ gr.inputs.Number(label="Rotational speed [rpm]"),
50
+ gr.inputs.Number(label="Torque [Nm]"),
51
+ gr.inputs.Number(label="Tool wear [min]")
52
+ ],
53
+ outputs=gr.outputs.Textbox(label="Prediction"),
54
+ title="Predictive Maintenance Model",
55
+ description="Enter the values for the features to get the model's prediction."
56
+ )
57
+
58
+ # Launch the app
59
+ interface.launch()