Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,36 +49,67 @@ with gr.Blocks(title=title) as demo:
|
|
| 49 |
gr.Markdown(description2)
|
| 50 |
gr.Markdown("---")
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
with gr.Row():
|
| 53 |
-
age = gr.Number(label="Age", value=35)
|
| 54 |
-
education_num = gr.Number(label="Education Level (numeric)", value=10)
|
| 55 |
with gr.Row():
|
| 56 |
-
sex = gr.Radio(["Male", "Female"], label="Sex")
|
| 57 |
-
capital_gain = gr.Number(label="Capital Gain", value=0)
|
| 58 |
-
capital_loss = gr.Number(label="Capital Loss", value=0)
|
| 59 |
-
hours_per_week = gr.Number(label="Hours per Week", value=40)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
| 63 |
with gr.Column(visible=True) as output_col:
|
| 64 |
-
label = gr.Label(label="Predicted Income")
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
submit_btn.click(
|
| 68 |
-
|
| 69 |
[age, education_num, sex, capital_gain, capital_loss, hours_per_week],
|
| 70 |
-
[label, local_plot],
|
| 71 |
api_name="Salary_Predictor"
|
| 72 |
)
|
| 73 |
|
| 74 |
-
gr.Markdown("### Examples:")
|
| 75 |
-
gr.Examples(
|
| 76 |
-
[
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
|
| 84 |
demo.launch()
|
|
|
|
| 49 |
gr.Markdown(description2)
|
| 50 |
gr.Markdown("---")
|
| 51 |
|
| 52 |
+
# 🎛 Preset scenario dropdown
|
| 53 |
+
scenario = gr.Dropdown(
|
| 54 |
+
["Select a Scenario", "Young, Educated, High Hours", "Older, Low Education, Low Hours"],
|
| 55 |
+
label="📋 Choose a Sample Profile (optional)"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# 🎯 Inputs
|
| 59 |
with gr.Row():
|
| 60 |
+
age = gr.Number(label="🧓 Age", value=35).info("Age of the individual")
|
| 61 |
+
education_num = gr.Number(label="🎓 Education Level (numeric)", value=10).info("e.g., 12 = HS grad, 16 = college")
|
| 62 |
with gr.Row():
|
| 63 |
+
sex = gr.Radio(["Male", "Female"], label="🧍 Sex")
|
| 64 |
+
capital_gain = gr.Number(label="📈 Capital Gain", value=0).info("Income from investment gains")
|
| 65 |
+
capital_loss = gr.Number(label="📉 Capital Loss", value=0).info("Losses reported for tax purposes")
|
| 66 |
+
hours_per_week = gr.Number(label="⏱ Hours per Week", value=40).info("Average hours worked per week")
|
| 67 |
+
|
| 68 |
+
submit_btn = gr.Button("🔎 Analyze")
|
| 69 |
+
|
| 70 |
+
# 🔁 Handle preset scenario changes
|
| 71 |
+
def fill_scenario(scenario_choice):
|
| 72 |
+
if scenario_choice == "Young, Educated, High Hours":
|
| 73 |
+
return [28, 16, "Male", 0, 0, 45]
|
| 74 |
+
elif scenario_choice == "Older, Low Education, Low Hours":
|
| 75 |
+
return [60, 8, "Female", 0, 0, 25]
|
| 76 |
+
else:
|
| 77 |
+
return [35, 10, "Male", 0, 0, 40]
|
| 78 |
|
| 79 |
+
scenario.change(
|
| 80 |
+
fn=fill_scenario,
|
| 81 |
+
inputs=[scenario],
|
| 82 |
+
outputs=[age, education_num, sex, capital_gain, capital_loss, hours_per_week]
|
| 83 |
+
)
|
| 84 |
|
| 85 |
+
# 🧠 Prediction output
|
| 86 |
with gr.Column(visible=True) as output_col:
|
| 87 |
+
label = gr.Label(label="🧠 Predicted Income")
|
| 88 |
+
confidence = gr.Slider(0, 100, value=50, label="📊 Confidence in > $50K", interactive=False)
|
| 89 |
+
local_plot = gr.Plot(label="🔍 Top SHAP Features")
|
| 90 |
+
|
| 91 |
+
# 🧠 Wrap predict + confidence slider logic
|
| 92 |
+
def wrapped_main(age, education_num, sex, capital_gain, capital_loss, hours_per_week):
|
| 93 |
+
result, shap_plot = main_func(age, education_num, sex, capital_gain, capital_loss, hours_per_week)
|
| 94 |
+
return result, float(result["> $50K"]) * 100, shap_plot
|
| 95 |
|
| 96 |
submit_btn.click(
|
| 97 |
+
wrapped_main,
|
| 98 |
[age, education_num, sex, capital_gain, capital_loss, hours_per_week],
|
| 99 |
+
[label, confidence, local_plot],
|
| 100 |
api_name="Salary_Predictor"
|
| 101 |
)
|
| 102 |
|
| 103 |
+
gr.Markdown("### 🧪 Try Some Examples:")
|
| 104 |
+
gr.Examples(
|
| 105 |
+
[
|
| 106 |
+
[28, 16, "Male", 0, 0, 45],
|
| 107 |
+
[60, 8, "Female", 0, 0, 25]
|
| 108 |
+
],
|
| 109 |
+
[age, education_num, sex, capital_gain, capital_loss, hours_per_week],
|
| 110 |
+
[label, confidence, local_plot],
|
| 111 |
+
wrapped_main,
|
| 112 |
+
cache_examples=True
|
| 113 |
+
)
|
| 114 |
|
| 115 |
demo.launch()
|