Download python_lora_dataset_builder/pipeline/sandbox.py from api-relay/sadASDasd: direct link, hf CLI and curl.
- Browser
- Download file 3.17 kB
-
https://huggingface.co/datasets/api-relay/sadASDasd/resolve/main/python_lora_dataset_builder/pipeline/sandbox.py
- Command line
-
hf download hf://datasets/api-relay/sadASDasd/python_lora_dataset_builder/pipeline/sandbox.py
-
curl -L -o sandbox.py https://huggingface.co/datasets/api-relay/sadASDasd/resolve/main/python_lora_dataset_builder/pipeline/sandbox.py
3.17 kB
| from __future__ import annotations | |
| import os | |
| import subprocess | |
| import sys | |
| import tempfile | |
| from dataclasses import dataclass | |
| class ExecutionResult: | |
| attempted: bool | |
| passed: bool | |
| timed_out: bool | |
| returncode: int | None | |
| stdout: str | |
| stderr: str | |
| status: str | |
| isolation: str | |
| warning: str | None = None | |
| def _linux_preexec(memory_limit_mb: int, cpu_seconds: int, process_limit: int): | |
| def apply_limits(): | |
| import resource | |
| memory = memory_limit_mb * 1024 * 1024 | |
| resource.setrlimit(resource.RLIMIT_AS, (memory, memory)) | |
| resource.setrlimit(resource.RLIMIT_CPU, (cpu_seconds, cpu_seconds)) | |
| resource.setrlimit(resource.RLIMIT_NPROC, (process_limit, process_limit)) | |
| resource.setrlimit(resource.RLIMIT_FSIZE, (16 * 1024 * 1024, 16 * 1024 * 1024)) | |
| resource.setrlimit(resource.RLIMIT_NOFILE, (64, 64)) | |
| return apply_limits | |
| def run_code(code: str, timeout_seconds: int = 3, memory_limit_mb: int = 1024, | |
| cpu_seconds: int = 2, process_limit: int = 512) -> ExecutionResult: | |
| """ | |
| Development executor. On Linux it applies rlimits. On Windows, Python has no | |
| stdlib equivalent to setrlimit, so execution is explicitly marked limited. | |
| Production runs should use the Docker executor with network disabled. | |
| """ | |
| with tempfile.TemporaryDirectory(prefix="py_lora_") as td: | |
| kwargs = { | |
| "cwd": td, | |
| "env": { | |
| **os.environ, | |
| "PYTHONIOENCODING": "utf-8", | |
| "PYTHONDONTWRITEBYTECODE": "1", | |
| "OPENBLAS_NUM_THREADS": "1", | |
| "OMP_NUM_THREADS": "1", | |
| "MKL_NUM_THREADS": "1", | |
| }, | |
| "text": True, | |
| "capture_output": True, | |
| "timeout": timeout_seconds, | |
| } | |
| isolation = "windows_limited" | |
| warning = None | |
| if sys.platform.startswith("linux"): | |
| kwargs["preexec_fn"] = _linux_preexec(memory_limit_mb, cpu_seconds, process_limit) | |
| isolation = "linux_rlimit" | |
| elif os.name == "nt": | |
| warning = ( | |
| "Windows local executor has timeout and temporary cwd only. " | |
| "Use Docker mode for production/untrusted code; Windows has no " | |
| "stdlib setrlimit equivalent." | |
| ) | |
| else: | |
| warning = "No OS resource limits are available in this local executor." | |
| try: | |
| p = subprocess.run( | |
| [sys.executable, "-I", "-c", code], | |
| **kwargs, | |
| ) | |
| status = "passed" if p.returncode == 0 else "failed" | |
| return ExecutionResult( | |
| True, p.returncode == 0, False, p.returncode, | |
| p.stdout[-4000:], p.stderr[-4000:], status, isolation, warning | |
| ) | |
| except subprocess.TimeoutExpired as e: | |
| return ExecutionResult( | |
| True, False, True, None, | |
| (e.stdout or "")[-4000:] if isinstance(e.stdout, str) else "", | |
| (e.stderr or "")[-4000:] if isinstance(e.stderr, str) else "", | |
| "failed", isolation, warning | |
| ) | |