Spaces:
Runtime error
Runtime error
Download agent/tools/code_runner.py from TheAbyss99/abyss-api: direct link, hf CLI and curl.
- Browser
- Download file 799 Bytes
-
https://huggingface.co/spaces/TheAbyss99/abyss-api/resolve/main/agent/tools/code_runner.py
- Command line
-
hf download hf://spaces/TheAbyss99/abyss-api/agent/tools/code_runner.py
-
curl -L -o code_runner.py https://huggingface.co/spaces/TheAbyss99/abyss-api/resolve/main/agent/tools/code_runner.py
799 Bytes
| import os | |
| import subprocess | |
| import tempfile | |
| TIMEOUT = 5 # SECONDS | |
| def run_code(code: str) -> str: | |
| try: | |
| with tempfile.NamedTemporaryFile(suffix=".py", mode="w", delete=False) as f: | |
| f.write(code) | |
| path = f.name | |
| result = subprocess.run( | |
| ["python", path], | |
| capture_output=True, | |
| text=True, | |
| timeout=TIMEOUT, | |
| ) | |
| os.unlink(path) | |
| stdout = result.stdout.strip() | |
| stderr = result.stderr.strip() | |
| if stderr: | |
| return f"stdout: {stdout}\nstderr: {stderr}" | |
| return stdout or "(no output)" | |
| except subprocess.TimeoutExpired: | |
| return f"Timed out after {TIMEOUT}s" | |
| except Exception as e: | |
| return f"Code runner error: {e}" |