tagny commited on
Commit
25649b1
·
verified ·
1 Parent(s): db7337d

try to reinitialize the conversition for each new session

Browse files
Files changed (1) hide show
  1. Gradio_UI.py +18 -1
Gradio_UI.py CHANGED
@@ -188,7 +188,21 @@ class GradioUI:
188
  if self.file_upload_folder is not None:
189
  if not os.path.exists(file_upload_folder):
190
  os.mkdir(file_upload_folder)
191
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  def interact_with_agent(self, prompt, messages):
193
  import gradio as gr
194
 
@@ -262,6 +276,7 @@ class GradioUI:
262
  import gradio as gr
263
 
264
  with gr.Blocks(fill_height=True) as demo:
 
265
  gr.Label("Wecome to my first smolagent! You can use tools to get the time in a location, visit a URL page, search with DuckDuckGo.")
266
  stored_messages = gr.State([])
267
  file_uploads_log = gr.State([])
@@ -292,6 +307,8 @@ class GradioUI:
292
  ).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
293
 
294
  demo.launch(debug=True, share=True, **kwargs)
 
 
295
 
296
 
297
  __all__ = ["stream_to_gradio", "GradioUI"]
 
188
  if self.file_upload_folder is not None:
189
  if not os.path.exists(file_upload_folder):
190
  os.mkdir(file_upload_folder)
191
+ # see https://www.gradio.app/guides/state-in-blocks#session-state
192
+ # for non deep copyable object / to distinguish data per conversation
193
+ all_users_stored_messages = {}
194
+ all_users_file_uploads_log = {}
195
+
196
+ def initialize_session_data(self, request: gr.Request):
197
+ all_users_stored_messages[request.session_hash] = gr.State([])
198
+ all_users_file_uploads_log[request.session_hash] = gr.State([])
199
+ return "Session initialized!"
200
+
201
+ def cleanup_session_data(self, request: gr.Request):
202
+ if request.session_hash in instances:
203
+ del all_users_stored_messages[request.session_hash]
204
+ del all_users_file_uploads_log[request.session_hash]
205
+
206
  def interact_with_agent(self, prompt, messages):
207
  import gradio as gr
208
 
 
276
  import gradio as gr
277
 
278
  with gr.Blocks(fill_height=True) as demo:
279
+ # user_state = gr.State(random.random) # to avoid sharing gr.State across users (that uses all the users conversations for any user prompts)
280
  gr.Label("Wecome to my first smolagent! You can use tools to get the time in a location, visit a URL page, search with DuckDuckGo.")
281
  stored_messages = gr.State([])
282
  file_uploads_log = gr.State([])
 
307
  ).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
308
 
309
  demo.launch(debug=True, share=True, **kwargs)
310
+ # Initialize instance when page loads
311
+ demo.load(random.random, inputs=None, outputs=None)
312
 
313
 
314
  __all__ = ["stream_to_gradio", "GradioUI"]