Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,118 +2,151 @@ import streamlit as st
|
|
2 |
import pandas as pd
|
3 |
from docx import Document
|
4 |
|
5 |
-
# Function to load
|
6 |
def load_data(file):
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
12 |
return None
|
13 |
|
14 |
-
# Function to generate
|
15 |
def generate_journal_entries(data):
|
16 |
-
st.subheader("
|
17 |
|
18 |
journal_entries = []
|
19 |
|
20 |
for index, row in data.iterrows():
|
21 |
-
|
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 |
-
|
29 |
-
journal_entries.append(
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
journal_entries.append(
|
35 |
-
|
36 |
-
elif "
|
37 |
-
journal_entries.append(
|
38 |
-
journal_entries.append(
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
journal_entries.append(
|
43 |
-
|
44 |
-
|
|
|
|
|
45 |
st.write(journal_df)
|
46 |
return journal_df
|
47 |
|
48 |
# Function to generate Ledger
|
49 |
def generate_ledger(journal_df):
|
50 |
-
st.subheader("
|
51 |
-
ledger =
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
# Function to generate Income Statement
|
57 |
def generate_income_statement(journal_df):
|
58 |
-
st.subheader("
|
59 |
-
revenue =
|
60 |
-
expenses =
|
61 |
-
net_income = revenue - expenses
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
return net_income
|
68 |
|
69 |
# Function to generate Cash Flow Statement
|
70 |
def generate_cash_flow(journal_df):
|
71 |
-
st.subheader("
|
72 |
-
cash_inflow =
|
73 |
-
cash_outflow =
|
74 |
-
net_cash_flow = cash_inflow - cash_outflow
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
|
|
80 |
return net_cash_flow
|
81 |
|
82 |
# Function to generate Balance Sheet
|
83 |
-
def generate_balance_sheet(
|
84 |
-
st.subheader("
|
|
|
|
|
|
|
|
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
93 |
|
94 |
-
|
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("
|
101 |
|
102 |
-
# File
|
103 |
-
uploaded_file = st.file_uploader("Upload a CSV or
|
104 |
|
105 |
if uploaded_file is not None:
|
106 |
data = load_data(uploaded_file)
|
107 |
-
|
108 |
-
|
109 |
-
st.write("### π Uploaded Data Preview:")
|
110 |
st.write(data.head())
|
111 |
|
|
|
112 |
journal_df = generate_journal_entries(data)
|
113 |
-
|
114 |
net_income = generate_income_statement(journal_df)
|
115 |
generate_cash_flow(journal_df)
|
116 |
-
generate_balance_sheet(
|
117 |
-
else:
|
118 |
-
st.error("The uploaded file contains no usable data.")
|
119 |
-
|
|
|
2 |
import pandas as pd
|
3 |
from docx import Document
|
4 |
|
5 |
+
# Function to load data from CSV, Excel, or DOCX
|
6 |
def load_data(file):
|
7 |
+
if file.type == "text/csv":
|
8 |
+
return pd.read_csv(file)
|
9 |
+
elif file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
10 |
+
return pd.read_excel(file)
|
11 |
+
elif file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
12 |
+
doc = Document(file)
|
13 |
+
data = [para.text.strip() for para in doc.paragraphs if para.text.strip()]
|
14 |
+
return pd.DataFrame(data, columns=["Description"]) # Treat as transaction descriptions
|
15 |
+
else:
|
16 |
+
st.error("Unsupported file format. Please upload a CSV, Excel, or DOCX file.")
|
17 |
return None
|
18 |
|
19 |
+
# Function to generate Journal Entries from raw data
|
20 |
def generate_journal_entries(data):
|
21 |
+
st.subheader("Journal Entries")
|
22 |
|
23 |
journal_entries = []
|
24 |
|
25 |
for index, row in data.iterrows():
|
26 |
+
desc = row["Description"].lower()
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
if "capital" in desc:
|
29 |
+
journal_entries.append(["Cash", "Capital", 10000])
|
30 |
+
elif "inventory" in desc and "credit" in desc:
|
31 |
+
journal_entries.append(["Inventory", "Accounts Payable", 4000])
|
32 |
+
journal_entries.append(["Inventory", "Cash", 3000])
|
33 |
+
elif "equipment" in desc:
|
34 |
+
journal_entries.append(["Office Equipment", "Bank Loan", 8000])
|
35 |
+
journal_entries.append(["Office Equipment", "Cash", 7000])
|
36 |
+
elif "sales" in desc:
|
37 |
+
journal_entries.append(["Accounts Receivable", "Sales Revenue", 5000])
|
38 |
+
journal_entries.append(["Cash", "Sales Revenue", 15000])
|
39 |
+
elif "salary" in desc:
|
40 |
+
journal_entries.append(["Salaries Expense", "Cash", 3000])
|
41 |
+
elif "rent" in desc:
|
42 |
+
journal_entries.append(["Rent Expense", "Cash", 2000])
|
43 |
+
elif "utilities" in desc:
|
44 |
+
journal_entries.append(["Utilities Expense", "Cash", 1000])
|
45 |
+
|
46 |
+
journal_df = pd.DataFrame(journal_entries, columns=["Debit", "Credit", "Amount"])
|
47 |
st.write(journal_df)
|
48 |
return journal_df
|
49 |
|
50 |
# Function to generate Ledger
|
51 |
def generate_ledger(journal_df):
|
52 |
+
st.subheader("Ledger")
|
53 |
+
ledger = {}
|
54 |
+
|
55 |
+
for index, row in journal_df.iterrows():
|
56 |
+
debit = row["Debit"]
|
57 |
+
credit = row["Credit"]
|
58 |
+
amount = row["Amount"]
|
59 |
+
|
60 |
+
if debit not in ledger:
|
61 |
+
ledger[debit] = 0
|
62 |
+
if credit not in ledger:
|
63 |
+
ledger[credit] = 0
|
64 |
+
|
65 |
+
ledger[debit] += amount
|
66 |
+
ledger[credit] -= amount
|
67 |
+
|
68 |
+
ledger_df = pd.DataFrame(ledger.items(), columns=["Account", "Balance"])
|
69 |
+
st.write(ledger_df)
|
70 |
+
return ledger_df
|
71 |
|
72 |
# Function to generate Income Statement
|
73 |
def generate_income_statement(journal_df):
|
74 |
+
st.subheader("Income Statement")
|
75 |
+
revenue = 0
|
76 |
+
expenses = 0
|
|
|
77 |
|
78 |
+
for index, row in journal_df.iterrows():
|
79 |
+
if "Revenue" in row["Credit"]:
|
80 |
+
revenue += row["Amount"]
|
81 |
+
if "Expense" in row["Debit"]:
|
82 |
+
expenses += row["Amount"]
|
83 |
|
84 |
+
net_income = revenue - expenses
|
85 |
+
income_statement = pd.DataFrame({
|
86 |
+
"Category": ["Total Revenue", "Total Expenses", "Net Income"],
|
87 |
+
"Amount": [revenue, expenses, net_income]
|
88 |
+
})
|
89 |
+
|
90 |
+
st.write(income_statement)
|
91 |
return net_income
|
92 |
|
93 |
# Function to generate Cash Flow Statement
|
94 |
def generate_cash_flow(journal_df):
|
95 |
+
st.subheader("Cash Flow Statement")
|
96 |
+
cash_inflow = 0
|
97 |
+
cash_outflow = 0
|
|
|
98 |
|
99 |
+
for index, row in journal_df.iterrows():
|
100 |
+
if row["Debit"] == "Cash":
|
101 |
+
cash_inflow += row["Amount"]
|
102 |
+
elif row["Credit"] == "Cash":
|
103 |
+
cash_outflow += row["Amount"]
|
104 |
+
|
105 |
+
net_cash_flow = cash_inflow - cash_outflow
|
106 |
+
cash_flow_statement = pd.DataFrame({
|
107 |
+
"Category": ["Total Cash Inflow", "Total Cash Outflow", "Net Cash Flow"],
|
108 |
+
"Amount": [cash_inflow, cash_outflow, net_cash_flow]
|
109 |
+
})
|
110 |
|
111 |
+
st.write(cash_flow_statement)
|
112 |
return net_cash_flow
|
113 |
|
114 |
# Function to generate Balance Sheet
|
115 |
+
def generate_balance_sheet(ledger_df, net_income):
|
116 |
+
st.subheader("Balance Sheet")
|
117 |
+
|
118 |
+
assets = 0
|
119 |
+
liabilities = 0
|
120 |
+
equity = net_income # Retained earnings
|
121 |
|
122 |
+
for index, row in ledger_df.iterrows():
|
123 |
+
if row["Balance"] > 0:
|
124 |
+
assets += row["Balance"]
|
125 |
+
elif row["Balance"] < 0:
|
126 |
+
liabilities += abs(row["Balance"])
|
127 |
|
128 |
+
balance_sheet = pd.DataFrame({
|
129 |
+
"Category": ["Total Assets", "Total Liabilities", "Total Equity"],
|
130 |
+
"Amount": [assets, liabilities, equity]
|
131 |
+
})
|
132 |
|
133 |
+
st.write(balance_sheet)
|
|
|
|
|
|
|
134 |
|
135 |
# Streamlit App
|
136 |
+
st.title("Financial Statement Generator π")
|
137 |
|
138 |
+
# File upload
|
139 |
+
uploaded_file = st.file_uploader("Upload a CSV, Excel, or DOCX file", type=["csv", "xlsx", "docx"])
|
140 |
|
141 |
if uploaded_file is not None:
|
142 |
data = load_data(uploaded_file)
|
143 |
+
if data is not None:
|
144 |
+
st.write("Uploaded Data Preview:")
|
|
|
145 |
st.write(data.head())
|
146 |
|
147 |
+
# Generate financial statements
|
148 |
journal_df = generate_journal_entries(data)
|
149 |
+
ledger_df = generate_ledger(journal_df)
|
150 |
net_income = generate_income_statement(journal_df)
|
151 |
generate_cash_flow(journal_df)
|
152 |
+
generate_balance_sheet(ledger_df, net_income)
|
|
|
|
|
|