jack-donlan commited on
Commit
2650ea5
·
verified ·
1 Parent(s): fa84578

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -18
app.py CHANGED
@@ -7,23 +7,24 @@ import numpy as np
7
  import matplotlib.pyplot as plt
8
 
9
  #load the model from disk
10
- loaded_model = pickle.load(open("h25_xgb_vanilla.pkl", 'rb'))
11
 
12
  #Setup SHAP
13
  explainer = shap.Explainer(loaded_model) #DO NOT CHANGE PER INSTRUCTIONS
14
 
15
  #Main Function for Server
16
- def main_func(Engagement,WorkLifeBalance,Voice,Workload,LearningDevelopment,Merit):
17
- new_row = pd.DataFrame.from_dict({'Voice':Voice, 'Merit':Merit, 'Workload':Workload,
18
- 'LearningDevelopment':LearningDevelopnent, 'WorkLifeBalance':WorkLifeBalance,
19
- 'Engagement': Engagement}, orient = 'index').transpose()
 
20
 
21
  prob = loaded_model.predict_proba(new_row)
22
 
23
  shap_values = explainer(new_row)
24
  # plot = shap.force(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()
@@ -35,11 +36,11 @@ def main_func(Engagement,WorkLifeBalance,Voice,Workload,LearningDevelopment,Meri
35
  #Create UI
36
  title = "**Employee Turnover Predictor & Intrepreter**"
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 Analyze.
43
  """
44
 
45
  with gr.Blocks(title = title) as demo:
@@ -50,12 +51,13 @@ with gr.Blocks(title = title) as demo:
50
  gr.Markdown("""---""")
51
  with gr.Row():
52
  with gr.Column():
53
- Voice = gr.Slider(label= "Voice", minimum = 1, maximum = 5, value = 4, step = .1)
54
- Merit = gr.Slider(label= "Merit Score", minimum = 1, maximum = 5, value = 4, step = .1)
55
- Workload = gr.Slider(label= "Workload Score", minimum = 1, maximum = 5, value = 4, step = .1)
56
- LearningDevelopment = gr.Slider(label= "LearningDevelopment Score", minimum = 1, maximum = 5, value = 4, step = .1)
57
- WorkLifeBalance = gr.Slider(label= "WorkLifeBalance Score", minimum = 1, maximum = 5, value = 4, step = .1)
58
- Engagement = gr.Slider(label= "Engagement Score", minimum = 1, maximum = 5, value = 4, step = .1)
 
59
  submit_btn = gr.Button("Analyze")
60
  with gr.Column(visible=True,scale=1, min_width=600) as output_col:
61
  label = gr.Label(label = "Predicted Label")
@@ -63,13 +65,13 @@ with gr.Blocks(title = title) as demo:
63
 
64
  submit_btn.click(
65
  main_func,
66
- [Voice,Merit,Workload,LearningDevelopment,WorkLifeBalance,Engagement],
67
  [label,local_plot], api_name="Employee_Turnover"
68
  )
69
 
70
  gr.Markdown('### Click on any of the examples below to see how it works:')
71
- gr.Examples([[4,4,4,4,5,5], [5,4,5,4,4,4]],
72
- [Voice,Merit,Workload,LearningDevelopment,WorkLifeBalance,Engagement],
73
  [label,local_plot], main_func, cache_examples=True)
74
 
75
  demo.launch()
 
7
  import matplotlib.pyplot as plt
8
 
9
  #load the model from disk
10
+ loaded_model = pickle.load(open("h25_xgb_hyperopt.pkl", 'rb'))
11
 
12
  #Setup SHAP
13
  explainer = shap.Explainer(loaded_model) #DO NOT CHANGE PER INSTRUCTIONS
14
 
15
  #Main Function for Server
16
+ def main_func(PassionateAtWork,Workload,SupportiveGM,WorkEnvironment,Informed,LearningDevelopment,JobSecurity):
17
+ new_row = pd.DataFrame.from_dict({'PassionateAtWork':PassionateAtWork, 'Workload':Workload,
18
+ 'SupportiveGM':SupportiveGM,'WorkEnvironment':WorkEnvironment, 'Informed':Informed,
19
+ 'LearningDevelopment': LearningDevelopment, 'JobSecurity':JobSecurity},
20
+ orient = 'index').transpose()
21
 
22
  prob = loaded_model.predict_proba(new_row)
23
 
24
  shap_values = explainer(new_row)
25
  # plot = shap.force(shap_values[0], matplotlib=True, figsize(30,30), show = False)
26
+ # plot = shap.plots.waterfall(shap_values[0], max_display = 7, show = False)
27
+ plot = shap.plots.bar(shap_values[0], max_display = 7, order=shap.Explanation.abs, show_data = 'auto', show = False)
28
 
29
  plt.tight_layout()
30
  local_plot = plt.gcf()
 
36
  #Create UI
37
  title = "**Employee Turnover Predictor & Intrepreter**"
38
  description1 = """
39
+ This app takes seven inputs about employees' satisfaction with different aspects of their work (such as Passionate at Work, ...) 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 seven employee satisfaction factors, and click Analyze.
44
  """
45
 
46
  with gr.Blocks(title = title) as demo:
 
51
  gr.Markdown("""---""")
52
  with gr.Row():
53
  with gr.Column():
54
+ PassionateAtWork = gr.Slider(label= "Passionate At Work", minimum = 1, maximum = 5, value = 4, step = .1)
55
+ Workload = gr.Slider(label= "Workload", minimum = 1, maximum = 5, value = 4, step = .1)
56
+ SupportiveGM = gr.Slider(label= "Supportive GM", minimum = 1, maximum = 5, value = 4, step = .1)
57
+ WorkEnvironment = gr.Slider(label= "Work Environment", minimum = 1, maximum = 5, value = 4, step = .1)
58
+ Informed = gr.Slider(label= "Informed", minimum = 1, maximum = 5, value = 4, step = .1)
59
+ LearningDevelopment = gr.Slider(label= "Learning Development", minimum = 1, maximum = 5, value = 4, step = .1)
60
+ JobSecurity = gr.Slider(label= "Job Security", 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")
 
65
 
66
  submit_btn.click(
67
  main_func,
68
+ [PassionateAtWork,Workload,SupportiveGM,WorkEnvironment,Informed,LearningDevelopment,JobSecurity],
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,4], [5,4,5,4,4,4,5]],
74
+ [PassionateAtWork,Workload,SupportiveGM,WorkEnvironment,Informed,LearningDevelopment,JobSecurity],
75
  [label,local_plot], main_func, cache_examples=True)
76
 
77
  demo.launch()