akazmi commited on
Commit
cdca1b3
·
verified ·
1 Parent(s): b688dbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -35
app.py CHANGED
@@ -1,41 +1,23 @@
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()
@@ -75,7 +57,7 @@ def generate_financial_statements(file):
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
 
@@ -87,18 +69,20 @@ def generate_financial_statements(file):
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__":
 
1
  import pandas as pd
2
  import gradio as gr
3
 
4
+ # Load account mapping from a mapping file
5
+ def load_mapping(mapping_file):
6
+ try:
7
+ mapping_df = pd.read_excel(mapping_file)
8
+ return dict(zip(mapping_df['Account Name'].str.lower(), mapping_df['Category']))
9
+ except Exception as e:
10
+ raise Exception(f"Error loading mapping file: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Generate financial statements based on the trial balance and mapping
13
+ def generate_financial_statements(file, mapping):
14
  try:
15
  df = pd.read_excel(file, usecols=['Account', 'Balance'])
 
16
  df['Account'] = df['Account'].astype(str).fillna('')
17
+ df['Balance'] = df['Balance'] / 1000 # Convert values to thousands (000)
18
 
 
 
 
19
  # Apply account mapping to categorize accounts
20
+ df['Category'] = df['Account'].str.lower().map(mapping).fillna('Other')
21
 
22
  # Income Statement calculation based on categories
23
  revenue = df[df['Category'] == 'Revenue']['Balance'].sum()
 
57
  Profit for the Year: {profit_for_the_year:,.2f}
58
  """
59
 
60
+ # Create Balance Sheet
61
  balance_sheet = df[df['Category'].isin(['Assets', 'Liabilities', 'Equity'])].copy()
62
  balance_sheet_summary = balance_sheet.groupby('Category').agg({'Balance': 'sum'}).reset_index()
63
 
 
69
  # Gradio Blocks interface
70
  with gr.Blocks() as demo:
71
  gr.Markdown("# Financial Statement Generator")
72
+ gr.Markdown("Upload a trial balance Excel file and a mapping file to generate an Income Statement and a Balance Sheet.")
73
 
74
  file_input = gr.File(label="Upload Trial Balance Excel File")
75
+ mapping_input = gr.File(label="Upload Mapping File")
76
  income_output = gr.Textbox(label="Income Statement", lines=20)
77
  balance_output = gr.Textbox(label="Balance Sheet", lines=10)
78
 
79
+ def process_file(trial_balance_file, mapping_file):
80
+ mapping = load_mapping(mapping_file)
81
+ income_statement, balance_sheet = generate_financial_statements(trial_balance_file, mapping)
82
  return income_statement, balance_sheet
83
 
84
+ # Trigger processing when files are uploaded
85
+ file_input.change(fn=process_file, inputs=[file_input, mapping_input], outputs=[income_output, balance_output])
86
 
87
  # Launch the Gradio app
88
  if __name__ == "__main__":