Spaces:
Build error
Build error
File size: 2,845 Bytes
cd3c548 | 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 | import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from datetime import date
from llm_utils import analyze_expenses_with_llm
st.set_page_config(page_title="AI Expense Analyzer", layout="wide")
st.title("πΈ AI Expense Analyzer")
st.write("Track expenses and get AI-powered insights using Grok")
# -------------------------
# Session State
# -------------------------
if "expenses" not in st.session_state:
st.session_state.expenses = pd.DataFrame(
columns=["Date", "Category", "Amount", "Description"]
)
# -------------------------
# Expense Form
# -------------------------
st.subheader("β Add Expense")
with st.form("expense_form"):
expense_date = st.date_input("Date", value=date.today())
category = st.selectbox(
"Category",
["Food", "Transport", "Shopping", "Bills", "Entertainment", "Other"]
)
amount = st.number_input("Amount", min_value=0.0, step=1.0)
description = st.text_input("Description")
submitted = st.form_submit_button("Add Expense")
if submitted:
new_row = {
"Date": expense_date,
"Category": category,
"Amount": amount,
"Description": description,
}
st.session_state.expenses = pd.concat(
[st.session_state.expenses, pd.DataFrame([new_row])],
ignore_index=True
)
st.success("Expense added successfully!")
# -------------------------
# Expense Table
# -------------------------
st.subheader("π Expense History")
st.dataframe(st.session_state.expenses, use_container_width=True)
if st.session_state.expenses.empty:
st.info("Add expenses to see charts and AI analysis.")
st.stop()
# -------------------------
# Charts
# -------------------------
st.subheader("π Expense Analysis")
col1, col2 = st.columns(2)
with col1:
category_sum = st.session_state.expenses.groupby("Category")["Amount"].sum()
fig, ax = plt.subplots()
category_sum.plot(kind="bar", ax=ax)
st.pyplot(fig)
with col2:
fig2, ax2 = plt.subplots()
ax2.pie(category_sum, labels=category_sum.index, autopct="%1.1f%%")
ax2.axis("equal")
st.pyplot(fig2)
# -------------------------
# Trend Chart
# -------------------------
st.write("### Spending Trend")
trend_df = st.session_state.expenses.copy()
trend_df["Date"] = pd.to_datetime(trend_df["Date"])
trend_df = trend_df.groupby("Date")["Amount"].sum()
fig3, ax3 = plt.subplots()
ax3.plot(trend_df.index, trend_df.values, marker="o")
st.pyplot(fig3)
# -------------------------
# AI Analysis
# -------------------------
st.subheader("π€ AI Expense Insights")
if st.button("Analyze My Expenses"):
with st.spinner("Analyzing your expenses..."):
insights = analyze_expenses_with_llm(st.session_state.expenses)
st.markdown(insights)
|