Update app.py
Browse files
app.py
CHANGED
@@ -2,15 +2,15 @@ import pandas as pd
|
|
2 |
import plotly.express as px
|
3 |
import streamlit as st
|
4 |
from transformers import pipeline
|
5 |
-
import
|
6 |
|
7 |
-
# Function to add
|
8 |
def add_bg_from_url(image_url):
|
9 |
st.markdown(
|
10 |
f"""
|
11 |
<style>
|
12 |
.stApp {{
|
13 |
-
background-image: url(
|
14 |
background-size: cover;
|
15 |
background-position: center center;
|
16 |
background-repeat: no-repeat;
|
@@ -20,23 +20,26 @@ def add_bg_from_url(image_url):
|
|
20 |
unsafe_allow_html=True
|
21 |
)
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
add_bg_from_url(background_image_url)
|
26 |
|
27 |
# File upload
|
28 |
uploaded_file = st.file_uploader("Upload your expense CSV file", type=["csv"])
|
29 |
if uploaded_file:
|
30 |
df = pd.read_csv(uploaded_file)
|
31 |
|
32 |
-
# Display
|
|
|
33 |
st.write(df.head())
|
34 |
|
35 |
-
#
|
|
|
|
|
|
|
36 |
classifier = pipeline('zero-shot-classification', model='roberta-large-mnli')
|
37 |
categories = ["Groceries", "Rent", "Utilities", "Entertainment", "Dining", "Transportation"]
|
38 |
|
39 |
-
# Function to categorize
|
40 |
def categorize_expense(description):
|
41 |
result = classifier(description, candidate_labels=categories)
|
42 |
return result['labels'][0] # Most probable category
|
@@ -44,9 +47,6 @@ if uploaded_file:
|
|
44 |
# Apply categorization
|
45 |
df['Category'] = df['Description'].apply(categorize_expense)
|
46 |
|
47 |
-
# Display categorized data
|
48 |
-
st.write("Categorized Data", df)
|
49 |
-
|
50 |
# Sidebar for setting the monthly budget using sliders
|
51 |
st.sidebar.header("Set Your Monthly Budget")
|
52 |
groceries_budget = st.sidebar.slider("Groceries Budget", 0, 1000, 300)
|
@@ -66,37 +66,49 @@ if uploaded_file:
|
|
66 |
"Transportation": transportation_budget
|
67 |
}
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
# Track if any category exceeds its budget
|
70 |
-
|
71 |
|
72 |
# Show categories that exceeded their budget
|
73 |
-
exceeded_budget =
|
74 |
st.write("Categories that exceeded the budget:", exceeded_budget[['Date', 'Category', 'Amount']])
|
75 |
|
76 |
# Visualizations
|
77 |
|
78 |
# 1. Pie Chart for expense distribution by category
|
79 |
-
category_expenses =
|
80 |
fig1 = px.pie(category_expenses, values=category_expenses.values, names=category_expenses.index, title="Expense Distribution by Category")
|
81 |
st.plotly_chart(fig1)
|
82 |
|
83 |
# 2. Monthly Spending Trends (Line Chart)
|
84 |
-
|
85 |
-
|
86 |
-
monthly_expenses = df.groupby('Month')['Amount'].sum()
|
87 |
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
# 3. Monthly Spending vs Budget (Bar Chart)
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
ax.set_ylabel('Amount ($)')
|
102 |
-
st.pyplot(fig3)
|
|
|
2 |
import plotly.express as px
|
3 |
import streamlit as st
|
4 |
from transformers import pipeline
|
5 |
+
import datetime
|
6 |
|
7 |
+
# Function to add background image to the app
|
8 |
def add_bg_from_url(image_url):
|
9 |
st.markdown(
|
10 |
f"""
|
11 |
<style>
|
12 |
.stApp {{
|
13 |
+
background-image: url({image_url});
|
14 |
background-size: cover;
|
15 |
background-position: center center;
|
16 |
background-repeat: no-repeat;
|
|
|
20 |
unsafe_allow_html=True
|
21 |
)
|
22 |
|
23 |
+
# Set background image (it will remain even after file upload)
|
24 |
+
add_bg_from_url('https://huggingface.co/spaces/engralimalik/Smart-Expense-Tracker/resolve/main/colorful-abstract-textured-background-design.jpg')
|
|
|
25 |
|
26 |
# File upload
|
27 |
uploaded_file = st.file_uploader("Upload your expense CSV file", type=["csv"])
|
28 |
if uploaded_file:
|
29 |
df = pd.read_csv(uploaded_file)
|
30 |
|
31 |
+
# Display first few rows to the user for format verification
|
32 |
+
st.write("Here are the first few entries in your file for format verification:")
|
33 |
st.write(df.head())
|
34 |
|
35 |
+
# Ensure 'Amount' is numeric
|
36 |
+
df['Amount'] = pd.to_numeric(df['Amount'], errors='coerce')
|
37 |
+
|
38 |
+
# Initialize Hugging Face model for zero-shot classification
|
39 |
classifier = pipeline('zero-shot-classification', model='roberta-large-mnli')
|
40 |
categories = ["Groceries", "Rent", "Utilities", "Entertainment", "Dining", "Transportation"]
|
41 |
|
42 |
+
# Function to categorize
|
43 |
def categorize_expense(description):
|
44 |
result = classifier(description, candidate_labels=categories)
|
45 |
return result['labels'][0] # Most probable category
|
|
|
47 |
# Apply categorization
|
48 |
df['Category'] = df['Description'].apply(categorize_expense)
|
49 |
|
|
|
|
|
|
|
50 |
# Sidebar for setting the monthly budget using sliders
|
51 |
st.sidebar.header("Set Your Monthly Budget")
|
52 |
groceries_budget = st.sidebar.slider("Groceries Budget", 0, 1000, 300)
|
|
|
66 |
"Transportation": transportation_budget
|
67 |
}
|
68 |
|
69 |
+
# Add a date slider for start and end date (default is the last month)
|
70 |
+
today = datetime.date.today()
|
71 |
+
last_month = today - pd.DateOffset(months=1)
|
72 |
+
start_date = st.sidebar.date_input("Start Date", last_month)
|
73 |
+
end_date = st.sidebar.date_input("End Date", today)
|
74 |
+
|
75 |
+
# Filter data by date range
|
76 |
+
df['Date'] = pd.to_datetime(df['Date'])
|
77 |
+
df_filtered = df[(df['Date'] >= pd.to_datetime(start_date)) & (df['Date'] <= pd.to_datetime(end_date))]
|
78 |
+
|
79 |
# Track if any category exceeds its budget
|
80 |
+
df_filtered['Budget_Exceeded'] = df_filtered.apply(lambda row: row['Amount'] > budgets.get(row['Category'], 0), axis=1)
|
81 |
|
82 |
# Show categories that exceeded their budget
|
83 |
+
exceeded_budget = df_filtered[df_filtered['Budget_Exceeded'] == True]
|
84 |
st.write("Categories that exceeded the budget:", exceeded_budget[['Date', 'Category', 'Amount']])
|
85 |
|
86 |
# Visualizations
|
87 |
|
88 |
# 1. Pie Chart for expense distribution by category
|
89 |
+
category_expenses = df_filtered.groupby('Category')['Amount'].sum()
|
90 |
fig1 = px.pie(category_expenses, values=category_expenses.values, names=category_expenses.index, title="Expense Distribution by Category")
|
91 |
st.plotly_chart(fig1)
|
92 |
|
93 |
# 2. Monthly Spending Trends (Line Chart)
|
94 |
+
df_filtered['Month'] = df_filtered['Date'].dt.to_period('M').astype(str) # Convert Period to string for Plotly compatibility
|
95 |
+
monthly_expenses = df_filtered.groupby('Month')['Amount'].sum()
|
|
|
96 |
|
97 |
+
# Convert monthly_expenses into DataFrame for correct plotting
|
98 |
+
monthly_expenses_df = monthly_expenses.reset_index()
|
99 |
+
if not monthly_expenses_df.empty:
|
100 |
+
fig2 = px.line(monthly_expenses_df, x='Month', y='Amount', title="Monthly Expenses", labels={"Month": "Month", "Amount": "Amount ($)"})
|
101 |
+
st.plotly_chart(fig2)
|
102 |
+
else:
|
103 |
+
st.write("No data to display for the selected date range.")
|
104 |
|
105 |
# 3. Monthly Spending vs Budget (Bar Chart)
|
106 |
+
if not monthly_expenses_df.empty:
|
107 |
+
monthly_expenses_df = pd.DataFrame({
|
108 |
+
'Actual': monthly_expenses,
|
109 |
+
'Budget': [sum(budgets.values())] * len(monthly_expenses) # Same budget for simplicity
|
110 |
+
})
|
111 |
+
fig3 = monthly_expenses_df.plot(kind='bar', figsize=(10, 6))
|
112 |
+
st.pyplot(fig3)
|
113 |
+
else:
|
114 |
+
st.write("No data to display for the selected date range.")
|
|
|
|