suratan.boon commited on
Commit ·
c529ed6
1
Parent(s): 1b36b31
connect backend with frontend
Browse files- src/chatbot/app_ui.py +0 -84
- src/chatbot/graph_manager.py +8 -4
- src/chatbot/kie.py +71 -2
- src/chatbot/nodes.py +5 -0
src/chatbot/app_ui.py
DELETED
|
@@ -1,84 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
-
def respond(message, history, image):
|
| 4 |
-
history = history or []
|
| 5 |
-
if message:
|
| 6 |
-
history.append({"role": "user", "content": message})
|
| 7 |
-
bot_reply = f"Echo: {message}"
|
| 8 |
-
if image:
|
| 9 |
-
history.append({"role": "user", "content": gr.Image(value=image, interactive=False)})
|
| 10 |
-
bot_reply = f"Echo: {image.name}"
|
| 11 |
-
|
| 12 |
-
history.append({"role": "assistant", "content": bot_reply})
|
| 13 |
-
return history, "", None
|
| 14 |
-
|
| 15 |
-
def upload_image(image, chat_history):
|
| 16 |
-
return respond("", chat_history, image)
|
| 17 |
-
|
| 18 |
-
def submit_form(bill_date, amount, account, description):
|
| 19 |
-
return {
|
| 20 |
-
"วันที่ของบิล": bill_date,
|
| 21 |
-
"ค่าใช้จ่ายทั้งหมด (บาท)": amount,
|
| 22 |
-
"บัญชีที่เกี่ยวข้อง": account,
|
| 23 |
-
"คำอธิบายเพิ่มเติม": description
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
with gr.Blocks() as demo:
|
| 27 |
-
new_btn = gr.Button("เริ่มใหม่", variant="primary")
|
| 28 |
-
new_btn.click(lambda: None, [], [])
|
| 29 |
-
|
| 30 |
-
with gr.Tabs():
|
| 31 |
-
# Chat tab
|
| 32 |
-
with gr.Tab("แชท"):
|
| 33 |
-
chatbot = gr.Chatbot(
|
| 34 |
-
label="Chat History",
|
| 35 |
-
height=400,
|
| 36 |
-
type="messages",
|
| 37 |
-
value=[]
|
| 38 |
-
)
|
| 39 |
-
with gr.Row():
|
| 40 |
-
img_upload = gr.UploadButton(label="📎", file_types=["image"])
|
| 41 |
-
user_input = gr.Textbox(placeholder="พิมพ์ข้อความ...", lines=1)
|
| 42 |
-
send_btn = gr.Button("+")
|
| 43 |
-
|
| 44 |
-
# Auto‐send the image and echo its filename, then clear the uploader
|
| 45 |
-
img_upload.upload(
|
| 46 |
-
fn=upload_image,
|
| 47 |
-
inputs=[img_upload, chatbot],
|
| 48 |
-
outputs=[chatbot, user_input, img_upload]
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
# Manual send for text
|
| 52 |
-
send_btn.click(
|
| 53 |
-
fn=respond,
|
| 54 |
-
inputs=[user_input, chatbot, img_upload],
|
| 55 |
-
outputs=[chatbot, user_input, img_upload]
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
# Form tab
|
| 59 |
-
with gr.Tab("ฟอร์ม"):
|
| 60 |
-
gr.Markdown("**วันที่ของบิล**")
|
| 61 |
-
dt_input = gr.DateTime(
|
| 62 |
-
value="now",
|
| 63 |
-
include_time=True,
|
| 64 |
-
type="datetime",
|
| 65 |
-
show_label=False
|
| 66 |
-
)
|
| 67 |
-
gr.Markdown("**ค่าใช้จ่ายทั้งหมด**")
|
| 68 |
-
with gr.Row():
|
| 69 |
-
amount = gr.Number(label="", container=False)
|
| 70 |
-
gr.Markdown("บาท")
|
| 71 |
-
gr.Markdown("**บัญชีที่เกี่ยวข้อง**")
|
| 72 |
-
account = gr.Textbox(label="", container=False)
|
| 73 |
-
gr.Markdown("**คำอธิบายเพิ่มเติม**")
|
| 74 |
-
description = gr.Textbox(label="", container=False, lines=5)
|
| 75 |
-
submit_btn = gr.Button("Submit", variant="primary")
|
| 76 |
-
output = gr.JSON(label="ผลลัพธ์")
|
| 77 |
-
submit_btn.click(
|
| 78 |
-
fn=submit_form,
|
| 79 |
-
inputs=[dt_input, amount, account, description],
|
| 80 |
-
outputs=output
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
if __name__ == "__main__":
|
| 84 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/chatbot/graph_manager.py
CHANGED
|
@@ -69,6 +69,10 @@ class GraphManager:
|
|
| 69 |
snapshot = self.graph.get_state(config)
|
| 70 |
return snapshot[0]["messages"][-1].content
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
graph_manager = GraphManager()
|
| 73 |
graph_manager.save_graph_image()
|
| 74 |
|
|
@@ -78,10 +82,10 @@ graph_manager.save_graph_image()
|
|
| 78 |
# 1
|
| 79 |
# )
|
| 80 |
|
| 81 |
-
response = graph_manager.process_user_input(
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
)
|
| 85 |
|
| 86 |
# response = graph_manager.process_user_input(
|
| 87 |
# r"C:\Users\Gavin\Desktop\hello-earth\data\jpeg_raw_data\bill\กองทุนพัฒนาบ้านห้วยทราย อ.แม่ออน จ.เชียงใหม่_page-0004.jpg",
|
|
|
|
| 69 |
snapshot = self.graph.get_state(config)
|
| 70 |
return snapshot[0]["messages"][-1].content
|
| 71 |
|
| 72 |
+
def user_input_handler(user_input, session_id):
|
| 73 |
+
response = graph_manager.process_user_input(user_input, session_id)
|
| 74 |
+
return response
|
| 75 |
+
|
| 76 |
graph_manager = GraphManager()
|
| 77 |
graph_manager.save_graph_image()
|
| 78 |
|
|
|
|
| 82 |
# 1
|
| 83 |
# )
|
| 84 |
|
| 85 |
+
# response = graph_manager.process_user_input(
|
| 86 |
+
# r"C:\Users\Gavin\Desktop\hello-earth\data\jpeg_raw_data\bill\กองทุนพัฒนาบ้านห้วยทราย อ.แม่ออน จ.เชียงใหม่_page-0005.jpg",
|
| 87 |
+
# 1
|
| 88 |
+
# )
|
| 89 |
|
| 90 |
# response = graph_manager.process_user_input(
|
| 91 |
# r"C:\Users\Gavin\Desktop\hello-earth\data\jpeg_raw_data\bill\กองทุนพัฒนาบ้านห้วยทราย อ.แม่ออน จ.เชียงใหม่_page-0004.jpg",
|
src/chatbot/kie.py
CHANGED
|
@@ -8,6 +8,7 @@ from dotenv import load_dotenv
|
|
| 8 |
import os
|
| 9 |
# from prompts import kie_prompt
|
| 10 |
from prompts.kie_prompts import kie_prompt
|
|
|
|
| 11 |
import ast
|
| 12 |
# Load environment variables from .env file
|
| 13 |
load_dotenv()
|
|
@@ -51,8 +52,8 @@ def receipt_kie(image_path: str) -> str:
|
|
| 51 |
temp_dict = temp_dict.replace("null", "None")
|
| 52 |
# print(temp_dict)
|
| 53 |
# print(type(temp_dict))
|
| 54 |
-
|
| 55 |
-
res_dict = ast.literal_eval(
|
| 56 |
except Exception as e:
|
| 57 |
print(f"Error: {e}")
|
| 58 |
res_dict = {
|
|
@@ -70,6 +71,74 @@ def receipt_kie(image_path: str) -> str:
|
|
| 70 |
}
|
| 71 |
return res_dict
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
# # test case
|
| 74 |
# res = receipt_kie(r"C:\Users\Gavin\Desktop\hello-earth\data\jpeg_raw_data\bill\กองทุนพัฒนาบ้านห้วยทราย อ.แม่ออน จ.เชียงใหม่_page-0004.jpg")
|
| 75 |
# # res = ast.literal_eval(res)
|
|
|
|
| 8 |
import os
|
| 9 |
# from prompts import kie_prompt
|
| 10 |
from prompts.kie_prompts import kie_prompt
|
| 11 |
+
from prompts.extract_form_information_prompts import extract_form_information_prompt
|
| 12 |
import ast
|
| 13 |
# Load environment variables from .env file
|
| 14 |
load_dotenv()
|
|
|
|
| 52 |
temp_dict = temp_dict.replace("null", "None")
|
| 53 |
# print(temp_dict)
|
| 54 |
# print(type(temp_dict))
|
| 55 |
+
res_dict = extract_form_information(client, temp_dict)
|
| 56 |
+
# res_dict = ast.literal_eval(format_dict)
|
| 57 |
except Exception as e:
|
| 58 |
print(f"Error: {e}")
|
| 59 |
res_dict = {
|
|
|
|
| 71 |
}
|
| 72 |
return res_dict
|
| 73 |
|
| 74 |
+
def extract_form_information(client, receipt_text, max_retries=100):
|
| 75 |
+
|
| 76 |
+
json_schema = {
|
| 77 |
+
"type": "object",
|
| 78 |
+
"properties": {
|
| 79 |
+
"Receipt Title": {"type": "string"},
|
| 80 |
+
"Project Name": {"type": "string"},
|
| 81 |
+
"Date": {"type": "string"},
|
| 82 |
+
"Receipt Number": {"type": "string"},
|
| 83 |
+
"Payer's Name": {"type": "string"},
|
| 84 |
+
"Payment Details Table": {
|
| 85 |
+
"type": "object",
|
| 86 |
+
"properties": {
|
| 87 |
+
"Column Headers": {
|
| 88 |
+
"type": "array",
|
| 89 |
+
"items": {"type": "string"}
|
| 90 |
+
},
|
| 91 |
+
"Row Entries": {
|
| 92 |
+
"type": "array",
|
| 93 |
+
"items": {
|
| 94 |
+
"type": "object",
|
| 95 |
+
"properties": {
|
| 96 |
+
"Description": {"type": "string"},
|
| 97 |
+
"Quantity": {"type": "string"},
|
| 98 |
+
"Unit Price": {"type": "number"},
|
| 99 |
+
"Total Amount": {"type": "number"}
|
| 100 |
+
},
|
| 101 |
+
"required": ["Description", "Quantity", "Unit Price", "Total Amount"]
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
},
|
| 105 |
+
"required": ["Column Headers", "Row Entries"]
|
| 106 |
+
},
|
| 107 |
+
"Total Payment Amount": {"type": "number"},
|
| 108 |
+
"Incompleteness Description": {"type": "string"},
|
| 109 |
+
"Expense Justification": {"type": "string"}
|
| 110 |
+
},
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
for attempt in range(1, max_retries + 1):
|
| 114 |
+
model = "gpt-4.1-mini"
|
| 115 |
+
|
| 116 |
+
response = client.chat.completions.create(
|
| 117 |
+
model=model,
|
| 118 |
+
messages=[
|
| 119 |
+
{
|
| 120 |
+
"role": "system",
|
| 121 |
+
"content": "You are a financial document analyst. Extract structured receipt data as a Python dictionary."
|
| 122 |
+
},
|
| 123 |
+
{
|
| 124 |
+
"role": "user",
|
| 125 |
+
"content": extract_form_information_prompt.format(receipt_text)
|
| 126 |
+
}
|
| 127 |
+
],
|
| 128 |
+
response_format={"type": "json_schema", "json_schema": {"name": "form_info", "schema": json_schema}}
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
content = response.choices[0].message.content
|
| 132 |
+
|
| 133 |
+
try:
|
| 134 |
+
print("content: ", content)
|
| 135 |
+
content = ast.literal_eval(content)
|
| 136 |
+
return content
|
| 137 |
+
except:
|
| 138 |
+
pass
|
| 139 |
+
|
| 140 |
+
raise ValueError("Failed to extract valid form info from receipt after multiple retries.")
|
| 141 |
+
|
| 142 |
# # test case
|
| 143 |
# res = receipt_kie(r"C:\Users\Gavin\Desktop\hello-earth\data\jpeg_raw_data\bill\กองทุนพัฒนาบ้านห้วยทราย อ.แม่ออน จ.เชียงใหม่_page-0004.jpg")
|
| 144 |
# # res = ast.literal_eval(res)
|
src/chatbot/nodes.py
CHANGED
|
@@ -32,6 +32,11 @@ form_info = {
|
|
| 32 |
"Expense Justification": 'None',
|
| 33 |
}
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
class State(TypedDict):
|
| 37 |
messages: Annotated[list, add_messages]
|
|
|
|
| 32 |
"Expense Justification": 'None',
|
| 33 |
}
|
| 34 |
|
| 35 |
+
def get_form_info():
|
| 36 |
+
return form_info
|
| 37 |
+
|
| 38 |
+
def set_form_info(key, value):
|
| 39 |
+
form_info[key] = value
|
| 40 |
|
| 41 |
class State(TypedDict):
|
| 42 |
messages: Annotated[list, add_messages]
|