|
import streamlit as st |
|
import pdfplumber |
|
import pandas as pd |
|
|
|
|
|
def classify_transaction(description): |
|
if not isinstance(description, str): |
|
return "Unknown" |
|
|
|
categories = { |
|
"Grocery": ["Walmart", "Kroger", "Whole Foods"], |
|
"Dining": ["McDonald's", "Starbucks", "Chipotle"], |
|
"Bills": ["Verizon", "AT&T", "Con Edison"], |
|
"Entertainment": ["Netflix", "Spotify", "Amazon Prime"], |
|
"Transport": ["Uber", "Lyft", "MetroCard"], |
|
} |
|
|
|
for category, keywords in categories.items(): |
|
if any(keyword in description for keyword in keywords): |
|
return category |
|
return "Other" |
|
|
|
|
|
def process_pdf(file): |
|
if file is None: |
|
st.error("No file uploaded.") |
|
return None |
|
|
|
|
|
with pdfplumber.open(file) as pdf: |
|
text = "\n".join([page.extract_text() for page in pdf.pages if page.extract_text()]) |
|
|
|
|
|
lines = text.split("\n") |
|
transactions = [line for line in lines if any(char.isdigit() for char in line)] |
|
|
|
|
|
df = pd.DataFrame([line.split()[:3] for line in transactions], columns=["Date", "Description", "Amount"]) |
|
|
|
|
|
df["Description"] = df["Description"].fillna("Unknown") |
|
|
|
|
|
df["Category"] = df["Description"].apply(classify_transaction) |
|
|
|
return df |
|
|
|
|
|
st.title("π Credit Card Statement Classifier") |
|
st.write("Upload a **PDF bank/credit card statement** to categorize transactions automatically.") |
|
|
|
uploaded_file = st.file_uploader("Upload PDF", type=["pdf"]) |
|
|
|
if uploaded_file is not None: |
|
st.success("β
File uploaded successfully!") |
|
|
|
|
|
df_result = process_pdf(uploaded_file) |
|
|
|
if df_result is not None: |
|
st.write("### π Classified Transactions:") |
|
st.dataframe(df_result) |
|
|