Spaces:
Paused
Paused
fix agent state sharing (#9)
Browse files- fixing agent session state (06415688c52d62649de9ca19dd747bfeeac414eb)
130 unmodified lines13113213313413513613713813914014114214314453 unmodified lines19819920020120220320420520620720820921021121221321464 unmodified lines27928028128228328420 unmodified lines305306307308309310311312130 unmodified linesTextInspectorTool(model, text_limit),]agent = CodeAgent(model=model,tools=[visualizer] + WEB_TOOLS,max_steps=5,verbosity_level=2,additional_authorized_imports=AUTHORIZED_IMPORTS,planning_interval=4,)document_inspection_tool = TextInspectorTool(model, 20000)53 unmodified linesclass GradioUI:"""A one-line interface to launch your agent in Gradio"""def __init__(self, agent, file_upload_folder: str | None = None):self.agent = agentself.file_upload_folder = file_upload_folderif self.file_upload_folder is not None:if not os.path.exists(file_upload_folder):os.mkdir(file_upload_folder)def interact_with_agent(self, prompt, messages):messages.append(gr.ChatMessage(role="user", content=prompt))yield messagesfor msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):messages.append(msg)yield messagesyield messages64 unmodified linesHowever, their agent has a huge downside: it's not open. So we've started a 24-hour rush to replicate and open-source it. Our resulting [open-Deep-Research agent](https://github.com/huggingface/smolagents/tree/main/examples/open_deep_research) took the #1 rank of any open submission on the GAIA leaderboard! ✨You can try a simplified version below. 👇""")stored_messages = gr.State([])file_uploads_log = gr.State([])chatbot = gr.Chatbot(20 unmodified linesself.log_user_message,[text_input, file_uploads_log],[stored_messages, text_input],).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])demo.launch(debug=True, share=True, **kwargs)GradioUI(agent).launch()130 unmodified lines13113213313413513613713813914014114214314414514614753 unmodified lines20120220320420520620720820921021121221321421521621721821922022122222364 unmodified lines28828929029129229329429520 unmodified lines316317318319320321322323324325326327130 unmodified linesTextInspectorTool(model, text_limit),]# Agent creation in a factory functiondef create_agent():"""Creates a fresh agent instance for each session"""return CodeAgent(model=model,tools=[visualizer] + WEB_TOOLS,max_steps=5,verbosity_level=2,additional_authorized_imports=AUTHORIZED_IMPORTS,planning_interval=4,)document_inspection_tool = TextInspectorTool(model, 20000)53 unmodified linesclass GradioUI:"""A one-line interface to launch your agent in Gradio"""def __init__(self, file_upload_folder: str | None = None):self.file_upload_folder = file_upload_folderif self.file_upload_folder is not None:if not os.path.exists(file_upload_folder):os.mkdir(file_upload_folder)def interact_with_agent(self, prompt, messages, session_state):# Get or create session-specific agentif 'agent' not in session_state:session_state['agent'] = create_agent()messages.append(gr.ChatMessage(role="user", content=prompt))yield messages# Use session's agent instancefor msg in stream_to_gradio(session_state['agent'], task=prompt, reset_agent_memory=False):messages.append(msg)yield messagesyield messages64 unmodified linesHowever, their agent has a huge downside: it's not open. So we've started a 24-hour rush to replicate and open-source it. Our resulting [open-Deep-Research agent](https://github.com/huggingface/smolagents/tree/main/examples/open_deep_research) took the #1 rank of any open submission on the GAIA leaderboard! ✨You can try a simplified version below. 👇""")# Add session state to store session-specific datasession_state = gr.State({}) # Initialize empty state for each sessionstored_messages = gr.State([])file_uploads_log = gr.State([])chatbot = gr.Chatbot(20 unmodified linesself.log_user_message,[text_input, file_uploads_log],[stored_messages, text_input],).then(self.interact_with_agent,# Include session_state in function calls[stored_messages, chatbot, session_state],[chatbot])demo.launch(debug=True, share=True, **kwargs)GradioUI().launch()
app.py+29-14