Spaces:
Runtime error
Runtime error
Download tools/PythonRunnerTool.py from Sborole/Sborole-Final-Assignment: direct link, hf CLI and curl.
- Browser
- Download file 847 Bytes
-
https://huggingface.co/spaces/Sborole/Sborole-Final-Assignment/resolve/12b457dcdc6927a2e920342df7d8f6d373b5cadd/tools/PythonRunnerTool.py
- Command line
-
hf download hf://spaces/Sborole/Sborole-Final-Assignment@12b457dcdc6927a2e920342df7d8f6d373b5cadd/tools/PythonRunnerTool.py
-
curl -L -o PythonRunnerTool.py https://huggingface.co/spaces/Sborole/Sborole-Final-Assignment/resolve/12b457dcdc6927a2e920342df7d8f6d373b5cadd/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}" | |