| import streamlit as st |
| import pandas as pd |
| import numpy as np |
| import matplotlib.pyplot as plt |
| import seaborn as sns |
| from transformers import pipeline |
| from io import StringIO |
|
|
| |
| st.set_page_config(page_title="Smart Expense Tracker", page_icon=":money_with_wings:") |
|
|
| |
| st.title("Smart Expense Tracker :money_with_wings:") |
|
|
| |
| st.sidebar.header("Upload your expense data") |
| uploaded_file = st.sidebar.file_uploader("Choose a CSV file", type=["csv"]) |
|
|
| |
| if uploaded_file is not None: |
| |
| df = pd.read_csv(uploaded_file) |
|
|
| |
| st.write("### Uploaded Data", df.head()) |
|
|
| |
| if 'Date' not in df.columns or 'Description' not in df.columns or 'Amount' not in df.columns: |
| st.error("CSV file should contain 'Date', 'Description', and 'Amount' columns.") |
| else: |
| |
| expense_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli") |
|
|
| |
| def categorize_transaction(description): |
| candidate_labels = ["Groceries", "Entertainment", "Rent", "Utilities", "Dining", "Transportation", "Shopping", "Others"] |
| result = expense_classifier(description, candidate_labels) |
| return result["labels"][0] |
|
|
| |
| df['Category'] = df['Description'].apply(categorize_transaction) |
|
|
| |
| st.write("### Categorized Expense Data", df.head()) |
|
|
| |
|
|
| |
| category_spending = df.groupby("Category")['Amount'].sum() |
| st.write("### Category-wise Spending") |
| fig, ax = plt.subplots() |
| category_spending.plot(kind='pie', autopct='%1.1f%%', ax=ax, figsize=(8, 8)) |
| ax.set_ylabel('') |
| st.pyplot(fig) |
|
|
| |
| df['Date'] = pd.to_datetime(df['Date']) |
| df['Month'] = df['Date'].dt.to_period('M') |
| monthly_spending = df.groupby('Month')['Amount'].sum() |
|
|
| st.write("### Monthly Spending Trends") |
| fig, ax = plt.subplots() |
| monthly_spending.plot(kind='line', ax=ax, figsize=(10, 6)) |
| ax.set_ylabel('Amount ($)') |
| ax.set_xlabel('Month') |
| ax.set_title('Monthly Spending Trends') |
| st.pyplot(fig) |
|
|
| |
| st.sidebar.header("Budget Tracker") |
| category_list = df['Category'].unique() |
| budget_dict = {} |
|
|
| for category in category_list: |
| budget_dict[category] = st.sidebar.number_input(f"Set budget for {category}", min_value=0, value=500) |
|
|
| |
| st.write("### Budget vs Actual Spending") |
| budget_spending = {category: [budget_dict[category], category_spending.get(category, 0)] for category in category_list} |
|
|
| budget_df = pd.DataFrame(budget_spending, index=["Budget", "Actual"]).T |
| fig, ax = plt.subplots() |
| budget_df.plot(kind='bar', ax=ax, figsize=(10, 6)) |
| ax.set_ylabel('Amount ($)') |
| ax.set_title('Budget vs Actual Spending') |
| st.pyplot(fig) |
|
|
| |
| st.write("### Suggested Savings Tips") |
| for category, actual in category_spending.items(): |
| if actual > budget_dict.get(category, 500): |
| st.write(f"- **{category}**: Over budget by ${actual - budget_dict.get(category, 500)}. Consider reducing this expense.") |
|
|
| else: |
| st.write("Upload a CSV file to start tracking your expenses!") |
|
|