Spaces:
Sleeping
Sleeping
Download tools/PythonRunnerTool.py from umarbalak/agents_course: direct link, hf CLI and curl.
- Browser
- Download file 847 Bytes
-
https://huggingface.co/spaces/umarbalak/agents_course/resolve/main/tools/PythonRunnerTool.py
- Command line
-
hf download hf://spaces/umarbalak/agents_course/tools/PythonRunnerTool.py
-
curl -L -o PythonRunnerTool.py https://huggingface.co/spaces/umarbalak/agents_course/resolve/main/tools/PythonRunnerTool.py
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}" | |