Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,18 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import pickle
|
|
|
|
| 4 |
|
| 5 |
-
# Load the saved model
|
| 6 |
with open('logistic_regression_model.pkl', 'rb') as model_file:
|
| 7 |
model = pickle.load(model_file)
|
| 8 |
|
| 9 |
with open('feature_columns.pkl', 'rb') as col_file:
|
| 10 |
feature_columns = pickle.load(col_file)
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
def predict_status(UDI, air_temp, proc_temp, speed, torque, tool_wear):
|
| 13 |
# Prepare the input data
|
| 14 |
sample_input = {
|
|
@@ -36,10 +40,19 @@ def predict_status(UDI, air_temp, proc_temp, speed, torque, tool_wear):
|
|
| 36 |
|
| 37 |
# Make a prediction
|
| 38 |
prediction = model.predict(sample_df)[0]
|
| 39 |
-
|
| 40 |
-
#
|
|
|
|
|
|
|
|
|
|
| 41 |
if prediction == 0:
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
else:
|
| 44 |
return "🟢 Machine is working."
|
| 45 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import pickle
|
| 4 |
+
import shap
|
| 5 |
|
| 6 |
+
# Load the saved model, feature columns, and SHAP explainer
|
| 7 |
with open('logistic_regression_model.pkl', 'rb') as model_file:
|
| 8 |
model = pickle.load(model_file)
|
| 9 |
|
| 10 |
with open('feature_columns.pkl', 'rb') as col_file:
|
| 11 |
feature_columns = pickle.load(col_file)
|
| 12 |
|
| 13 |
+
# Create a SHAP explainer
|
| 14 |
+
explainer = shap.Explainer(model)
|
| 15 |
+
|
| 16 |
def predict_status(UDI, air_temp, proc_temp, speed, torque, tool_wear):
|
| 17 |
# Prepare the input data
|
| 18 |
sample_input = {
|
|
|
|
| 40 |
|
| 41 |
# Make a prediction
|
| 42 |
prediction = model.predict(sample_df)[0]
|
| 43 |
+
|
| 44 |
+
# Get SHAP values
|
| 45 |
+
shap_values = explainer(sample_df)
|
| 46 |
+
|
| 47 |
+
# Determine the most impactful features if prediction is 0 (machine is not working)
|
| 48 |
if prediction == 0:
|
| 49 |
+
explanation = shap_values[0].values
|
| 50 |
+
features = list(sample_df.columns)
|
| 51 |
+
feature_importance = sorted(zip(features, explanation), key=lambda x: abs(x[1]), reverse=True)
|
| 52 |
+
reason = "Machine is NOT working due to the following features:\n"
|
| 53 |
+
for feature, importance in feature_importance:
|
| 54 |
+
reason += f"{feature}: {'High' if importance > 0 else 'Low'} impact\n"
|
| 55 |
+
return reason
|
| 56 |
else:
|
| 57 |
return "🟢 Machine is working."
|
| 58 |
|