gabejavitt commited on
Commit
6b4981b
·
verified ·
1 Parent(s): 30244aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -66,27 +66,33 @@ def search_tool(query: str) -> str:
66
  except Exception as e:
67
  return f"Error running search: {e}"
68
 
69
- @tool
70
  def code_interpreter(code: str) -> str:
71
  """
72
  Executes a string of Python code and returns its stdout, stderr, and any error.
73
- Use this for calculations, data manipulation (including pandas on dataframes read from files), list operations, string manipulations, or to solve logic puzzles step-by-step.
74
- When solving a logic problem, write out all the steps of your reasoning as comments in the code.
75
- The code runs in a sandboxed environment. 'pandas' (as pd) and 'openpyxl' are available.
76
- Ensure the code is complete and executable. If printing, use print().
77
  """
78
  print(f"--- Calling Code Interpreter with code:\n{code}\n---")
79
  output_stream = io.StringIO()
80
  error_stream = io.StringIO()
81
  try:
82
  with contextlib.redirect_stdout(output_stream), contextlib.redirect_stderr(error_stream):
83
- exec(code, {"pd": pd}, {})
 
 
 
 
 
 
 
 
 
84
  stdout = output_stream.getvalue(); stderr = error_stream.getvalue()
85
  if stderr: return f"Error: {stderr}\nStdout: {stdout}"
86
  if stdout: return f"Success:\n{stdout}"
87
  return "Success: Code executed without error and produced no stdout."
88
  except Exception as e: return f"Execution failed with error: {str(e)}"
89
-
90
  @tool
91
  def read_file(path: str) -> str:
92
  """Reads the content of a file at the specified path. Use this to examine files provided in the question."""
 
66
  except Exception as e:
67
  return f"Error running search: {e}"
68
 
69
+ @tooldef
70
  def code_interpreter(code: str) -> str:
71
  """
72
  Executes a string of Python code and returns its stdout, stderr, and any error.
73
+ ...
 
 
 
74
  """
75
  print(f"--- Calling Code Interpreter with code:\n{code}\n---")
76
  output_stream = io.StringIO()
77
  error_stream = io.StringIO()
78
  try:
79
  with contextlib.redirect_stdout(output_stream), contextlib.redirect_stderr(error_stream):
80
+ # --- FIX IS HERE ---
81
+ # Create a safe environment that includes 'pd' and standard Python built-ins
82
+ safe_globals = {
83
+ "pd": pd,
84
+ "__builtins__": __builtins__ # This is the crucial addition
85
+ }
86
+ # Execute the code within this safe environment
87
+ exec(code, safe_globals, {})
88
+ # --- END FIX ---
89
+
90
  stdout = output_stream.getvalue(); stderr = error_stream.getvalue()
91
  if stderr: return f"Error: {stderr}\nStdout: {stdout}"
92
  if stdout: return f"Success:\n{stdout}"
93
  return "Success: Code executed without error and produced no stdout."
94
  except Exception as e: return f"Execution failed with error: {str(e)}"
95
+
96
  @tool
97
  def read_file(path: str) -> str:
98
  """Reads the content of a file at the specified path. Use this to examine files provided in the question."""