Update app.py
Browse files
app.py
CHANGED
|
@@ -26,4 +26,47 @@ def process_expenses(file):
|
|
| 26 |
df['Category'] = categorize_transaction_batch(df['Description'].tolist())
|
| 27 |
|
| 28 |
# Create visualizations:
|
| 29 |
-
# 1. Pie chart for Category-wise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
df['Category'] = categorize_transaction_batch(df['Description'].tolist())
|
| 27 |
|
| 28 |
# Create visualizations:
|
| 29 |
+
# 1. Pie chart for Category-wise spending
|
| 30 |
+
category_spending = df.groupby("Category")['Amount'].sum()
|
| 31 |
+
fig1 = px.pie(category_spending, names=category_spending.index, values=category_spending.values, title="Category-wise Spending")
|
| 32 |
+
|
| 33 |
+
# 2. Monthly spending trends (Line plot)
|
| 34 |
+
df['Date'] = pd.to_datetime(df['Date'])
|
| 35 |
+
df['Month'] = df['Date'].dt.to_period('M')
|
| 36 |
+
monthly_spending = df.groupby('Month')['Amount'].sum()
|
| 37 |
+
fig2 = px.line(monthly_spending, x=monthly_spending.index, y=monthly_spending.values, title="Monthly Spending Trends")
|
| 38 |
+
|
| 39 |
+
# 3. Budget vs Actual Spending (Bar chart)
|
| 40 |
+
category_list = df['Category'].unique()
|
| 41 |
+
budget_dict = {category: 500 for category in category_list} # Default budget is 500 for each category
|
| 42 |
+
budget_spending = {category: [budget_dict[category], category_spending.get(category, 0)] for category in category_list}
|
| 43 |
+
budget_df = pd.DataFrame(budget_spending, index=["Budget", "Actual"]).T
|
| 44 |
+
fig3 = px.bar(budget_df, x=budget_df.index, y=["Budget", "Actual"], title="Budget vs Actual Spending")
|
| 45 |
+
|
| 46 |
+
# 4. Suggested savings (only calculate if over budget)
|
| 47 |
+
savings_tips = []
|
| 48 |
+
for category, actual in category_spending.items():
|
| 49 |
+
if actual > budget_dict.get(category, 500):
|
| 50 |
+
savings_tips.append(f"- **{category}**: Over budget by ${actual - budget_dict.get(category, 500)}. Consider reducing this expense.")
|
| 51 |
+
|
| 52 |
+
return df.head(), fig1, fig2, fig3, savings_tips
|
| 53 |
+
|
| 54 |
+
# Gradio interface definition
|
| 55 |
+
inputs = gr.File(label="Upload Expense CSV")
|
| 56 |
+
outputs = [
|
| 57 |
+
gr.Dataframe(label="Categorized Expense Data"),
|
| 58 |
+
gr.Plot(label="Category-wise Spending (Pie Chart)"),
|
| 59 |
+
gr.Plot(label="Monthly Spending Trends (Line Chart)"),
|
| 60 |
+
gr.Plot(label="Budget vs Actual Spending (Bar Chart)"),
|
| 61 |
+
gr.Textbox(label="Savings Tips")
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
# Launch Gradio interface
|
| 65 |
+
gr.Interface(
|
| 66 |
+
fn=process_expenses,
|
| 67 |
+
inputs=inputs,
|
| 68 |
+
outputs=outputs,
|
| 69 |
+
live=True,
|
| 70 |
+
title="Smart Expense Tracker",
|
| 71 |
+
description="Upload your CSV of transactions, categorize them, and view insights like spending trends and budget analysis."
|
| 72 |
+
).launch()
|