Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| from groq import Groq | |
| from datetime import datetime | |
| # ------------------------------- | |
| # Groq Client | |
| # ------------------------------- | |
| client = Groq( | |
| api_key=os.environ.get("GROQ_API_KEY") | |
| ) | |
| # ------------------------------- | |
| # Session State | |
| # ------------------------------- | |
| def init_session(): | |
| return pd.DataFrame(columns=["Date", "Category", "Description", "Amount"]) | |
| # ------------------------------- | |
| # Add Expense | |
| # ------------------------------- | |
| def add_expense(date, category, description, amount, df): | |
| if df is None: | |
| df = init_session() | |
| new_row = { | |
| "Date": date, | |
| "Category": category, | |
| "Description": description, | |
| "Amount": float(amount) | |
| } | |
| df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True) | |
| return df, df | |
| # ------------------------------- | |
| # Visualization | |
| # ------------------------------- | |
| def visualize(df, chart_type): | |
| if df is None or df.empty: | |
| return None | |
| plt.figure() | |
| if chart_type == "Line Chart": | |
| df.groupby("Date")["Amount"].sum().plot(kind="line") | |
| elif chart_type == "Bar Chart": | |
| df.groupby("Category")["Amount"].sum().plot(kind="bar") | |
| elif chart_type == "Column Chart": | |
| df.groupby("Category")["Amount"].sum().plot(kind="bar") | |
| elif chart_type == "Pie Chart": | |
| df.groupby("Category")["Amount"].sum().plot(kind="pie", autopct="%1.1f%%") | |
| plt.title("Expense Analysis") | |
| plt.ylabel("Amount") | |
| plt.tight_layout() | |
| return plt.gcf() | |
| # ------------------------------- | |
| # AI Analysis using Groq | |
| # ------------------------------- | |
| def ai_analysis(df): | |
| if df is None or df.empty: | |
| return "No expenses available for analysis." | |
| summary = df.groupby("Category")["Amount"].sum().to_dict() | |
| total = df["Amount"].sum() | |
| prompt = f""" | |
| You are an AI financial assistant. | |
| Analyze the following expense data: | |
| Category-wise Expenses: {summary} | |
| Total Expenses: {total} | |
| Provide: | |
| 1. Spending insights | |
| 2. Cost-saving suggestions | |
| 3. Any unusual spending patterns | |
| """ | |
| chat_completion = client.chat.completions.create( | |
| messages=[ | |
| {"role": "user", "content": prompt} | |
| ], | |
| model="llama-3.3-70b-versatile", | |
| ) | |
| return chat_completion.choices[0].message.content | |
| # ------------------------------- | |
| # Gradio UI | |
| # ------------------------------- | |
| with gr.Blocks(title="AI Expense Analyzer") as demo: | |
| gr.Markdown("# π° AI Expense Analyzer") | |
| gr.Markdown("Track expenses, visualize data, and get AI-powered insights.") | |
| df_state = gr.State(init_session()) | |
| with gr.Row(): | |
| date = gr.Textbox(label="Date", value=str(datetime.today().date())) | |
| category = gr.Textbox(label="Category (Food, Rent, Travel, etc.)") | |
| description = gr.Textbox(label="Description") | |
| amount = gr.Number(label="Amount") | |
| add_btn = gr.Button("β Add Expense") | |
| expense_table = gr.Dataframe( | |
| headers=["Date", "Category", "Description", "Amount"], | |
| interactive=False | |
| ) | |
| add_btn.click( | |
| add_expense, | |
| inputs=[date, category, description, amount, df_state], | |
| outputs=[df_state, expense_table] | |
| ) | |
| gr.Markdown("## π Visualization") | |
| chart_type = gr.Dropdown( | |
| ["Line Chart", "Bar Chart", "Column Chart", "Pie Chart"], | |
| value="Bar Chart" | |
| ) | |
| chart_btn = gr.Button("Generate Chart") | |
| chart_output = gr.Plot() | |
| chart_btn.click( | |
| visualize, | |
| inputs=[df_state, chart_type], | |
| outputs=chart_output | |
| ) | |
| gr.Markdown("## π€ AI Expense Analysis") | |
| ai_btn = gr.Button("Analyze with AI") | |
| ai_output = gr.Textbox(lines=12) | |
| ai_btn.click( | |
| ai_analysis, | |
| inputs=df_state, | |
| outputs=ai_output | |
| ) | |
| demo.launch() | |