|
import streamlit as st |
|
import pandas as pd |
|
from huggingface_hub import InferenceClient |
|
import re |
|
from datetime import datetime |
|
|
|
|
|
client = InferenceClient(model="google/flan-t5-base") |
|
|
|
|
|
account_map = { |
|
"rent": "60001", |
|
"utilities": "60002", |
|
"capital": "30000", |
|
"cash": "10001", |
|
"bank": "10002", |
|
"sales": "40001", |
|
"supplies": "50001", |
|
"salary": "50002" |
|
} |
|
|
|
|
|
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" |
|
|
|
|
|
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 = "" |
|
|
|
|
|
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) |
|
|
|
|
|
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.") |
|
|
|
|
|
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)) |
|
|