Aditya Das Gupta commited on
Commit
098c97f
·
verified ·
1 Parent(s): 958fa4c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """app.py
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1lNcY0pIAsfXb4cwcI-pgasdUnKuzMqiL
8
+ """
9
+
10
+ import gradio as gr
11
+ import pandas as pd
12
+ import joblib
13
+
14
+ # --- Step 1: Load the Pre-Trained Model and Scaler ---
15
+ # These files must be in the same repository on Hugging Face Spaces.
16
+ model = joblib.load('lgbm_model.pkl')
17
+ scaler = joblib.load('scaler.pkl')
18
+
19
+ # --- Step 2: Create a Prediction Function ---
20
+ def predict_failure(air_temp, process_temp, rotational_speed, torque, tool_wear, type_l, type_m):
21
+ # Create a dictionary from the inputs
22
+ input_data = {
23
+ 'Air temperature [K]': air_temp,
24
+ 'Process temperature [K]': process_temp,
25
+ 'Rotational speed [rpm]': rotational_speed,
26
+ 'Torque [Nm]': torque,
27
+ 'Tool wear [min]': tool_wear,
28
+ 'Type_L': 1 if type_l else 0,
29
+ 'Type_M': 1 if type_m else 0
30
+ }
31
+
32
+ # Create a DataFrame from the dictionary
33
+ input_df = pd.DataFrame([input_data])
34
+
35
+ # --- Feature Engineering (must be identical to training) ---
36
+ input_df['TempDiff_K'] = input_df['Process temperature [K]'] - input_df['Air temperature [K]']
37
+ input_df['Power_W'] = input_df['Torque [Nm]'] * input_df['Rotational speed [rpm]'] * (2 * 3.14159 / 60)
38
+ input_df['ToolWear_Torque'] = input_df['Tool wear [min]'] * input_df['Torque [Nm]']
39
+
40
+ # --- Scaling (using the loaded scaler) ---
41
+ # Ensure columns are in the correct order before scaling
42
+ final_cols = scaler.get_feature_names_out()
43
+ input_df_reordered = input_df[final_cols]
44
+ input_scaled = scaler.transform(input_df_reordered)
45
+
46
+ # --- Prediction ---
47
+ prediction_proba = model.predict_proba(input_scaled)
48
+ failure_probability = prediction_proba[0][1]
49
+
50
+ # Return the probability in a nice format
51
+ return {
52
+ "No Failure Probability": 1 - failure_probability,
53
+ "Failure Probability": failure_probability
54
+ }
55
+
56
+ # --- Step 3: Create the Gradio Interface ---
57
+ inputs = [
58
+ gr.Slider(295, 305, step=0.1, label="Air temperature [K]"),
59
+ gr.Slider(305, 315, step=0.1, label="Process temperature [K]"),
60
+ gr.Slider(1100, 2900, step=1, label="Rotational speed [rpm]"),
61
+ gr.Slider(3, 80, step=0.1, label="Torque [Nm]"),
62
+ gr.Slider(0, 260, step=1, label="Tool wear [min]"),
63
+ gr.Checkbox(label="Type L (Low quality variant)"),
64
+ gr.Checkbox(label="Type M (Medium quality variant)")
65
+ ]
66
+
67
+ outputs = gr.Label(num_top_classes=2, label="Prediction")
68
+
69
+ # --- Step 4: Launch the Interface ---
70
+ interface = gr.Interface(
71
+ fn=predict_failure,
72
+ inputs=inputs,
73
+ outputs=outputs,
74
+ title="Predictive Maintenance: Machine Failure Predictor",
75
+ description="Enter the machine's sensor readings to predict the probability of failure. Based on a LightGBM model trained on the AI4I 2020 dataset."
76
+ )
77
+
78
+ interface.launch() # No share=True needed on Hugging Face