engralimalik commited on
Commit
c7d0bb8
·
verified ·
1 Parent(s): a4cb248

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -80
app.py CHANGED
@@ -1,93 +1,32 @@
1
- import streamlit as st
2
  import pandas as pd
3
  import matplotlib.pyplot as plt
4
- from transformers import pipeline
5
  import plotly.express as px
 
 
6
 
7
- # Initialize Hugging Face model pipelines for categorization and question-answering
8
- expense_classifier = pipeline('zero-shot-classification', model='distilbert-base-uncased')
9
- qa_model = pipeline('question-answering', model='distilbert-base-uncased-distilled-squad')
10
-
11
- # List of possible expense categories
12
- categories = ["Groceries", "Rent", "Utilities", "Entertainment", "Dining", "Transportation", "Salary"]
13
-
14
- # Function to categorize transactions
15
- def categorize_expense(description):
16
- result = expense_classifier(description, candidate_labels=categories)
17
- return result['labels'][0] # Choose the most probable category
18
-
19
- # Streamlit UI
20
- st.title("Smart Expense Tracker")
21
- st.write("""
22
- Upload a CSV file containing your transaction data, and this app will categorize your expenses, visualize trends,
23
- and provide insights on your spending habits.
24
- """)
25
 
26
- # Upload CSV file
27
- uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
28
  if uploaded_file is not None:
 
29
  df = pd.read_csv(uploaded_file)
30
 
31
- # Display the first few rows of the data
32
- st.subheader("Transaction Data")
33
- st.dataframe(df.head())
34
-
35
- # Check if 'Description', 'Amount', and 'Date' columns exist in the file
36
- if 'Description' in df.columns and 'Amount' in df.columns and 'Date' in df.columns:
37
- # Categorize expenses
38
- df['Category'] = df['Description'].apply(categorize_expense)
39
-
40
- # Visualizations
41
- st.subheader("Expense Distribution by Category (Pie Chart)")
42
- category_expenses = df.groupby('Category')['Amount'].sum()
43
- fig1 = px.pie(category_expenses, names=category_expenses.index, values=category_expenses.values, title="Category-wise Spending")
44
- st.plotly_chart(fig1)
45
 
46
- # Monthly spending trends (Line Chart)
47
- df['Date'] = pd.to_datetime(df['Date'])
48
- df['Month'] = df['Date'].dt.to_period('M')
49
- monthly_expenses = df.groupby('Month')['Amount'].sum()
50
- st.subheader("Monthly Spending Trends")
51
- fig2 = px.line(monthly_expenses, x=monthly_expenses.index, y=monthly_expenses.values, title="Monthly Spending")
52
- st.plotly_chart(fig2)
53
 
54
- # Budget vs Actual Spending (Bar Chart)
55
- budgets = {
56
- "Groceries": 300,
57
- "Rent": 1000,
58
- "Utilities": 150,
59
- "Entertainment": 100,
60
- "Dining": 150,
61
- "Transportation": 120,
62
- }
63
- budget_df = pd.DataFrame({
64
- 'Actual': monthly_expenses,
65
- 'Budget': [sum(budgets.values())] * len(monthly_expenses)
66
- })
67
- st.subheader("Monthly Spending vs Budget")
68
- fig3 = px.bar(budget_df, x=budget_df.index, y=["Actual", "Budget"], title="Budget vs Actual Spending")
69
- st.plotly_chart(fig3)
70
 
71
- # Savings Tips (Alert if exceeding budget)
72
- st.subheader("Savings Tips")
73
- savings_tips = []
74
- category_expenses = df.groupby("Category")['Amount'].sum()
75
- for category, actual in category_expenses.items():
76
- if actual > budgets.get(category, 0):
77
- savings_tips.append(f"- **{category}**: Over budget by ${actual - budgets.get(category, 0)}. Consider reducing this expense.")
78
- if savings_tips:
79
- for tip in savings_tips:
80
- st.write(tip)
81
- else:
82
- st.write("No categories exceeded their budget.")
83
 
84
- # Question Answering Feature
85
- st.subheader("Ask Questions About Your Expenses")
86
- question = st.text_input("Ask a question about your expenses (e.g., 'How much did I spend on groceries?')")
87
- if question:
88
- knowledge_base = "\n".join(df.apply(lambda row: f"Description: {row['Description']}, Amount: {row['Amount']}, Category: {row['Category']}", axis=1))
89
- answer = qa_model(question=question, context=knowledge_base)
90
- st.write(f"Answer: {answer['answer']}")
91
 
92
- else:
93
- st.error("CSV file should contain 'Description', 'Amount', and 'Date' columns.")
 
 
1
  import pandas as pd
2
  import matplotlib.pyplot as plt
 
3
  import plotly.express as px
4
+ import streamlit as st
5
+ from transformers import pipeline
6
 
7
+ # Upload CSV file containing transaction data
8
+ uploaded_file = st.file_uploader("Upload Expense CSV", type="csv")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
 
 
10
  if uploaded_file is not None:
11
+ # Load the file into a DataFrame
12
  df = pd.read_csv(uploaded_file)
13
 
14
+ # Display the first few rows of the dataset
15
+ st.write("First few rows of the dataset:", df.head())
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ # Initialize Hugging Face's zero-shot text classification model
18
+ model_name = 'distilbert-base-uncased'
19
+ classifier = pipeline('zero-shot-classification', model=model_name)
 
 
 
 
20
 
21
+ # List of possible expense categories
22
+ categories = ["Groceries", "Rent", "Utilities", "Entertainment", "Dining", "Transportation", "Salary"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ # Function to classify transaction descriptions into categories
25
+ def categorize_expense(description):
26
+ result = classifier(description, candidate_labels=categories)
27
+ return result['labels'][0] # Choose the most probable category
 
 
 
 
 
 
 
 
28
 
29
+ # Apply the categorization function to the 'Description' column in the dataset
30
+ df['Category'] = df['Description'].apply(categorize_expense)
 
 
 
 
 
31
 
32
+ # Show the ca