| import pandas as pd |
| import gradio as gr |
| import plotly.express as px |
|
|
|
|
| def calculate_budget(income, currency, needs_pct, wants_pct, savings_pct): |
| if needs_pct + wants_pct + savings_pct != 100: |
| return gr.Markdown("❌ **Percentages must total 100%**"), None, None |
|
|
| symbols = { |
| "USD": "$", |
| "INR": "₹", |
| "EUR": "€", |
| "GBP": "£", |
| "AUD": "A$", |
| "CAD": "C$", |
| } |
| sym = symbols.get(currency, "") |
|
|
| data = { |
| "Needs": income * needs_pct / 100, |
| "Wants": income * wants_pct / 100, |
| "Savings": income * savings_pct / 100, |
| } |
|
|
| df = pd.DataFrame( |
| { |
| "Category": data.keys(), |
| "Amount": data.values(), |
| } |
| ) |
|
|
| fig = px.pie( |
| df, |
| names="Category", |
| values="Amount", |
| title="Budget Allocation", |
| hole=0.4, |
| ) |
|
|
| summary = f""" |
| ### 💸 Monthly Budget Summary |
| |
| - **Income:** {sym}{income:,.0f} |
| - **Needs:** {sym}{data['Needs']:,.0f} |
| - **Wants:** {sym}{data['Wants']:,.0f} |
| - **Savings:** {sym}{data['Savings']:,.0f} |
| |
| ✅ Allocation looks good! |
| """ |
|
|
| csv = df.to_csv(index=False) |
|
|
| return gr.Markdown(summary), fig, csv |
|
|
|
|
| with gr.Blocks(theme=gr.themes.Soft(), title="Monthly Budget Tracker") as demo: |
| gr.Markdown( |
| """ |
| ## 💸 Monthly Budget Tracker |
| Plan your money using the **50 / 30 / 20 rule** or customize it to your life. |
| """ |
| ) |
|
|
| with gr.Row(): |
| income = gr.Number(label="Monthly Income", value=5000) |
| currency = gr.Dropdown( |
| ["USD", "INR", "EUR", "GBP", "AUD", "CAD"], |
| value="INR", |
| label="Currency", |
| ) |
|
|
| gr.Markdown("### 🎯 Allocation (%)") |
|
|
| with gr.Row(): |
| needs = gr.Slider(0, 100, value=50, label="Needs") |
| wants = gr.Slider(0, 100, value=30, label="Wants") |
| savings = gr.Slider(0, 100, value=20, label="Savings") |
|
|
| calculate_btn = gr.Button("✨ Calculate Budget", variant="primary") |
|
|
| with gr.Row(): |
| output_md = gr.Markdown() |
| with gr.Row(): |
| output_chart = gr.Plot() |
| with gr.Row(): |
| output_csv = gr.File(label="📥 Download CSV") |
|
|
| calculate_btn.click( |
| calculate_budget, |
| inputs=[income, currency, needs, wants, savings], |
| outputs=[output_md, output_chart, output_csv], |
| ) |
|
|
| demo.launch() |
|
|