Spaces:
Sleeping
Sleeping
Create python_exec_tool.py
Browse files- tools/python_exec_tool.py +24 -0
tools/python_exec_tool.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from crewai.tools import BaseTool
|
| 2 |
+
import subprocess
|
| 3 |
+
import tempfile
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class PythonExecTool(BaseTool):
|
| 7 |
+
name: str = "Python Execution Tool"
|
| 8 |
+
description: str = "Executes Python code safely"
|
| 9 |
+
|
| 10 |
+
def _run(self, code: str):
|
| 11 |
+
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as file:
|
| 12 |
+
file.write(code)
|
| 13 |
+
file_path = file.name
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
result = subprocess.check_output(
|
| 17 |
+
["python", file_path],
|
| 18 |
+
stderr=subprocess.STDOUT,
|
| 19 |
+
text=True,
|
| 20 |
+
)
|
| 21 |
+
return result
|
| 22 |
+
|
| 23 |
+
except subprocess.CalledProcessError as error:
|
| 24 |
+
return error.output
|