starboywilliam commited on
Commit
fdd34e1
·
verified ·
1 Parent(s): 3786221

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -25
app.py CHANGED
@@ -12,13 +12,12 @@ with open("salar_xgb_team.pkl", "rb") as f:
12
  # Set up SHAP
13
  explainer = shap.Explainer(model)
14
 
15
- # Define prediction function
16
  def predict_salary(age, education_num, sex, capital_gain, capital_loss, hours_per_week):
17
  sex_num = 0 if sex == "Male" else 1
18
  input_data = pd.DataFrame([[age, int(education_num), sex_num, capital_gain, capital_loss, hours_per_week]],
19
  columns=['age', 'education-num', 'sex', 'capital-gain', 'capital-loss', 'hours-per-week'])
20
 
21
- # Prediction & confidence
22
  pred = model.predict(input_data)[0]
23
  prob = model.predict_proba(input_data)[0][1]
24
  label = ">50K" if pred == 1 else "<=50K"
@@ -32,31 +31,45 @@ def predict_salary(age, education_num, sex, capital_gain, capital_loss, hours_pe
32
 
33
  return label, confidence, fig
34
 
 
35
  # Build UI
36
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
37
- gr.Markdown("## 💼 Income Prediction App")
38
  gr.Markdown(
39
- """
40
- This tool uses a trained XGBoost model to predict whether someone earns more than $50K/year based on demographic and financial information.
41
- It also shows which features influenced the prediction the most, using SHAP explainability.
42
- """
43
  )
44
 
45
  with gr.Row():
46
- with gr.Column():
47
- age = gr.Slider(0, 100, label="Age", value=35, step=1)
48
- education = gr.Dropdown(choices=[str(i) for i in range(1, 17)],
49
- label="Education Level (1-16)", value="10")
50
- sex = gr.Radio(["Male", "Female"], label="Sex", value="Male")
51
- cap_gain = gr.Number(label="Capital Gain", value=0)
52
- cap_loss = gr.Number(label="Capital Loss", value=0)
53
- hours = gr.Number(label="Hours per Week", value=40)
54
- submit_btn = gr.Button("🔮 Predict")
55
-
56
- with gr.Column():
57
- result = gr.Label(label="Predicted Income")
58
- confidence = gr.Label(label="Prediction Confidence")
59
- shap_plot = gr.Plot(label="Feature Importance (SHAP)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  gr.Markdown("### 🧪 Try Example Inputs")
62
  gr.Examples(
@@ -75,8 +88,5 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
75
  outputs=[result, confidence, shap_plot]
76
  )
77
 
78
- # Launch
79
  demo.launch()
80
-
81
-
82
-
 
12
  # Set up SHAP
13
  explainer = shap.Explainer(model)
14
 
15
+ # Prediction function
16
  def predict_salary(age, education_num, sex, capital_gain, capital_loss, hours_per_week):
17
  sex_num = 0 if sex == "Male" else 1
18
  input_data = pd.DataFrame([[age, int(education_num), sex_num, capital_gain, capital_loss, hours_per_week]],
19
  columns=['age', 'education-num', 'sex', 'capital-gain', 'capital-loss', 'hours-per-week'])
20
 
 
21
  pred = model.predict(input_data)[0]
22
  prob = model.predict_proba(input_data)[0][1]
23
  label = ">50K" if pred == 1 else "<=50K"
 
31
 
32
  return label, confidence, fig
33
 
34
+
35
  # Build UI
36
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
37
+ gr.Markdown("<div id='pulse'>💼 AI-Powered Income Prediction</div>", elem_id="pulse")
38
  gr.Markdown(
39
+ "Welcome! This app predicts if a person earns more than $50K/year using a trained XGBoost model, based on demographic and financial information.<br>SHAP values help explain the prediction visually.",
40
+ elem_id="description"
 
 
41
  )
42
 
43
  with gr.Row():
44
+ with gr.Column(scale=1, min_width=400):
45
+ with gr.Group():
46
+ age = gr.Slider(0, 100, label="Age", value=35, step=1)
47
+
48
+ with gr.Group():
49
+ education = gr.Dropdown(
50
+ choices=[str(i) for i in range(1, 17)],
51
+ label="Education Level (1–16)", value="10"
52
+ )
53
+
54
+ with gr.Group():
55
+ sex = gr.Radio(["Male", "Female"], label="Sex", value="Male")
56
+
57
+ with gr.Group():
58
+ cap_gain = gr.Number(label="Capital Gain", value=0, placeholder="Enter Numerical Value")
59
+
60
+ with gr.Group():
61
+ cap_loss = gr.Number(label="Capital Loss", value=0, placeholder="Enter Numerical Value")
62
+
63
+ with gr.Group():
64
+ hours = gr.Number(label="Hours per Week", value=40)
65
+
66
+ with gr.Row():
67
+ submit_btn = gr.Button("🔮 Predict")
68
+
69
+ with gr.Column(scale=2):
70
+ result = gr.Label(label="📊 Prediction")
71
+ confidence = gr.Label(label="📈 Confidence Level")
72
+ shap_plot = gr.Plot(label="📌 Top Feature Contributions")
73
 
74
  gr.Markdown("### 🧪 Try Example Inputs")
75
  gr.Examples(
 
88
  outputs=[result, confidence, shap_plot]
89
  )
90
 
91
+ # Launch app
92
  demo.launch()