Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from datetime import date
|
| 5 |
+
|
| 6 |
+
from llm_utils import analyze_expenses_with_llm
|
| 7 |
+
|
| 8 |
+
st.set_page_config(page_title="AI Expense Analyzer", layout="wide")
|
| 9 |
+
|
| 10 |
+
st.title("๐ธ AI Expense Analyzer")
|
| 11 |
+
st.write("Track expenses and get AI-powered insights using Grok")
|
| 12 |
+
|
| 13 |
+
# -------------------------
|
| 14 |
+
# Session State
|
| 15 |
+
# -------------------------
|
| 16 |
+
if "expenses" not in st.session_state:
|
| 17 |
+
st.session_state.expenses = pd.DataFrame(
|
| 18 |
+
columns=["Date", "Category", "Amount", "Description"]
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# -------------------------
|
| 22 |
+
# Expense Form
|
| 23 |
+
# -------------------------
|
| 24 |
+
st.subheader("โ Add Expense")
|
| 25 |
+
|
| 26 |
+
with st.form("expense_form"):
|
| 27 |
+
expense_date = st.date_input("Date", value=date.today())
|
| 28 |
+
|
| 29 |
+
category = st.selectbox(
|
| 30 |
+
"Category",
|
| 31 |
+
["Food", "Transport", "Shopping", "Bills", "Entertainment", "Other"]
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
amount = st.number_input("Amount", min_value=0.0, step=1.0)
|
| 35 |
+
|
| 36 |
+
description = st.text_input("Description")
|
| 37 |
+
|
| 38 |
+
submitted = st.form_submit_button("Add Expense")
|
| 39 |
+
|
| 40 |
+
if submitted:
|
| 41 |
+
new_row = {
|
| 42 |
+
"Date": expense_date,
|
| 43 |
+
"Category": category,
|
| 44 |
+
"Amount": amount,
|
| 45 |
+
"Description": description,
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
st.session_state.expenses = pd.concat(
|
| 49 |
+
[st.session_state.expenses, pd.DataFrame([new_row])],
|
| 50 |
+
ignore_index=True
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
st.success("Expense added successfully!")
|
| 54 |
+
|
| 55 |
+
# -------------------------
|
| 56 |
+
# Expense Table
|
| 57 |
+
# -------------------------
|
| 58 |
+
st.subheader("๐ Expense History")
|
| 59 |
+
st.dataframe(st.session_state.expenses, use_container_width=True)
|
| 60 |
+
|
| 61 |
+
if st.session_state.expenses.empty:
|
| 62 |
+
st.info("Add expenses to see charts and AI analysis.")
|
| 63 |
+
st.stop()
|
| 64 |
+
|
| 65 |
+
# -------------------------
|
| 66 |
+
# Charts
|
| 67 |
+
# -------------------------
|
| 68 |
+
st.subheader("๐ Expense Analysis")
|
| 69 |
+
|
| 70 |
+
col1, col2 = st.columns(2)
|
| 71 |
+
|
| 72 |
+
with col1:
|
| 73 |
+
category_sum = st.session_state.expenses.groupby("Category")["Amount"].sum()
|
| 74 |
+
fig, ax = plt.subplots()
|
| 75 |
+
category_sum.plot(kind="bar", ax=ax)
|
| 76 |
+
st.pyplot(fig)
|
| 77 |
+
|
| 78 |
+
with col2:
|
| 79 |
+
fig2, ax2 = plt.subplots()
|
| 80 |
+
ax2.pie(category_sum, labels=category_sum.index, autopct="%1.1f%%")
|
| 81 |
+
ax2.axis("equal")
|
| 82 |
+
st.pyplot(fig2)
|
| 83 |
+
|
| 84 |
+
# -------------------------
|
| 85 |
+
# Trend Chart
|
| 86 |
+
# -------------------------
|
| 87 |
+
st.write("### Spending Trend")
|
| 88 |
+
trend_df = st.session_state.expenses.copy()
|
| 89 |
+
trend_df["Date"] = pd.to_datetime(trend_df["Date"])
|
| 90 |
+
trend_df = trend_df.groupby("Date")["Amount"].sum()
|
| 91 |
+
|
| 92 |
+
fig3, ax3 = plt.subplots()
|
| 93 |
+
ax3.plot(trend_df.index, trend_df.values, marker="o")
|
| 94 |
+
st.pyplot(fig3)
|
| 95 |
+
|
| 96 |
+
# -------------------------
|
| 97 |
+
# AI Analysis
|
| 98 |
+
# -------------------------
|
| 99 |
+
st.subheader("๐ค AI Expense Insights")
|
| 100 |
+
|
| 101 |
+
if st.button("Analyze My Expenses"):
|
| 102 |
+
with st.spinner("Analyzing your expenses..."):
|
| 103 |
+
insights = analyze_expenses_with_llm(st.session_state.expenses)
|
| 104 |
+
st.markdown(insights)
|