import streamlit as st import pandas as pd import plotly.express as px from datetime import datetime, date from transformers import pipeline # Initialize sentiment analysis pipeline (Hugging Face) classifier = pipeline("zero-shot-classification") # Streamlit App st.title("Personal Finance Analysis") # User Input st.subheader("Enter Transaction Details:") date_input = st.date_input("Date") amount = st.number_input("Amount", min_value=0.0) category = st.selectbox("Category", ["Groceries", "Rent", "Entertainment", "Utilities", "Other"]) description = st.text_input("Description (Optional)") # Add description field # Spending Limit Input if "monthly_limit" not in st.session_state: st.session_state.monthly_limit = 0.0 # Initialize monthly limit monthly_limit = st.number_input("Set Monthly Spending Limit", min_value=0.0, value=st.session_state.monthly_limit) st.session_state.monthly_limit = monthly_limit # Update the session state # Add Transaction Button if st.button("Add Transaction"): transaction = {"category": category, "date": str(date_input), "amount": amount, "description": description} if "transactions" not in st.session_state: st.session_state.transactions = [] st.session_state.transactions.append(transaction) st.success("Transaction added successfully!") # Display Transactions if "transactions" in st.session_state and st.session_state.transactions: st.subheader("Transaction History:") df = pd.DataFrame(st.session_state.transactions) st.dataframe(df) # Data Analysis and Charts st.subheader("Data Analysis:") # Convert date to datetime for proper sorting and analysis df['date'] = pd.to_datetime(df['date']) df['month'] = df['date'].dt.to_period('M') # Add month column for analysis # Chart 1: Spending by Category (Pie Chart) category_spending = df.groupby("category")["amount"].sum().reset_index() fig1 = px.pie(category_spending, values="amount", names="category", title="Spending by Category") st.plotly_chart(fig1) # Chart 2: Spending Over Time (Line Chart) daily_spending = df.groupby("date")["amount"].sum().reset_index() fig2 = px.line(daily_spending, x="date", y="amount", title="Spending Over Time") st.plotly_chart(fig2) # Chart 3: Spending by Category (Bar Chart) fig3 = px.bar(category_spending, x="category", y="amount", title="Spending by Category (Bar)") st.plotly_chart(fig3) # Chart 4: Average Spending per Category average_spending = df.groupby('category')['amount'].mean().reset_index() fig4 = px.bar(average_spending, x='category', y='amount', title='Average Spending per Category') st.plotly_chart(fig4) # Monthly Spending and Limit Check current_month = pd.Timestamp.now().to_period('M') monthly_spent = df[df['month'] == current_month]['amount'].sum() st.subheader("Monthly Spending Summary") st.write(f"Current Month: {current_month}") st.write(f"Total Spent This Month: {monthly_spent:.2f}") st.write(f"Monthly Limit: {monthly_limit:.2f}") if monthly_limit > 0: # Check to avoid division by zero percentage_spent = (monthly_spent / monthly_limit) * 100 st.write(f"Percentage of Limit Used: {percentage_spent:.2f}%") if monthly_spent > monthly_limit: st.warning(f"You have exceeded your monthly spending limit! ({monthly_spent:.2f} / {monthly_limit:.2f})") elif percentage_spent > 80: # Example threshold (80%) st.info("You are approaching your monthly spending limit.") # Sentiment Analysis (Example) if description: # Only perform if the user provided one candidate_labels = ["positive", "negative", "neutral"] classified = classifier(description, candidate_labels) st.write("Sentiment Analysis of Description:", classified)