Spaces:
No application file
No application file
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import pickle
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import shap
|
| 5 |
+
from shap.plots._force_matplotlib import draw_additive_plot
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import numpy as np
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
|
| 10 |
+
# load the model from disk
|
| 11 |
+
loaded_model = pickle.load(open("h22_xgb.pkl", 'rb'))
|
| 12 |
+
|
| 13 |
+
# Setup SHAP
|
| 14 |
+
explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS.
|
| 15 |
+
|
| 16 |
+
# Create the main function for server
|
| 17 |
+
def main_func(ValueDiversity,AdequateResources,Voice,GrowthAdvancement,Workload,WorkLifeBalance):
|
| 18 |
+
new_row = pd.DataFrame.from_dict({'ValueDiversity':ValueDiversity,'AdequateResources':AdequateResources,
|
| 19 |
+
'Voice':Voice,'GrowthAdvancement':GrowthAdvancement,'Workload':Workload,
|
| 20 |
+
'WorkLifeBalance':WorkLifeBalance}, orient = 'index').transpose()
|
| 21 |
+
|
| 22 |
+
prob = loaded_model.predict_proba(new_row)
|
| 23 |
+
|
| 24 |
+
shap_values = explainer(new_row)
|
| 25 |
+
# plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
|
| 26 |
+
# plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
|
| 27 |
+
plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
|
| 28 |
+
|
| 29 |
+
plt.tight_layout()
|
| 30 |
+
local_plot = plt.gcf()
|
| 31 |
+
plt.rcParams['figure.figsize'] = 6,4
|
| 32 |
+
plt.close()
|
| 33 |
+
|
| 34 |
+
return {"Leave": float(prob[0][0]), "Stay": 1-float(prob[0][0])}, local_plot
|
| 35 |
+
|
| 36 |
+
# Create the UI
|
| 37 |
+
title = "**Employee Turnover Predictor & Interpreter** 🪐"
|
| 38 |
+
description1 = """
|
| 39 |
+
This app takes six inputs about employees' satisfaction with different aspects of their work (such as work-life balance, ...) and predicts whether the employee intends to stay with the employer or leave. There are two outputs from the app: 1- the predicted probability of stay or leave, 2- Shapley's force-plot which visualizes the extent to which each factor impacts the stay/ leave prediction.
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
description2 = """
|
| 43 |
+
To use the app, click on one of the examples, or adjust the values of the six employee satisfaction factors, and click on Analyze. ✨
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
with gr.Blocks(title=title) as demo:
|
| 47 |
+
gr.Markdown(f"## {title}")
|
| 48 |
+
# gr.Markdown("""""")
|
| 49 |
+
gr.Markdown(description1)
|
| 50 |
+
gr.Markdown("""---""")
|
| 51 |
+
gr.Markdown(description2)
|
| 52 |
+
gr.Markdown("""---""")
|
| 53 |
+
with gr.Row():
|
| 54 |
+
with gr.Column():
|
| 55 |
+
ValueDiversity = gr.Slider(label="ValueDiversity Score", minimum=1, maximum=5, value=4, step=.1)
|
| 56 |
+
AdequateResources = gr.Slider(label="AdequateResources Score", minimum=1, maximum=5, value=4, step=.1)
|
| 57 |
+
Voice = gr.Slider(label="Voice Score", minimum=1, maximum=5, value=4, step=.1)
|
| 58 |
+
GrowthAdvancement = gr.Slider(label="GrowthAdvancement Score", minimum=1, maximum=5, value=4, step=.1)
|
| 59 |
+
Workload = gr.Slider(label="Workload Score", minimum=1, maximum=5, value=4, step=.1)
|
| 60 |
+
WorkLifeBalance = gr.Slider(label="WorkLifeBalance Score", minimum=1, maximum=5, value=4, step=.1)
|
| 61 |
+
submit_btn = gr.Button("Analyze")
|
| 62 |
+
with gr.Column(visible=True,scale=1, min_width=600) as output_col:
|
| 63 |
+
label = gr.Label(label = "Predicted Label")
|
| 64 |
+
local_plot = gr.Plot(label = 'Shap:')
|
| 65 |
+
|
| 66 |
+
submit_btn.click(
|
| 67 |
+
main_func,
|
| 68 |
+
[ValueDiversity,AdequateResources,Voice,GrowthAdvancement,Workload,WorkLifeBalance],
|
| 69 |
+
[label,local_plot], api_name="Employee_Turnover"
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
gr.Markdown("### Click on any of the examples below to see how it works:")
|
| 73 |
+
gr.Examples([[4,4,4,4,5,5], [5,4,5,4,4,4]],
|
| 74 |
+
[ValueDiversity,AdequateResources,Voice,GrowthAdvancement,Workload,WorkLifeBalance],
|
| 75 |
+
[label,local_plot], main_func, cache_examples=True)
|
| 76 |
+
|
| 77 |
+
demo.launch()
|