Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
%%writefile app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
import os
|
| 6 |
+
from groq import Groq
|
| 7 |
+
|
| 8 |
+
os.environ["GROQ_API_KEY"] = "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"
|
| 9 |
+
|
| 10 |
+
st.set_page_config(page_title="Personal Expense Analyzer", layout="wide")
|
| 11 |
+
st.title("π° Personal Expense Analyzer")
|
| 12 |
+
|
| 13 |
+
if "expenses" not in st.session_state:
|
| 14 |
+
st.session_state["expenses"] = pd.DataFrame(columns=["Name", "Description", "Date", "Amount"])
|
| 15 |
+
|
| 16 |
+
st.sidebar.header("β Add New Expense")
|
| 17 |
+
with st.sidebar.form("expense_form", clear_on_submit=True):
|
| 18 |
+
name = st.text_input("Expense Name")
|
| 19 |
+
desc = st.text_area("Description")
|
| 20 |
+
date = st.date_input("Date of Expense")
|
| 21 |
+
amount = st.number_input("Amount (SAR)", min_value=0.0, step=1.0)
|
| 22 |
+
submitted = st.form_submit_button("Add Expense")
|
| 23 |
+
|
| 24 |
+
if submitted:
|
| 25 |
+
if name and amount > 0:
|
| 26 |
+
new_exp = pd.DataFrame(
|
| 27 |
+
{"Name": [name], "Description": [desc], "Date": [pd.to_datetime(date)], "Amount": [amount]}
|
| 28 |
+
)
|
| 29 |
+
st.session_state["expenses"] = pd.concat([st.session_state["expenses"], new_exp], ignore_index=True)
|
| 30 |
+
st.success("Expense added successfully!")
|
| 31 |
+
else:
|
| 32 |
+
st.error("Please enter both name and amount.")
|
| 33 |
+
|
| 34 |
+
df = st.session_state["expenses"]
|
| 35 |
+
|
| 36 |
+
if not df.empty:
|
| 37 |
+
st.subheader("π Expense Summary")
|
| 38 |
+
st.dataframe(df.sort_values("Date", ascending=False), use_container_width=True)
|
| 39 |
+
|
| 40 |
+
col1, col2 = st.columns(2)
|
| 41 |
+
with col1:
|
| 42 |
+
st.markdown("### πΈ Expense Distribution (Pie Chart)")
|
| 43 |
+
fig1 = px.pie(df, names="Name", values="Amount", title="Expenses by Category", hole=0.4)
|
| 44 |
+
st.plotly_chart(fig1, use_container_width=True)
|
| 45 |
+
|
| 46 |
+
with col2:
|
| 47 |
+
st.markdown("### π Expense Trend (Bar Chart)")
|
| 48 |
+
df_sorted = df.groupby("Date")["Amount"].sum().reset_index()
|
| 49 |
+
fig2 = px.bar(df_sorted, x="Date", y="Amount", title="Daily Spending Trend")
|
| 50 |
+
st.plotly_chart(fig2, use_container_width=True)
|
| 51 |
+
|
| 52 |
+
st.markdown("## π§ Expense Insights (Powered by Groq Llama Model)")
|
| 53 |
+
if st.button("π Analyze My Spending"):
|
| 54 |
+
with st.spinner("Analyzing your expenses using Groq..."):
|
| 55 |
+
try:
|
| 56 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 57 |
+
summary_text = df.to_string(index=False)
|
| 58 |
+
prompt = f"""
|
| 59 |
+
Analyze the following expense records and provide:
|
| 60 |
+
1. Spending patterns (e.g., high spend periods or frequent categories)
|
| 61 |
+
2. Financial control recommendations
|
| 62 |
+
3. Suggestions for saving or budgeting
|
| 63 |
+
|
| 64 |
+
Expense data:
|
| 65 |
+
{summary_text}
|
| 66 |
+
"""
|
| 67 |
+
response = client.chat.completions.create(
|
| 68 |
+
messages=[
|
| 69 |
+
{"role": "system", "content": "You are a financial advisor analyzing personal expenses."},
|
| 70 |
+
{"role": "user", "content": prompt}
|
| 71 |
+
],
|
| 72 |
+
model="llama-3.3-70b-versatile"
|
| 73 |
+
)
|
| 74 |
+
result = response.choices[0].message.content
|
| 75 |
+
st.success("β
Analysis Complete!")
|
| 76 |
+
st.markdown(f"### πͺ AI Recommendations:\n{result}")
|
| 77 |
+
except Exception as e:
|
| 78 |
+
st.error(f"Error while analyzing: {e}")
|
| 79 |
+
else:
|
| 80 |
+
st.info("No expenses added yet. Use the sidebar to start adding your expenses!")
|
| 81 |
+
|
| 82 |
+
st.markdown("---")
|
| 83 |
+
st.caption("Built with β€οΈ using Streamlit + Groq LLM")
|