File size: 3,916 Bytes
73c01cd
b44b932
d863f1f
287d1e9
 
 
 
d863f1f
287d1e9
b44b932
287d1e9
b44b932
 
73c01cd
287d1e9
 
73c01cd
 
b44b932
91be5e6
b44b932
 
 
 
 
73c01cd
 
 
d863f1f
b44b932
73c01cd
b44b932
 
 
d863f1f
287d1e9
73c01cd
d863f1f
73c01cd
d863f1f
 
73c01cd
 
 
 
287d1e9
 
 
d863f1f
ad92db5
287d1e9
b44b932
 
91be5e6
 
b44b932
 
287d1e9
b44b932
287d1e9
c0424d8
73c01cd
 
b44b932
287d1e9
 
b44b932
287d1e9
 
 
 
 
73c01cd
 
287d1e9
b44b932
73c01cd
d863f1f
 
 
 
 
 
73c01cd
b44b932
73c01cd
1a541cc
 
73c01cd
 
287d1e9
 
73c01cd
 
287d1e9
ba916aa
b44b932
 
91be5e6
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#This is the basic app with generations 5 & 6 and middle management
#This is also pre-filtered for chain scale 4: upper upscale

import pickle
import pandas as pd
import shap
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
import gradio.themes as gt #Here I imported a pre-set gradio theme. The one that I used is called "soft"


# This loads the model in which the data is trained on
loaded_model = pickle.load(open("h47_xgb.pkl", 'rb'))

# Setup SHAP
explainer = shap.Explainer(loaded_model)

#Creating employee profiles for Mr.Bean and Tom Hanks
employee_selection = {
    "Mr.Bean - At Risk/Medium Risk πŸŸ₯⚠️": [4.2, 3.6, 3.4, 3.5, 3.7, 3.9],
    "Tom Hanks - Happy 🟒": [5.0, 4.8, 4.7, 4.8, 4.9, 4.9],
    "Default": [3, 3, 3, 3, 3, 3]
}

# Create the main function
def main_func(Engage2, Voice, Merit, Workload, WellBeing, SupportiveGM,
              ChainScale=4, ManagementLevel=2):
    new_row = pd.DataFrame.from_dict({
        'ManagementLevel': ManagementLevel, 'Engage2': Engage2, 'Voice': Voice,
        'Merit': Merit,
        'Workload': Workload, 'ChainScale': ChainScale, 'WellBeing': WellBeing,
        'SupportiveGM': SupportiveGM
    }, orient='index').transpose()

    prob = loaded_model.predict_proba(new_row)

    shap_values = explainer(new_row)
    selected_features = ["Engage2", "Voice", "Merit", "Workload", "WellBeing", "SupportiveGM"]
    shap_values_filtered = shap_values[:, selected_features]

    # Generate SHAP bar plot
    plt.figure(figsize=(6, 4))
    shap.plots.bar(shap_values[0], max_display=6, show=False)

    plt.tight_layout()
    local_plot = plt.gcf()
    plt.close()

    return {"Leave ❌": float(prob[0][0]), "Stay βœ… ": 1 - float(prob[0][0])}, local_plot

# Updates the sliders so that they show the values of each of the profiles
def update_sliders(profile):
    if profile in employee_selection:
        return employee_selection [profile]
    return [3,3,3,3,3,3]

# Create the UI
title = "Hilton Employee Turnover Predictor & Interpreter 🏨"
description1 = """
This app predicts whether a Millennial/Generation Z employee in upper upscale hotels will stay or leave based on the top six important variables impacting intent to stay.
"""
description2 = """
Choose from the pre-set employee categories, or adjust the values to identify who will stay βœ… or leave ❌!
"""

with gr.Blocks(theme = gt.Soft()) as demo:
    gr.Markdown(f"## {title}")
    gr.Markdown(description1)
    gr.Markdown("""---""")
    gr.Markdown(description2)
    gr.Markdown("""---""")

    with gr.Row():
        with gr.Column():
            profile_dropdown = gr.Dropdown(choices=["Default", "Tom Hanks - Happy 🟒", "Mr.Bean - At Risk/Medium Risk πŸŸ₯⚠️"], label="Select a profile to learn more!")

            Engage2 = gr.Slider(label="Engagement (Engage2)", minimum=1, maximum=5, value=4, step=0.1)
            Voice = gr.Slider(label="Voice", minimum=1, maximum=5, value=4, step=0.1)
            Merit = gr.Slider(label="Merit", minimum=1, maximum=5, value=4, step=0.1)
            Workload = gr.Slider(label="Workload", minimum=1, maximum=5, value=4, step=0.1)
            WellBeing = gr.Slider(label="Well-being", minimum=1, maximum=5, value=4, step=0.1)
            SupportiveGM = gr.Slider(label="Supportive GM", minimum=1, maximum=5, value=4, step=0.1)

            submit_btn = gr.Button("Predict πŸ”")

        with gr.Column(visible=True, scale=1, min_width=600) as output_col:
            label = gr.Label(label="Predicted Label")
            local_plot = gr.Plot(label="SHAP Analysis")

            submit_btn.click(
                main_func,
                [Engage2, Voice, Merit, Workload, WellBeing, SupportiveGM],
                [label, local_plot]
            )

    profile_dropdown.change(update_sliders, inputs=[profile_dropdown], outputs=[Engage2, Voice, Merit, Workload, WellBeing, SupportiveGM])

demo.launch()