Spaces:
Paused
Paused
Yuvraj Sharma
commited on
Commit
Β·
0641568
1
Parent(s):
4b6e9e0
fixing agent session state
Browse files
app.py
CHANGED
|
@@ -131,14 +131,17 @@ WEB_TOOLS = [
|
|
| 131 |
TextInspectorTool(model, text_limit),
|
| 132 |
]
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
document_inspection_tool = TextInspectorTool(model, 20000)
|
| 144 |
|
|
@@ -198,17 +201,23 @@ def stream_to_gradio(
|
|
| 198 |
class GradioUI:
|
| 199 |
"""A one-line interface to launch your agent in Gradio"""
|
| 200 |
|
| 201 |
-
def __init__(self,
|
| 202 |
-
|
| 203 |
self.file_upload_folder = file_upload_folder
|
| 204 |
if self.file_upload_folder is not None:
|
| 205 |
if not os.path.exists(file_upload_folder):
|
| 206 |
os.mkdir(file_upload_folder)
|
| 207 |
|
| 208 |
-
def interact_with_agent(self, prompt, messages):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
messages.append(gr.ChatMessage(role="user", content=prompt))
|
| 210 |
yield messages
|
| 211 |
-
|
|
|
|
|
|
|
| 212 |
messages.append(msg)
|
| 213 |
yield messages
|
| 214 |
yield messages
|
|
@@ -279,6 +288,8 @@ OpenAI just published [Deep Research](https://openai.com/index/introducing-deep-
|
|
| 279 |
However, 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! β¨
|
| 280 |
|
| 281 |
You can try a simplified version below. π""")
|
|
|
|
|
|
|
| 282 |
stored_messages = gr.State([])
|
| 283 |
file_uploads_log = gr.State([])
|
| 284 |
chatbot = gr.Chatbot(
|
|
@@ -305,8 +316,12 @@ You can try a simplified version below. π""")
|
|
| 305 |
self.log_user_message,
|
| 306 |
[text_input, file_uploads_log],
|
| 307 |
[stored_messages, text_input],
|
| 308 |
-
).then(self.interact_with_agent,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
demo.launch(debug=True, share=True, **kwargs)
|
| 311 |
|
| 312 |
-
GradioUI(
|
|
|
|
| 131 |
TextInspectorTool(model, text_limit),
|
| 132 |
]
|
| 133 |
|
| 134 |
+
# Agent creation in a factory function
|
| 135 |
+
def create_agent():
|
| 136 |
+
"""Creates a fresh agent instance for each session"""
|
| 137 |
+
return CodeAgent(
|
| 138 |
+
model=model,
|
| 139 |
+
tools=[visualizer] + WEB_TOOLS,
|
| 140 |
+
max_steps=5,
|
| 141 |
+
verbosity_level=2,
|
| 142 |
+
additional_authorized_imports=AUTHORIZED_IMPORTS,
|
| 143 |
+
planning_interval=4,
|
| 144 |
+
)
|
| 145 |
|
| 146 |
document_inspection_tool = TextInspectorTool(model, 20000)
|
| 147 |
|
|
|
|
| 201 |
class GradioUI:
|
| 202 |
"""A one-line interface to launch your agent in Gradio"""
|
| 203 |
|
| 204 |
+
def __init__(self, file_upload_folder: str | None = None):
|
| 205 |
+
|
| 206 |
self.file_upload_folder = file_upload_folder
|
| 207 |
if self.file_upload_folder is not None:
|
| 208 |
if not os.path.exists(file_upload_folder):
|
| 209 |
os.mkdir(file_upload_folder)
|
| 210 |
|
| 211 |
+
def interact_with_agent(self, prompt, messages, session_state):
|
| 212 |
+
# Get or create session-specific agent
|
| 213 |
+
if 'agent' not in session_state:
|
| 214 |
+
session_state['agent'] = create_agent()
|
| 215 |
+
|
| 216 |
messages.append(gr.ChatMessage(role="user", content=prompt))
|
| 217 |
yield messages
|
| 218 |
+
|
| 219 |
+
# Use session's agent instance
|
| 220 |
+
for msg in stream_to_gradio(session_state['agent'], task=prompt, reset_agent_memory=False):
|
| 221 |
messages.append(msg)
|
| 222 |
yield messages
|
| 223 |
yield messages
|
|
|
|
| 288 |
However, 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! β¨
|
| 289 |
|
| 290 |
You can try a simplified version below. π""")
|
| 291 |
+
# Add session state to store session-specific data
|
| 292 |
+
session_state = gr.State({}) # Initialize empty state for each session
|
| 293 |
stored_messages = gr.State([])
|
| 294 |
file_uploads_log = gr.State([])
|
| 295 |
chatbot = gr.Chatbot(
|
|
|
|
| 316 |
self.log_user_message,
|
| 317 |
[text_input, file_uploads_log],
|
| 318 |
[stored_messages, text_input],
|
| 319 |
+
).then(self.interact_with_agent,
|
| 320 |
+
# Include session_state in function calls
|
| 321 |
+
[stored_messages, chatbot, session_state],
|
| 322 |
+
[chatbot]
|
| 323 |
+
)
|
| 324 |
|
| 325 |
demo.launch(debug=True, share=True, **kwargs)
|
| 326 |
|
| 327 |
+
GradioUI().launch()
|