Spaces:
Sleeping
Sleeping
File size: 3,445 Bytes
048ae63 33d7858 eabe57c 33d7858 |
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 |
import streamlit as st
import pandas as pd
import plotly.express as px
import os
from groq import Groq
os.environ["GROQ_API_KEY"] = "Put your key here"
st.set_page_config(page_title="Personal Expense Analyzer", layout="wide")
st.title("π° Personal Expense Analyzer")
if "expenses" not in st.session_state:
st.session_state["expenses"] = pd.DataFrame(columns=["Name", "Description", "Date", "Amount"])
st.sidebar.header("β Add New Expense")
with st.sidebar.form("expense_form", clear_on_submit=True):
name = st.text_input("Expense Name")
desc = st.text_area("Description")
date = st.date_input("Date of Expense")
amount = st.number_input("Amount (SAR)", min_value=0.0, step=1.0)
submitted = st.form_submit_button("Add Expense")
if submitted:
if name and amount > 0:
new_exp = pd.DataFrame(
{"Name": [name], "Description": [desc], "Date": [pd.to_datetime(date)], "Amount": [amount]}
)
st.session_state["expenses"] = pd.concat([st.session_state["expenses"], new_exp], ignore_index=True)
st.success("Expense added successfully!")
else:
st.error("Please enter both name and amount.")
df = st.session_state["expenses"]
if not df.empty:
st.subheader("π Expense Summary")
st.dataframe(df.sort_values("Date", ascending=False), use_container_width=True)
col1, col2 = st.columns(2)
with col1:
st.markdown("### πΈ Expense Distribution (Pie Chart)")
fig1 = px.pie(df, names="Name", values="Amount", title="Expenses by Category", hole=0.4)
st.plotly_chart(fig1, use_container_width=True)
with col2:
st.markdown("### π Expense Trend (Bar Chart)")
df_sorted = df.groupby("Date")["Amount"].sum().reset_index()
fig2 = px.bar(df_sorted, x="Date", y="Amount", title="Daily Spending Trend")
st.plotly_chart(fig2, use_container_width=True)
st.markdown("## π§ Expense Insights (Powered by Groq Llama Model)")
if st.button("π Analyze My Spending"):
with st.spinner("Analyzing your expenses using Groq..."):
try:
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
summary_text = df.to_string(index=False)
prompt = f"""
Analyze the following expense records and provide:
1. Spending patterns (e.g., high spend periods or frequent categories)
2. Financial control recommendations
3. Suggestions for saving or budgeting
Expense data:
{summary_text}
"""
response = client.chat.completions.create(
messages=[
{"role": "system", "content": "You are a financial advisor analyzing personal expenses."},
{"role": "user", "content": prompt}
],
model="llama-3.3-70b-versatile"
)
result = response.choices[0].message.content
st.success("β
Analysis Complete!")
st.markdown(f"### πͺ AI Recommendations:\n{result}")
except Exception as e:
st.error(f"Error while analyzing: {e}")
else:
st.info("No expenses added yet. Use the sidebar to start adding your expenses!")
st.markdown("---")
st.caption("Built with β€οΈ using Streamlit + Groq LLM")
|