Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,25 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
# Function to read trial balance and prepare financial statements
|
| 5 |
def generate_financial_statements(file):
|
|
|
|
| 6 |
# Read the Excel file
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
return "Invalid file format. Please ensure the columns are 'Account', 'Debit', and 'Credit'."
|
| 12 |
|
|
|
|
| 13 |
# Calculate totals
|
| 14 |
total_debit = df['Debit'].sum()
|
| 15 |
total_credit = df['Credit'].sum()
|
|
|
|
| 16 |
|
| 17 |
# Create Profit & Loss Statement
|
| 18 |
pnl = df[df['Debit'] > 0].copy()
|
|
@@ -20,19 +27,23 @@ def generate_financial_statements(file):
|
|
| 20 |
pnl.loc[pnl['Credit'] > 0, 'Type'] = 'Income'
|
| 21 |
|
| 22 |
pnl_summary = pnl.groupby('Type')['Debit', 'Credit'].sum().reset_index()
|
|
|
|
| 23 |
|
| 24 |
# Create Balance Sheet
|
| 25 |
balance_sheet = df.groupby(df['Account'].str.contains('Asset|Liability|Equity')).agg({'Debit': 'sum', 'Credit': 'sum'}).reset_index()
|
| 26 |
balance_sheet.columns = ['Type', 'Debit', 'Credit']
|
|
|
|
| 27 |
|
| 28 |
-
return pnl_summary, balance_sheet
|
| 29 |
|
| 30 |
# Gradio interface
|
| 31 |
def gradio_interface(file):
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
#
|
| 36 |
iface = gr.Interface(
|
| 37 |
fn=gradio_interface,
|
| 38 |
inputs=gr.inputs.File(label="Upload Trial Balance Excel File"),
|
|
@@ -44,4 +55,6 @@ iface = gr.Interface(
|
|
| 44 |
description="Upload a trial balance Excel file to generate Profit & Loss account and Balance Sheet."
|
| 45 |
)
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import gradio as gr
|
| 3 |
|
|
|
|
| 4 |
def generate_financial_statements(file):
|
| 5 |
+
print("Starting file read...")
|
| 6 |
# Read the Excel file
|
| 7 |
+
try:
|
| 8 |
+
df = pd.read_excel(file)
|
| 9 |
+
print("File read successfully.")
|
| 10 |
+
except Exception as e:
|
| 11 |
+
return f"Error reading the file: {e}"
|
| 12 |
+
|
| 13 |
+
# Check for required columns
|
| 14 |
+
required_columns = ['Account', 'Debit', 'Credit']
|
| 15 |
+
if not all(col in df.columns for col in required_columns):
|
| 16 |
return "Invalid file format. Please ensure the columns are 'Account', 'Debit', and 'Credit'."
|
| 17 |
|
| 18 |
+
print("Calculating totals...")
|
| 19 |
# Calculate totals
|
| 20 |
total_debit = df['Debit'].sum()
|
| 21 |
total_credit = df['Credit'].sum()
|
| 22 |
+
print(f"Total Debit: {total_debit}, Total Credit: {total_credit}")
|
| 23 |
|
| 24 |
# Create Profit & Loss Statement
|
| 25 |
pnl = df[df['Debit'] > 0].copy()
|
|
|
|
| 27 |
pnl.loc[pnl['Credit'] > 0, 'Type'] = 'Income'
|
| 28 |
|
| 29 |
pnl_summary = pnl.groupby('Type')['Debit', 'Credit'].sum().reset_index()
|
| 30 |
+
print("Profit & Loss statement created.")
|
| 31 |
|
| 32 |
# Create Balance Sheet
|
| 33 |
balance_sheet = df.groupby(df['Account'].str.contains('Asset|Liability|Equity')).agg({'Debit': 'sum', 'Credit': 'sum'}).reset_index()
|
| 34 |
balance_sheet.columns = ['Type', 'Debit', 'Credit']
|
| 35 |
+
print("Balance Sheet created.")
|
| 36 |
|
| 37 |
+
return pnl_summary.to_string(), balance_sheet.to_string()
|
| 38 |
|
| 39 |
# Gradio interface
|
| 40 |
def gradio_interface(file):
|
| 41 |
+
print("Processing file...")
|
| 42 |
+
result = generate_financial_statements(file)
|
| 43 |
+
print("Processing complete.")
|
| 44 |
+
return result
|
| 45 |
|
| 46 |
+
# Set up Gradio interface
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=gradio_interface,
|
| 49 |
inputs=gr.inputs.File(label="Upload Trial Balance Excel File"),
|
|
|
|
| 55 |
description="Upload a trial balance Excel file to generate Profit & Loss account and Balance Sheet."
|
| 56 |
)
|
| 57 |
|
| 58 |
+
# Launch the Gradio app
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
iface.launch()
|