File size: 4,204 Bytes
bc1c435 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
import streamlit as st
import pandas as pd
from huggingface_hub import InferenceClient
import re
# Initialize hosted inference client
client = InferenceClient(model="google/flan-t5-base")
# Simulated chart of accounts mapping
account_map = {
"rent": "60001",
"utilities": "60002",
"capital": "30000",
"cash": "10001",
"bank": "10002",
"sales": "40001",
"supplies": "50001",
"salary": "50002"
}
# Simulated business segments
segment = {
"company": "01",
"business_type": "102",
"location": "001",
"cost_center": "001",
"future": "000"
}
# Session state to store entries
if "gl_entries" not in st.session_state:
st.session_state.gl_entries = []
# Inference logic
def parse_prompt(prompt):
return client.text_generation(prompt=f"Extract accounting entry: {prompt}", max_new_tokens=50).strip()
def handle_gl_entry(prompt):
prompt_lower = prompt.lower()
amount = 0
account_name = ""
# Extract amount using regex
amount_match = re.search(r'(\d{1,3}(,\d{3})*|\d+)(\.\d{1,2})?', prompt)
if amount_match:
amount = float(amount_match.group().replace(',', ''))
# Identify transaction type
if any(word in prompt_lower for word in ["invest", "capital", "start"]):
account_name = "capital"
description = "Owner Capital Contribution"
debit_account = "cash"
credit_account = account_name
elif "rent" in prompt_lower:
account_name = "rent"
description = "Rent Expense"
debit_account = account_name
credit_account = "cash"
elif "utilities" in prompt_lower:
account_name = "utilities"
description = "Utilities Expense"
debit_account = account_name
credit_account = "cash"
elif any(word in prompt_lower for word in ["sale", "revenue"]):
account_name = "sales"
description = "Sales Revenue"
debit_account = "cash"
credit_account = account_name
elif "supplies" in prompt_lower:
account_name = "supplies"
description = "Supplies Purchase"
debit_account = account_name
credit_account = "cash"
elif "salary" in prompt_lower or "payroll" in prompt_lower:
account_name = "salary"
description = "Salary Expense"
debit_account = account_name
credit_account = "cash"
else:
description = "Unrecognized Entry"
return pd.DataFrame([{"Date": "2025-04-01", "Description": description, "Account Code": "N/A", "Debit": 0, "Credit": 0}])
debit_code = f"{segment['company']}-{segment['business_type']}-{segment['location']}-{segment['cost_center']}-{account_map[debit_account]}-{segment['future']}"
credit_code = f"{segment['company']}-{segment['business_type']}-{segment['location']}-{segment['cost_center']}-{account_map[credit_account]}-{segment['future']}"
entry = [
{
"Date": "2025-04-01",
"Description": description,
"Account Code": debit_code,
"Debit": amount,
"Credit": 0
},
{
"Date": "2025-04-01",
"Description": f"Offset for {description.lower()}",
"Account Code": credit_code,
"Debit": 0,
"Credit": amount
}
]
st.session_state.gl_entries.extend(entry)
return pd.DataFrame(entry)
# Streamlit UI
st.set_page_config(page_title="AI ERP App", layout="wide")
st.title("AI-Powered ERP Accounting App")
prompt = st.text_input("π Enter your accounting instruction:")
download = st.download_button("π₯ Download All Entries (CSV)",
data=pd.DataFrame(st.session_state.gl_entries).to_csv(index=False),
file_name="gl_entries.csv",
mime="text/csv")
delete_records = st.button("ποΈ Delete All Records")
if delete_records:
st.session_state.gl_entries = []
st.success("β
All records have been deleted.")
if prompt:
result = handle_gl_entry(prompt)
st.dataframe(result)
if st.session_state.gl_entries:
st.subheader("π All Journal Entries")
st.dataframe(pd.DataFrame(st.session_state.gl_entries))
|