Spaces:
Sleeping
Sleeping
Update utils/chatbot_interface.py
Browse files- utils/chatbot_interface.py +29 -12
utils/chatbot_interface.py
CHANGED
@@ -46,10 +46,10 @@ class ChatbotInterface:
|
|
46 |
api_key=api_key,
|
47 |
meta_prompt_file=meta_prompt_file
|
48 |
)
|
49 |
-
# Start a user session
|
50 |
-
self.session_id = self.response_manager.start_session()
|
51 |
-
logging.info(f"New session started with session id {self.session_id}.")
|
52 |
-
self.generate_response = lambda query, history: self.response_manager.generate_response(query, self.session_id)
|
53 |
|
54 |
logging.info(
|
55 |
"ChatbotInterface initialized with the following parameters:\n"
|
@@ -91,14 +91,13 @@ class ChatbotInterface:
|
|
91 |
logging.info("Configuration loaded successfully.")
|
92 |
return config
|
93 |
|
94 |
-
def reset_output(self) ->
|
95 |
"""
|
96 |
Reset the chatbot output and start a new session.
|
97 |
-
:return: An empty list to reset the output.
|
98 |
"""
|
99 |
-
|
100 |
-
|
101 |
-
return []
|
102 |
|
103 |
def create_interface(self) -> gr.Blocks:
|
104 |
"""
|
@@ -121,15 +120,33 @@ class ChatbotInterface:
|
|
121 |
placeholder=self.input_placeholder
|
122 |
)
|
123 |
|
|
|
|
|
|
|
124 |
# Buttons
|
125 |
with gr.Row():
|
126 |
reset = gr.Button(self.reset_button, variant="secondary")
|
127 |
submit = gr.Button(self.submit_button, variant="primary")
|
128 |
|
129 |
# Button actions
|
130 |
-
submit.click(
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
logging.info("Gradio interface created successfully.")
|
135 |
return demo
|
|
|
46 |
api_key=api_key,
|
47 |
meta_prompt_file=meta_prompt_file
|
48 |
)
|
49 |
+
# # Start a user session
|
50 |
+
# self.session_id = self.response_manager.start_session()
|
51 |
+
# logging.info(f"New session started with session id {self.session_id}.")
|
52 |
+
# self.generate_response = lambda query, history: self.response_manager.generate_response(query, self.session_id)
|
53 |
|
54 |
logging.info(
|
55 |
"ChatbotInterface initialized with the following parameters:\n"
|
|
|
91 |
logging.info("Configuration loaded successfully.")
|
92 |
return config
|
93 |
|
94 |
+
def reset_output(self) -> tuple:
|
95 |
"""
|
96 |
Reset the chatbot output and start a new session.
|
97 |
+
:return: An empty list and a new session ID to reset the output.
|
98 |
"""
|
99 |
+
new_session_id = self.response_manager.start_session()
|
100 |
+
return [], new_session_id
|
|
|
101 |
|
102 |
def create_interface(self) -> gr.Blocks:
|
103 |
"""
|
|
|
120 |
placeholder=self.input_placeholder
|
121 |
)
|
122 |
|
123 |
+
# Session state
|
124 |
+
session_state = gr.State(self.response_manager.start_session())
|
125 |
+
|
126 |
# Buttons
|
127 |
with gr.Row():
|
128 |
reset = gr.Button(self.reset_button, variant="secondary")
|
129 |
submit = gr.Button(self.submit_button, variant="primary")
|
130 |
|
131 |
# Button actions
|
132 |
+
submit.click(
|
133 |
+
fn=lambda query, session_id: self.response_manager.generate_response(query, session_id),
|
134 |
+
inputs=[user_input, session_state], outputs=[chatbot_output]
|
135 |
+
)
|
136 |
+
user_input.submit(
|
137 |
+
fn=lambda query, session_id: self.response_manager.generate_response(query, session_id),
|
138 |
+
inputs=[user_input, session_state], outputs=[chatbot_output]
|
139 |
+
)
|
140 |
+
reset.click(
|
141 |
+
fn=self.reset_output,
|
142 |
+
inputs=None,
|
143 |
+
outputs=[chatbot_output, session_state]
|
144 |
+
)
|
145 |
+
|
146 |
+
# # Button actions
|
147 |
+
# submit.click(fn=self.generate_response, inputs=[user_input, chatbot_output], outputs=chatbot_output)
|
148 |
+
# user_input.submit(fn=self.generate_response, inputs=[user_input, chatbot_output], outputs=chatbot_output)
|
149 |
+
# reset.click(fn=self.reset_output, inputs=None, outputs=chatbot_output)
|
150 |
|
151 |
logging.info("Gradio interface created successfully.")
|
152 |
return demo
|