Spaces:
Sleeping
Sleeping
File size: 3,506 Bytes
8f627dd 4bad5a8 8f627dd a1d84e4 f94e3fc a1d84e4 8f627dd 83e82c8 8f627dd a1d84e4 7765e5a a1d84e4 8f627dd a1d84e4 83e82c8 a1d84e4 90a5355 a1d84e4 90a5355 a1d84e4 90a5355 a1d84e4 8e6700d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import pickle
import pandas as pd
import shap
from shap.plots._force_matplotlib import draw_additive_plot
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
# load the model from disk
loaded_model = pickle.load(open("filtered_xgb_model.pkl", 'rb'))
# Setup SHAP
explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS.
example_options = {
"๐ข Well Engaged": [4.9, 5, 5, 4.9, 5, 5],
"๐ก Marginal": [5, 4.6, 5, 5, 5, 4.7],
"๐ด At Risk": [4.5, 4.7, 4.8, 4.5, 4.7, 4.5]
}
# Function to apply the example values
def fill_example(example_label):
return example_options[example_label]
# Create the main function for server
def main_func(GM3, WorkEnv3, WellBeing2, GM2, JobSecurity, WellBeing1):
new_row = pd.DataFrame.from_dict({
'GM3': GM3,
'WorkEnv3': WorkEnv3,
'WellBeing2': WellBeing2,
'GM2': GM2,
'JobSecurity': JobSecurity,
'WellBeing1': WellBeing1
}, orient='index').transpose()
prob = loaded_model.predict_proba(new_row)
shap_values = explainer(new_row)
# plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
# plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
plt.tight_layout()
local_plot = plt.gcf()
plt.rcParams['figure.figsize'] = 6,4
plt.close()
return {"Leave": float(prob[0][0]), "Stay": 1-float(prob[0][0])}, local_plot
with gr.Blocks(title="๐ Intent to Stay Prediction") as demo:
gr.Image("https://1000logos.net/wp-content/uploads/2017/02/Font-Hilton-Logo.jpg", elem_id="banner")
gr.Markdown("# ๐ Employee Retention Predictor")
gr.Markdown("Predict if an employee will **Stay** or **Leave** based on key workplace factors.")
gr.Markdown("---")
with gr.Row():
with gr.Column():
GM3 = gr.Slider(label="๐จโ๐ผ My General Manager is an effective leader", minimum=1, maximum=5, value=4, step=0.1)
WorkEnv3 = gr.Slider(label="๐ข My Work Environment is comfortable and welcoming", minimum=1, maximum=5, value=4, step=0.1)
WellBeing2 = gr.Slider(label="๐ I feel balanced and healthy", minimum=1, maximum=5, value=4, step=0.1)
GM2 = gr.Slider(label="๐ My General Manager uses feedback from Team Members", minimum=1, maximum=5, value=4, step=0.1)
JobSecurity = gr.Slider(label="๐ Job Security", minimum=1, maximum=5, value=4, step=0.1)
WellBeing1 = gr.Slider(label="๐ง My mental health is good", minimum=1, maximum=5, value=4, step=0.1)
submit_btn = gr.Button("๐ Analyze Now", variant="primary")
with gr.Column():
label = gr.Label(label="๐ฎ Prediction Result")
local_plot = gr.Plot(label="SHAP Analysis")
# Dropdown for labeled examples
gr.Markdown("### ๐ท๏ธ Select an Example:")
example_dropdown = gr.Dropdown(
label="Choose a scenario",
choices=list(example_options.keys())
)
# Apply example values when selected
example_dropdown.change(
fill_example,
inputs=[example_dropdown],
outputs=[GM3, WorkEnv3, WellBeing2, GM2, JobSecurity, WellBeing1]
)
# Submit button functionality
submit_btn.click(main_func, [GM3, WorkEnv3, WellBeing2, GM2, JobSecurity, WellBeing1], [label, local_plot])
demo.launch() |