tilghmanpope commited on
Commit
ab6b6dc
·
verified ·
1 Parent(s): 647dcad

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -89
app.py DELETED
@@ -1,89 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- """gcom_app_project(1).ipynb
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, sex, education_num, capital_gain, capital_loss, hours_per_week, salary_class):
26
- sex = 1 if sex == "Male" else 0
27
- new_row = pd.DataFrame.from_dict({
28
- 'age': age,
29
- 'sex': sex,
30
- 'education-num': education_num,
31
- 'capital-gain': capital_gain,
32
- 'capital-loss': capital_loss,
33
- 'hours-per-week': hours_per_week,
34
- 'salary-class': salary_class
35
- }, orient='index').transpose()
36
-
37
- prob = loaded_model.predict_proba(new_row)
38
-
39
- shap_values = explainer(new_row)
40
- plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
41
-
42
- plt.tight_layout()
43
- local_plot = plt.gcf()
44
- plt.close()
45
-
46
- return {"≤ $50K": float(prob[0][0]), "> $50K": float(prob[0][1])}, local_plot
47
-
48
- # Gradio UI
49
- title = "**Household Income Predictor & Interpreter** 💰"
50
- description1 = """This app takes demographic and economic features to predict whether a household earns ≤ $50K or > $50K annually."""
51
- description2 = """Adjust the values and click Analyze to get predictions and feature importance."""
52
-
53
- with gr.Blocks(title=title) as demo:
54
- gr.Markdown(f"## {title}")
55
- gr.Markdown(description1)
56
- gr.Markdown("""---""")
57
- gr.Markdown(description2)
58
- gr.Markdown("""---""")
59
-
60
- age = gr.Number(label="Age", value=35)
61
- sex = gr.Radio(choices=["Male", "Female"], label="Sex", value="Male") # ✅ Fixed: checkbox -> radio
62
- education_num = gr.Number(label="Education Level (numeric)", value=10)
63
- capital_gain = gr.Number(label="Capital Gain", value=0)
64
- capital_loss = gr.Number(label="Capital Loss", value=0)
65
- hours_per_week = gr.Number(label="Hours per Week", value=40)
66
- salary_class = gr.Number(label="(Optional) Salary Class for SHAP Context", value=0) # Can remove if not needed
67
-
68
- submit_btn = gr.Button("Analyze")
69
-
70
- with gr.Column(visible=True) as output_col:
71
- label = gr.Label(label="Predicted Income")
72
- local_plot = gr.Plot(label='SHAP Interpretation:')
73
-
74
- submit_btn.click(
75
- main_func,
76
- [age, sex, education_num, capital_gain, capital_loss, hours_per_week, salary_class],
77
- [label, local_plot], api_name="Income_Predictor"
78
- )
79
-
80
- gr.Markdown("### Try these examples:")
81
- gr.Examples(
82
- [[39, "Male", 13, 0, 0, 40, 0], [52, "Female", 9, 0, 1876, 45, 1]],
83
- [age, sex, education_num, capital_gain, capital_loss, hours_per_week, salary_class],
84
- [label, local_plot],
85
- main_func,
86
- cache_examples=True
87
- )
88
-
89
- demo.launch()