Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
from huggingface_hub import InferenceClient
|
|
|
|
| 4 |
|
| 5 |
# Initialize hosted inference client
|
| 6 |
client = InferenceClient(model="google/flan-t5-base")
|
|
@@ -9,60 +10,111 @@ client = InferenceClient(model="google/flan-t5-base")
|
|
| 9 |
account_map = {
|
| 10 |
"rent": "60001",
|
| 11 |
"utilities": "60002",
|
|
|
|
| 12 |
"cash": "10001",
|
| 13 |
-
"bank": "10002"
|
|
|
|
|
|
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
# Simulated business segments
|
| 17 |
segment = {
|
| 18 |
"company": "01",
|
| 19 |
-
"business_type": "102",
|
| 20 |
"location": "001",
|
| 21 |
"cost_center": "001",
|
| 22 |
"future": "000"
|
| 23 |
}
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def parse_prompt(prompt):
|
| 26 |
-
|
| 27 |
-
return response
|
| 28 |
|
| 29 |
def handle_gl_entry(prompt):
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
account_name = "rent"
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
account_name = "utilities"
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
entry =
|
| 45 |
{
|
| 46 |
"Date": "2025-04-01",
|
| 47 |
-
"Description":
|
| 48 |
-
"Account Code":
|
| 49 |
"Debit": amount,
|
| 50 |
"Credit": 0
|
| 51 |
},
|
| 52 |
{
|
| 53 |
"Date": "2025-04-01",
|
| 54 |
-
"Description": f"
|
| 55 |
-
"Account Code":
|
| 56 |
"Debit": 0,
|
| 57 |
"Credit": amount
|
| 58 |
}
|
| 59 |
-
]
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
# Streamlit UI
|
| 63 |
st.title("AI ERP Chat - MVP")
|
| 64 |
prompt = st.text_input("Enter your accounting instruction:")
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if prompt:
|
| 67 |
result = handle_gl_entry(prompt)
|
| 68 |
-
st.dataframe(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|
|
|
|
| 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 |
def parse_prompt(prompt):
|
| 35 |
+
return client.text_generation(prompt=f"Extract accounting entry: {prompt}", max_new_tokens=50).strip()
|
|
|
|
| 36 |
|
| 37 |
def handle_gl_entry(prompt):
|
| 38 |
+
prompt_lower = prompt.lower()
|
| 39 |
+
amount = 0
|
| 40 |
+
account_name = ""
|
| 41 |
+
|
| 42 |
+
# Extract amount using regex
|
| 43 |
+
amount_match = re.search(r'(\d{1,3}(,\d{3})*|\d+)(\.\d{1,2})?', prompt)
|
| 44 |
+
if amount_match:
|
| 45 |
+
amount = float(amount_match.group().replace(',', ''))
|
| 46 |
+
|
| 47 |
+
# Identify transaction type
|
| 48 |
+
if any(word in prompt_lower for word in ["invest", "capital", "start"]):
|
| 49 |
+
account_name = "capital"
|
| 50 |
+
description = "Owner Capital Contribution"
|
| 51 |
+
debit_account = "cash"
|
| 52 |
+
credit_account = account_name
|
| 53 |
+
elif "rent" in prompt_lower:
|
| 54 |
account_name = "rent"
|
| 55 |
+
description = "Rent Expense"
|
| 56 |
+
debit_account = account_name
|
| 57 |
+
credit_account = "cash"
|
| 58 |
+
elif "utilities" in prompt_lower:
|
| 59 |
account_name = "utilities"
|
| 60 |
+
description = "Utilities Expense"
|
| 61 |
+
debit_account = account_name
|
| 62 |
+
credit_account = "cash"
|
| 63 |
+
elif any(word in prompt_lower for word in ["sale", "revenue"]):
|
| 64 |
+
account_name = "sales"
|
| 65 |
+
description = "Sales Revenue"
|
| 66 |
+
debit_account = "cash"
|
| 67 |
+
credit_account = account_name
|
| 68 |
+
elif "supplies" in prompt_lower:
|
| 69 |
+
account_name = "supplies"
|
| 70 |
+
description = "Supplies Purchase"
|
| 71 |
+
debit_account = account_name
|
| 72 |
+
credit_account = "cash"
|
| 73 |
+
elif "salary" in prompt_lower or "payroll" in prompt_lower:
|
| 74 |
+
account_name = "salary"
|
| 75 |
+
description = "Salary Expense"
|
| 76 |
+
debit_account = account_name
|
| 77 |
+
credit_account = "cash"
|
| 78 |
+
else:
|
| 79 |
+
description = "Unrecognized Entry"
|
| 80 |
+
return pd.DataFrame([{"Date": "2025-04-01", "Description": description, "Account Code": "N/A", "Debit": 0, "Credit": 0}])
|
| 81 |
|
| 82 |
+
debit_code = f"{segment['company']}-{segment['business_type']}-{segment['location']}-{segment['cost_center']}-{account_map[debit_account]}-{segment['future']}"
|
| 83 |
+
credit_code = f"{segment['company']}-{segment['business_type']}-{segment['location']}-{segment['cost_center']}-{account_map[credit_account]}-{segment['future']}"
|
| 84 |
|
| 85 |
+
entry = [
|
| 86 |
{
|
| 87 |
"Date": "2025-04-01",
|
| 88 |
+
"Description": description,
|
| 89 |
+
"Account Code": debit_code,
|
| 90 |
"Debit": amount,
|
| 91 |
"Credit": 0
|
| 92 |
},
|
| 93 |
{
|
| 94 |
"Date": "2025-04-01",
|
| 95 |
+
"Description": f"Offset for {description.lower()}",
|
| 96 |
+
"Account Code": credit_code,
|
| 97 |
"Debit": 0,
|
| 98 |
"Credit": amount
|
| 99 |
}
|
| 100 |
+
]
|
| 101 |
+
st.session_state.gl_entries.extend(entry)
|
| 102 |
+
return pd.DataFrame(entry)
|
| 103 |
|
| 104 |
# Streamlit UI
|
| 105 |
st.title("AI ERP Chat - MVP")
|
| 106 |
prompt = st.text_input("Enter your accounting instruction:")
|
| 107 |
|
| 108 |
+
delete_records = st.button("Delete All Records")
|
| 109 |
+
if delete_records:
|
| 110 |
+
st.session_state.gl_entries = []
|
| 111 |
+
st.success("All records have been deleted.")
|
| 112 |
+
|
| 113 |
if prompt:
|
| 114 |
result = handle_gl_entry(prompt)
|
| 115 |
+
st.dataframe(result)
|
| 116 |
+
|
| 117 |
+
# Show saved entries
|
| 118 |
+
if st.session_state.gl_entries:
|
| 119 |
+
st.subheader("All Recorded Entries")
|
| 120 |
+
st.dataframe(pd.DataFrame(st.session_state.gl_entries))
|