engralimalik's picture
Update app.py
e6dd97e verified
raw
history blame
4.14 kB
import pandas as pd
import plotly.express as px
import streamlit as st
from transformers import pipeline
import base64
# Function to add custom background image
def add_bg_from_url(image_path):
# Convert image to base64
with open(image_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode()
# HTML for background image
st.markdown(
f"""
<style>
.stApp {{
background-image: url("data:image/png;base64,{encoded_string}");
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
}}
</style>
""",
unsafe_allow_html=True
)
# Add background image (replace with your image path)
add_bg_from_url('path_to_your_image.jpg') # Replace with your image path
# Title and header styling
st.markdown("""
<h1 style="color: #00f7b7; font-family: 'Arial', sans-serif; text-align: center;">Smart Expense Tracker</h1>
""", unsafe_allow_html=True)
# File upload
uploaded_file = st.file_uploader("Upload your expense CSV file", type=["csv"])
if uploaded_file:
df = pd.read_csv(uploaded_file)
# Display Dataframe
st.write(df.head())
# Initialize Hugging Face model for zero-shot classification
classifier = pipeline('zero-shot-classification', model='distilbert-base-uncased')
categories = ["Groceries", "Rent", "Utilities", "Entertainment", "Dining", "Transportation"]
# Function to categorize
def categorize_expense(description):
result = classifier(description, candidate_labels=categories)
return result['labels'][0] # Most probable category
# Apply categorization
df['Category'] = df['Description'].apply(categorize_expense)
# Display categorized data
st.write("Categorized Data", df)
# Sidebar for setting the monthly budget using sliders
st.sidebar.header("Set Your Monthly Budget")
groceries_budget = st.sidebar.slider("Groceries Budget", 0, 1000, 300)
rent_budget = st.sidebar.slider("Rent Budget", 0, 5000, 1000)
utilities_budget = st.sidebar.slider("Utilities Budget", 0, 500, 150)
entertainment_budget = st.sidebar.slider("Entertainment Budget", 0, 1000, 100)
dining_budget = st.sidebar.slider("Dining Budget", 0, 1000, 150)
transportation_budget = st.sidebar.slider("Transportation Budget", 0, 500, 120)
# Store the updated budget values
budgets = {
"Groceries": groceries_budget,
"Rent": rent_budget,
"Utilities": utilities_budget,
"Entertainment": entertainment_budget,
"Dining": dining_budget,
"Transportation": transportation_budget
}
# Track if any category exceeds its budget
df['Budget_Exceeded'] = df.apply(lambda row: row['Amount'] > budgets.get(row['Category'], 0), axis=1)
# Show categories that exceeded their budget
exceeded_budget = df[df['Budget_Exceeded'] == True]
st.write("Categories that exceeded the budget:", exceeded_budget[['Date', 'Category', 'Amount']])
# Visualizations
# 1. Pie Chart for expense distribution by category
category_expenses = df.groupby('Category')['Amount'].sum()
fig1 = px.pie(category_expenses, values=category_expenses.values, names=category_expenses.index, title="Expense Distribution by Category")
st.plotly_chart(fig1)
# 2. Monthly Spending Trends (Line Chart)
df['Date'] = pd.to_datetime(df['Date'])
df['Month'] = df['Date'].dt.to_period('M').astype(str) # Convert Period to string for Plotly compatibility
monthly_expenses = df.groupby('Month')['Amount'].sum()
fig2 = px.line(monthly_expenses, x=monthly_expenses.index, y=monthly_expenses.values, title="Monthly Expenses", labels={"x": "Month", "y": "Amount ($)"})
st.plotly_chart(fig2)
# 3. Monthly Spending vs Budget (Bar Chart)
monthly_expenses_df = pd.DataFrame({
'Actual': monthly_expenses,
'Budget': [sum(budgets.values())] * len(monthly_expenses) # Same budget for simplicity
})
fig3 = monthly_expenses_df.plot(kind='bar', figsize=(10, 6))
st.pyplot(fig3)