Download engines/general/replication_storage.py from Poindexter-Labs/Samples: direct link, hf CLI and curl.
- Browser
- Download file 4.08 kB
-
https://huggingface.co/datasets/Poindexter-Labs/Samples/resolve/main/engines/general/replication_storage.py
- Command line
-
hf download hf://datasets/Poindexter-Labs/Samples/engines/general/replication_storage.py
-
curl -L -o replication_storage.py https://huggingface.co/datasets/Poindexter-Labs/Samples/resolve/main/engines/general/replication_storage.py
4.08 kB
| """Local immutable inputs and durable outputs; no Firebase, GCS or Redis.""" | |
| import base64 | |
| from contextlib import contextmanager | |
| from contextvars import ContextVar | |
| import hashlib | |
| import json | |
| import mimetypes | |
| import os | |
| from pathlib import Path | |
| import re | |
| import uuid | |
| ROOT = ContextVar("artifact_root") | |
| MAX_FILE = 10 * 1024 * 1024 | |
| MAX_TOTAL = 25 * 1024 * 1024 | |
| def digest(data): | |
| return hashlib.sha256(data).hexdigest() | |
| def atomic(path, value): | |
| path = Path(path) | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| data = ( | |
| value | |
| if isinstance(value, bytes) | |
| else (json.dumps(value, ensure_ascii=False, indent=2) + "\n").encode() | |
| ) | |
| tmp = path.with_name(path.name + ".tmp-" + uuid.uuid4().hex) | |
| with tmp.open("xb") as out: | |
| out.write(data) | |
| out.flush() | |
| os.fsync(out.fileno()) | |
| tmp.replace(path) | |
| fd = os.open(path.parent, os.O_RDONLY) | |
| try: | |
| os.fsync(fd) | |
| finally: | |
| os.close(fd) | |
| def workspace(path): | |
| path = Path(path).resolve() | |
| path.mkdir(parents=True, exist_ok=True) | |
| token = ROOT.set(path) | |
| try: | |
| yield | |
| finally: | |
| ROOT.reset(token) | |
| def bucket(): | |
| return ROOT.get() | |
| def save_file(store, run_id, filename, data, project=None): | |
| if not data or len(data) > MAX_FILE: | |
| raise ValueError("Output file is empty or exceeds 10 MiB") | |
| file_id = uuid.uuid4().hex | |
| filename = filename.replace("\\", "/").split("/")[-1] | |
| ref = dict( | |
| provider="rl_env", | |
| container_id=run_id, | |
| file_id=file_id, | |
| filename=filename, | |
| mime_type=mimetypes.guess_type(filename)[0] or "application/octet-stream", | |
| size_bytes=len(data), | |
| sha256=digest(data), | |
| source="assistant", | |
| ) | |
| target = Path(store) / "artifacts" / run_id / file_id | |
| atomic(target, data) | |
| atomic(target.with_suffix(".json"), ref) | |
| return ref | |
| def read_file(ref, limit=MAX_FILE, project=None): | |
| ids = [ref.get("container_id", ""), ref.get("file_id", "")] | |
| if not all(re.fullmatch(r"[a-f0-9]{32}", str(x)) for x in ids): | |
| raise ValueError("Invalid local artifact handle") | |
| path = ROOT.get() / "artifacts" / ids[0] / ids[1] | |
| meta = json.loads(path.with_suffix(".json").read_text()) | |
| with path.open("rb") as stream: | |
| data = stream.read(limit + 1) | |
| if not data or len(data) > limit or digest(data) != meta["sha256"]: | |
| raise ValueError("Artifact failed integrity/size check") | |
| return data | |
| def download_input(url, max_bytes=25 * 1024 * 1024): | |
| # Every attachment comes from a validated dataset file. No model URL fetching. | |
| if not url.startswith("data:") or ";base64," not in url: | |
| raise ValueError("Replication inputs must be local dataset attachments") | |
| encoded = url.split(";base64,", 1)[1] | |
| if len(encoded) > (max_bytes + 2) // 3 * 4: | |
| raise ValueError("Attachment exceeds size limit") | |
| data = base64.b64decode(encoded, validate=True) | |
| if len(data) > max_bytes: | |
| raise ValueError("Attachment exceeds size limit") | |
| return data | |
| def upload_inputs(sandbox, inputs): | |
| from server.phoenix import _normalize_mm_files | |
| inputs = _normalize_mm_files(inputs) | |
| if len(inputs) > 20: | |
| raise ValueError("Too many input files") | |
| manifest, total = [], 0 | |
| for i, item in enumerate(inputs, 1): | |
| name = re.sub(r"[^A-Za-z0-9_.-]", "_", item["name"] or f"input-{i}")[-120:] | |
| path = f"/home/user/inputs/{i}-{name}" | |
| data = download_input(item["url"], min(MAX_FILE, MAX_TOTAL - total)) | |
| total += len(data) | |
| sandbox.files.write(path, data) | |
| manifest.append({"path": path, "mime": item["mime"], "bytes": len(data)}) | |
| return manifest | |
| def validate(project, benchmark=False): | |
| from replication_clients import required | |
| from server.rl_env import timeout_seconds | |
| timeout_seconds() | |
| required("E2B_API_KEY") | |
| required("E2B_TEMPLATE_ID") | |
| def slot(): | |
| # CLI executes sequentially. Parallel experiments use separate result directories. | |
| yield None | |