kshitijthakkar commited on
Commit
ef80791
·
verified ·
1 Parent(s): f9c3337

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -2
app.py CHANGED
@@ -7,9 +7,31 @@ from smolagents import (
7
  DuckDuckGoSearchTool,
8
  OpenAIServerModel,
9
  )
10
- from smolagents.tools.python_repl import PythonREPL
11
  import traceback # Import traceback for detailed error logging
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
@@ -24,7 +46,7 @@ class GaiaAgent:
24
  )
25
  # 2) Define the tools
26
  self.search_tool = DuckDuckGoSearchTool()
27
- self.python_tool = PythonREPL(timeout=10) # Initialize PythonREPLTool
28
  # 3) Create the CodeAgent
29
  self.agent = CodeAgent(
30
  model=self.model,
 
7
  DuckDuckGoSearchTool,
8
  OpenAIServerModel,
9
  )
10
+ #from smolagents.tools.python_repl import PythonREPL
11
  import traceback # Import traceback for detailed error logging
12
 
13
+ from smolagents import BaseTool
14
+
15
+ class PythonREPLTool(BaseTool):
16
+ def __init__(self, timeout=10):
17
+ self.timeout = timeout
18
+
19
+ def run(self, code: str) -> str:
20
+ import subprocess
21
+ try:
22
+ result = subprocess.run(
23
+ ["python3", "-c", code],
24
+ capture_output=True,
25
+ text=True,
26
+ timeout=self.timeout,
27
+ )
28
+ if result.returncode == 0:
29
+ return result.stdout
30
+ else:
31
+ return f"Error:\n{result.stderr}"
32
+ except subprocess.TimeoutExpired:
33
+ return "Execution timed out"
34
+
35
  # --- Constants ---
36
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
37
 
 
46
  )
47
  # 2) Define the tools
48
  self.search_tool = DuckDuckGoSearchTool()
49
+ self.python_tool = PythonREPLTool(timeout=10) # Initialize PythonREPLTool
50
  # 3) Create the CodeAgent
51
  self.agent = CodeAgent(
52
  model=self.model,