engralimalik commited on
Commit
a477cd2
·
verified ·
1 Parent(s): 1fa102f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -24
app.py CHANGED
@@ -32,58 +32,54 @@ if uploaded_file is not None:
32
  expense_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
33
 
34
  # Function to categorize transactions
35
- def categorize_transaction(description):
36
  candidate_labels = ["Groceries", "Entertainment", "Rent", "Utilities", "Dining", "Transportation", "Shopping", "Others"]
37
- result = expense_classifier(description, candidate_labels)
38
- return result["labels"][0]
39
 
40
- # Apply categorization to the descriptions
41
- df['Category'] = df['Description'].apply(categorize_transaction)
42
 
43
  # Show categorized data
44
  st.write("### Categorized Expense Data", df.head())
45
 
46
  # Visualizations
47
 
48
- # Pie chart for Category-wise spending
49
  category_spending = df.groupby("Category")['Amount'].sum()
50
  st.write("### Category-wise Spending")
51
- fig, ax = plt.subplots()
52
- category_spending.plot(kind='pie', autopct='%1.1f%%', ax=ax, figsize=(8, 8))
53
  ax.set_ylabel('')
54
- st.pyplot(fig) # Explicitly call st.pyplot for matplotlib images
55
 
56
- # Monthly spending trends (Line plot)
57
  df['Date'] = pd.to_datetime(df['Date'])
58
  df['Month'] = df['Date'].dt.to_period('M')
59
  monthly_spending = df.groupby('Month')['Amount'].sum()
60
 
61
  st.write("### Monthly Spending Trends")
62
- fig, ax = plt.subplots()
63
- monthly_spending.plot(kind='line', ax=ax, figsize=(10, 6))
64
  ax.set_ylabel('Amount ($)')
65
  ax.set_xlabel('Month')
66
  ax.set_title('Monthly Spending Trends')
67
- st.pyplot(fig) # Explicitly call st.pyplot for matplotlib images
68
 
69
- # Budget Tracker
70
  st.sidebar.header("Budget Tracker")
71
  category_list = df['Category'].unique()
72
- budget_dict = {}
73
 
74
- for category in category_list:
75
- budget_dict[category] = st.sidebar.number_input(f"Set budget for {category}", min_value=0, value=500)
76
-
77
- # Budget vs Actual Spending (Bar chart)
78
  st.write("### Budget vs Actual Spending")
79
- budget_spending = {category: [budget_dict[category], category_spending.get(category, 0)] for category in category_list}
80
-
81
  budget_df = pd.DataFrame(budget_spending, index=["Budget", "Actual"]).T
82
- fig, ax = plt.subplots()
83
- budget_df.plot(kind='bar', ax=ax, figsize=(10, 6))
 
84
  ax.set_ylabel('Amount ($)')
85
  ax.set_title('Budget vs Actual Spending')
86
- st.pyplot(fig) # Explicitly call st.pyplot for matplotlib images
87
 
88
  # Suggestions for saving
89
  st.write("### Suggested Savings Tips")
 
32
  expense_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
33
 
34
  # Function to categorize transactions
35
+ def categorize_transaction_batch(descriptions):
36
  candidate_labels = ["Groceries", "Entertainment", "Rent", "Utilities", "Dining", "Transportation", "Shopping", "Others"]
37
+ return [expense_classifier(description, candidate_labels)["labels"][0] for description in descriptions]
 
38
 
39
+ # Batch process all descriptions at once for efficiency
40
+ df['Category'] = categorize_transaction_batch(df['Description'].tolist())
41
 
42
  # Show categorized data
43
  st.write("### Categorized Expense Data", df.head())
44
 
45
  # Visualizations
46
 
47
+ # Optimized Pie chart for Category-wise spending
48
  category_spending = df.groupby("Category")['Amount'].sum()
49
  st.write("### Category-wise Spending")
50
+ fig, ax = plt.subplots(figsize=(8, 8))
51
+ category_spending.plot(kind='pie', autopct='%1.1f%%', ax=ax)
52
  ax.set_ylabel('')
53
+ st.pyplot(fig)
54
 
55
+ # Optimized Monthly spending trends (Line plot)
56
  df['Date'] = pd.to_datetime(df['Date'])
57
  df['Month'] = df['Date'].dt.to_period('M')
58
  monthly_spending = df.groupby('Month')['Amount'].sum()
59
 
60
  st.write("### Monthly Spending Trends")
61
+ fig, ax = plt.subplots(figsize=(10, 6))
62
+ monthly_spending.plot(kind='line', ax=ax)
63
  ax.set_ylabel('Amount ($)')
64
  ax.set_xlabel('Month')
65
  ax.set_title('Monthly Spending Trends')
66
+ st.pyplot(fig)
67
 
68
+ # Optimized Budget Tracker
69
  st.sidebar.header("Budget Tracker")
70
  category_list = df['Category'].unique()
71
+ budget_dict = {category: st.sidebar.number_input(f"Set budget for {category}", min_value=0, value=500) for category in category_list}
72
 
73
+ # Optimized Budget vs Actual Spending (Bar chart)
 
 
 
74
  st.write("### Budget vs Actual Spending")
75
+ budget_spending = {category: [budget_dict.get(category, 500), category_spending.get(category, 0)] for category in category_list}
 
76
  budget_df = pd.DataFrame(budget_spending, index=["Budget", "Actual"]).T
77
+
78
+ fig, ax = plt.subplots(figsize=(10, 6))
79
+ budget_df.plot(kind='bar', ax=ax)
80
  ax.set_ylabel('Amount ($)')
81
  ax.set_title('Budget vs Actual Spending')
82
+ st.pyplot(fig)
83
 
84
  # Suggestions for saving
85
  st.write("### Suggested Savings Tips")