File size: 3,220 Bytes
2c359f1 f10ec56 2c359f1 f10ec56 2c359f1 f10ec56 2c359f1 f10ec56 2c359f1 f10ec56 2c359f1 f10ec56 2c359f1 |
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 |
import gradio as gr
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from transformers import pipeline
import plotly.express as px
# Initialize the Hugging Face model for expense categorization
expense_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
# Function to categorize expenses
def categorize_transaction_batch(descriptions):
candidate_labels = ["Groceries", "Entertainment", "Rent", "Utilities", "Dining", "Transportation", "Shopping", "Others"]
return [expense_classifier(description, candidate_labels)["labels"][0] for description in descriptions]
# Function to process the uploaded CSV and generate visualizations
def process_expenses(file):
df = pd.read_csv(file.name)
# Check required columns
if 'Date' not in df.columns or 'Description' not in df.columns or 'Amount' not in df.columns:
return "CSV file should contain 'Date', 'Description', and 'Amount' columns."
# Categorize the expenses
df['Category'] = categorize_transaction_batch(df['Description'].tolist())
# Pie chart for Category-wise spending
category_spending = df.groupby("Category")['Amount'].sum()
fig1 = px.pie(category_spending, names=category_spending.index, values=category_spending.values, title="Category-wise Spending")
# Monthly spending trends (Line plot)
df['Date'] = pd.to_datetime(df['Date'])
df['Month'] = df['Date'].dt.to_period('M')
monthly_spending = df.groupby('Month')['Amount'].sum()
fig2 = px.line(monthly_spending, x=monthly_spending.index, y=monthly_spending.values, title="Monthly Spending Trends")
# Budget vs Actual Spending (Bar chart)
category_list = df['Category'].unique()
budget_dict = {category: 500 for category in category_list} # Set default budget of 500 for each category
budget_spending = {category: [budget_dict[category], category_spending.get(category, 0)] for category in category_list}
budget_df = pd.DataFrame(budget_spending, index=["Budget", "Actual"]).T
fig3 = px.bar(budget_df, x=budget_df.index, y=["Budget", "Actual"], title="Budget vs Actual Spending")
# Suggested savings
savings_tips = []
for category, actual in category_spending.items():
if actual > budget_dict.get(category, 500):
savings_tips.append(f"- **{category}**: Over budget by ${actual - budget_dict.get(category, 500)}. Consider reducing this expense.")
return df.head(), fig1, fig2, fig3, savings_tips
# Gradio interface definition
inputs = gr.inputs.File(label="Upload Expense CSV", type="file")
outputs = [
gr.outputs.Dataframe(label="Categorized Expense Data"),
gr.outputs.Plotly(label="Category-wise Spending (Pie Chart)"),
gr.outputs.Plotly(label="Monthly Spending Trends (Line Chart)"),
gr.outputs.Plotly(label="Budget vs Actual Spending (Bar Chart)"),
gr.outputs.Textbox(label="Savings Tips")
]
# Launch Gradio interface
gr.Interface(
fn=process_expenses,
inputs=inputs,
outputs=outputs,
live=True,
title="Smart Expense Tracker",
description="Upload your CSV of transactions, categorize them, and view insights like spending trends and budget analysis."
).launch()
|