atsuari commited on
Commit
280fc94
·
verified ·
1 Parent(s): 7985f78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -28
app.py CHANGED
@@ -1,20 +1,26 @@
1
  import pickle
2
  import pandas as pd
3
  import shap
4
- from shap.plots._force_matplotlib import draw_additive_plot
5
  import gradio as gr
6
  import numpy as np
7
  import matplotlib.pyplot as plt
8
-
9
- # load the model from disk
10
- loaded_model = pickle.load(open("salar_xgb_team.pkl", 'rb'))
11
-
12
  # Setup SHAP
13
- explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS.
14
-
15
- # Main prediction function
16
  def main_func(age, education_num, sex, capital_gain, capital_loss, hours_per_week):
 
 
 
 
 
17
  sex_binary = 0 if sex == "Male" else 1
 
 
18
  new_row = pd.DataFrame({
19
  'age': [age],
20
  'education-num': [education_num],
@@ -24,21 +30,32 @@ def main_func(age, education_num, sex, capital_gain, capital_loss, hours_per_wee
24
  'hours-per-week': [hours_per_week]
25
  })
26
 
 
27
  prob = loaded_model.predict_proba(new_row)
28
  shap_values = explainer(new_row)
29
 
30
- # SHAP bar plot
31
- plot = shap.plots.bar(shap_values[0], max_display=6, show=False)
 
32
  plt.tight_layout()
33
  local_plot = plt.gcf()
34
  plt.close()
35
 
36
- return {"≤50K": float(prob[0][0]), ">50K": float(prob[0][1])}, local_plot
37
-
38
- # Gradio UI
39
- title = "**Salary Predictor & SHAP Explainer** 💼"
40
- description1 = "This app uses demographic and financial info to predict if someone earns over 50K annually."
41
- description2 = "Adjust the inputs below and click Analyze to see the prediction and SHAP feature importance."
 
 
 
 
 
 
 
 
 
42
 
43
  with gr.Blocks(title=title) as demo:
44
  gr.Markdown(f"## {title}")
@@ -47,23 +64,26 @@ with gr.Blocks(title=title) as demo:
47
  gr.Markdown(description2)
48
  gr.Markdown("---")
49
 
50
- age = gr.Slider(label="Age", minimum=18, maximum=70, value=35)
51
- education_num = gr.Slider(label="Education Number", minimum=1, maximum=16, value=10)
52
- sex = gr.Radio(["Male", "Female"], label="Sex")
53
- capital_gain = gr.Number(label="Capital Gain", value=0)
54
- capital_loss = gr.Number(label="Capital Loss", value=0)
55
- hours_per_week = gr.Slider(label="Hours per Week", minimum=1, maximum=100, value=40)
 
 
56
 
57
- submit_btn = gr.Button("Analyze")
58
 
59
- with gr.Column(visible=True) as output_col:
60
- label = gr.Label(label="Predicted Probability")
61
- local_plot = gr.Plot(label="SHAP Feature Importance")
 
62
 
63
  submit_btn.click(
64
  main_func,
65
  [age, education_num, sex, capital_gain, capital_loss, hours_per_week],
66
- [label, local_plot],
67
  api_name="Salary_Predictor"
68
  )
69
 
@@ -75,9 +95,12 @@ with gr.Blocks(title=title) as demo:
75
  [35, 9, "Male", 0, 1902, 40]
76
  ],
77
  inputs=[age, education_num, sex, capital_gain, capital_loss, hours_per_week],
78
- outputs=[label, local_plot],
79
  fn=main_func,
80
  cache_examples=True
81
  )
82
 
 
 
 
83
  demo.launch()
 
1
  import pickle
2
  import pandas as pd
3
  import shap
 
4
  import gradio as gr
5
  import numpy as np
6
  import matplotlib.pyplot as plt
7
+
8
+ # Load the model
9
+ loaded_model = pickle.load(open("salar_xgb_team.pkl", "rb"))
10
+
11
  # Setup SHAP
12
+ explainer = shap.Explainer(loaded_model) # DO NOT CHANGE THIS
13
+
14
+ # Main function
15
  def main_func(age, education_num, sex, capital_gain, capital_loss, hours_per_week):
16
+ # Input validation
17
+ if age < 18 or age > 100 or education_num < 1 or hours_per_week < 1 or hours_per_week > 100:
18
+ return {"≤50K": 0.0, ">50K": 0.0}, None, "❌ Invalid inputs. Please check your entries."
19
+
20
+ # Process categorical
21
  sex_binary = 0 if sex == "Male" else 1
22
+
23
+ # Create input row
24
  new_row = pd.DataFrame({
25
  'age': [age],
26
  'education-num': [education_num],
 
30
  'hours-per-week': [hours_per_week]
31
  })
32
 
33
+ # Predict
34
  prob = loaded_model.predict_proba(new_row)
35
  shap_values = explainer(new_row)
36
 
37
+ # SHAP plot
38
+ plt.figure(figsize=(8, 4))
39
+ shap.plots.bar(shap_values[0], max_display=6, show=False)
40
  plt.tight_layout()
41
  local_plot = plt.gcf()
42
  plt.close()
43
 
44
+ # Predicted class and confidence
45
+ pred_class = ">50K" if prob[0][1] > 0.5 else "≤50K"
46
+ confidence = round(prob[0][1] if pred_class == ">50K" else prob[0][0], 2)
47
+
48
+ interpretation = f"💼 Prediction: **{pred_class}**\nConfidence: {confidence * 100:.2f}%"
49
+
50
+ return {
51
+ "≤50K": round(prob[0][0], 2),
52
+ ">50K": round(prob[0][1], 2)
53
+ }, local_plot, interpretation
54
+
55
+ # ----------- Gradio UI -----------
56
+ title = "**Salary Predictor & SHAP Explainer** 💰"
57
+ description1 = "This app uses demographic and financial info to predict whether someone earns more than $50K annually."
58
+ description2 = "Adjust the sliders and inputs below, then click **Analyze** to see the prediction and SHAP explanation."
59
 
60
  with gr.Blocks(title=title) as demo:
61
  gr.Markdown(f"## {title}")
 
64
  gr.Markdown(description2)
65
  gr.Markdown("---")
66
 
67
+ with gr.Row():
68
+ with gr.Column(scale=1):
69
+ age = gr.Slider(label="Age (Years)", minimum=18, maximum=100, value=35, info="Enter age between 18 and 100")
70
+ education_num = gr.Slider(label="Education Level (Numerical)", minimum=1, maximum=16, value=10, info="E.g., 1 = Preschool, 16 = Doctorate")
71
+ sex = gr.Radio(["Male", "Female"], label="Sex")
72
+ capital_gain = gr.Number(label="Capital Gain", value=0)
73
+ capital_loss = gr.Number(label="Capital Loss", value=0)
74
+ hours_per_week = gr.Slider(label="Hours Worked per Week", minimum=1, maximum=100, value=40)
75
 
76
+ submit_btn = gr.Button("🔍 Analyze")
77
 
78
+ with gr.Column(scale=1):
79
+ label = gr.Label(label="Predicted Probabilities")
80
+ local_plot = gr.Plot(label="SHAP Feature Importance")
81
+ result_text = gr.Textbox(label="Prediction Summary", lines=2)
82
 
83
  submit_btn.click(
84
  main_func,
85
  [age, education_num, sex, capital_gain, capital_loss, hours_per_week],
86
+ [label, local_plot, result_text],
87
  api_name="Salary_Predictor"
88
  )
89
 
 
95
  [35, 9, "Male", 0, 1902, 40]
96
  ],
97
  inputs=[age, education_num, sex, capital_gain, capital_loss, hours_per_week],
98
+ outputs=[label, local_plot, result_text],
99
  fn=main_func,
100
  cache_examples=True
101
  )
102
 
103
+ gr.Markdown("---")
104
+ gr.Markdown("Built with ❤️ by Tania Ramesh for the 2025 AI Applications Project.")
105
+
106
  demo.launch()