Dearsawan commited on
Commit
f1912e6
·
verified ·
1 Parent(s): 1a1dfec

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ import seaborn as sns
5
+
6
+ # Initialize an empty DataFrame to hold expenses data
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
+ # Function to generate response for the chatbot
21
+ def chatbot_response(user_input):
22
+ # If the user wants to add an expense, we can extract the details from the input
23
+ if "add" in user_input.lower() and "expense" in user_input.lower():
24
+ # Ask the user for details about the expense
25
+ st.session_state['messages'].append({"role": "assistant", "content": "Please enter the date of the expense."})
26
+ return "Please enter the date of the expense."
27
+
28
+ elif "view" in user_input.lower():
29
+ st.session_state['messages'].append({"role": "assistant", "content": str(st.session_state['expenses_df'])})
30
+ return "Here are all your expenses:\n" + str(st.session_state['expenses_df'])
31
+
32
+ elif "summary" in user_input.lower():
33
+ category_summary = st.session_state['expenses_df'].groupby('Category')['Amount'].sum().reset_index()
34
+ st.session_state['messages'].append({"role": "assistant", "content": str(category_summary)})
35
+ return "Here is the expense summary by category:\n" + str(category_summary)
36
+
37
+ elif "visualize" in user_input.lower():
38
+ fig, ax = plt.subplots(figsize=(10, 6))
39
+ sns.barplot(x='Category', y='Amount', data=st.session_state['expenses_df'], ax=ax)
40
+ ax.set_title('Total Expenses by Category')
41
+ ax.set_xlabel('Category')
42
+ ax.set_ylabel('Amount Spent ($)')
43
+ ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
44
+ st.pyplot(fig)
45
+ return "Here is the bar chart showing total expenses by category."
46
+
47
+ else:
48
+ return "I'm sorry, I didn't understand that. You can add expenses, view expenses, or ask for a summary."
49
+
50
+ # Streamlit UI
51
+ st.title("Daily Expense Tracker - Chatbot")
52
+
53
+ # Display messages (chat history)
54
+ for message in st.session_state['messages']:
55
+ if message["role"] == "assistant":
56
+ st.chat_message("assistant").markdown(message["content"])
57
+ else:
58
+ st.chat_message("user").markdown(message["content"])
59
+
60
+ # User input for chatbot
61
+ user_input = st.text_input("You:", key="user_input")
62
+
63
+ if user_input:
64
+ st.session_state['messages'].append({"role": "user", "content": user_input})
65
+
66
+ # Get response from chatbot
67
+ response = chatbot_response(user_input)
68
+
69
+ st.session_state['messages'].append({"role": "assistant", "content": response})
70
+
71
+ # Expense Addition Flow
72
+ if "Please enter the date of the expense." in st.session_state['messages'][-1].get("content", ""):
73
+ date = st.date_input("Enter Expense Date")
74
+ category = st.selectbox("Category", ['Food', 'Transport', 'Entertainment', 'Other'])
75
+ description = st.text_input("Description")
76
+ amount = st.number_input("Amount", min_value=0.0, format="%.2f")
77
+
78
+ if st.button("Submit Expense"):
79
+ add_expense(date, category, description, amount)
80
+ st.session_state['messages'].append({"role": "assistant", "content": f"Expense added: {description} - ${amount:.2f}"})
81
+ st.success(f"Added expense: {description} - ${amount:.2f}")