Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,105 +2,118 @@ import streamlit as st
|
|
2 |
import pandas as pd
|
3 |
from docx import Document
|
4 |
|
5 |
-
# Function to load data
|
6 |
def load_data(file):
|
7 |
try:
|
8 |
-
if file.type == "text/csv"
|
9 |
-
|
10 |
-
elif file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
11 |
-
return pd.read_excel(file)
|
12 |
-
elif file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
13 |
-
doc = Document(file)
|
14 |
-
data = [para.text for para in doc.paragraphs if para.text.strip()] # Remove empty lines
|
15 |
-
return pd.DataFrame(data, columns=["Text"])
|
16 |
-
else:
|
17 |
-
st.error("Unsupported file format. Please upload a CSV, Excel, or DOCX file.")
|
18 |
-
return None
|
19 |
except Exception as e:
|
20 |
st.error(f"Error loading file: {e}")
|
21 |
return None
|
22 |
|
23 |
-
# Function to generate
|
24 |
-
def
|
25 |
-
st.subheader("
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
else:
|
81 |
-
st.
|
82 |
|
83 |
# Streamlit App
|
84 |
-
st.title("
|
85 |
|
86 |
-
# File
|
87 |
-
uploaded_file = st.file_uploader("Upload a CSV
|
88 |
|
89 |
if uploaded_file is not None:
|
90 |
data = load_data(uploaded_file)
|
91 |
-
|
92 |
if data is not None and not data.empty:
|
93 |
-
st.write("### Uploaded Data Preview:")
|
94 |
st.write(data.head())
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
generate_balance_sheet(data)
|
102 |
else:
|
103 |
st.error("The uploaded file contains no usable data.")
|
104 |
|
105 |
-
|
106 |
-
|
|
|
2 |
import pandas as pd
|
3 |
from docx import Document
|
4 |
|
5 |
+
# Function to load financial data
|
6 |
def load_data(file):
|
7 |
try:
|
8 |
+
df = pd.read_csv(file) if file.type == "text/csv" else pd.read_excel(file)
|
9 |
+
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
except Exception as e:
|
11 |
st.error(f"Error loading file: {e}")
|
12 |
return None
|
13 |
|
14 |
+
# Function to generate journal entries automatically
|
15 |
+
def generate_journal_entries(data):
|
16 |
+
st.subheader("π Generated Journal Entries")
|
17 |
+
|
18 |
+
journal_entries = []
|
19 |
+
|
20 |
+
for index, row in data.iterrows():
|
21 |
+
description = row.get("Description", "Unknown Transaction").lower()
|
22 |
+
amount = row.get("Amount", 0)
|
23 |
+
|
24 |
+
if "sale" in description or "revenue" in description:
|
25 |
+
journal_entries.append({"Date": row.get("Date"), "Account": "Cash", "Debit": amount, "Credit": 0})
|
26 |
+
journal_entries.append({"Date": row.get("Date"), "Account": "Sales Revenue", "Debit": 0, "Credit": amount})
|
27 |
+
|
28 |
+
elif "expense" in description or "cost" in description:
|
29 |
+
journal_entries.append({"Date": row.get("Date"), "Account": description.capitalize(), "Debit": amount, "Credit": 0})
|
30 |
+
journal_entries.append({"Date": row.get("Date"), "Account": "Cash", "Debit": 0, "Credit": amount})
|
31 |
+
|
32 |
+
elif "purchase" in description or "inventory" in description:
|
33 |
+
journal_entries.append({"Date": row.get("Date"), "Account": "Inventory", "Debit": amount, "Credit": 0})
|
34 |
+
journal_entries.append({"Date": row.get("Date"), "Account": "Cash", "Debit": 0, "Credit": amount})
|
35 |
+
|
36 |
+
elif "loan" in description or "debt" in description:
|
37 |
+
journal_entries.append({"Date": row.get("Date"), "Account": "Cash", "Debit": amount, "Credit": 0})
|
38 |
+
journal_entries.append({"Date": row.get("Date"), "Account": "Loan Payable", "Debit": 0, "Credit": amount})
|
39 |
+
|
40 |
+
elif "investment" in description:
|
41 |
+
journal_entries.append({"Date": row.get("Date"), "Account": "Cash", "Debit": amount, "Credit": 0})
|
42 |
+
journal_entries.append({"Date": row.get("Date"), "Account": "Owner's Equity", "Debit": 0, "Credit": amount})
|
43 |
+
|
44 |
+
journal_df = pd.DataFrame(journal_entries)
|
45 |
+
st.write(journal_df)
|
46 |
+
return journal_df
|
47 |
+
|
48 |
+
# Function to generate Ledger
|
49 |
+
def generate_ledger(journal_df):
|
50 |
+
st.subheader("π Ledger")
|
51 |
+
ledger = journal_df.groupby("Account")[["Debit", "Credit"]].sum().reset_index()
|
52 |
+
ledger["Balance"] = ledger["Debit"] - ledger["Credit"]
|
53 |
+
st.write(ledger)
|
54 |
+
return ledger
|
55 |
+
|
56 |
+
# Function to generate Income Statement
|
57 |
+
def generate_income_statement(journal_df):
|
58 |
+
st.subheader("π Income Statement")
|
59 |
+
revenue = journal_df[journal_df["Account"] == "Sales Revenue"]["Credit"].sum()
|
60 |
+
expenses = journal_df[journal_df["Account"].str.contains("Expense|Cost", case=False, na=False)]["Debit"].sum()
|
61 |
+
net_income = revenue - expenses
|
62 |
+
|
63 |
+
st.write(f"**Total Revenue:** ${revenue:,.2f}")
|
64 |
+
st.write(f"**Total Expenses:** ${expenses:,.2f}")
|
65 |
+
st.write(f"**Net Income:** ${net_income:,.2f}")
|
66 |
+
|
67 |
+
return net_income
|
68 |
+
|
69 |
+
# Function to generate Cash Flow Statement
|
70 |
+
def generate_cash_flow(journal_df):
|
71 |
+
st.subheader("π° Cash Flow Statement")
|
72 |
+
cash_inflow = journal_df[journal_df["Account"] == "Cash"]["Debit"].sum()
|
73 |
+
cash_outflow = journal_df[journal_df["Account"] == "Cash"]["Credit"].sum()
|
74 |
+
net_cash_flow = cash_inflow - cash_outflow
|
75 |
+
|
76 |
+
st.write(f"**Total Cash Inflow:** ${cash_inflow:,.2f}")
|
77 |
+
st.write(f"**Total Cash Outflow:** ${cash_outflow:,.2f}")
|
78 |
+
st.write(f"**Net Cash Flow:** ${net_cash_flow:,.2f}")
|
79 |
+
|
80 |
+
return net_cash_flow
|
81 |
+
|
82 |
+
# Function to generate Balance Sheet
|
83 |
+
def generate_balance_sheet(journal_df, net_income):
|
84 |
+
st.subheader("π Balance Sheet")
|
85 |
+
|
86 |
+
assets = journal_df[journal_df["Account"].str.contains("Cash|Inventory", case=False, na=False)]["Debit"].sum()
|
87 |
+
liabilities = journal_df[journal_df["Account"].str.contains("Payable|Loan", case=False, na=False)]["Credit"].sum()
|
88 |
+
equity = journal_df[journal_df["Account"] == "Owner's Equity"]["Credit"].sum() + net_income
|
89 |
+
|
90 |
+
st.write(f"**Total Assets:** ${assets:,.2f}")
|
91 |
+
st.write(f"**Total Liabilities:** ${liabilities:,.2f}")
|
92 |
+
st.write(f"**Total Equity:** ${equity:,.2f}")
|
93 |
+
|
94 |
+
if abs(assets - (liabilities + equity)) < 0.01:
|
95 |
+
st.success("β
The Balance Sheet is balanced!")
|
96 |
else:
|
97 |
+
st.warning("β οΈ The Balance Sheet is not balanced!")
|
98 |
|
99 |
# Streamlit App
|
100 |
+
st.title("π Automated Financial Statement Generator")
|
101 |
|
102 |
+
# File Upload
|
103 |
+
uploaded_file = st.file_uploader("Upload a CSV or Excel file with financial transactions", type=["csv", "xlsx"])
|
104 |
|
105 |
if uploaded_file is not None:
|
106 |
data = load_data(uploaded_file)
|
107 |
+
|
108 |
if data is not None and not data.empty:
|
109 |
+
st.write("### π Uploaded Data Preview:")
|
110 |
st.write(data.head())
|
111 |
|
112 |
+
journal_df = generate_journal_entries(data)
|
113 |
+
ledger = generate_ledger(journal_df)
|
114 |
+
net_income = generate_income_statement(journal_df)
|
115 |
+
generate_cash_flow(journal_df)
|
116 |
+
generate_balance_sheet(journal_df, net_income)
|
|
|
117 |
else:
|
118 |
st.error("The uploaded file contains no usable data.")
|
119 |
|
|
|
|