Update app.py
Browse files
app.py
CHANGED
|
@@ -5,66 +5,40 @@ import matplotlib.pyplot as plt
|
|
| 5 |
from transformers import pipeline
|
| 6 |
import plotly.express as px
|
| 7 |
|
| 8 |
-
# Initialize the Hugging Face model for expense categorization
|
| 9 |
expense_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
def categorize_transaction_batch(descriptions):
|
| 13 |
candidate_labels = ["Groceries", "Entertainment", "Rent", "Utilities", "Dining", "Transportation", "Shopping", "Others"]
|
| 14 |
return [expense_classifier(description, candidate_labels)["labels"][0] for description in descriptions]
|
| 15 |
|
| 16 |
# Function to process the uploaded CSV and generate visualizations
|
| 17 |
def process_expenses(file):
|
|
|
|
| 18 |
df = pd.read_csv(file.name)
|
| 19 |
-
|
| 20 |
-
# Check required columns
|
| 21 |
if 'Date' not in df.columns or 'Description' not in df.columns or 'Amount' not in df.columns:
|
| 22 |
return "CSV file should contain 'Date', 'Description', and 'Amount' columns."
|
| 23 |
|
| 24 |
-
# Categorize the expenses
|
| 25 |
df['Category'] = categorize_transaction_batch(df['Description'].tolist())
|
| 26 |
|
| 27 |
-
#
|
|
|
|
| 28 |
category_spending = df.groupby("Category")['Amount'].sum()
|
| 29 |
fig1 = px.pie(category_spending, names=category_spending.index, values=category_spending.values, title="Category-wise Spending")
|
| 30 |
|
| 31 |
-
# Monthly spending trends (Line plot)
|
| 32 |
df['Date'] = pd.to_datetime(df['Date'])
|
| 33 |
df['Month'] = df['Date'].dt.to_period('M')
|
| 34 |
monthly_spending = df.groupby('Month')['Amount'].sum()
|
| 35 |
fig2 = px.line(monthly_spending, x=monthly_spending.index, y=monthly_spending.values, title="Monthly Spending Trends")
|
| 36 |
|
| 37 |
-
# Budget vs Actual Spending (Bar chart)
|
| 38 |
category_list = df['Category'].unique()
|
| 39 |
-
budget_dict = {category: 500 for category in category_list} #
|
| 40 |
budget_spending = {category: [budget_dict[category], category_spending.get(category, 0)] for category in category_list}
|
| 41 |
budget_df = pd.DataFrame(budget_spending, index=["Budget", "Actual"]).T
|
| 42 |
-
fig3 = px.bar(budget_df, x=budget_df.index, y=["Budget", "Actual"], title=
|
| 43 |
-
|
| 44 |
-
# Suggested savings
|
| 45 |
-
savings_tips = []
|
| 46 |
-
for category, actual in category_spending.items():
|
| 47 |
-
if actual > budget_dict.get(category, 500):
|
| 48 |
-
savings_tips.append(f"- **{category}**: Over budget by ${actual - budget_dict.get(category, 500)}. Consider reducing this expense.")
|
| 49 |
-
|
| 50 |
-
return df.head(), fig1, fig2, fig3, savings_tips
|
| 51 |
-
|
| 52 |
-
# Gradio interface definition
|
| 53 |
-
inputs = gr.File(label="Upload Expense CSV")
|
| 54 |
-
outputs = [
|
| 55 |
-
gr.Dataframe(label="Categorized Expense Data"),
|
| 56 |
-
gr.Plot(label="Category-wise Spending (Pie Chart)"),
|
| 57 |
-
gr.Plot(label="Monthly Spending Trends (Line Chart)"),
|
| 58 |
-
gr.Plot(label="Budget vs Actual Spending (Bar Chart)"),
|
| 59 |
-
gr.Textbox(label="Savings Tips")
|
| 60 |
-
]
|
| 61 |
-
|
| 62 |
-
# Launch Gradio interface
|
| 63 |
-
gr.Interface(
|
| 64 |
-
fn=process_expenses,
|
| 65 |
-
inputs=inputs,
|
| 66 |
-
outputs=outputs,
|
| 67 |
-
live=True,
|
| 68 |
-
title="Smart Expense Tracker",
|
| 69 |
-
description="Upload your CSV of transactions, categorize them, and view insights like spending trends and budget analysis."
|
| 70 |
-
).launch() # Correct method name: launch() instead of launc
|
|
|
|
| 5 |
from transformers import pipeline
|
| 6 |
import plotly.express as px
|
| 7 |
|
| 8 |
+
# Initialize the Hugging Face model for expense categorization (use zero-shot classification)
|
| 9 |
expense_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 10 |
|
| 11 |
+
# Batch categorization function for efficiency
|
| 12 |
def categorize_transaction_batch(descriptions):
|
| 13 |
candidate_labels = ["Groceries", "Entertainment", "Rent", "Utilities", "Dining", "Transportation", "Shopping", "Others"]
|
| 14 |
return [expense_classifier(description, candidate_labels)["labels"][0] for description in descriptions]
|
| 15 |
|
| 16 |
# Function to process the uploaded CSV and generate visualizations
|
| 17 |
def process_expenses(file):
|
| 18 |
+
# Read CSV data
|
| 19 |
df = pd.read_csv(file.name)
|
| 20 |
+
|
| 21 |
+
# Check if required columns are present
|
| 22 |
if 'Date' not in df.columns or 'Description' not in df.columns or 'Amount' not in df.columns:
|
| 23 |
return "CSV file should contain 'Date', 'Description', and 'Amount' columns."
|
| 24 |
|
| 25 |
+
# Categorize the expenses (using batch processing to minimize model calls)
|
| 26 |
df['Category'] = categorize_transaction_batch(df['Description'].tolist())
|
| 27 |
|
| 28 |
+
# Create visualizations:
|
| 29 |
+
# 1. Pie chart for Category-wise spending
|
| 30 |
category_spending = df.groupby("Category")['Amount'].sum()
|
| 31 |
fig1 = px.pie(category_spending, names=category_spending.index, values=category_spending.values, title="Category-wise Spending")
|
| 32 |
|
| 33 |
+
# 2. Monthly spending trends (Line plot)
|
| 34 |
df['Date'] = pd.to_datetime(df['Date'])
|
| 35 |
df['Month'] = df['Date'].dt.to_period('M')
|
| 36 |
monthly_spending = df.groupby('Month')['Amount'].sum()
|
| 37 |
fig2 = px.line(monthly_spending, x=monthly_spending.index, y=monthly_spending.values, title="Monthly Spending Trends")
|
| 38 |
|
| 39 |
+
# 3. Budget vs Actual Spending (Bar chart)
|
| 40 |
category_list = df['Category'].unique()
|
| 41 |
+
budget_dict = {category: 500 for category in category_list} # Default budget is 500 for each category
|
| 42 |
budget_spending = {category: [budget_dict[category], category_spending.get(category, 0)] for category in category_list}
|
| 43 |
budget_df = pd.DataFrame(budget_spending, index=["Budget", "Actual"]).T
|
| 44 |
+
fig3 = px.bar(budget_df, x=budget_df.index, y=["Budget", "Actual"], title=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|