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 # Streamlit page configuration st.set_page_config(page_title="Smart Expense Tracker", page_icon=":money_with_wings:") # Title st.title("Smart Expense Tracker :money_with_wings:") # File uploader to upload CSV st.sidebar.header("Upload your expense data") uploaded_file = st.sidebar.file_uploader("Choose a CSV file", type=["csv"]) # Check if file is uploaded if uploaded_file is not None: # Read CSV df = pd.read_csv(uploaded_file) # Display first few rows of the uploaded data st.write("### Uploaded Data", df.head()) # Ensure correct column names 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: # Initialize Hugging Face pipeline for text classification (expense categorization) expense_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli") # Function to categorize transactions def categorize_transaction_batch(descriptions): candidate_labels = ["Groceries", "Entertainment", "Rent", "Utilities", "Dining", "Transportation", "Shopping", "Others"] return [expense_classifier(description, candidate_labels)["labels"][0] for description in descriptions] # Batch process all descriptions at once for efficiency df['Category'] = categorize_transaction_batch(df['Description'].tolist()) # Show categorized data st.write("### Categorized Expense Data", df.head()) # Visualizations # Optimized Pie chart for Category-wise spending category_spending = df.groupby("Category")['Amount'].sum() st.write("### Category-wise Spending") fig, ax = plt.subplots(figsize=(8, 8)) category_spending.plot(kind='pie', autopct='%1.1f%%', ax=ax) ax.set_ylabel('') st.pyplot(fig) # Optimized Monthly spending trends (Line plot) 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(figsize=(10, 6)) monthly_spending.plot(kind='line', ax=ax) ax.set_ylabel('Amount ($)') ax.set_xlabel('Month') ax.set_title('Monthly Spending Trends') st.pyplot(fig) # Optimized Budget Tracker st.sidebar.header("Budget Tracker") category_list = df['Category'].unique() budget_dict = {category: st.sidebar.number_input(f"Set budget for {category}", min_value=0, value=500) for category in category_list} # Optimized Budget vs Actual Spending (Bar chart) st.write("### Budget vs Actual Spending") budget_spending = {category: [budget_dict.get(category, 500), category_spending.get(category, 0)] for category in category_list} budget_df = pd.DataFrame(budget_spending, index=["Budget", "Actual"]).T fig, ax = plt.subplots(figsize=(10, 6)) budget_df.plot(kind='bar', ax=ax) ax.set_ylabel('Amount ($)') ax.set_title('Budget vs Actual Spending') st.pyplot(fig) # Suggestions for saving 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!")