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