tilghmanpope commited on
Commit
bd7143c
·
verified ·
1 Parent(s): 4e4a8a4

Upload app (2).py

Browse files
Files changed (1) hide show
  1. app (2).py +90 -0
app (2).py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """app
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1B_g2XLYu46kFDIFzNnnJzBQ0GBPssCQw
8
+ """
9
+
10
+ import pickle
11
+ import pandas as pd
12
+ import shap
13
+ from shap.plots._force_matplotlib import draw_additive_plot
14
+ import gradio as gr
15
+ import numpy as np
16
+ import matplotlib.pyplot as plt
17
+
18
+ # Load the model
19
+ loaded_model = pickle.load(open("salar_xgb_team.pkl", 'rb'))
20
+
21
+ # Setup SHAP (do not change)
22
+ explainer = shap.Explainer(loaded_model)
23
+
24
+ # Define main prediction function
25
+ def main_func(age, education_num, sex, capital_gain, capital_loss, hours_per_week):
26
+ sex = 1 if sex == "Male" else 0
27
+ new_row = pd.DataFrame.from_dict({
28
+ 'age': age,
29
+ 'education-num': education_num,
30
+ 'sex': sex,
31
+ 'capital-gain': capital_gain,
32
+ 'capital-loss': capital_loss,
33
+ 'hours-per-week': hours_per_week
34
+ }, orient='index').transpose()
35
+
36
+ prob = loaded_model.predict_proba(new_row)
37
+
38
+ shap_values = explainer(new_row)
39
+ plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
40
+
41
+ plt.tight_layout()
42
+ local_plot = plt.gcf()
43
+ plt.close()
44
+
45
+ return {"≤ $50K": float(prob[0][0]), "> $50K": float(prob[0][1])}, local_plot
46
+
47
+ # Gradio UI
48
+ title = "**Household Income Predictor & Interpreter** 💰"
49
+ description1 = """This app takes demographic and economic features to predict whether a household earns ≤ $50K or > $50K annually.🚀"""
50
+ description2 = """Adjust the values and click Analyze to get predictions and feature importance."""
51
+
52
+ with gr.Blocks(title=title) as demo:
53
+ gr.Markdown(f"## {title}")
54
+ gr.Markdown(description1)
55
+ gr.Markdown("""---""")
56
+ gr.Markdown(description2)
57
+ gr.Markdown("""---""")
58
+
59
+ gr.Image("Household.png")
60
+
61
+ age = gr.Number(label="Age", value=35)
62
+ education_num = gr.Number(label="Education Level (numeric)", value=10)
63
+ sex = gr.Radio(choices=["Male", "Female"], label="Sex", value="Male")
64
+ capital_gain = gr.Number(label="Capital Gain", value=0)
65
+ capital_loss = gr.Number(label="Capital Loss", value=0)
66
+ hours_per_week = gr.Number(label="Hours per Week", value=40)
67
+ # salary_class = gr.Number(label="(Optional) Salary Class for SHAP Context", value=0) # Can remove if not needed
68
+
69
+ submit_btn = gr.Button("Analyze")
70
+
71
+ with gr.Column(visible=True) as output_col:
72
+ label = gr.Label(label="Predicted Income")
73
+ local_plot = gr.Plot(label='SHAP Interpretation:')
74
+
75
+ submit_btn.click(
76
+ main_func,
77
+ [age, education_num, sex, capital_gain, capital_loss, hours_per_week],
78
+ [label, local_plot], api_name="Income_Predictor"
79
+ )
80
+
81
+ gr.Markdown("### Try these examples:")
82
+ gr.Examples(
83
+ [[39,13, "Male", 0, 0, 40], [52, 9, "Female", 0, 1876, 45]],
84
+ [age, education_num, sex, capital_gain, capital_loss, hours_per_week],
85
+ [label, local_plot],
86
+ main_func,
87
+ cache_examples=True
88
+ )
89
+
90
+ demo.launch()