AaronShih commited on
Commit
09fc712
·
verified ·
1 Parent(s): ae7a494

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -9,14 +9,34 @@ from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -55,7 +75,7 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
+ def execute_python_code(code: str) -> str:
13
+ """A tool that safely executes a snippet of Python code and returns its output.
14
+
15
  Args:
16
+ code: A string containing the Python code to execute.
 
17
  """
18
+ try:
19
+ import io
20
+ import sys
21
+ # Create a buffer to capture stdout
22
+ buffer = io.StringIO()
23
+ old_stdout = sys.stdout
24
+ sys.stdout = buffer
25
+
26
+ # Execute the code in a new, isolated global environment
27
+ exec(code, {})
28
+
29
+ # Reset stdout back to normal
30
+ sys.stdout = old_stdout
31
+
32
+ output = buffer.getvalue()
33
+ if output.strip() == "":
34
+ return "Code executed successfully with no output."
35
+ return f"Output:\n{output}"
36
+ except Exception as e:
37
+ # Ensure stdout is reset in case of error
38
+ sys.stdout = old_stdout
39
+ return f"Error executing code: {str(e)}"
40
 
41
  @tool
42
  def get_current_time_in_timezone(timezone: str) -> str:
 
75
 
76
  agent = CodeAgent(
77
  model=model,
78
+ tools=[final_answer,get_current_time_in_timezone,execute_python_code], ## add your tools here (don't remove final answer)
79
  max_steps=6,
80
  verbosity_level=1,
81
  grammar=None,