engralimalik's picture
Create app.py
f10ec56 verified
raw
history blame
3.76 kB
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(description):
candidate_labels = ["Groceries", "Entertainment", "Rent", "Utilities", "Dining", "Transportation", "Shopping", "Others"]
result = expense_classifier(description, candidate_labels)
return result["labels"][0]
# Apply categorization to the descriptions
df['Category'] = df['Description'].apply(categorize_transaction)
# Show categorized data
st.write("### Categorized Expense Data", df.head())
# Visualizations
# Pie chart for Category-wise spending
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)
# 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()
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)
# Budget Tracker
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)
# Budget vs Actual Spending (Bar chart)
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)
# 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!")