ZeeAI1 commited on
Commit
802eb89
Β·
verified Β·
1 Parent(s): 3592641

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -61
app.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
  import pandas as pd
3
  from huggingface_hub import InferenceClient
4
  import re
 
5
 
6
  # Initialize hosted inference client
7
  client = InferenceClient(model="google/flan-t5-base")
@@ -18,84 +19,84 @@ account_map = {
18
  "salary": "50002"
19
  }
20
 
21
- # Simulated business segments
22
- segment = {
23
- "company": "01",
24
- "business_type": "102",
25
- "location": "001",
26
- "cost_center": "001",
27
- "future": "000"
28
- }
29
-
30
- # Session state to store entries
31
  if "gl_entries" not in st.session_state:
32
  st.session_state.gl_entries = []
 
 
33
 
34
- # Inference logic
 
 
 
35
 
36
- def parse_prompt(prompt):
37
- return client.text_generation(prompt=f"Extract accounting entry: {prompt}", max_new_tokens=50).strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  def handle_gl_entry(prompt):
40
  prompt_lower = prompt.lower()
41
- amount = 0
42
- account_name = ""
 
43
 
44
- # Extract amount using regex
45
- amount_match = re.search(r'(\d{1,3}(,\d{3})*|\d+)(\.\d{1,2})?', prompt)
46
- if amount_match:
47
- amount = float(amount_match.group().replace(',', ''))
48
 
49
- # Identify transaction type
50
  if any(word in prompt_lower for word in ["invest", "capital", "start"]):
51
- account_name = "capital"
52
  description = "Owner Capital Contribution"
53
- debit_account = "cash"
54
- credit_account = account_name
55
  elif "rent" in prompt_lower:
56
- account_name = "rent"
57
  description = "Rent Expense"
58
- debit_account = account_name
59
- credit_account = "cash"
60
  elif "utilities" in prompt_lower:
61
- account_name = "utilities"
62
  description = "Utilities Expense"
63
- debit_account = account_name
64
- credit_account = "cash"
65
  elif any(word in prompt_lower for word in ["sale", "revenue"]):
66
- account_name = "sales"
67
  description = "Sales Revenue"
68
- debit_account = "cash"
69
- credit_account = account_name
70
  elif "supplies" in prompt_lower:
71
- account_name = "supplies"
72
  description = "Supplies Purchase"
73
- debit_account = account_name
74
- credit_account = "cash"
75
  elif "salary" in prompt_lower or "payroll" in prompt_lower:
76
- account_name = "salary"
77
  description = "Salary Expense"
78
- debit_account = account_name
79
- credit_account = "cash"
80
  else:
81
- description = "Unrecognized Entry"
82
- return pd.DataFrame([{"Date": "2025-04-01", "Description": description, "Account Code": "N/A", "Debit": 0, "Credit": 0}])
83
 
84
- debit_code = f"{segment['company']}-{segment['business_type']}-{segment['location']}-{segment['cost_center']}-{account_map[debit_account]}-{segment['future']}"
85
- credit_code = f"{segment['company']}-{segment['business_type']}-{segment['location']}-{segment['cost_center']}-{account_map[credit_account]}-{segment['future']}"
86
 
87
  entry = [
88
  {
89
- "Date": "2025-04-01",
90
  "Description": description,
91
- "Account Code": debit_code,
 
92
  "Debit": amount,
93
  "Credit": 0
94
  },
95
  {
96
- "Date": "2025-04-01",
97
  "Description": f"Offset for {description.lower()}",
98
- "Account Code": credit_code,
 
99
  "Debit": 0,
100
  "Credit": amount
101
  }
@@ -103,25 +104,24 @@ def handle_gl_entry(prompt):
103
  st.session_state.gl_entries.extend(entry)
104
  return pd.DataFrame(entry)
105
 
106
- # Streamlit UI
107
- st.set_page_config(page_title="AI ERP App", layout="wide")
108
- st.title("AI-Powered ERP Accounting App")
109
- prompt = st.text_input("πŸ“Œ Enter your accounting instruction:")
110
-
111
- download = st.download_button("πŸ“₯ Download All Entries (CSV)",
112
- data=pd.DataFrame(st.session_state.gl_entries).to_csv(index=False),
113
- file_name="gl_entries.csv",
114
- mime="text/csv")
115
-
116
- delete_records = st.button("πŸ—‘οΈ Delete All Records")
117
- if delete_records:
118
- st.session_state.gl_entries = []
119
- st.success("βœ… All records have been deleted.")
120
-
121
  if prompt:
122
  result = handle_gl_entry(prompt)
123
  st.dataframe(result)
124
 
 
125
  if st.session_state.gl_entries:
126
  st.subheader("πŸ“Š All Journal Entries")
127
  st.dataframe(pd.DataFrame(st.session_state.gl_entries))
 
2
  import pandas as pd
3
  from huggingface_hub import InferenceClient
4
  import re
5
+ from datetime import datetime
6
 
7
  # Initialize hosted inference client
8
  client = InferenceClient(model="google/flan-t5-base")
 
19
  "salary": "50002"
20
  }
21
 
22
+ # Session state
 
 
 
 
 
 
 
 
 
23
  if "gl_entries" not in st.session_state:
24
  st.session_state.gl_entries = []
25
+ if "company_name" not in st.session_state:
26
+ st.session_state.company_name = "My Company"
27
 
28
+ # Streamlit UI
29
+ st.set_page_config(page_title="AI ERP App", layout="wide")
30
+ st.title(f"πŸ“˜ {st.session_state.company_name} Ledger - AI-Powered ERP")
31
+ prompt = st.text_input("πŸ“Œ Enter your accounting instruction:")
32
 
33
+ def extract_amount(prompt):
34
+ match = re.search(r'\$?(\d{1,3}(,\d{3})*|\d+)(\.\d{1,2})?', prompt)
35
+ return float(match.group().replace(',', '').replace('$', '')) if match else 0
36
+
37
+ def extract_date(prompt):
38
+ date_match = re.search(r'(\d{1,2}[/-]\d{1,2}[/-]\d{2,4})|today', prompt.lower())
39
+ if date_match:
40
+ if "today" in date_match.group().lower():
41
+ return datetime.today().strftime("%Y-%m-%d")
42
+ try:
43
+ return datetime.strptime(date_match.group(), "%d/%m/%Y").strftime("%Y-%m-%d")
44
+ except:
45
+ try:
46
+ return datetime.strptime(date_match.group(), "%d-%m-%Y").strftime("%Y-%m-%d")
47
+ except:
48
+ return datetime.today().strftime("%Y-%m-%d")
49
+ return datetime.today().strftime("%Y-%m-%d")
50
 
51
  def handle_gl_entry(prompt):
52
  prompt_lower = prompt.lower()
53
+ amount = extract_amount(prompt)
54
+ date_str = extract_date(prompt)
55
+ description = ""
56
 
57
+ # Capture new company name
58
+ company_match = re.search(r"company with a name of ([\w\s&.-]+)", prompt_lower)
59
+ if company_match:
60
+ st.session_state.company_name = company_match.group(1).strip().upper()
61
 
 
62
  if any(word in prompt_lower for word in ["invest", "capital", "start"]):
 
63
  description = "Owner Capital Contribution"
64
+ debit_account, credit_account = "cash", "capital"
 
65
  elif "rent" in prompt_lower:
 
66
  description = "Rent Expense"
67
+ debit_account, credit_account = "rent", "cash"
 
68
  elif "utilities" in prompt_lower:
 
69
  description = "Utilities Expense"
70
+ debit_account, credit_account = "utilities", "cash"
 
71
  elif any(word in prompt_lower for word in ["sale", "revenue"]):
 
72
  description = "Sales Revenue"
73
+ debit_account, credit_account = "cash", "sales"
 
74
  elif "supplies" in prompt_lower:
 
75
  description = "Supplies Purchase"
76
+ debit_account, credit_account = "supplies", "cash"
 
77
  elif "salary" in prompt_lower or "payroll" in prompt_lower:
 
78
  description = "Salary Expense"
79
+ debit_account, credit_account = "salary", "cash"
 
80
  else:
81
+ return pd.DataFrame([{"Date": date_str, "Description": "Unrecognized Entry", "Account Code": "N/A", "Account Type": "N/A", "Debit": 0, "Credit": 0}])
 
82
 
83
+ def format_code(name):
84
+ return f"01-102-001-001-{account_map[name]}-000"
85
 
86
  entry = [
87
  {
88
+ "Date": date_str,
89
  "Description": description,
90
+ "Account Code": format_code(debit_account),
91
+ "Account Type": debit_account.title(),
92
  "Debit": amount,
93
  "Credit": 0
94
  },
95
  {
96
+ "Date": date_str,
97
  "Description": f"Offset for {description.lower()}",
98
+ "Account Code": format_code(credit_account),
99
+ "Account Type": credit_account.title(),
100
  "Debit": 0,
101
  "Credit": amount
102
  }
 
104
  st.session_state.gl_entries.extend(entry)
105
  return pd.DataFrame(entry)
106
 
107
+ # Buttons
108
+ col1, col2 = st.columns([1, 1])
109
+ with col1:
110
+ st.download_button("πŸ“₯ Download All Entries (CSV)",
111
+ data=pd.DataFrame(st.session_state.gl_entries).to_csv(index=False),
112
+ file_name="gl_entries.csv",
113
+ mime="text/csv")
114
+ with col2:
115
+ if st.button("πŸ—‘οΈ Delete All Records"):
116
+ st.session_state.gl_entries = []
117
+ st.success("βœ… All records have been deleted.")
118
+
119
+ # Handle input
 
 
120
  if prompt:
121
  result = handle_gl_entry(prompt)
122
  st.dataframe(result)
123
 
124
+ # Display ledger
125
  if st.session_state.gl_entries:
126
  st.subheader("πŸ“Š All Journal Entries")
127
  st.dataframe(pd.DataFrame(st.session_state.gl_entries))