akazmi commited on
Commit
e134f7b
·
verified ·
1 Parent(s): 246b429

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -10
app.py CHANGED
@@ -1,6 +1,30 @@
1
  import pandas as pd
2
  import gradio as gr
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  def generate_financial_statements(file):
5
  try:
6
  print("Starting file read...")
@@ -18,23 +42,26 @@ def generate_financial_statements(file):
18
  try:
19
  # Ensure that 'Account' column has no NaN and is treated as string
20
  df['Account'] = df['Account'].astype(str).fillna('')
21
-
22
  # Convert values in the 'Balance' column to thousands (000)
23
  df['Balance'] = df['Balance'] / 1000
24
 
25
- # Create Income Statement as per IAS
26
- revenue = df[df['Account'].str.contains('Revenue|Sales', case=False, na=False)]['Balance'].sum()
27
- cost_of_sales = df[df['Account'].str.contains('Cost of Sales|COGS', case=False, na=False)]['Balance'].sum()
 
 
 
28
  gross_profit = revenue - cost_of_sales
29
 
30
- other_income = df[df['Account'].str.contains('Other Income', case=False, na=False)]['Balance'].sum()
31
- operating_expenses = df[df['Account'].str.contains('Operating Expenses|Administrative|Selling', case=False, na=False)]['Balance'].sum()
32
  operating_profit = gross_profit + other_income - operating_expenses
33
 
34
- finance_costs = df[df['Account'].str.contains('Finance Costs|Interest Expense', case=False, na=False)]['Balance'].sum()
35
  profit_before_tax = operating_profit - finance_costs
36
 
37
- tax_expense = df[df['Account'].str.contains('Tax Expense|Income Tax', case=False, na=False)]['Balance'].sum()
38
  profit_for_the_year = profit_before_tax - tax_expense
39
 
40
  # Generate the income statement with formatting
@@ -63,8 +90,8 @@ def generate_financial_statements(file):
63
  print("Income Statement created.")
64
 
65
  # Create Balance Sheet - Filter for Asset, Liability, or Equity Accounts
66
- balance_sheet = df[df['Account'].str.contains('Asset|Liability|Equity', na=False, case=False)].copy()
67
- balance_sheet_summary = balance_sheet.groupby('Account').agg({'Balance': 'sum'}).reset_index()
68
  print("Balance Sheet created.")
69
 
70
  return income_statement, balance_sheet_summary.to_string(index=False)
 
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
  print("Starting file read...")
 
42
  try:
43
  # Ensure that 'Account' column has no NaN and is treated as string
44
  df['Account'] = df['Account'].astype(str).fillna('')
45
+
46
  # Convert values in the 'Balance' column to thousands (000)
47
  df['Balance'] = df['Balance'] / 1000
48
 
49
+ # Apply account mapping to categorize accounts
50
+ df['Category'] = df['Account'].apply(map_account_category)
51
+
52
+ # Income Statement calculation based on categories
53
+ revenue = df[df['Category'] == 'Revenue']['Balance'].sum()
54
+ cost_of_sales = df[df['Category'] == 'Cost of Sales']['Balance'].sum()
55
  gross_profit = revenue - cost_of_sales
56
 
57
+ other_income = df[df['Category'] == 'Other']['Balance'].sum()
58
+ operating_expenses = df[df['Category'] == 'Operating Expenses']['Balance'].sum()
59
  operating_profit = gross_profit + other_income - operating_expenses
60
 
61
+ finance_costs = df[df['Category'] == 'Finance Costs']['Balance'].sum()
62
  profit_before_tax = operating_profit - finance_costs
63
 
64
+ tax_expense = df[df['Category'] == 'Tax Expense']['Balance'].sum()
65
  profit_for_the_year = profit_before_tax - tax_expense
66
 
67
  # Generate the income statement with formatting
 
90
  print("Income Statement created.")
91
 
92
  # Create Balance Sheet - Filter for Asset, Liability, or Equity Accounts
93
+ balance_sheet = df[df['Category'].isin(['Assets', 'Liabilities', 'Equity'])].copy()
94
+ balance_sheet_summary = balance_sheet.groupby('Category').agg({'Balance': 'sum'}).reset_index()
95
  print("Balance Sheet created.")
96
 
97
  return income_statement, balance_sheet_summary.to_string(index=False)