Update app.py
Browse files
app.py
CHANGED
@@ -109,7 +109,6 @@ def parse_prompt(prompt, state):
|
|
109 |
except Exception as e:
|
110 |
logging.error(f"Model parsing failed: {e}")
|
111 |
|
112 |
-
# Fallback parser
|
113 |
prompt_lower = prompt.lower().strip()
|
114 |
amount = None
|
115 |
match = re.search(r'\$[\d,.]+', prompt_lower)
|
@@ -122,7 +121,6 @@ def parse_prompt(prompt, state):
|
|
122 |
if not amount:
|
123 |
return {"error": "No amount found in prompt."}, state
|
124 |
|
125 |
-
# Account mappings
|
126 |
account_mappings = {
|
127 |
"laptop": ("Laptop", "Asset"),
|
128 |
"inventory": ("Inventory", "Asset"),
|
@@ -150,7 +148,6 @@ def parse_prompt(prompt, state):
|
|
150 |
credit_type = None
|
151 |
payment_method = None
|
152 |
|
153 |
-
# Handle follow-up response
|
154 |
if state.get("pending_prompt"):
|
155 |
follow_up = prompt_lower
|
156 |
if follow_up in ["cash", "credit", "bank"]:
|
@@ -168,9 +165,7 @@ def parse_prompt(prompt, state):
|
|
168 |
state = {} # Clear state
|
169 |
else:
|
170 |
return {"error": "Please respond with 'cash', 'credit', or 'bank'."}, state
|
171 |
-
|
172 |
else:
|
173 |
-
# Parse new prompt
|
174 |
for keyword, (account, acc_type) in account_mappings.items():
|
175 |
if keyword in prompt_lower:
|
176 |
if keyword in ["bought", "purchased"]:
|
@@ -235,7 +230,6 @@ def generate_journal_entry(parsed, state):
|
|
235 |
credit_account = parsed["credit"]["account"]
|
236 |
credit_type = parsed["credit"]["type"]
|
237 |
|
238 |
-
# Validate accounts
|
239 |
cursor.execute("SELECT account_id, account_type, allow_posting FROM chart_of_accounts WHERE account_name = ?", (debit_account,))
|
240 |
debit_result = cursor.fetchone()
|
241 |
cursor.execute("SELECT account_id, account_type, allow_posting FROM chart_of_accounts WHERE account_name = ?", (credit_account,))
|
@@ -248,7 +242,6 @@ def generate_journal_entry(parsed, state):
|
|
248 |
if debit_result[1] != parsed["debit"]["type"] or credit_result[1] != credit_type:
|
249 |
return "Account type mismatch.", state
|
250 |
|
251 |
-
# Create journal entry
|
252 |
entry_id = str(uuid.uuid4())
|
253 |
date = datetime.datetime.now().isoformat()
|
254 |
description = state.get("pending_prompt", "Transaction")
|
@@ -310,19 +303,25 @@ def chat_function(message, history, state=None):
|
|
310 |
|
311 |
logging.info(f"Received message: {message}")
|
312 |
|
313 |
-
# Handle T-account request
|
314 |
if message.lower().startswith("t-account "):
|
315 |
account_name = message[10:].strip()
|
316 |
if account_name:
|
317 |
return {"role": "assistant", "content": generate_t_account(account_name)}, state
|
318 |
return {"role": "assistant", "content": "Please specify an account name."}, state
|
319 |
|
320 |
-
# Parse prompt and generate entry
|
321 |
parsed, state = parse_prompt(message, state)
|
322 |
response, state = generate_journal_entry(parsed, state)
|
323 |
|
324 |
-
|
325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
326 |
|
327 |
# Gradio interface
|
328 |
with gr.Blocks() as demo:
|
@@ -332,11 +331,10 @@ with gr.Blocks() as demo:
|
|
332 |
msg = gr.Textbox(placeholder="Type your prompt here...", lines=2)
|
333 |
clear = gr.Button("Clear")
|
334 |
|
335 |
-
# Maintain state
|
336 |
state = gr.State({})
|
337 |
|
338 |
-
msg.submit(chat_function, [msg, chatbot, state], [chatbot, state])
|
339 |
-
clear.click(lambda: (
|
340 |
|
341 |
# Launch Gradio
|
342 |
if __name__ == "__main__":
|
|
|
109 |
except Exception as e:
|
110 |
logging.error(f"Model parsing failed: {e}")
|
111 |
|
|
|
112 |
prompt_lower = prompt.lower().strip()
|
113 |
amount = None
|
114 |
match = re.search(r'\$[\d,.]+', prompt_lower)
|
|
|
121 |
if not amount:
|
122 |
return {"error": "No amount found in prompt."}, state
|
123 |
|
|
|
124 |
account_mappings = {
|
125 |
"laptop": ("Laptop", "Asset"),
|
126 |
"inventory": ("Inventory", "Asset"),
|
|
|
148 |
credit_type = None
|
149 |
payment_method = None
|
150 |
|
|
|
151 |
if state.get("pending_prompt"):
|
152 |
follow_up = prompt_lower
|
153 |
if follow_up in ["cash", "credit", "bank"]:
|
|
|
165 |
state = {} # Clear state
|
166 |
else:
|
167 |
return {"error": "Please respond with 'cash', 'credit', or 'bank'."}, state
|
|
|
168 |
else:
|
|
|
169 |
for keyword, (account, acc_type) in account_mappings.items():
|
170 |
if keyword in prompt_lower:
|
171 |
if keyword in ["bought", "purchased"]:
|
|
|
230 |
credit_account = parsed["credit"]["account"]
|
231 |
credit_type = parsed["credit"]["type"]
|
232 |
|
|
|
233 |
cursor.execute("SELECT account_id, account_type, allow_posting FROM chart_of_accounts WHERE account_name = ?", (debit_account,))
|
234 |
debit_result = cursor.fetchone()
|
235 |
cursor.execute("SELECT account_id, account_type, allow_posting FROM chart_of_accounts WHERE account_name = ?", (credit_account,))
|
|
|
242 |
if debit_result[1] != parsed["debit"]["type"] or credit_result[1] != credit_type:
|
243 |
return "Account type mismatch.", state
|
244 |
|
|
|
245 |
entry_id = str(uuid.uuid4())
|
246 |
date = datetime.datetime.now().isoformat()
|
247 |
description = state.get("pending_prompt", "Transaction")
|
|
|
303 |
|
304 |
logging.info(f"Received message: {message}")
|
305 |
|
|
|
306 |
if message.lower().startswith("t-account "):
|
307 |
account_name = message[10:].strip()
|
308 |
if account_name:
|
309 |
return {"role": "assistant", "content": generate_t_account(account_name)}, state
|
310 |
return {"role": "assistant", "content": "Please specify an account name."}, state
|
311 |
|
|
|
312 |
parsed, state = parse_prompt(message, state)
|
313 |
response, state = generate_journal_entry(parsed, state)
|
314 |
|
315 |
+
if history is not None:
|
316 |
+
history.append({"role": "user", "content": message})
|
317 |
+
history.append({"role": "assistant", "content": response})
|
318 |
+
else:
|
319 |
+
history = [
|
320 |
+
{"role": "user", "content": message},
|
321 |
+
{"role": "assistant", "content": response}
|
322 |
+
]
|
323 |
+
|
324 |
+
return history, state
|
325 |
|
326 |
# Gradio interface
|
327 |
with gr.Blocks() as demo:
|
|
|
331 |
msg = gr.Textbox(placeholder="Type your prompt here...", lines=2)
|
332 |
clear = gr.Button("Clear")
|
333 |
|
|
|
334 |
state = gr.State({})
|
335 |
|
336 |
+
msg.submit(chat_function, [msg, chatbot, state], [chatbot, state], _js="() => {}")
|
337 |
+
clear.click(lambda: ([], {}), None, [chatbot, state], queue=False)
|
338 |
|
339 |
# Launch Gradio
|
340 |
if __name__ == "__main__":
|