File size: 1,283 Bytes
2c359f1
f10ec56
 
 
 
2c359f1
f10ec56
4a7d41a
2c359f1
f10ec56
4a7d41a
2c359f1
 
 
f10ec56
2c359f1
 
4a7d41a
2c359f1
4a7d41a
 
f10ec56
2c359f1
 
4a7d41a
2c359f1
 
4a7d41a
3ce8ba3
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
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 spendin