Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
from io import StringIO
|
| 8 |
+
|
| 9 |
+
# Streamlit page configuration
|
| 10 |
+
st.set_page_config(page_title="Smart Expense Tracker", page_icon=":money_with_wings:")
|
| 11 |
+
|
| 12 |
+
# Title
|
| 13 |
+
st.title("Smart Expense Tracker :money_with_wings:")
|
| 14 |
+
|
| 15 |
+
# File uploader to upload CSV
|
| 16 |
+
st.sidebar.header("Upload your expense data")
|
| 17 |
+
uploaded_file = st.sidebar.file_uploader("Choose a CSV file", type=["csv"])
|
| 18 |
+
|
| 19 |
+
# Check if file is uploaded
|
| 20 |
+
if uploaded_file is not None:
|
| 21 |
+
# Read CSV
|
| 22 |
+
df = pd.read_csv(uploaded_file)
|
| 23 |
+
|
| 24 |
+
# Display first few rows of the uploaded data
|
| 25 |
+
st.write("### Uploaded Data", df.head())
|
| 26 |
+
|
| 27 |
+
# Ensure correct column names
|
| 28 |
+
if 'Date' not in df.columns or 'Description' not in df.columns or 'Amount' not in df.columns:
|
| 29 |
+
st.error("CSV file should contain 'Date', 'Description', and 'Amount' columns.")
|
| 30 |
+
else:
|
| 31 |
+
# Initialize Hugging Face pipeline for text classification (expense categorization)
|
| 32 |
+
expense_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 33 |
+
|
| 34 |
+
# Function to categorize transactions
|
| 35 |
+
def categorize_transaction(description):
|
| 36 |
+
candidate_labels = ["Groceries", "Entertainment", "Rent", "Utilities", "Dining", "Transportation", "Shopping", "Others"]
|
| 37 |
+
result = expense_classifier(description, candidate_labels)
|
| 38 |
+
return result["labels"][0]
|
| 39 |
+
|
| 40 |
+
# Apply categorization to the descriptions
|
| 41 |
+
df['Category'] = df['Description'].apply(categorize_transaction)
|
| 42 |
+
|
| 43 |
+
# Show categorized data
|
| 44 |
+
st.write("### Categorized Expense Data", df.head())
|
| 45 |
+
|
| 46 |
+
# Visualizations
|
| 47 |
+
|
| 48 |
+
# Pie chart for Category-wise spending
|
| 49 |
+
category_spending = df.groupby("Category")['Amount'].sum()
|
| 50 |
+
st.write("### Category-wise Spending")
|
| 51 |
+
fig, ax = plt.subplots()
|
| 52 |
+
category_spending.plot(kind='pie', autopct='%1.1f%%', ax=ax, figsize=(8, 8))
|
| 53 |
+
ax.set_ylabel('')
|
| 54 |
+
st.pyplot(fig)
|
| 55 |
+
|
| 56 |
+
# Monthly spending trends (Line plot)
|
| 57 |
+
df['Date'] = pd.to_datetime(df['Date'])
|
| 58 |
+
df['Month'] = df['Date'].dt.to_period('M')
|
| 59 |
+
monthly_spending = df.groupby('Month')['Amount'].sum()
|
| 60 |
+
|
| 61 |
+
st.write("### Monthly Spending Trends")
|
| 62 |
+
fig, ax = plt.subplots()
|
| 63 |
+
monthly_spending.plot(kind='line', ax=ax, figsize=(10, 6))
|
| 64 |
+
ax.set_ylabel('Amount ($)')
|
| 65 |
+
ax.set_xlabel('Month')
|
| 66 |
+
ax.set_title('Monthly Spending Trends')
|
| 67 |
+
st.pyplot(fig)
|
| 68 |
+
|
| 69 |
+
# Budget Tracker
|
| 70 |
+
st.sidebar.header("Budget Tracker")
|
| 71 |
+
category_list = df['Category'].unique()
|
| 72 |
+
budget_dict = {}
|
| 73 |
+
|
| 74 |
+
for category in category_list:
|
| 75 |
+
budget_dict[category] = st.sidebar.number_input(f"Set budget for {category}", min_value=0, value=500)
|
| 76 |
+
|
| 77 |
+
# Budget vs Actual Spending (Bar chart)
|
| 78 |
+
st.write("### Budget vs Actual Spending")
|
| 79 |
+
budget_spending = {category: [budget_dict[category], category_spending.get(category, 0)] for category in category_list}
|
| 80 |
+
|
| 81 |
+
budget_df = pd.DataFrame(budget_spending, index=["Budget", "Actual"]).T
|
| 82 |
+
fig, ax = plt.subplots()
|
| 83 |
+
budget_df.plot(kind='bar', ax=ax, figsize=(10, 6))
|
| 84 |
+
ax.set_ylabel('Amount ($)')
|
| 85 |
+
ax.set_title('Budget vs Actual Spending')
|
| 86 |
+
st.pyplot(fig)
|
| 87 |
+
|
| 88 |
+
# Suggestions for saving
|
| 89 |
+
st.write("### Suggested Savings Tips")
|
| 90 |
+
for category, actual in category_spending.items():
|
| 91 |
+
if actual > budget_dict.get(category, 500):
|
| 92 |
+
st.write(f"- **{category}**: Over budget by ${actual - budget_dict.get(category, 500)}. Consider reducing this expense.")
|
| 93 |
+
|
| 94 |
+
else:
|
| 95 |
+
st.write("Upload a CSV file to start tracking your expenses!")
|