jkorstad commited on
Commit
f501ab7
·
verified ·
1 Parent(s): 46f5e5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -18
app.py CHANGED
@@ -108,13 +108,12 @@ tools.append(space_search_tool)
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,7 +174,6 @@ def gradio_interface(user_prompt, input_image_path, input_audio_path, input_vide
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)
@@ -183,26 +181,38 @@ def gradio_interface(user_prompt, input_image_path, input_audio_path, input_vide
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...")
 
108
  model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
109
 
110
  # Create the agent
111
+ # Removed python_globals from constructor
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
  )
118
 
119
  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.
 
174
  progress(0, desc="Initializing Agent...")
175
  full_prompt_with_instructions = f"{AGENT_INSTRUCTIONS}\n\nUSER PROMPT: {user_prompt}"
176
 
 
177
  dynamic_globals_for_run = {}
178
  if input_image_path: dynamic_globals_for_run["input_image_path"] = str(input_image_path)
179
  if input_audio_path: dynamic_globals_for_run["input_audio_path"] = str(input_audio_path)
 
181
  if input_3d_model_path: dynamic_globals_for_run["input_3d_model_path"] = str(input_3d_model_path)
182
  if input_file_path: dynamic_globals_for_run["input_file_path"] = str(input_file_path)
183
 
184
+ # Access the agent's python_interpreter's globals
185
+ # The python_interpreter tool is usually named 'python_interpreter' or 'python'
186
+ # We need to ensure it exists and has a 'globals' attribute.
187
+ interpreter_tool = None
188
+ if hasattr(agent, 'python_interpreter') and agent.python_interpreter is not None: # Common attribute name
189
+ interpreter_tool = agent.python_interpreter
190
+ elif 'python_interpreter' in agent.tools and agent.tools['python_interpreter'] is not None:
191
+ interpreter_tool = agent.tools['python_interpreter']
192
+ elif 'python' in agent.tools and agent.tools['python'] is not None: # Another common name for the tool
193
+ interpreter_tool = agent.tools['python']
194
+
195
+
196
+ original_interpreter_globals = {}
197
+ if interpreter_tool and hasattr(interpreter_tool, 'globals') and isinstance(interpreter_tool.globals, dict):
198
+ original_interpreter_globals = interpreter_tool.globals.copy()
199
+ interpreter_tool.globals.update(dynamic_globals_for_run)
200
+ print(f"Updated agent.python_interpreter.globals with: {dynamic_globals_for_run}")
201
+ else:
202
+ print("Warning: Could not find or update python_interpreter globals on the agent.")
203
+
204
 
205
  progress(0.2, desc="Agent processing request...")
206
  result = None
207
  try:
 
 
208
  result = agent.run(full_prompt_with_instructions)
209
  finally:
210
+ # Restore the agent's original python_interpreter globals
211
+ if interpreter_tool and hasattr(interpreter_tool, 'globals'):
212
+ interpreter_tool.globals = original_interpreter_globals
213
+ print(f"Restored agent.python_interpreter.globals.")
214
+ else:
215
+ print("Warning: Could not restore python_interpreter globals.")
216
 
217
 
218
  progress(0.8, desc="Processing result...")