Sborole-Final-Assignment / tools /PythonRunnerTool.py
Sborole's picture
Update tools/PythonRunnerTool.py
f3d4327 verified
raw
history blame contribute delete
847 Bytes
from smolagents import Tool
import subprocess
class PythonRunnerTool(Tool):
name = "python_runner"
description = "Runs Python code from a file and returns the output."
inputs = {
"file_path": {"type": "string", "description": "Path to the Python file to run."}
}
output_type = "string"
def forward(self, file_path: str) -> str:
try:
result = subprocess.run(["python3", file_path],
capture_output=True,
text = True,
timeout=10
)
if result.stderr:
return f"Error: {result.stderr}"
return result.stdout
except Exception as e:
return f"Execution failed: {e}"