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

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -88
app.py DELETED
@@ -1,88 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- """app.py
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
- age = gr.Number(label="Age", value=35)
60
- education_num = gr.Number(label="Education Level (numeric)", value=10)
61
- sex = gr.Radio(choices=[0,1], label="Sex", value=0) # ✅ Fixed: checkbox -> radio
62
- capital_gain = gr.Number(label="Capital Gain", value=0)
63
- capital_loss = gr.Number(label="Capital Loss", value=0)
64
- hours_per_week = gr.Number(label="Hours per Week", value=40)
65
- # salary_class = gr.Number(label="(Optional) Salary Class for SHAP Context", value=0) # Can remove if not needed
66
-
67
- submit_btn = gr.Button("Analyze")
68
-
69
- with gr.Column(visible=True) as output_col:
70
- label = gr.Label(label="Predicted Income")
71
- local_plot = gr.Plot(label='SHAP Interpretation:')
72
-
73
- submit_btn.click(
74
- main_func,
75
- [age, education_num, sex, capital_gain, capital_loss, hours_per_week],
76
- [label, local_plot], api_name="Income_Predictor"
77
- )
78
-
79
- gr.Markdown("### Try these examples:")
80
- gr.Examples(
81
- [[39,13, 0, 0, 0, 40], [52, 9, 1, 0, 1876, 45]],
82
- [age, education_num, sex, capital_gain, capital_loss, hours_per_week],
83
- [label, local_plot],
84
- main_func,
85
- cache_examples=True
86
- )
87
-
88
- demo.launch()