File size: 4,106 Bytes
a6ee9ca
f10ec56
 
 
2c359f1
f10ec56
a6ee9ca
 
 
f10ec56
a6ee9ca
 
f10ec56
a6ee9ca
 
 
 
2c359f1
a6ee9ca
 
 
 
 
 
2c359f1
a6ee9ca
 
 
 
6ca4f9e
a6ee9ca
 
 
6ca4f9e
a6ee9ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from transformers import pipeline
import plotly.express as px

# Initialize Hugging Face model pipelines for categorization and question-answering
expense_classifier = pipeline('zero-shot-classification', model='distilbert-base-uncased')
qa_model = pipeline('question-answering', model='distilbert-base-uncased-distilled-squad')

# List of possible expense categories
categories = ["Groceries", "Rent", "Utilities", "Entertainment", "Dining", "Transportation", "Salary"]

# Function to categorize transactions
def categorize_expense(description):
    result = expense_classifier(description, candidate_labels=categories)
    return result['labels'][0]  # Choose the most probable category

# Streamlit UI
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.
""")

# Upload CSV file
uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
if uploaded_file is not None:
    df = pd.read_csv(uploaded_file)

    # Display the first few rows of the data
    st.subheader("Transaction Data")
    st.dataframe(df.head())

    # Check if 'Description', 'Amount', and 'Date' columns exist in the file
    if 'Description' in df.columns and 'Amount' in df.columns and 'Date' in df.columns:
        # Categorize expenses
        df['Category'] = df['Description'].apply(categorize_expense)
        
        # Visualizations
        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)

        # Monthly spending trends (Line Chart)
        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)

        # Budget vs Actual Spending (Bar Chart)
        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)

        # Savings Tips (Alert if exceeding budget)
        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.")

        # Question Answering Feature
        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.")