File size: 11,349 Bytes
139af8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import altair as alt
import google.generativeai as genai
from datetime import datetime
import os
import re
import json

# App title and configuration
st.set_page_config(page_title="Expense Tracker", layout="wide")

# Initialize session state
if 'expenses' not in st.session_state:
    st.session_state.expenses = []
if 'df' not in st.session_state:
    st.session_state.df = pd.DataFrame(columns=['Date', 'Category', 'Amount', 'Description'])
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []

# Load Gemini API key from secrets
def configure_genai():
    # For local development, use st.secrets
    # For Hugging Face deployment, use environment variables
    if 'GEMINI_API_KEY' in st.secrets:
        api_key = st.secrets['GEMINI_API_KEY']
    else:
        api_key = os.environ.get('GEMINI_API_KEY')
    
    if not api_key:
        st.error("Gemini API key not found. Please add it to the secrets or environment variables.")
        st.stop()
    
    genai.configure(api_key=api_key)
    return genai.GenerativeModel('gemini-2.0-flash')

model = configure_genai()

# Function to extract expense data using Gemini
def extract_expense_data(text):
    prompt = f"""
    Extract expense information from the following text. 
    Return a JSON object with these fields:
    - date: in YYYY-MM-DD format (use today's date if not specified)
    - category: the expense category (e.g., food, transport, entertainment)
    - amount: the numerical amount (just the number, no currency symbol)
    - description: brief description of the expense

    Example output format:
    {{
        "date": "2025-03-19",
        "category": "food",
        "amount": 25.50,
        "description": "lunch at cafe"
    }}

    If multiple expenses are mentioned, return an array of such objects.
    
    Text: {text}
    """
    
    try:
        response = model.generate_content(prompt)
        response_text = response.text
        
        # Extract JSON from the response
        json_match = re.search(r'```json\n(.*?)```', response_text, re.DOTALL)
        if json_match:
            json_str = json_match.group(1)
        else:
            # If no code block, try to find JSON directly
            json_str = response_text
        
        # Parse the JSON
        data = json.loads(json_str)
        return data
    except Exception as e:
        st.error(f"Error extracting expense data: {e}")
        return None

# Function to add expenses to the dataframe
def add_expense_to_df(expense_data):
    if isinstance(expense_data, list):
        # Handle multiple expenses
        for expense in expense_data:
            add_single_expense(expense)
    else:
        # Handle single expense
        add_single_expense(expense_data)
    
    # Sort by date
    st.session_state.df = st.session_state.df.sort_values(by='Date', ascending=False)

def add_single_expense(expense):
    # Convert amount to float
    try:
        amount = float(expense['amount'])
    except:
        amount = 0.0
    
    # Create a new row
    new_row = pd.DataFrame({
        'Date': [expense.get('date', datetime.now().strftime('%Y-%m-%d'))],
        'Category': [expense.get('category', 'Other')],
        'Amount': [amount],
        'Description': [expense.get('description', '')]
    })
    
    # Append to the dataframe
    st.session_state.df = pd.concat([st.session_state.df, new_row], ignore_index=True)

# Function to get AI insights about expenses
def get_expense_insights(query):
    if st.session_state.df.empty:
        return "No expense data available yet. Please add some expenses first."
    
    # Convert dataframe to string representation
    df_str = st.session_state.df.to_string()
    
    prompt = f"""
    Here is a dataset of expenses:
    {df_str}
    
    User query: {query}
    
    Please analyze this expense data and answer the query.
    Provide your analysis in a clear and concise way.
    If the query is about visualizations, describe what kind of chart would be helpful.
    """
    
    try:
        response = model.generate_content(prompt)
        return response.text
    except Exception as e:
        return f"Error getting insights: {e}"

# Function to create visualizations
def create_visualizations():
    if st.session_state.df.empty:
        st.info("Add some expenses to see visualizations")
        return
    
    # Create a copy of the dataframe for visualization
    df = st.session_state.df.copy()
    
    # Ensure Date is datetime
    df['Date'] = pd.to_datetime(df['Date'])
    
    # Create tabs for different visualizations
    tab1, tab2, tab3 = st.tabs(["Expenses by Category", "Expenses Over Time", "Recent Expenses"])
    
    with tab1:
        st.subheader("Expenses by Category")
        category_totals = df.groupby('Category')['Amount'].sum().reset_index()
        
        # Create a pie chart
        fig, ax = plt.subplots(figsize=(8, 8))
        ax.pie(category_totals['Amount'], labels=category_totals['Category'], autopct='%1.1f%%')
        ax.set_title('Expenses by Category')
        st.pyplot(fig)
        
        # Create a bar chart
        category_chart = alt.Chart(category_totals).mark_bar().encode(
            x=alt.X('Category:N', sort='-y'),
            y=alt.Y('Amount:Q'),
            color='Category:N'
        ).properties(
            title='Total Expenses by Category'
        )
        st.altair_chart(category_chart, use_container_width=True)
    
    with tab2:
        st.subheader("Expenses Over Time")
        # Group by date and sum amounts
        daily_totals = df.groupby(df['Date'].dt.date)['Amount'].sum().reset_index()
        
        # Create a line chart
        time_chart = alt.Chart(daily_totals).mark_line(point=True).encode(
            x='Date:T',
            y='Amount:Q',
            tooltip=['Date:T', 'Amount:Q']
        ).properties(
            title='Daily Expenses Over Time'
        )
        st.altair_chart(time_chart, use_container_width=True)
    
    with tab3:
        st.subheader("Recent Expenses")
        # Sort by date and get the last 10 expenses
        recent = df.sort_values('Date', ascending=False).head(10)
        
        # Create a bar chart
        recent_chart = alt.Chart(recent).mark_bar().encode(
            x=alt.X('Description:N', sort='-y'),
            y='Amount:Q',
            color='Category:N',
            tooltip=['Date:T', 'Category:N', 'Amount:Q', 'Description:N']
        ).properties(
            title='Most Recent Expenses'
        )
        st.altair_chart(recent_chart, use_container_width=True)

# App layout
st.title("💰 Expense Tracker with AI")

# Sidebar for app navigation
page = st.sidebar.radio("Navigation", ["Add Expenses", "View & Analyze", "Chat with your Data"])

if page == "Add Expenses":
    st.header("Add Your Expenses")
    st.write("Describe your expenses in natural language, and AI will extract the details.")
    
    with st.form("expense_form"):
        user_input = st.text_area(
            "Enter your expenses:", 
            height=100,
            placeholder="Example: I spent $25 on lunch today, $15 on transport yesterday, and $50 on groceries on March 15th"
        )
        submit_button = st.form_submit_button("Add Expenses")
    
    if submit_button and user_input:
        with st.spinner("Processing your expenses..."):
            expense_data = extract_expense_data(user_input)
            
            if expense_data:
                add_expense_to_df(expense_data)
                st.success("Expenses added successfully!")
                st.write("Extracted information:")
                st.json(expense_data)
            else:
                st.error("Failed to extract expense data. Please try again with a clearer description.")
    
    # Show the current expenses
    if not st.session_state.df.empty:
        st.subheader("Your Recent Expenses")
        st.dataframe(st.session_state.df.sort_values(by='Date', ascending=False), use_container_width=True)

elif page == "View & Analyze":
    st.header("Your Expense Data")
    
    # Show the current expenses as a table
    if not st.session_state.df.empty:
        st.dataframe(st.session_state.df.sort_values(by='Date', ascending=False), use_container_width=True)
        
        # Add download button
        csv = st.session_state.df.to_csv(index=False)
        st.download_button(
            label="Download CSV",
            data=csv,
            file_name="expenses.csv",
            mime="text/csv"
        )
        
        # Show summary statistics
        st.subheader("Summary Statistics")
        col1, col2, col3 = st.columns(3)
        with col1:
            st.metric("Total Expenses", f"${st.session_state.df['Amount'].sum():.2f}")
        with col2:
            st.metric("Average Expense", f"${st.session_state.df['Amount'].mean():.2f}")
        with col3:
            st.metric("Number of Expenses", f"{len(st.session_state.df)}")
        
        # Create visualizations
        st.subheader("Visualizations")
        create_visualizations()
    else:
        st.info("No expense data available yet. Please add some expenses first.")

elif page == "Chat with your Data":
    st.header("Chat with Your Expense Data")
    
    if st.session_state.df.empty:
        st.info("No expense data available yet. Please add some expenses first.")
    else:
        st.write("Ask questions about your expenses to get insights.")
        
        # Display chat history
        for message in st.session_state.chat_history:
            with st.chat_message(message["role"]):
                st.write(message["content"])
        
        # Get user input
        user_query = st.chat_input("Ask about your expenses...")
        
        if user_query:
            # Add user message to chat history
            st.session_state.chat_history.append({"role": "user", "content": user_query})
            
            # Display user message
            with st.chat_message("user"):
                st.write(user_query)
            
            # Get AI response
            with st.spinner("Thinking..."):
                response = get_expense_insights(user_query)
            
            # Add AI response to chat history
            st.session_state.chat_history.append({"role": "assistant", "content": response})
            
            # Display AI response
            with st.chat_message("assistant"):
                st.write(response)

# Add instructions for Hugging Face deployment in the sidebar
with st.sidebar.expander("Deployment Instructions"):
    st.write("""
    ### How to deploy to Hugging Face:
    
    1. Save this code as `app.py`
    2. Create a `requirements.txt` file with these dependencies:
       ```
       streamlit
       pandas
       matplotlib
       seaborn
       altair
       google-generativeai
       ```
    3. Create a `README.md` file describing your app
    4. Add your Gemini API key to your Hugging Face Space secrets with the name `GEMINI_API_KEY`
    5. Push your code to a GitHub repository
    6. Create a new Hugging Face Space, select Streamlit as the SDK, and connect your GitHub repository
    """)

# Bottom credits
st.sidebar.markdown("---")
st.sidebar.caption("Built with Streamlit and Gemini AI")