Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,9 @@ import pickle
|
|
| 5 |
import shap
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
# Load model
|
| 9 |
with open("salar_xgb_team.pkl", "rb") as f:
|
| 10 |
model = pickle.load(f)
|
|
@@ -21,10 +24,10 @@ def predict_salary(age, education_num, sex, capital_gain, capital_loss, hours_pe
|
|
| 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 "
|
| 25 |
confidence = f"{prob * 100:.2f}%" if pred == 1 else f"{(1 - prob) * 100:.2f}%"
|
| 26 |
|
| 27 |
-
# SHAP values
|
| 28 |
shap_values = explainer(input_data)
|
| 29 |
fig, ax = plt.subplots(figsize=(6, 2.5))
|
| 30 |
shap.plots.bar(shap_values[0], max_display=6, show=False)
|
|
@@ -33,30 +36,75 @@ def predict_salary(age, education_num, sex, capital_gain, capital_loss, hours_pe
|
|
| 33 |
return label, confidence, fig
|
| 34 |
|
| 35 |
# Build UI
|
| 36 |
-
with gr.Blocks(
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
gr.Markdown(
|
| 39 |
"""
|
| 40 |
-
This
|
| 41 |
-
|
| 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
|
| 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("
|
| 55 |
|
| 56 |
with gr.Column():
|
| 57 |
-
result = gr.Label(label="
|
| 58 |
-
confidence = gr.Label(label="
|
| 59 |
-
shap_plot = gr.Plot(label="Feature
|
| 60 |
|
| 61 |
gr.Markdown("### 🧪 Try Example Inputs")
|
| 62 |
gr.Examples(
|
|
@@ -78,3 +126,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 78 |
# Launch
|
| 79 |
demo.launch()
|
| 80 |
|
|
|
|
|
|
| 5 |
import shap
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
|
| 8 |
+
# Set dark style for plots
|
| 9 |
+
plt.style.use("dark_background")
|
| 10 |
+
|
| 11 |
# Load model
|
| 12 |
with open("salar_xgb_team.pkl", "rb") as f:
|
| 13 |
model = pickle.load(f)
|
|
|
|
| 24 |
# Prediction & confidence
|
| 25 |
pred = model.predict(input_data)[0]
|
| 26 |
prob = model.predict_proba(input_data)[0][1]
|
| 27 |
+
label = "🎯 > $50K" if pred == 1 else "💼 ≤ $50K"
|
| 28 |
confidence = f"{prob * 100:.2f}%" if pred == 1 else f"{(1 - prob) * 100:.2f}%"
|
| 29 |
|
| 30 |
+
# SHAP values with dark theme
|
| 31 |
shap_values = explainer(input_data)
|
| 32 |
fig, ax = plt.subplots(figsize=(6, 2.5))
|
| 33 |
shap.plots.bar(shap_values[0], max_display=6, show=False)
|
|
|
|
| 36 |
return label, confidence, fig
|
| 37 |
|
| 38 |
# Build UI
|
| 39 |
+
with gr.Blocks(css="""
|
| 40 |
+
body {
|
| 41 |
+
background-color: #191414;
|
| 42 |
+
color: #FFFFFF;
|
| 43 |
+
font-family: 'Helvetica Neue', sans-serif;
|
| 44 |
+
}
|
| 45 |
+
.gr-box {
|
| 46 |
+
background-color: #1DB954 !important;
|
| 47 |
+
color: #191414 !important;
|
| 48 |
+
}
|
| 49 |
+
input[type=number] {
|
| 50 |
+
font-size: 1.5em !important;
|
| 51 |
+
text-align: center !important;
|
| 52 |
+
color: #191414 !important;
|
| 53 |
+
}
|
| 54 |
+
.gr-slider input[type=range] {
|
| 55 |
+
accent-color: #1DB954;
|
| 56 |
+
}
|
| 57 |
+
.gr-button {
|
| 58 |
+
background-color: #1DB954 !important;
|
| 59 |
+
color: #191414 !important;
|
| 60 |
+
border: none !important;
|
| 61 |
+
}
|
| 62 |
+
.gr-button:hover {
|
| 63 |
+
filter: brightness(1.1);
|
| 64 |
+
}
|
| 65 |
+
#title {
|
| 66 |
+
font-size: 2em;
|
| 67 |
+
text-align: center;
|
| 68 |
+
margin-bottom: 0.3em;
|
| 69 |
+
}
|
| 70 |
+
#pulse {
|
| 71 |
+
animation: pulse 2s infinite;
|
| 72 |
+
color: #1DB954;
|
| 73 |
+
font-size: 2.5em;
|
| 74 |
+
text-align: center;
|
| 75 |
+
}
|
| 76 |
+
@keyframes pulse {
|
| 77 |
+
0% { transform: scale(1); opacity: 1; }
|
| 78 |
+
50% { transform: scale(1.05); opacity: 0.7; }
|
| 79 |
+
100% { transform: scale(1); opacity: 1; }
|
| 80 |
+
}
|
| 81 |
+
""") as demo:
|
| 82 |
+
|
| 83 |
+
gr.HTML("<div id='pulse'>🔮 AI-Powered Income Prediction</div>")
|
| 84 |
gr.Markdown(
|
| 85 |
"""
|
| 86 |
+
Welcome! This app predicts if a person earns more than $50K/year using a trained XGBoost model,
|
| 87 |
+
based on demographic and financial information.
|
| 88 |
+
|
| 89 |
+
SHAP values help explain the prediction visually.
|
| 90 |
+
""", elem_id="description"
|
| 91 |
)
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
with gr.Column():
|
| 95 |
age = gr.Slider(0, 100, label="Age", value=35, step=1)
|
| 96 |
education = gr.Dropdown(choices=[str(i) for i in range(1, 17)],
|
| 97 |
+
label="Education Level (1–16)", value="10")
|
| 98 |
sex = gr.Radio(["Male", "Female"], label="Sex", value="Male")
|
| 99 |
cap_gain = gr.Number(label="Capital Gain", value=0)
|
| 100 |
cap_loss = gr.Number(label="Capital Loss", value=0)
|
| 101 |
hours = gr.Number(label="Hours per Week", value=40)
|
| 102 |
+
submit_btn = gr.Button("🔍 Predict")
|
| 103 |
|
| 104 |
with gr.Column():
|
| 105 |
+
result = gr.Label(label="Prediction")
|
| 106 |
+
confidence = gr.Label(label="Confidence Level")
|
| 107 |
+
shap_plot = gr.Plot(label="Top Feature Contributions")
|
| 108 |
|
| 109 |
gr.Markdown("### 🧪 Try Example Inputs")
|
| 110 |
gr.Examples(
|
|
|
|
| 126 |
# Launch
|
| 127 |
demo.launch()
|
| 128 |
|
| 129 |
+
|