ABC2 / app.py
ZeeAI1's picture
Update app.py
802eb89 verified
import streamlit as st
import pandas as pd
from huggingface_hub import InferenceClient
import re
from datetime import datetime
# 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"
}
# Session state
if "gl_entries" not in st.session_state:
st.session_state.gl_entries = []
if "company_name" not in st.session_state:
st.session_state.company_name = "My Company"
# Streamlit UI
st.set_page_config(page_title="AI ERP App", layout="wide")
st.title(f"πŸ“˜ {st.session_state.company_name} Ledger - AI-Powered ERP")
prompt = st.text_input("πŸ“Œ Enter your accounting instruction:")
def extract_amount(prompt):
match = re.search(r'\$?(\d{1,3}(,\d{3})*|\d+)(\.\d{1,2})?', prompt)
return float(match.group().replace(',', '').replace('$', '')) if match else 0
def extract_date(prompt):
date_match = re.search(r'(\d{1,2}[/-]\d{1,2}[/-]\d{2,4})|today', prompt.lower())
if date_match:
if "today" in date_match.group().lower():
return datetime.today().strftime("%Y-%m-%d")
try:
return datetime.strptime(date_match.group(), "%d/%m/%Y").strftime("%Y-%m-%d")
except:
try:
return datetime.strptime(date_match.group(), "%d-%m-%Y").strftime("%Y-%m-%d")
except:
return datetime.today().strftime("%Y-%m-%d")
return datetime.today().strftime("%Y-%m-%d")
def handle_gl_entry(prompt):
prompt_lower = prompt.lower()
amount = extract_amount(prompt)
date_str = extract_date(prompt)
description = ""
# Capture new company name
company_match = re.search(r"company with a name of ([\w\s&.-]+)", prompt_lower)
if company_match:
st.session_state.company_name = company_match.group(1).strip().upper()
if any(word in prompt_lower for word in ["invest", "capital", "start"]):
description = "Owner Capital Contribution"
debit_account, credit_account = "cash", "capital"
elif "rent" in prompt_lower:
description = "Rent Expense"
debit_account, credit_account = "rent", "cash"
elif "utilities" in prompt_lower:
description = "Utilities Expense"
debit_account, credit_account = "utilities", "cash"
elif any(word in prompt_lower for word in ["sale", "revenue"]):
description = "Sales Revenue"
debit_account, credit_account = "cash", "sales"
elif "supplies" in prompt_lower:
description = "Supplies Purchase"
debit_account, credit_account = "supplies", "cash"
elif "salary" in prompt_lower or "payroll" in prompt_lower:
description = "Salary Expense"
debit_account, credit_account = "salary", "cash"
else:
return pd.DataFrame([{"Date": date_str, "Description": "Unrecognized Entry", "Account Code": "N/A", "Account Type": "N/A", "Debit": 0, "Credit": 0}])
def format_code(name):
return f"01-102-001-001-{account_map[name]}-000"
entry = [
{
"Date": date_str,
"Description": description,
"Account Code": format_code(debit_account),
"Account Type": debit_account.title(),
"Debit": amount,
"Credit": 0
},
{
"Date": date_str,
"Description": f"Offset for {description.lower()}",
"Account Code": format_code(credit_account),
"Account Type": credit_account.title(),
"Debit": 0,
"Credit": amount
}
]
st.session_state.gl_entries.extend(entry)
return pd.DataFrame(entry)
# Buttons
col1, col2 = st.columns([1, 1])
with col1:
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")
with col2:
if st.button("πŸ—‘οΈ Delete All Records"):
st.session_state.gl_entries = []
st.success("βœ… All records have been deleted.")
# Handle input
if prompt:
result = handle_gl_entry(prompt)
st.dataframe(result)
# Display ledger
if st.session_state.gl_entries:
st.subheader("πŸ“Š All Journal Entries")
st.dataframe(pd.DataFrame(st.session_state.gl_entries))