kshitijthakkar commited on
Commit
6210d5d
·
verified ·
1 Parent(s): 89f5686

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -1
app.py CHANGED
@@ -7,9 +7,32 @@ from smolagents import (
7
  DuckDuckGoSearchTool,
8
  OpenAIServerModel,
9
  )
10
- from smolagents.tools.python_repl import PythonREPLTool # Changed the import
11
  import traceback # Import traceback for detailed error logging
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
 
7
  DuckDuckGoSearchTool,
8
  OpenAIServerModel,
9
  )
 
10
  import traceback # Import traceback for detailed error logging
11
 
12
+ import subprocess
13
+
14
+ class PythonREPLTool:
15
+ name = "python_repl"
16
+ description = "Runs Python code and returns the output or error."
17
+
18
+
19
+ def __init__(self, timeout=10):
20
+ self.timeout = timeout
21
+
22
+ def run(self, code: str) -> str:
23
+
24
+ try:
25
+ result = subprocess.run(
26
+ ["python3", "-c", code],
27
+ timeout=self.timeout,
28
+ )
29
+ if result.returncode == 0:
30
+ return result.stdout.strip()
31
+ else:
32
+ return f"Error:\n{result.stderr.strip()}"
33
+ except subprocess.TimeoutExpired:
34
+ return "Execution timed out."
35
+
36
  # --- Constants ---
37
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
38