jkorstad commited on
Commit
6973e68
·
verified ·
1 Parent(s): fbf4491

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -11
app.py CHANGED
@@ -108,11 +108,13 @@ tools.append(space_search_tool)
108
  model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
109
 
110
  # Create the agent
 
111
  agent = CodeAgent(
112
  tools=tools,
113
  model=model,
114
  additional_authorized_imports=['PIL', 'Pillow', 'os', 'sys', 'numpy', 'huggingface_hub', 'gradio_client', 'uuid'],
115
  add_base_tools=True,
 
116
  )
117
 
118
  AGENT_INSTRUCTIONS = """You are a highly capable AI assistant. Your primary goal is to accomplish tasks using a variety of tools, prioritizing Hugging Face Spaces.
@@ -173,19 +175,35 @@ def gradio_interface(user_prompt, input_image_path, input_audio_path, input_vide
173
  progress(0, desc="Initializing Agent...")
174
  full_prompt_with_instructions = f"{AGENT_INSTRUCTIONS}\n\nUSER PROMPT: {user_prompt}"
175
 
176
- # Prepare a dictionary of global variables for the agent's execution scope
177
- # These will be available as global variables in the agent's Python execution environment
178
- # when agent.run is called with the code_globals parameter.
179
- agent_code_globals = {}
180
- if input_image_path: agent_code_globals["input_image_path"] = str(input_image_path)
181
- if input_audio_path: agent_code_globals["input_audio_path"] = str(input_audio_path)
182
- if input_video_path: agent_code_globals["input_video_path"] = str(input_video_path)
183
- if input_3d_model_path: agent_code_globals["input_3d_model_path"] = str(input_3d_model_path)
184
- if input_file_path: agent_code_globals["input_file_path"] = str(input_file_path)
 
 
 
 
 
 
 
 
185
 
186
  progress(0.2, desc="Agent processing request...")
187
- # Pass the global variables using the code_globals parameter
188
- result = agent.run(full_prompt_with_instructions, code_globals=agent_code_globals)
 
 
 
 
 
 
 
 
189
 
190
  progress(0.8, desc="Processing result...")
191
  outputs = {
 
108
  model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
109
 
110
  # Create the agent
111
+ # python_globals here are for fixed globals. Dynamic ones will be handled per-run.
112
  agent = CodeAgent(
113
  tools=tools,
114
  model=model,
115
  additional_authorized_imports=['PIL', 'Pillow', 'os', 'sys', 'numpy', 'huggingface_hub', 'gradio_client', 'uuid'],
116
  add_base_tools=True,
117
+ python_globals=None # Explicitly setting to None or an empty dict if preferred
118
  )
119
 
120
  AGENT_INSTRUCTIONS = """You are a highly capable AI assistant. Your primary goal is to accomplish tasks using a variety of tools, prioritizing Hugging Face Spaces.
 
175
  progress(0, desc="Initializing Agent...")
176
  full_prompt_with_instructions = f"{AGENT_INSTRUCTIONS}\n\nUSER PROMPT: {user_prompt}"
177
 
178
+ # Prepare a dictionary of dynamic global variables for the agent's execution scope
179
+ dynamic_globals_for_run = {}
180
+ if input_image_path: dynamic_globals_for_run["input_image_path"] = str(input_image_path)
181
+ if input_audio_path: dynamic_globals_for_run["input_audio_path"] = str(input_audio_path)
182
+ if input_video_path: dynamic_globals_for_run["input_video_path"] = str(input_video_path)
183
+ if input_3d_model_path: dynamic_globals_for_run["input_3d_model_path"] = str(input_3d_model_path)
184
+ if input_file_path: dynamic_globals_for_run["input_file_path"] = str(input_file_path)
185
+
186
+ # Temporarily update the agent's python_globals for this specific run
187
+ # These globals are used by the CodeAgent when it executes Python code.
188
+ original_python_globals = agent.python_globals.copy() if agent.python_globals is not None else {}
189
+
190
+ # Ensure agent.python_globals is a dict before updating
191
+ if agent.python_globals is None:
192
+ agent.python_globals = {}
193
+
194
+ agent.python_globals.update(dynamic_globals_for_run)
195
 
196
  progress(0.2, desc="Agent processing request...")
197
+ result = None
198
+ try:
199
+ # Call agent.run() without the unexpected keyword arguments.
200
+ # The dynamic_globals_for_run are now part of agent.python_globals for this execution.
201
+ result = agent.run(full_prompt_with_instructions)
202
+ finally:
203
+ # Restore the agent's original python_globals
204
+ agent.python_globals = original_python_globals
205
+ print(f"Restored agent.python_globals to: {agent.python_globals}")
206
+
207
 
208
  progress(0.8, desc="Processing result...")
209
  outputs = {