Spaces:
Runtime error
Runtime error
paragon-analytics
commited on
Commit
•
0c0e111
1
Parent(s):
b892b04
Upload 2 files
Browse files- app.py +81 -0
- requirements-2.txt +10 -0
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("xgb_h.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(Generation,Tenure,Engagement, LearningDevelopment,WorkEnvironment, RewardsBenefits, EmployeeWellBeing):
|
17 |
+
new_row = pd.DataFrame.from_dict({'Generation': Generation,
|
18 |
+
'Tenure':Tenure,
|
19 |
+
'Engagement':Engagement,
|
20 |
+
'LearningDevelopment':LearningDevelopment,
|
21 |
+
'WorkEnvironment':WorkEnvironment,
|
22 |
+
'RewardsBenefits':RewardsBenefits,
|
23 |
+
'EmployeeWellBeing':EmployeeWellBeing}, orient = 'index').transpose()
|
24 |
+
|
25 |
+
prob = loaded_model.predict_proba(new_row)
|
26 |
+
|
27 |
+
shap_values = explainer(new_row)
|
28 |
+
# plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
|
29 |
+
# plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
|
30 |
+
plot = shap.plots.bar(shap_values[0], max_display=7, order=shap.Explanation.abs, show_data='auto', show=False)
|
31 |
+
|
32 |
+
plt.tight_layout()
|
33 |
+
local_plot = plt.gcf()
|
34 |
+
plt.rcParams['figure.figsize'] = 7,4
|
35 |
+
plt.close()
|
36 |
+
|
37 |
+
return {"Leave": float(prob[0][0]), "Stay": 1-float(prob[0][0])}, local_plot
|
38 |
+
|
39 |
+
# Create the UI
|
40 |
+
title = "**Employee Turnover Predictor & Interpreter** 🪐"
|
41 |
+
description1 = """
|
42 |
+
This app takes five inputs about employees' satisfaction with different aspects of their work and two demographic inputs then 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.
|
43 |
+
"""
|
44 |
+
|
45 |
+
description2 = """
|
46 |
+
To use the app, click on one of the examples, or adjust the values of the seven employee satisfaction factors, and click on Analyze. ✨
|
47 |
+
"""
|
48 |
+
|
49 |
+
with gr.Blocks(title=title) as demo:
|
50 |
+
gr.Markdown(f"## {title}")
|
51 |
+
# gr.Markdown("""![marketing](types-of-employee-turnover.jpg)""")
|
52 |
+
gr.Markdown(description1)
|
53 |
+
gr.Markdown("""---""")
|
54 |
+
gr.Markdown(description2)
|
55 |
+
gr.Markdown("""---""")
|
56 |
+
with gr.Row():
|
57 |
+
with gr.Column():
|
58 |
+
Generation = gr.Slider(label="Generation", minimum=1, maximum=6, value=4, step=1)
|
59 |
+
Tenure = gr.Slider(label="Tenure Level", minimum=1, maximum=8, value=4, step=1)
|
60 |
+
Engagement = gr.Slider(label="Engagement Score", minimum=1, maximum=5, value=4, step=.1)
|
61 |
+
LearningDevelopment = gr.Slider(label="LearningDevelopment Score", minimum=1, maximum=5, value=4, step=.1)
|
62 |
+
WorkEnvironment = gr.Slider(label="WorkEnvironment Score", minimum=1, maximum=5, value=4, step=.1)
|
63 |
+
RewardsBenefits = gr.Slider(label="RewardsBenefits Score", minimum=1, maximum=5, value=4, step=.1)
|
64 |
+
EmployeeWellBeing = gr.Slider(label="EmployeeWellBeing Score", minimum=1, maximum=5, value=4, step=.1)
|
65 |
+
submit_btn = gr.Button("Analyze")
|
66 |
+
with gr.Column(visible=True,scale=1, min_width=600) as output_col:
|
67 |
+
label = gr.Label(label = "Predicted Label")
|
68 |
+
local_plot = gr.Plot(label = 'Shap:')
|
69 |
+
|
70 |
+
submit_btn.click(
|
71 |
+
main_func,
|
72 |
+
[Generation, Tenure,Engagement,LearningDevelopment,WorkEnvironment,RewardsBenefits,EmployeeWellBeing],
|
73 |
+
[label,local_plot], api_name="Employee_Turnover"
|
74 |
+
)
|
75 |
+
|
76 |
+
gr.Markdown("### Click on any of the examples below to see how it works:")
|
77 |
+
gr.Examples([[3,4,4,4,4,5,5], [4,5,4,5,4,4,5]],
|
78 |
+
[Generation,Tenure,Engagement,LearningDevelopment,WorkEnvironment,RewardsBenefits,EmployeeWellBeing],
|
79 |
+
[label,local_plot], main_func, cache_examples=True)
|
80 |
+
|
81 |
+
demo.launch()
|
requirements-2.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.44.1
|
2 |
+
Pillow
|
3 |
+
yake
|
4 |
+
pandas
|
5 |
+
scikit-learn
|
6 |
+
shap
|
7 |
+
xgboost
|
8 |
+
matplotlib
|
9 |
+
numpy
|
10 |
+
streamlit
|