Spaces:
Sleeping
Sleeping
File size: 19,257 Bytes
9f46025 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | """
app.py β Smart Budget Planner (Gradio)
Hugging Face Space entry point.
Run locally:
pip install gradio
python app.py
"""
import gradio as gr
# βββββββββββββββββββββββββββββββββββββββββ
# Python calculation engine
# βββββββββββββββββββββββββββββββββββββββββ
def calculate_budget(
gross,
fed_tax, fica, retire, health,
rent, electric, internet, phone,
groceries, dining, transport, subs, personal,
emergency, invest, vacation,
):
"""
Pure-Python budget engine.
Returns a dict of all derived metrics.
"""
gross = float(gross or 0)
# ββ Deductions (pre-tax) ββ
ded_total = float(fed_tax + fica + retire + health)
net = gross - ded_total
# ββ Housing ββ
housing_total = float(rent + electric + internet + phone)
# ββ Living ββ
living_total = float(groceries + dining + transport + subs + personal)
# ββ Goals ββ
goals_total = float(emergency + invest + vacation)
# ββ Fixed vs variable ββ
fixed_total = float(
fed_tax + fica + retire + health + # deductions always fixed
rent + internet + phone + # fixed housing
subs + # fixed living
emergency + invest + vacation # goals always fixed
)
var_total = float(electric + groceries + dining + transport + personal)
total_exp = housing_total + living_total + goals_total
leftover = net - housing_total - living_total - goals_total
# ββ Budget score 0β100 ββ
if net > 0:
score = max(0, min(100, round(
(leftover / net) * 60 +
(goals_total / net) * 40 * 3
)))
else:
score = 0
def pct(val, base):
return round(val / base * 100) if base > 0 else 0
def fmt(val):
return f"${abs(round(val)):,}"
# ββ Score label ββ
if score >= 70:
score_label = "Healthy"
score_color = "green"
elif score >= 40:
score_label = "Fair β room to improve"
score_color = "orange"
else:
score_label = "Needs attention"
score_color = "red"
leftover_label = "Over budget!" if leftover < 0 else f"{pct(leftover, net)}% unallocated"
# ββ Suggestions ββ
suggestions = []
if pct(dining, net) > 8:
suggestions.append(("β οΈ", f"Dining out is {pct(dining, net)}% of net. Trimming to 6% would save {fmt(dining - net * 0.06)}/mo."))
else:
suggestions.append(("β
", f"Dining spend is healthy at {pct(dining, net)}% of net income."))
if pct(retire, gross) < 10:
suggestions.append(("β οΈ", f"Retirement is {pct(retire, gross)}% of gross. Target 10β15% for long-term security."))
else:
suggestions.append(("β
", f"Retirement contribution is {pct(retire, gross)}% of gross β solid."))
if pct(emergency, net) < 5:
suggestions.append(("π΄", f"Emergency fund is only {pct(emergency, net)}% of net. Aim for at least 5% monthly."))
else:
suggestions.append(("β
", f"Emergency savings rate is on track at {pct(emergency, net)}% of net."))
if leftover < 0:
suggestions.append(("π΄", f"Over budget by {fmt(abs(leftover))}. Review variable expenses to find savings."))
elif pct(leftover, net) > 15:
suggestions.append(("β
", f"{pct(leftover, net)}% unallocated ({fmt(leftover)}). Consider boosting investments or savings goals."))
if pct(fixed_total, net) > 72:
suggestions.append(("β οΈ", f"Fixed costs are {pct(fixed_total, net)}% of net β high. This reduces financial flexibility."))
suggestions_md = "\n".join(f"{icon} {text}" for icon, text in suggestions[:5])
return {
# metric cards
"net": fmt(net),
"net_pct": f"{pct(net, gross)}% of gross",
"total_exp": fmt(total_exp),
"total_exp_sub":f"{pct(total_exp, net)}% of net Β· Fixed {pct(fixed_total, net)}% Β· Variable {pct(var_total, net)}%",
"score": f"{score} / 100",
"score_label": score_label,
"leftover": ('' if leftover >= 0 else '-') + fmt(leftover),
"leftover_sub": leftover_label,
# chart data (raw numbers for Plotly)
"chart_deductions": round(ded_total),
"chart_housing": round(housing_total),
"chart_living": round(living_total),
"chart_goals": round(goals_total),
"chart_leftover": round(max(0, leftover)),
# expense % breakdown for the table
"pct_rent": pct(rent, net),
"pct_groceries": pct(groceries, net),
"pct_dining": pct(dining, net),
"pct_retire": pct(retire, gross),
"pct_emergency": pct(emergency, net),
# suggestions
"suggestions": suggestions_md,
# raw values for What-if echo
"net_raw": round(net),
"leftover_raw": round(leftover),
"score_raw": score,
}
def run_budget(*args):
"""
Gradio callback β receives all slider/number inputs,
returns formatted output strings for each Output component.
"""
r = calculate_budget(*args)
metric_md = f"""
### π Budget summary
| Metric | Value | Detail |
|--------|-------|--------|
| Take-home (net) | **{r['net']}** | {r['net_pct']} |
| Total expenses | **{r['total_exp']}** | {r['total_exp_sub']} |
| Budget score | **{r['score']}** | {r['score_label']} |
| Leftover | **{r['leftover']}** | {r['leftover_sub']} |
"""
suggestions_md = f"### π‘ Suggestions\n\n{r['suggestions']}"
return metric_md, suggestions_md
def run_whatif(
gross,
fed_tax, fica, retire, health,
rent, electric, internet, phone,
groceries, dining, transport, subs, personal,
emergency, invest, vacation,
wi_rent, wi_groceries, wi_dining, wi_transport,
wi_dining_out, wi_electric, wi_personal, wi_subs,
wi_emergency, wi_invest, wi_vacation,
):
"""
What-if callback β compares original vs adjusted values.
"""
orig = calculate_budget(
gross,
fed_tax, fica, retire, health,
rent, electric, internet, phone,
groceries, dining, transport, subs, personal,
emergency, invest, vacation,
)
# Build adjusted inputs by substituting what-if overrides
adj = calculate_budget(
gross,
fed_tax, fica, retire, health,
wi_rent, wi_electric, internet, phone,
wi_groceries, wi_dining_out, wi_transport, wi_subs, wi_personal,
wi_emergency, wi_invest, wi_vacation,
)
left_delta = adj["leftover_raw"] - orig["leftover_raw"]
score_delta = adj["score_raw"] - orig["score_raw"]
sign = lambda v: ("+" if v >= 0 else "") + f"${abs(v):,}"
impact_md = f"""
### β‘ Ripple impact
| | Original | Adjusted | Change |
|--|---------|----------|--------|
| Leftover | **{orig['leftover']}** | **{adj['leftover']}** | {sign(left_delta)} |
| Budget score | **{orig['score']}** | **{adj['score']}** | {sign(score_delta).replace('$','')} pts |
| Total expenses | **{orig['total_exp']}** | **{adj['total_exp']}** | β |
{adj['suggestions']}
"""
return impact_md
# βββββββββββββββββββββββββββββββββββββββββ
# Gradio UI
# βββββββββββββββββββββββββββββββββββββββββ
THEME = gr.themes.Soft(
primary_hue="green",
secondary_hue="blue",
neutral_hue="slate",
font=[gr.themes.GoogleFont("DM Sans"), "ui-sans-serif", "system-ui"],
font_mono=[gr.themes.GoogleFont("JetBrains Mono"), "monospace"],
)
CSS = """
/* ββ General ββ */
.gradio-container { max-width: 1100px !important; margin: 0 auto; }
h1.app-title { font-size: 28px; font-weight: 700; margin-bottom: 2px; }
p.app-sub { color: #6b7280; font-size: 14px; margin-bottom: 1.5rem; }
/* ββ Section headers ββ */
.section-label { font-size: 11px; font-weight: 700; text-transform: uppercase;
letter-spacing: 0.06em; color: #6b7280; margin-bottom: 6px; }
/* ββ Badge-style group labels ββ */
.badge-fixed { color: #0C447C; background: #E6F1FB; padding: 2px 8px; border-radius: 12px; font-size: 11px; font-weight: 600; }
.badge-variable { color: #633806; background: #FAEEDA; padding: 2px 8px; border-radius: 12px; font-size: 11px; font-weight: 600; }
.badge-pretax { color: #72243E; background: #FBEAF0; padding: 2px 8px; border-radius: 12px; font-size: 11px; font-weight: 600; }
.badge-goals { color: #3C3489; background: #EEEDFE; padding: 2px 8px; border-radius: 12px; font-size: 11px; font-weight: 600; }
/* ββ Output markdown ββ */
.output-box { background: #f8fafc; border-radius: 12px; padding: 1rem 1.25rem;
border: 1px solid #e2e8f0; }
"""
with gr.Blocks(theme=THEME, css=CSS, title="Smart Budget Planner") as demo:
# ββ Header ββ
gr.HTML("""
<div style="display:flex;align-items:center;gap:12px;padding:1.25rem 0 0.5rem">
<div style="width:40px;height:40px;background:#1D9E75;border-radius:10px;
display:flex;align-items:center;justify-content:center;flex-shrink:0">
<svg width="22" height="22" viewBox="0 0 24 24" fill="white">
<path d="M11.8 10.9c-2.27-.59-3-1.2-3-2.15 0-1.09 1.01-1.85 2.7-1.85
1.78 0 2.44.85 2.5 2.1h2.21c-.07-1.72-1.12-3.3-3.21-3.81V3h-3v2.16
c-1.94.42-3.5 1.68-3.5 3.61 0 2.31 1.91 3.46 4.7 4.13 2.5.6 3 1.48
3 2.41 0 .69-.49 1.79-2.7 1.79-2.06 0-2.87-.92-2.98-2.1h-2.2
c.12 2.19 1.76 3.42 3.68 3.83V21h3v-2.15c1.95-.37 3.5-1.5 3.5-3.55
0-2.84-2.43-3.81-4.7-4.4z"/>
</svg>
</div>
<div>
<h1 style="font-size:24px;font-weight:700;margin:0;line-height:1.2">Smart Budget Planner</h1>
<p style="font-size:13px;color:#6b7280;margin:2px 0 0">Track Β· Analyze Β· Plan β edit any field and click Calculate</p>
</div>
</div>
""")
with gr.Tabs():
# ββββββββββββββββββββββββββββββββββ
# TAB 1 β OVERVIEW
# ββββββββββββββββββββββββββββββββββ
with gr.TabItem("π Overview"):
gr.Markdown("### Income")
gross = gr.Number(label="Monthly gross income ($)", value=6000, minimum=0, step=100)
with gr.Row():
# ββ Deductions ββ
with gr.Column():
gr.HTML('<span class="badge-pretax">Pre-tax deductions</span>')
fed_tax = gr.Number(label="Federal tax ($)", value=780, minimum=0, step=10)
fica = gr.Number(label="FICA / Social Security ($)", value=459, minimum=0, step=10)
retire = gr.Number(label="401(k) contribution ($)", value=300, minimum=0, step=10)
health = gr.Number(label="Health insurance ($)", value=220, minimum=0, step=10)
# ββ Housing ββ
with gr.Column():
gr.HTML('<span class="badge-fixed">Housing & utilities β fixed</span>')
rent = gr.Number(label="Rent / Mortgage ($)", value=1400, minimum=0, step=50)
electric = gr.Number(label="Electricity ($)", value=95, minimum=0, step=5)
internet = gr.Number(label="Internet ($)", value=60, minimum=0, step=5)
phone = gr.Number(label="Phone ($)", value=45, minimum=0, step=5)
with gr.Row():
# ββ Living ββ
with gr.Column():
gr.HTML('<span class="badge-variable">Living expenses β variable</span>')
groceries = gr.Number(label="Groceries ($)", value=350, minimum=0, step=10)
dining = gr.Number(label="Dining out ($)", value=180, minimum=0, step=10)
transport = gr.Number(label="Transportation ($)", value=120, minimum=0, step=10)
subs = gr.Number(label="Subscriptions ($)", value=55, minimum=0, step=5)
personal = gr.Number(label="Personal care ($)", value=70, minimum=0, step=5)
# ββ Goals ββ
with gr.Column():
gr.HTML('<span class="badge-goals">Goals & savings β planned</span>')
emergency = gr.Number(label="Emergency fund ($)", value=200, minimum=0, step=10)
invest = gr.Number(label="Investments ($)", value=150, minimum=0, step=10)
vacation = gr.Number(label="Vacation savings ($)", value=100, minimum=0, step=10)
calc_btn = gr.Button("Calculate budget", variant="primary", size="lg")
with gr.Row():
metric_out = gr.Markdown(elem_classes="output-box")
suggestion_out = gr.Markdown(elem_classes="output-box")
# All input components for convenience
all_inputs = [
gross,
fed_tax, fica, retire, health,
rent, electric, internet, phone,
groceries, dining, transport, subs, personal,
emergency, invest, vacation,
]
calc_btn.click(
fn=run_budget,
inputs=all_inputs,
outputs=[metric_out, suggestion_out],
)
# Also recalc on any change for a live feel
for comp in all_inputs:
comp.change(
fn=run_budget,
inputs=all_inputs,
outputs=[metric_out, suggestion_out],
)
# ββββββββββββββββββββββββββββββββββ
# TAB 2 β WHAT-IF PLANNER
# ββββββββββββββββββββββββββββββββββ
with gr.TabItem("β‘ What-if planner"):
gr.Markdown("""
> **Scenario mode** β your original budget (entered on the Overview tab) stays unchanged.
> Adjust the sliders below to explore "what if?" scenarios and instantly see the ripple effect.
""")
with gr.Row():
with gr.Column():
gr.HTML('<span class="badge-fixed">Housing adjustments</span>')
wi_rent = gr.Slider(label="Rent / Mortgage ($)", minimum=0, maximum=4000, value=1400, step=50)
wi_electric = gr.Slider(label="Electricity ($)", minimum=0, maximum=500, value=95, step=5)
with gr.Column():
gr.HTML('<span class="badge-variable">Living adjustments</span>')
wi_groceries = gr.Slider(label="Groceries ($)", minimum=0, maximum=1000, value=350, step=10)
wi_dining_out = gr.Slider(label="Dining out ($)", minimum=0, maximum=800, value=180, step=10)
wi_transport = gr.Slider(label="Transportation ($)", minimum=0, maximum=600, value=120, step=10)
wi_subs = gr.Slider(label="Subscriptions ($)", minimum=0, maximum=300, value=55, step=5)
wi_personal = gr.Slider(label="Personal care ($)", minimum=0, maximum=400, value=70, step=5)
with gr.Row():
with gr.Column():
gr.HTML('<span class="badge-goals">Goals adjustments</span>')
wi_emergency = gr.Slider(label="Emergency fund ($)", minimum=0, maximum=1000, value=200, step=10)
wi_invest = gr.Slider(label="Investments ($)", minimum=0, maximum=1000, value=150, step=10)
wi_vacation = gr.Slider(label="Vacation savings ($)", minimum=0, maximum=500, value=100, step=10)
with gr.Column():
impact_out = gr.Markdown(elem_classes="output-box")
wi_inputs = [
gross,
fed_tax, fica, retire, health,
rent, electric, internet, phone,
groceries, dining, transport, subs, personal,
emergency, invest, vacation,
wi_rent, wi_groceries, wi_dining_out, wi_transport,
wi_dining_out, wi_electric, wi_personal, wi_subs,
wi_emergency, wi_invest, wi_vacation,
]
for sl in [wi_rent, wi_electric, wi_groceries, wi_dining_out,
wi_transport, wi_subs, wi_personal,
wi_emergency, wi_invest, wi_vacation]:
sl.change(fn=run_whatif, inputs=wi_inputs, outputs=impact_out)
# ββββββββββββββββββββββββββββββββββ
# TAB 3 β ABOUT
# ββββββββββββββββββββββββββββββββββ
with gr.TabItem("βΉοΈ About"):
gr.Markdown("""
## Smart Budget Planner
A personal finance tool for tracking monthly income and expenses with built-in
suggestions and a What-if scenario planner.
### How to use
1. **Overview tab** β Enter your gross income and edit any expense field. Hit *Calculate budget* or just change any value β results update automatically.
2. **What-if tab** β Keep your real budget on the Overview tab, then drag sliders here to simulate changes (cutting dining, boosting savings, etc.) and instantly see the ripple effect on your leftover and budget score.
### Budget score
The 0β100 score is calculated from two components:
- **Up to 60 points** based on leftover income as a share of net pay (positive leftover = good)
- **Up to 40 points** based on your savings/goals rate (encourages prioritising future security)
| Score | Rating |
|-------|--------|
| 70β100 | Healthy |
| 40β69 | Fair |
| 0β39 | Needs attention |
### Suggestions engine
The suggestions panel checks:
- Dining out as % of net (target: under 8%)
- Retirement contributions as % of gross (target: 10β15%)
- Emergency fund savings rate (target: at least 5% of net)
- Overall leftover / over-budget status
- Fixed cost load (warning if fixed > 72% of net)
### Tech stack
Built with **Gradio** and pure Python. No external data is stored or transmitted.
""")
# ββ Initial load ββ
demo.load(
fn=run_budget,
inputs=all_inputs,
outputs=[metric_out, suggestion_out],
)
# βββββββββββββββββββββββββββββββββββββββββ
# Launch
# βββββββββββββββββββββββββββββββββββββββββ
if __name__ == "__main__":
demo.launch()
|