Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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")
|
8 |
+
|
9 |
+
# Simulated chart of accounts mapping
|
10 |
+
account_map = {
|
11 |
+
"rent": "60001",
|
12 |
+
"utilities": "60002",
|
13 |
+
"capital": "30000",
|
14 |
+
"cash": "10001",
|
15 |
+
"bank": "10002",
|
16 |
+
"sales": "40001",
|
17 |
+
"supplies": "50001",
|
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 |
+
}
|
102 |
+
]
|
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))
|