Create PythonRunnerTool.py
Browse files- tools/PythonRunnerTool.py +23 -0
tools/PythonRunnerTool.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
import subprocess
|
| 3 |
+
|
| 4 |
+
class PythonRunnerTool(Tool):
|
| 5 |
+
name =
|
| 6 |
+
description =
|
| 7 |
+
inputs = {"file_path: {"type": "string", "description": }
|
| 8 |
+
output_type = "string"
|
| 9 |
+
|
| 10 |
+
def forward(self, file_path: str) -> str:
|
| 11 |
+
try:
|
| 12 |
+
result = subprocess.run(["python3", file_path],
|
| 13 |
+
capture_output=True,
|
| 14 |
+
text = True,
|
| 15 |
+
timeout=10
|
| 16 |
+
)
|
| 17 |
+
if result.stderr:
|
| 18 |
+
return f"Error: {result.stderr}"
|
| 19 |
+
return result.stdout
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"Execution failed: {e}"
|
| 22 |
+
|
| 23 |
+
|