Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
# Define functions for preparing financial statements
|
| 5 |
+
def load_gl_data(file):
|
| 6 |
+
# Load the GL data from the uploaded CSV file
|
| 7 |
+
gl_data = pd.read_csv(file.name)
|
| 8 |
+
|
| 9 |
+
# Ensure correct data types for debit and credit columns
|
| 10 |
+
for col in ['BEGIN_BALANCE_DR', 'BEGIN_BALANCE_CR', 'PERIOD_NET_DR', 'PERIOD_NET_CR', 'YTD_DR', 'YTD_CR']:
|
| 11 |
+
gl_data[col] = pd.to_numeric(gl_data[col], errors='coerce').fillna(0)
|
| 12 |
+
|
| 13 |
+
# Calculate net balances
|
| 14 |
+
gl_data['BEGIN_BALANCE'] = gl_data['BEGIN_BALANCE_DR'] - gl_data['BEGIN_BALANCE_CR']
|
| 15 |
+
gl_data['PERIOD_NET'] = gl_data['PERIOD_NET_DR'] - gl_data['PERIOD_NET_CR']
|
| 16 |
+
gl_data['YTD_NET'] = gl_data['YTD_DR'] - gl_data['YTD_CR']
|
| 17 |
+
|
| 18 |
+
# Separate Balance Sheet and Income Statement items based on `Level1`
|
| 19 |
+
balance_sheet = gl_data[gl_data['Level1'].isin(['TOTAL ASSETS', 'TOTAL LIABILITIES', 'TOTAL EQUITY'])]
|
| 20 |
+
income_statement = gl_data[gl_data['Level1'].isin(['TOTAL REVENUE', 'TOTAL EXPENSES'])]
|
| 21 |
+
|
| 22 |
+
# Prepare Balance Sheet: Sum up Assets, Liabilities, and Equity based on Levels
|
| 23 |
+
assets = balance_sheet[balance_sheet['Level1'] == 'TOTAL ASSETS']['YTD_NET'].sum()
|
| 24 |
+
liabilities = balance_sheet[balance_sheet['Level1'] == 'TOTAL LIABILITIES']['YTD_NET'].sum()
|
| 25 |
+
equity = balance_sheet[balance_sheet['Level1'] == 'TOTAL EQUITY']['YTD_NET'].sum()
|
| 26 |
+
|
| 27 |
+
# Prepare Income Statement Totals: Revenue and Expenses using YTD_NET
|
| 28 |
+
revenue = income_statement[income_statement['Level1'] == 'TOTAL REVENUE']['YTD_NET'].sum()
|
| 29 |
+
expenses = income_statement[income_statement['Level1'] == 'TOTAL EXPENSES']['YTD_NET'].sum()
|
| 30 |
+
net_income = revenue - expenses
|
| 31 |
+
|
| 32 |
+
# Format Balance Sheet output
|
| 33 |
+
balance_sheet_statement = {
|
| 34 |
+
"Assets": assets,
|
| 35 |
+
"Liabilities": liabilities,
|
| 36 |
+
"Equity": equity,
|
| 37 |
+
"Total Liabilities and Equity": liabilities + equity
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# Format Income Statement output
|
| 41 |
+
income_statement_statement = {
|
| 42 |
+
"Revenue": revenue,
|
| 43 |
+
"Expenses": expenses,
|
| 44 |
+
"Net Income": net_income
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
return balance_sheet_statement, income_statement_statement
|
| 48 |
+
|
| 49 |
+
# Gradio Interface
|
| 50 |
+
def generate_statements(file):
|
| 51 |
+
balance_sheet, income_statement = load_gl_data(file)
|
| 52 |
+
return balance_sheet, income_statement
|
| 53 |
+
|
| 54 |
+
# Set up Gradio app interface
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
gr.Markdown("# Financial Statement Generator")
|
| 57 |
+
gr.Markdown("Upload your GL data to generate the Balance Sheet and Income Statement.")
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
file_input = gr.File(label="Upload GL Data CSV")
|
| 61 |
+
balance_sheet_output = gr.JSON(label="Balance Sheet")
|
| 62 |
+
income_statement_output = gr.JSON(label="Income Statement")
|
| 63 |
+
|
| 64 |
+
generate_button = gr.Button("Generate Statements")
|
| 65 |
+
generate_button.click(generate_statements, inputs=file_input, outputs=[balance_sheet_output, income_statement_output])
|
| 66 |
+
|
| 67 |
+
# Launch the app
|
| 68 |
+
demo.launch()
|