Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,81 +2,73 @@ import streamlit as st
|
|
2 |
import pandas as pd
|
3 |
import matplotlib.pyplot as plt
|
4 |
import seaborn as sns
|
5 |
-
|
6 |
# Create an empty DataFrame for expenses
|
7 |
columns = ['Date', 'Category', 'Description', 'Amount']
|
8 |
-
expenses_df
|
9 |
-
|
10 |
-
# Streamlit app layout
|
11 |
-
st.title('Expense Tracker')
|
12 |
-
|
13 |
-
# Add new expense entry
|
14 |
-
st.subheader("Add New Expense")
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
amount = st.number_input("Amount", min_value=0.0, format="%.2f")
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
st.
|
25 |
|
26 |
-
#
|
27 |
-
st.
|
28 |
-
st.write(expenses_df)
|
29 |
|
30 |
-
#
|
31 |
-
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
#
|
34 |
-
st.
|
35 |
-
category_summary = expenses_df.groupby('Category')['Amount'].sum().reset_index()
|
36 |
-
st.write(category_summary)
|
37 |
|
38 |
-
|
39 |
-
st.
|
40 |
|
41 |
-
# Get
|
42 |
-
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
ax.set_xlabel('Category')
|
53 |
-
ax.set_ylabel('Amount Spent ($)')
|
54 |
-
ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
|
55 |
-
st.pyplot(fig)
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
64 |
|
65 |
-
|
66 |
-
st.download_button(
|
67 |
-
label="Download Expenses as CSV",
|
68 |
-
data=expenses_df.to_csv(index=False),
|
69 |
-
file_name="expenses.csv",
|
70 |
-
mime="text/csv"
|
71 |
-
)
|
72 |
|
73 |
-
#
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
76 |
|
77 |
-
|
|
|
|
|
|
|
78 |
|
79 |
-
if response.status_code == 200:
|
80 |
-
st.write("API Data:", response.json()) # Display data from API response
|
81 |
-
else:
|
82 |
-
st.error("Failed to fetch data from API")
|
|
|
2 |
import pandas as pd
|
3 |
import matplotlib.pyplot as plt
|
4 |
import seaborn as sns
|
5 |
+
|
6 |
# Create an empty DataFrame for expenses
|
7 |
columns = ['Date', 'Category', 'Description', 'Amount']
|
8 |
+
if 'expenses_df' not in st.session_state:
|
9 |
+
st.session_state['expenses_df'] = pd.DataFrame(columns=columns)
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Initialize a list for storing messages between user and bot
|
12 |
+
if 'messages' not in st.session_state:
|
13 |
+
st.session_state['messages'] = []
|
|
|
14 |
|
15 |
+
# Function to add an expense
|
16 |
+
def add_expense(date, category, description, amount):
|
17 |
+
new_expense = pd.DataFrame([[date, category, description, amount]], columns=st.session_state['expenses_df'].columns)
|
18 |
+
st.session_state['expenses_df'] = pd.concat([st.session_state['expenses_df'], new_expense], ignore_index=True)
|
19 |
|
20 |
+
# Streamlit UI
|
21 |
+
st.title("Daily Expense Tracker")
|
|
|
22 |
|
23 |
+
# Display messages (chat history)
|
24 |
+
for message in st.session_state['messages']:
|
25 |
+
if message["role"] == "assistant":
|
26 |
+
st.chat_message("assistant").markdown(message["content"])
|
27 |
+
else:
|
28 |
+
st.chat_message("user").markdown(message["content"])
|
29 |
|
30 |
+
# User input for chatbot
|
31 |
+
user_input = st.text_input("You:", key="user_input")
|
|
|
|
|
32 |
|
33 |
+
if user_input:
|
34 |
+
st.session_state['messages'].append({"role": "user", "content": user_input})
|
35 |
|
36 |
+
# Get response from chatbot
|
37 |
+
response = "I'm sorry, I didn't understand that."
|
38 |
+
if "add" in user_input.lower() and "expense" in user_input.lower():
|
39 |
+
st.session_state['messages'].append({"role": "assistant", "content": "Please enter the date of the expense."})
|
40 |
+
response = "Please enter the date of the expense."
|
41 |
|
42 |
+
elif "view" in user_input.lower():
|
43 |
+
st.session_state['messages'].append({"role": "assistant", "content": str(st.session_state['expenses_df'])})
|
44 |
+
response = "Here are all your expenses:\n" + str(st.session_state['expenses_df'])
|
45 |
|
46 |
+
elif "summary" in user_input.lower():
|
47 |
+
category_summary = st.session_state['expenses_df'].groupby('Category')['Amount'].sum().reset_index()
|
48 |
+
st.session_state['messages'].append({"role": "assistant", "content": str(category_summary)})
|
49 |
+
response = "Here is the expense summary by category:\n" + str(category_summary)
|
|
|
|
|
|
|
|
|
50 |
|
51 |
+
elif "visualize" in user_input.lower():
|
52 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
53 |
+
sns.barplot(x='Category', y='Amount', data=st.session_state['expenses_df'], ax=ax)
|
54 |
+
ax.set_title('Total Expenses by Category')
|
55 |
+
ax.set_xlabel('Category')
|
56 |
+
ax.set_ylabel('Amount Spent ($)')
|
57 |
+
ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
|
58 |
+
st.pyplot(fig)
|
59 |
+
response = "Here is the bar chart showing total expenses by category."
|
60 |
|
61 |
+
st.session_state['messages'].append({"role": "assistant", "content": response})
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
# Expense Addition Flow
|
64 |
+
if st.session_state['messages'] and "Please enter the date of the expense." in st.session_state['messages'][-1].get("content", ""):
|
65 |
+
date = st.date_input("Enter Expense Date")
|
66 |
+
category = st.selectbox("Category", ['Food', 'Transport', 'Entertainment', 'Other'])
|
67 |
+
description = st.text_input("Description")
|
68 |
+
amount = st.number_input("Amount", min_value=0.0, format="%.2f")
|
69 |
|
70 |
+
if st.button("Submit Expense"):
|
71 |
+
add_expense(date, category, description, amount)
|
72 |
+
st.session_state['messages'].append({"role": "assistant", "content": f"Expense added: {description} - ${amount:.2f}"})
|
73 |
+
st.success(f"Added expense: {description} - ${amount:.2f}")
|
74 |
|
|
|
|
|
|
|
|