fix/session-id-and-inspect-tool-bug
Browse files- src/chatbot/app_interface.py +18 -10
- src/chatbot/nodes.py +4 -4
src/chatbot/app_interface.py
CHANGED
|
@@ -11,9 +11,10 @@ logging.basicConfig(level=logging.INFO)
|
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
def generate_session_id():
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
CURRENT_SESSION_ID = generate_session_id()
|
| 17 |
DB_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'database', 'hello_earth_data_2.db')
|
| 18 |
|
| 19 |
def connect_db():
|
|
@@ -128,14 +129,14 @@ _chat_prefill = [
|
|
| 128 |
{"role": "assistant", "content": "Hello, my name is Emmie. I'm happy to help you with submitting your expense items. You can start by uploading a picture of one bill."}
|
| 129 |
]
|
| 130 |
|
| 131 |
-
def chat_handler(message,
|
| 132 |
-
global CURRENT_SESSION_ID
|
| 133 |
logger.info(f"Received message: {message}")
|
| 134 |
if len(message['files']) > 0:
|
| 135 |
-
response = user_input_handler(str(message['files'][0]),
|
| 136 |
elif message['text'] != "":
|
| 137 |
try:
|
| 138 |
-
response = user_input_handler(message['text'],
|
| 139 |
except Exception as e:
|
| 140 |
raise e
|
| 141 |
form_information = get_form_info()
|
|
@@ -144,7 +145,9 @@ def chat_handler(message, history):
|
|
| 144 |
|
| 145 |
def create_expense_register_page():
|
| 146 |
with gr.Blocks() as page:
|
|
|
|
| 147 |
form_information = gr.State(form_info)
|
|
|
|
| 148 |
new_btn = gr.Button("Reset", variant="primary")
|
| 149 |
with gr.Tabs():
|
| 150 |
with gr.Tab("Chat"):
|
|
@@ -156,6 +159,7 @@ def create_expense_register_page():
|
|
| 156 |
submit_btn="Send Message",
|
| 157 |
autofocus=True,
|
| 158 |
examples=None,
|
|
|
|
| 159 |
additional_outputs=[form_information]
|
| 160 |
)
|
| 161 |
|
|
@@ -207,11 +211,12 @@ def create_expense_register_page():
|
|
| 207 |
submit_btn.click(fn=handle_submit, inputs=[], outputs=[feedback_message])
|
| 208 |
|
| 209 |
def reset_chat():
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
logger.info(f"Chat reset with new session ID: {CURRENT_SESSION_ID}")
|
| 213 |
form_info.clear()
|
| 214 |
return (
|
|
|
|
|
|
|
| 215 |
_chat_prefill, # ChatInterface.chatbot_value
|
| 216 |
form_info, # State(form_info)
|
| 217 |
"None", # deliverable_dropdown
|
|
@@ -230,6 +235,8 @@ def create_expense_register_page():
|
|
| 230 |
fn=reset_chat,
|
| 231 |
inputs=[],
|
| 232 |
outputs=[
|
|
|
|
|
|
|
| 233 |
chat_interface.chatbot_value,
|
| 234 |
form_information,
|
| 235 |
deliverable_dropdown,
|
|
@@ -244,7 +251,8 @@ def create_expense_register_page():
|
|
| 244 |
feedback_message
|
| 245 |
],
|
| 246 |
)
|
| 247 |
-
|
|
|
|
| 248 |
return page
|
| 249 |
|
| 250 |
if __name__ == "__main__":
|
|
|
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
def generate_session_id():
|
| 14 |
+
session_id = str(uuid.uuid4())
|
| 15 |
+
return session_id, f"Session ID: {session_id}"
|
| 16 |
|
| 17 |
+
# CURRENT_SESSION_ID = generate_session_id()
|
| 18 |
DB_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'database', 'hello_earth_data_2.db')
|
| 19 |
|
| 20 |
def connect_db():
|
|
|
|
| 129 |
{"role": "assistant", "content": "Hello, my name is Emmie. I'm happy to help you with submitting your expense items. You can start by uploading a picture of one bill."}
|
| 130 |
]
|
| 131 |
|
| 132 |
+
def chat_handler(message,history,session_id):
|
| 133 |
+
# global CURRENT_SESSION_ID
|
| 134 |
logger.info(f"Received message: {message}")
|
| 135 |
if len(message['files']) > 0:
|
| 136 |
+
response = user_input_handler(str(message['files'][0]), session_id)
|
| 137 |
elif message['text'] != "":
|
| 138 |
try:
|
| 139 |
+
response = user_input_handler(message['text'], session_id)
|
| 140 |
except Exception as e:
|
| 141 |
raise e
|
| 142 |
form_information = get_form_info()
|
|
|
|
| 145 |
|
| 146 |
def create_expense_register_page():
|
| 147 |
with gr.Blocks() as page:
|
| 148 |
+
session_id = gr.State("")
|
| 149 |
form_information = gr.State(form_info)
|
| 150 |
+
session_id_display = gr.Markdown("Session ID: Loading...")
|
| 151 |
new_btn = gr.Button("Reset", variant="primary")
|
| 152 |
with gr.Tabs():
|
| 153 |
with gr.Tab("Chat"):
|
|
|
|
| 159 |
submit_btn="Send Message",
|
| 160 |
autofocus=True,
|
| 161 |
examples=None,
|
| 162 |
+
additional_inputs=[session_id],
|
| 163 |
additional_outputs=[form_information]
|
| 164 |
)
|
| 165 |
|
|
|
|
| 211 |
submit_btn.click(fn=handle_submit, inputs=[], outputs=[feedback_message])
|
| 212 |
|
| 213 |
def reset_chat():
|
| 214 |
+
new_session_id , new_session_id_display = generate_session_id()
|
| 215 |
+
logger.info(f"Chat reset with new session ID: {new_session_id}")
|
|
|
|
| 216 |
form_info.clear()
|
| 217 |
return (
|
| 218 |
+
new_session_id,
|
| 219 |
+
new_session_id_display,
|
| 220 |
_chat_prefill, # ChatInterface.chatbot_value
|
| 221 |
form_info, # State(form_info)
|
| 222 |
"None", # deliverable_dropdown
|
|
|
|
| 235 |
fn=reset_chat,
|
| 236 |
inputs=[],
|
| 237 |
outputs=[
|
| 238 |
+
session_id,
|
| 239 |
+
session_id_display,
|
| 240 |
chat_interface.chatbot_value,
|
| 241 |
form_information,
|
| 242 |
deliverable_dropdown,
|
|
|
|
| 251 |
feedback_message
|
| 252 |
],
|
| 253 |
)
|
| 254 |
+
|
| 255 |
+
page.load(generate_session_id, outputs=[session_id, session_id_display])
|
| 256 |
return page
|
| 257 |
|
| 258 |
if __name__ == "__main__":
|
src/chatbot/nodes.py
CHANGED
|
@@ -74,7 +74,7 @@ class State(TypedDict):
|
|
| 74 |
|
| 75 |
# session_id: str might need to be added later when there are concurrent users
|
| 76 |
@tool
|
| 77 |
-
def apply_ocr(image_path: str) ->
|
| 78 |
"""
|
| 79 |
Use this tool when the user provides an image path.
|
| 80 |
Apply optical-character-recognition (OCR) to the image and initalize the form information.
|
|
@@ -109,7 +109,7 @@ def apply_ocr(image_path: str) -> dict:
|
|
| 109 |
"""
|
| 110 |
|
| 111 |
@tool
|
| 112 |
-
def edit_form(key: str, value: str) ->
|
| 113 |
"""
|
| 114 |
This is used to edit the form data.
|
| 115 |
The key must be one of the keys in the form_info dictionary.
|
|
@@ -152,13 +152,13 @@ def edit_form(key: str, value: str) -> dict:
|
|
| 152 |
return f"Invalid key: {key}. No changes made."
|
| 153 |
|
| 154 |
@tool
|
| 155 |
-
def inspect_form() ->
|
| 156 |
"""
|
| 157 |
This is used to inspect the form data.
|
| 158 |
Use this when you need to address any inquiries the user might have about the current state of the form.
|
| 159 |
"""
|
| 160 |
print("using inspect_form tool")
|
| 161 |
-
return """
|
| 162 |
<form_status>
|
| 163 |
{form_info}
|
| 164 |
</form_status>
|
|
|
|
| 74 |
|
| 75 |
# session_id: str might need to be added later when there are concurrent users
|
| 76 |
@tool
|
| 77 |
+
def apply_ocr(image_path: str) -> str:
|
| 78 |
"""
|
| 79 |
Use this tool when the user provides an image path.
|
| 80 |
Apply optical-character-recognition (OCR) to the image and initalize the form information.
|
|
|
|
| 109 |
"""
|
| 110 |
|
| 111 |
@tool
|
| 112 |
+
def edit_form(key: str, value: str) -> str:
|
| 113 |
"""
|
| 114 |
This is used to edit the form data.
|
| 115 |
The key must be one of the keys in the form_info dictionary.
|
|
|
|
| 152 |
return f"Invalid key: {key}. No changes made."
|
| 153 |
|
| 154 |
@tool
|
| 155 |
+
def inspect_form() -> str:
|
| 156 |
"""
|
| 157 |
This is used to inspect the form data.
|
| 158 |
Use this when you need to address any inquiries the user might have about the current state of the form.
|
| 159 |
"""
|
| 160 |
print("using inspect_form tool")
|
| 161 |
+
return f"""
|
| 162 |
<form_status>
|
| 163 |
{form_info}
|
| 164 |
</form_status>
|