Download environment/stem_env.py from ulamai/STEM-RL-Tasks: direct link, hf CLI and curl.
- Browser
- Download file 3.02 kB
-
https://huggingface.co/datasets/ulamai/STEM-RL-Tasks/resolve/main/environment/stem_env.py
- Command line
-
hf download hf://datasets/ulamai/STEM-RL-Tasks/environment/stem_env.py
-
curl -L -o stem_env.py https://huggingface.co/datasets/ulamai/STEM-RL-Tasks/resolve/main/environment/stem_env.py
3.02 kB
| """One-step numerical-answer environment for the combined STEM dataset.""" | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| import random | |
| import tempfile | |
| from typing import Any | |
| try: | |
| from .grade import DOMAINS, TASKS, grade_task, package_path | |
| except ImportError: | |
| from grade import DOMAINS, TASKS, grade_task, package_path | |
| class STEMEnv: | |
| """Select a fixed task, a domain, or any of the 40 tasks. Not a security sandbox.""" | |
| def __init__(self, task_id: str | None = None, domain: str | None = None): | |
| if domain is not None: | |
| domain = domain.upper() | |
| if domain not in DOMAINS: | |
| raise ValueError("Unknown domain: " + domain) | |
| if task_id is not None and task_id not in TASKS: | |
| raise ValueError("Unknown task ID: " + task_id) | |
| if task_id is not None and domain is not None and TASKS[task_id]["domain"] != domain: | |
| raise ValueError("Task does not belong to the requested domain") | |
| self.task_ids = [tid for tid, info in TASKS.items() | |
| if (domain is None or info["domain"] == domain) | |
| and (task_id is None or tid == task_id)] | |
| self.task_id: str | None = None | |
| self.terminated = False | |
| def reset(self, seed: int | None = None) -> tuple[dict[str, Any], dict[str, Any]]: | |
| self.task_id = random.Random(seed).choice(self.task_ids) | |
| self.terminated = False | |
| task = TASKS[self.task_id] | |
| base = package_path(task["path"]) | |
| observation = { | |
| "task_id": self.task_id, | |
| "domain": task["domain"], | |
| "instruction": (base / "instruction.md").read_text(encoding="utf-8"), | |
| "instance": json.loads((base / "instance.json").read_text(encoding="utf-8")), | |
| } | |
| return observation, {"task_id": self.task_id, "domain": task["domain"], "max_steps": 1} | |
| def step(self, action: dict[str, Any] | str | bytes) -> tuple[dict[str, Any], float, bool, bool, dict[str, Any]]: | |
| if self.task_id is None or self.terminated: | |
| raise RuntimeError("Call reset() before each one-action episode") | |
| if isinstance(action, dict): | |
| raw = json.dumps(action, ensure_ascii=False, allow_nan=False).encode("utf-8") | |
| elif isinstance(action, str): | |
| raw = action.encode("utf-8") | |
| elif isinstance(action, bytes): | |
| raw = action | |
| else: | |
| raise TypeError("Action must be a JSON object, string, or bytes") | |
| if len(raw) > TASKS[self.task_id]["candidate_maximum_bytes"]: | |
| raise ValueError("Candidate exceeds size limit") | |
| with tempfile.TemporaryDirectory(prefix="stem-answer-") as scratch: | |
| candidate = Path(scratch) / "candidate.json" | |
| candidate.write_bytes(raw) | |
| result = grade_task(self.task_id, candidate) | |
| self.terminated = True | |
| return {"task_id": self.task_id, "passed": result["passed"]}, result["reward"], True, False, result | |