File size: 6,175 Bytes
40f8030
008cbc7
 
bedc31f
d7039db
008cbc7
bedc31f
b39af27
5fcb1c0
313ffa3
d640ea6
f7ae4d3
d7039db
9c8d7b2
 
 
 
 
 
 
ccb396a
b39af27
 
4036c1c
b39af27
9c8d7b2
 
 
 
 
 
ccb396a
b39af27
9c8d7b2
 
 
 
 
 
be405fd
4036c1c
b39af27
9c8d7b2
 
 
b39af27
9c8d7b2
 
4036c1c
9c8d7b2
 
 
 
 
b39af27
9c8d7b2
 
 
ccb396a
9c8d7b2
cae59a3
 
 
 
 
 
 
313ffa3
ccb396a
055398c
 
9c8d7b2
d640ea6
 
 
 
 
008cbc7
 
 
9c8d7b2
cae59a3
008cbc7
 
9c8d7b2
008cbc7
a482377
 
40f8030
942efe4
 
40f8030
942efe4
 
a482377
008cbc7
 
 
b39af27
 
008cbc7
 
 
 
 
 
 
9c8d7b2
 
008cbc7
9c8d7b2
b39af27
553dc41
9c8d7b2
 
 
b39af27
553dc41
9c8d7b2
 
b39af27
9c8d7b2
 
 
b39af27
9c8d7b2
 
 
b39af27
9c8d7b2
 
 
 
 
 
 
b39af27
9c8d7b2
 
 
 
 
 
 
008cbc7
 
 
2be2025
 
008cbc7
9c8d7b2
 
 
 
 
 
d7039db
9c8d7b2
b39af27
9c8d7b2
 
4e440e2
be405fd
 
9c8d7b2
 
 
 
 
 
be405fd
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import pickle
import pandas as pd
import shap
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt

# Load the XGBoost model from Pickle
loaded_model = pickle.load(open("h22_xgb_Final(2).pkl", 'rb'))

# Setup SHAP Explainer for XGBoost (Do not change this)
explainer = shap.Explainer(loaded_model)

def safe_convert(value, default, min_val, max_val):
    try:
        num = float(value)
        return max(min_val, min(num, max_val))  # Ensure within range
    except (TypeError, ValueError):
        return default  # Use default if conversion fails

# Create the main function for the model
def main_func(Department, ChainScale, SupportiveGM, Merit, LearningDevelopment, WorkEnvironment, Engagement, WellBeing):
    
    
    ChainScale_mapping = {
        'Luxury': 1,
        'Upper Midscale': 2,
        'Upper Upscale': 3,
        'Upscale': 4,
        'Independent': 5,
    }
    
    department_mapping = {
        "Guest Services": 1,
        "Food and Beverage": 2,
        "Housekeeping": 3,
        "Front Office Operations": 4,
        "Guest Activities": 5,
    }

    
    LearningDevelopment = safe_convert(LearningDevelopment, 3.0, 1, 5)
    SupportiveGM = safe_convert(SupportiveGM, 3.0, 1, 5)
    Merit = safe_convert(Merit, 3.0, 1, 5)
    WorkEnvironment = safe_convert(WorkEnvironment, 3.0, 1, 5)
    Engagement = safe_convert(Engagement, 3.0, 1, 5)
    WellBeing = safe_convert(WellBeing, 3.0, 1, 5)

    
    new_row = pd.DataFrame({
        'SupportiveGM': [SupportiveGM],
        'Merit': [Merit],
        'LearningDevelopment': [LearningDevelopment],
        'WorkEnvironment': [WorkEnvironment],
        'Engagement': [Engagement],
        'WellBeing': [WellBeing]
    }).astype(float)

    # Predict probabilities
    prob = loaded_model.predict_proba(new_row) 
    
    if prob.shape[1] == 2:
        leave_prob = float(prob[0][0])
        stay_prob = float(prob[0][1])
    else:
        leave_prob = float(prob[0])
        stay_prob = 1 - leave_prob

    # Generate SHAP values
    shap_values = explainer(new_row)

    fig, ax = plt.subplots(figsize=(8, 4))
    shap.waterfall_plot(shap.Explanation(
        values=shap_values.values[0],
        base_values=shap_values.base_values[0],
        data=new_row.iloc[0]
    ))
    plt.tight_layout()
    local_plot = plt.gcf()
    plt.close()

    return {"Leave": leave_prob, "Stay": stay_prob}, local_plot

# Create the UI
title = "**Mod 3 Team 5: Employee Turnover Predictor & Interpreter**"
description1 = """
This app evaluates six key factors affecting employee satisfaction—Supportive GM, Merit, Learning & Development, Work Environment, Engagement, and Well-Being—to predict whether an employee is likely to stay with Hilton or leave.
The app provides two key outputs:

**Predicted Probability**  
A likelihood score indicating whether an employee will stay or leave.

**SHAP Force Plot**  
A dynamic visualization that illustrates how each factor influences the prediction, helping to pinpoint the most impactful drivers of employee retention.
Designed for HR teams at both departmental and hotel chain levels, this tool delivers data-driven insights to improve employee experience and retention strategies across Hilton properties.
"""

description2 = """
To use the app, adjust the values of the employee satisfaction factors and click on Analyze.
"""

with gr.Blocks(title=title) as demo:
    gr.Markdown(f"## {title}")
    gr.Markdown(description1)
    gr.Markdown("""---""")
    gr.Markdown(description2)
    gr.Markdown("""---""")
    
    with gr.Row():      
        with gr.Column():
            Department = gr.Radio(
                ["Guest Services", "Food and Beverage", "Housekeeping", "Front Office Operations", "Guest Activities"],
                label="Department", 
                value="Guest Services"
            )
            ChainScale = gr.Dropdown(
                ["Luxury", "Upper Midscale", "Upper Upscale", "Upscale", "Independent"],
                label="ChainScale (Example)", 
                value="Upper Upscale"
            )
            SupportiveGM = gr.Slider(
                label="SupportiveGM Score", minimum=1, maximum=5, value=4, step=0.1,
                interactive=True
            )
            Merit = gr.Slider(
                label="Merit Score", minimum=1, maximum=5, value=4, step=0.1,
                interactive=True
            )
            LearningDevelopment = gr.Slider(
                label="Learning and Development Score", minimum=1, maximum=5, value=4, step=0.1,
                interactive=True
            )
            WorkEnvironment = gr.Slider(
                label="Work Environment Score", minimum=1, maximum=5, value=4, step=0.1,
                interactive=True
            )
            Engagement = gr.Slider(
                label="Engagement Score", minimum=1, maximum=5, value=4, step=0.1,
                interactive=True
            )
            WellBeing = gr.Slider(
                label="Well-Being Score", minimum=1, maximum=5, value=4, step=0.1,
                interactive=True
            )
            submit_btn = gr.Button("Analyze")
        
        with gr.Column(visible=True, scale=1, min_width=600) as output_col:
            label = gr.Label(label="Predicted Intent to Stay vs Leave")
            local_plot = gr.Plot(label='SHAP Waterfall Analysis')

    submit_btn.click(
        main_func,
        [Department, ChainScale, SupportiveGM, Merit, LearningDevelopment, WorkEnvironment, Engagement, WellBeing],
        [label, local_plot],
        api_name="Employee_Turnover"
    )
    
    gr.Markdown("### Click on any of the examples below to see how it works:")
    
    gr.Examples(
        [
            ["Guest Services", "Upper Upscale", 4.1, 3.7, 3.9, 4.2, 4.4, 4.3],  
            ["Food and Beverage", "Upper Upscale", 3.9, 3.7, 4.1, 4.3, 4.5, 4.4],  
            ["Housekeeping", "Upper Upscale", 4.3, 4.0, 4.3, 4.4, 4.5, 4.4],  
        ],
        [Department, ChainScale, SupportiveGM, Merit, LearningDevelopment, WorkEnvironment, Engagement, WellBeing],
        [label, local_plot],
        main_func,
        cache_examples=True
    )
demo.launch()