akazmi commited on
Commit
8003d92
·
verified ·
1 Parent(s): 6fac2dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -6
app.py CHANGED
@@ -1,13 +1,105 @@
1
  import pandas as pd
2
  import gradio as gr
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  def generate_financial_statements(file):
5
  try:
6
- # Try reading the Excel file with only relevant columns to reduce overhead
7
- df = pd.read_excel(file, usecols=['Account', 'Balance'], nrows=1000)
8
- print("File read successfully.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  except Exception as e:
10
- return f"Error reading the file: {e}", None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # Continue with the previous logic, now optimized
13
- # (Rest of the code remains the same, including the mapping logic)
 
 
1
  import pandas as pd
2
  import gradio as gr
3
 
4
+ # Sample account mapping for the trial balance accounts
5
+ account_mapping = {
6
+ 'Revenue': 'Revenue',
7
+ 'Sales': 'Revenue',
8
+ 'Cost of Sales': 'Cost of Sales',
9
+ 'COGS': 'Cost of Sales',
10
+ 'Operating Expenses': 'Operating Expenses',
11
+ 'Selling': 'Operating Expenses',
12
+ 'Administrative': 'Operating Expenses',
13
+ 'Finance Costs': 'Finance Costs',
14
+ 'Interest Expense': 'Finance Costs',
15
+ 'Tax Expense': 'Tax Expense',
16
+ 'Income Tax': 'Tax Expense',
17
+ 'Assets': 'Assets',
18
+ 'Liability': 'Liabilities',
19
+ 'Equity': 'Equity'
20
+ }
21
+
22
+ def map_account_category(account_name):
23
+ for key, value in account_mapping.items():
24
+ if key.lower() in account_name.lower():
25
+ return value
26
+ return 'Other'
27
+
28
  def generate_financial_statements(file):
29
  try:
30
+ df = pd.read_excel(file, usecols=['Account', 'Balance'])
31
+ # Ensure that 'Account' column has no NaN and is treated as string
32
+ df['Account'] = df['Account'].astype(str).fillna('')
33
+
34
+ # Convert values in the 'Balance' column to thousands (000)
35
+ df['Balance'] = df['Balance'] / 1000
36
+
37
+ # Apply account mapping to categorize accounts
38
+ df['Category'] = df['Account'].apply(map_account_category)
39
+
40
+ # Income Statement calculation based on categories
41
+ revenue = df[df['Category'] == 'Revenue']['Balance'].sum()
42
+ cost_of_sales = df[df['Category'] == 'Cost of Sales']['Balance'].sum()
43
+ gross_profit = revenue - cost_of_sales
44
+
45
+ other_income = df[df['Category'] == 'Other']['Balance'].sum()
46
+ operating_expenses = df[df['Category'] == 'Operating Expenses']['Balance'].sum()
47
+ operating_profit = gross_profit + other_income - operating_expenses
48
+
49
+ finance_costs = df[df['Category'] == 'Finance Costs']['Balance'].sum()
50
+ profit_before_tax = operating_profit - finance_costs
51
+
52
+ tax_expense = df[df['Category'] == 'Tax Expense']['Balance'].sum()
53
+ profit_for_the_year = profit_before_tax - tax_expense
54
+
55
+ # Generate the income statement with formatting
56
+ income_statement = f"""
57
+ INCOME STATEMENT (in 000s)
58
+
59
+ Revenue: {revenue:,.2f}
60
+ Cost of Sales: ({cost_of_sales:,.2f})
61
+ ------------------------------------------------
62
+ Gross Profit: {gross_profit:,.2f}
63
+
64
+ Other Income: {other_income:,.2f}
65
+ Operating Expenses: ({operating_expenses:,.2f})
66
+ ------------------------------------------------
67
+ Operating Profit: {operating_profit:,.2f}
68
+
69
+ Finance Costs: ({finance_costs:,.2f})
70
+ ------------------------------------------------
71
+ Profit Before Tax: {profit_before_tax:,.2f}
72
+
73
+ Tax Expense: ({tax_expense:,.2f})
74
+ ------------------------------------------------
75
+ Profit for the Year: {profit_for_the_year:,.2f}
76
+ """
77
+
78
+ # Create Balance Sheet - Filter for Asset, Liability, or Equity Accounts
79
+ balance_sheet = df[df['Category'].isin(['Assets', 'Liabilities', 'Equity'])].copy()
80
+ balance_sheet_summary = balance_sheet.groupby('Category').agg({'Balance': 'sum'}).reset_index()
81
+
82
+ return income_statement, balance_sheet_summary.to_string(index=False)
83
+
84
  except Exception as e:
85
+ return f"Error processing the file: {e}", None
86
+
87
+ # Gradio Blocks interface
88
+ with gr.Blocks() as demo:
89
+ gr.Markdown("# Financial Statement Generator")
90
+ gr.Markdown("Upload a trial balance Excel file to generate an Income Statement and a Balance Sheet.")
91
+
92
+ file_input = gr.File(label="Upload Trial Balance Excel File")
93
+ income_output = gr.Textbox(label="Income Statement", lines=20)
94
+ balance_output = gr.Textbox(label="Balance Sheet", lines=10)
95
+
96
+ def process_file(file):
97
+ income_statement, balance_sheet = generate_financial_statements(file)
98
+ return income_statement, balance_sheet
99
+
100
+ # Trigger processing when file is uploaded
101
+ file_input.change(fn=process_file, inputs=file_input, outputs=[income_output, balance_output])
102
 
103
+ # Launch the Gradio app
104
+ if __name__ == "__main__":
105
+ demo.launch(share=True)