engralimalik's picture
Update app.py
4a7d41a verified
raw
history blame
2.29 kB
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 (use zero-shot classification)
expense_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
# Batch categorization function for efficiency
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):
# Read CSV data
df = pd.read_csv(file.name)
# Check if required columns are present
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 (using batch processing to minimize model calls)
df['Category'] = categorize_transaction_batch(df['Description'].tolist())
# Create visualizations:
# 1. 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")
# 2. 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")
# 3. Budget vs Actual Spending (Bar chart)
category_list = df['Category'].unique()
budget_dict = {category: 500 for category in category_list} # Default budget is 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=