File size: 4,638 Bytes
d3f1cea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import pickle

import pandas as pd

import shap

import gradio as gr

import numpy as np

import matplotlib.pyplot as plt
 
# Load model

loaded_model = pickle.load(open("salar_xgb_team.pkl", "rb"))
 
# SHAP explainer (Do not change)

explainer = shap.Explainer(loaded_model)
 
# Mapping from dropdown labels to numeric education levels

education_map = {

    "Preschool": 1,

    "1st-4th": 2,

    "5th-6th": 3,

    "7th-8th": 4,

    "9th": 5,

    "10th": 6,

    "11th": 7,

    "12th": 8,

    "HS-grad": 9,

    "Some-college": 10,

    "Assoc-voc": 11,

    "Assoc-acdm": 12,

    "Bachelors": 13,

    "Masters": 14,

    "Prof-school": 15,

    "Doctorate": 16

}
 
# Main prediction function

def main_func(age, education_label, sex, capital_gain, capital_loss, hours_per_week):

    # Validate input

    if age < 18 or age > 100 or hours_per_week < 1 or hours_per_week > 100:

        return {"≀50K": 0.0, ">50K": 0.0}, None, "❌ Invalid inputs. Please check your entries."
 
    # Convert to model format

    education_num = education_map.get(education_label, 9)  # default to HS-grad

    sex_binary = 0 if sex == "Male" else 1
 
    # Build dataframe

    new_row = pd.DataFrame({

        'age': [age],

        'education-num': [education_num],

        'sex': [sex_binary],

        'capital-gain': [capital_gain],

        'capital-loss': [capital_loss],

        'hours-per-week': [hours_per_week]

    })
 
    # Predict and explain

    prob = loaded_model.predict_proba(new_row)

    shap_values = explainer(new_row)
 
    # SHAP plot

    plt.figure(figsize=(8, 4))

    shap.plots.bar(shap_values[0], max_display=6, show=False)

    plt.tight_layout()

    local_plot = plt.gcf()

    plt.close()
 
    # Class and confidence

    pred_class = ">50K" if prob[0][1] > 0.5 else "≀50K"

    confidence = round(prob[0][1] if pred_class == ">50K" else prob[0][0], 2)

    interpretation = f"πŸ’Ό Prediction: **{pred_class}**\nConfidence: {confidence * 100:.2f}%"
 
    return {

        "≀50K": round(prob[0][0], 2),

        ">50K": round(prob[0][1], 2)

    }, local_plot, interpretation
 
# UI layout

title = "**Salary Predictor & SHAP Explainer** πŸ’°"

description1 = "This app uses demographic and financial info to predict whether someone earns more than $50K annually."

description2 = "Adjust the inputs and click **Analyze** to see prediction and SHAP feature contributions."
 
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(scale=1):

            age = gr.Number(label="Age", value=35, precision=0)

            education_label = gr.Dropdown(

                choices=list(education_map.keys()),

                label="Education Level",

                value="HS-grad"

            )

            sex = gr.Radio(["Male", "Female"], label="Sex")

            capital_gain = gr.Number(label="Capital Gain", value=0)

            capital_loss = gr.Number(label="Capital Loss", value=0)

            hours_per_week = gr.Slider(label="Hours Worked per Week", minimum=1, maximum=100, value=40)
 
            submit_btn = gr.Button("πŸ” Analyze")
 
        with gr.Column(scale=1):

            label = gr.Label(label="Predicted Probabilities")

            local_plot = gr.Plot(label="SHAP Feature Importance")

            result_text = gr.Textbox(label="Prediction Summary", lines=2)
 
    submit_btn.click(

        main_func,

        [age, education_label, sex, capital_gain, capital_loss, hours_per_week],

        [label, local_plot, result_text],

        api_name="Salary_Predictor"

    )
 
    gr.Markdown("### Try one of the following examples:")

    gr.Examples(

        examples=[

            [28, "Some-college", "Male", 0, 0, 45],

            [52, "Masters", "Female", 7688, 0, 60],

            [35, "HS-grad", "Male", 0, 1902, 40]

        ],

        inputs=[age, education_label, sex, capital_gain, capital_loss, hours_per_week],

        outputs=[label, local_plot, result_text],

        fn=main_func,

        cache_examples=True

    )
 
    gr.Markdown("---")

    gr.Markdown("Built with love by Team 3 for the 2025 AI Applications Project!")
 
 
    gr.Markdown("---")

    gr.Markdown("πŸ“Š Thanks for using the Salary Predictor!")

    gr.Image(

        value="https://media.giphy.com/media/l0MYt5jPR6QX5pnqM/giphy.gif",

        label="",

        show_label=False,

        show_download_button=False,

        height=200

    )

demo.launch(server_name="0.0.0.0", server_port=7860)