|
import streamlit as st |
|
import pandas as pd |
|
import matplotlib.pyplot as plt |
|
from transformers import pipeline |
|
import plotly.express as px |
|
|
|
|
|
expense_classifier = pipeline('zero-shot-classification', model='distilbert-base-uncased') |
|
qa_model = pipeline('question-answering', model='distilbert-base-uncased-distilled-squad') |
|
|
|
|
|
categories = ["Groceries", "Rent", "Utilities", "Entertainment", "Dining", "Transportation", "Salary"] |
|
|
|
|
|
def categorize_expense(description): |
|
result = expense_classifier(description, candidate_labels=categories) |
|
return result['labels'][0] |
|
|
|
|
|
st.title("Smart Expense Tracker") |
|
st.write(""" |
|
Upload a CSV file containing your transaction data, and this app will categorize your expenses, visualize trends, |
|
and provide insights on your spending habits. |
|
""") |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"]) |
|
if uploaded_file is not None: |
|
df = pd.read_csv(uploaded_file) |
|
|
|
|
|
st.subheader("Transaction Data") |
|
st.dataframe(df.head()) |
|
|
|
|
|
if 'Description' in df.columns and 'Amount' in df.columns and 'Date' in df.columns: |
|
|
|
df['Category'] = df['Description'].apply(categorize_expense) |
|
|
|
|
|
st.subheader("Expense Distribution by Category (Pie Chart)") |
|
category_expenses = df.groupby('Category')['Amount'].sum() |
|
fig1 = px.pie(category_expenses, names=category_expenses.index, values=category_expenses.values, title="Category-wise Spending") |
|
st.plotly_chart(fig1) |
|
|
|
|
|
df['Date'] = pd.to_datetime(df['Date']) |
|
df['Month'] = df['Date'].dt.to_period('M') |
|
monthly_expenses = df.groupby('Month')['Amount'].sum() |
|
st.subheader("Monthly Spending Trends") |
|
fig2 = px.line(monthly_expenses, x=monthly_expenses.index, y=monthly_expenses.values, title="Monthly Spending") |
|
st.plotly_chart(fig2) |
|
|
|
|
|
budgets = { |
|
"Groceries": 300, |
|
"Rent": 1000, |
|
"Utilities": 150, |
|
"Entertainment": 100, |
|
"Dining": 150, |
|
"Transportation": 120, |
|
} |
|
budget_df = pd.DataFrame({ |
|
'Actual': monthly_expenses, |
|
'Budget': [sum(budgets.values())] * len(monthly_expenses) |
|
}) |
|
st.subheader("Monthly Spending vs Budget") |
|
fig3 = px.bar(budget_df, x=budget_df.index, y=["Actual", "Budget"], title="Budget vs Actual Spending") |
|
st.plotly_chart(fig3) |
|
|
|
|
|
st.subheader("Savings Tips") |
|
savings_tips = [] |
|
category_expenses = df.groupby("Category")['Amount'].sum() |
|
for category, actual in category_expenses.items(): |
|
if actual > budgets.get(category, 0): |
|
savings_tips.append(f"- **{category}**: Over budget by ${actual - budgets.get(category, 0)}. Consider reducing this expense.") |
|
if savings_tips: |
|
for tip in savings_tips: |
|
st.write(tip) |
|
else: |
|
st.write("No categories exceeded their budget.") |
|
|
|
|
|
st.subheader("Ask Questions About Your Expenses") |
|
question = st.text_input("Ask a question about your expenses (e.g., 'How much did I spend on groceries?')") |
|
if question: |
|
knowledge_base = "\n".join(df.apply(lambda row: f"Description: {row['Description']}, Amount: {row['Amount']}, Category: {row['Category']}", axis=1)) |
|
answer = qa_model(question=question, context=knowledge_base) |
|
st.write(f"Answer: {answer['answer']}") |
|
|
|
else: |
|
st.error("CSV file should contain 'Description', 'Amount', and 'Date' columns.") |
|
|