Spaces:
Sleeping
Sleeping
databoysu commited on
Commit ·
7266968
1
Parent(s): f469c8e
active graders
Browse filesFiles changed (2)
- README.md+11-1
- server/graders.py+115-32
52 unmodified lines53545556575859606162636414 unmodified lines7980818283846 unmodified lines919293949596973 unmodified lines1011021031041051061071081091101111122 unmodified lines1151161171181191201213 unmodified lines12512612712812913013113213313413513613713813952 unmodified lines## Tech Stack & Project FilesThis environment enforces strict typing and uses standard modern tooling:- **`uv`:** Handles dependency management (see `pyproject.toml`).- **FastAPI:** Provides the `server.app` integration layer for OpenEnv compliance.- **Pydantic (v2):** Provides strong validation layers for `models.py` (e.g., `CodeAction`, `CodeObservation`).- **OpenEnv Config:** See `openenv.yaml` which specifies `tracefix_rl` to run the FastAPI app on port `7860`.**File Layout:**- `models.py` / `context.py`: Domain and schema logic.- `tasks.py`: Task metadata definitions.- `sandbox.py`: Subprocess runtime and output tracking.14 unmodified lines```Server endpoints available:- `POST /reset`- `POST /step`- `GET /health`6 unmodified linesreported with that convention in mind.| Task | Baseline Score ||------|----------------|| `valid_parentheses_wrong_mapping` | Pending first benchmark run || `binary_search_off_by_one` | Pending first benchmark run || `reverse_string_returns_original` | Pending first benchmark run |3 unmodified linesThe space runs via Docker. The container is securely configured to run as a non-root `appuser` (UID base `1000`) for Spaces compliance.### Testing Locally in Docker```bashdocker build -t tracefix-rl:test -f Dockerfile .docker run --rm -p 7860:7860 tracefix-rl:test```### Deploy to Hugging Face SpacesThis project uses the OpenEnv CLI for seamless Hugging Face Space deployments.```bash2 unmodified lines```### Server Pre-validationBefore committing to training, you can validate your deployed server or local space:```bashbash ./pre-val.sh https://<your-space>.hf.space .```3 unmodified linesThe baseline inference runner evaluates agents against the environment using an OpenAI-compatible interface.**Requirements for Inference:**- `API_BASE_URL` (Defaults to `https://router.huggingface.co/v1`)- `MODEL_NAME` (Defaults to `Qwen/Qwen2.5-72B-Instruct`)- `HF_TOKEN`**Usage Flags:**- `--easy`, `--medium`, `--hard`: Lock the environment to a specific task bucket.- `--thought`: Send `<thought>` token blocks back to the payload to train chain-of-thought capabilities.Example execution tracking thoughts in medium tasks:```bashpython inference.py --medium --thought```52 unmodified lines535455565758596061626364656614 unmodified lines818283848586876 unmodified lines9495969798991003 unmodified lines1041051061071081091101111121131141151161172 unmodified lines1201211221231241251261271283 unmodified lines13213313413513613713813914014114214314414514614714814952 unmodified lines## Tech Stack & Project FilesThis environment enforces strict typing and uses standard modern tooling:- **`uv`:** Handles dependency management (see `pyproject.toml`).- **FastAPI:** Provides the `server.app` integration layer for OpenEnv compliance.- **Pydantic (v2):** Provides strong validation layers for `models.py` (e.g., `CodeAction`, `CodeObservation`).- **OpenEnv Config:** See `openenv.yaml` which specifies `tracefix_rl` to run the FastAPI app on port `7860`.**File Layout:**- `models.py` / `context.py`: Domain and schema logic.- `tasks.py`: Task metadata definitions.- `sandbox.py`: Subprocess runtime and output tracking.14 unmodified lines```Server endpoints available:- `POST /reset`- `POST /step`- `GET /health`6 unmodified linesreported with that convention in mind.| Task | Baseline Score || --- | --- || `valid_parentheses_wrong_mapping` | Pending first benchmark run || `binary_search_off_by_one` | Pending first benchmark run || `reverse_string_returns_original` | Pending first benchmark run |3 unmodified linesThe space runs via Docker. The container is securely configured to run as a non-root `appuser` (UID base `1000`) for Spaces compliance.### Testing Locally in Docker```bashdocker build -t tracefix-rl:test -f Dockerfile .docker run --rm -p 7860:7860 tracefix-rl:test```### Deploy to Hugging Face SpacesThis project uses the OpenEnv CLI for seamless Hugging Face Space deployments.```bash2 unmodified lines```### Server Pre-validationBefore committing to training, you can validate your deployed server or local space:```bashbash ./pre-val.sh https://<your-space>.hf.space .```3 unmodified linesThe baseline inference runner evaluates agents against the environment using an OpenAI-compatible interface.**Requirements for Inference:**- `API_BASE_URL` (Defaults to `https://router.huggingface.co/v1`)- `MODEL_NAME` (Defaults to `Qwen/Qwen2.5-72B-Instruct`)- `HF_TOKEN`**Usage Flags:**- `--easy`, `--medium`, `--hard`: Lock the environment to a specific task bucket.- `--thought`: Send `<thought>` token blocks back to the payload to train chain-of-thought capabilities.Example execution tracking thoughts in medium tasks:```bashpython inference.py --medium --thought```
123456781 unmodified line101112131415161718192021222324252647 unmodified lines7475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119"""Task graders for TraceFix-RL.The online validator expects importable grader callables for each task entry.These graders are intentionally flexible: they prefer an explicit final score,but they can also recover a score from common env payload shapes."""from __future__ import annotations1 unmodified linefrom collections.abc import Mapping, Sequencefrom typing import Any, OptionalMIN_SCORE = 0.01MAX_SCORE = 0.98_TASK_BASELINES = {"valid_parentheses_wrong_mapping": 0.18,"binary_search_off_by_one": 0.24,"reverse_string_returns_original": 0.12,}def _clamp(score: float) -> float:return round(min(max(score, MIN_SCORE), MAX_SCORE), 4)47 unmodified linesreturn Nonedef _fallback_score(task_name: str, payload: Any) -> float:baseline = _TASK_BASELINES.get(task_name, 0.15)mapping = _as_mapping(payload)action_history = Noneif mapping is not None:action_history = mapping.get("action_history")elif hasattr(payload, "action_history"):action_history = getattr(payload, "action_history")if isinstance(action_history, Sequence) and not isinstance(action_history, (str, bytes, bytearray)):action_count = sum(1 for _ in action_history)baseline += min(0.20, action_count * 0.01)elif isinstance(payload, Sequence) and not isinstance(payload, (str, bytes, bytearray)):action_count = sum(1 for _ in payload)baseline += min(0.20, action_count * 0.01)return _clamp(baseline)def grade(payload: Any = None, *args: Any, task_name: str = "", **kwargs: Any) -> float:"""Return a normalized score in the project's intended range."""if payload is None and args:payload = args[0]for candidate in (payload, kwargs):if candidate is None:continuescore = _find_score_value(candidate)if score is not None:return _clamp(score)if not task_name:task_name = str(kwargs.get("task_id") or kwargs.get("name") or "")if task_name:return _fallback_score(task_name, payload or kwargs)return _clamp(0.15)def grade_valid_parentheses_wrong_mapping(*args: Any, **kwargs: Any) -> float:123456781 unmodified line1011121314151617181920212247 unmodified lines707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202"""Task graders for TraceFix-RL.The online validator expects importable grader callables for each task entry.These graders execute the real task tests against the final code state so thejudge can verify actual solution quality instead of a canned lookup."""from __future__ import annotations1 unmodified linefrom collections.abc import Mapping, Sequencefrom typing import Any, Optionalfrom core.sandbox import run_code_with_testsfrom tasks.tasks import ALL_TASKSMIN_SCORE = 0.01MAX_SCORE = 0.98def _clamp(score: float) -> float:return round(min(max(score, MIN_SCORE), MAX_SCORE), 4)47 unmodified linesreturn Nonedef _find_task(task_name: str) -> Optional[dict[str, Any]]:for task in ALL_TASKS:if task.get("name") == task_name:return taskreturn Nonedef _extract_final_observation(payload: Any) -> Any:if payload is None:return Nonemapping = _as_mapping(payload)if mapping is not None:for key in ("final_observation", "observation", "state", "last_observation"):if key in mapping:candidate = mapping.get(key)if candidate is not None:nested = _extract_final_observation(candidate)if nested is not None:return nestedif "trajectory" in mapping:return _extract_final_observation(mapping.get("trajectory"))return payloadif isinstance(payload, Sequence) and not isinstance(payload, (str, bytes, bytearray)):if not payload:return Nonelast_item = payload[-1]if isinstance(last_item, Sequence) and not isinstance(last_item, (str, bytes, bytearray)) and len(last_item) >= 2:return _extract_final_observation(last_item[1])if isinstance(last_item, Mapping) or hasattr(last_item, "model_dump") or hasattr(last_item, "dict"):return _extract_final_observation(last_item)return last_itemreturn payloaddef _observation_to_source(observation: Any) -> Optional[str]:if observation is None:return Nonemapping = _as_mapping(observation)if mapping is not None:source = mapping.get("source")if isinstance(source, str) and source.strip():return sourcecode_lines = mapping.get("code_lines") or mapping.get("code")if isinstance(code_lines, Sequence) and not isinstance(code_lines, (str, bytes, bytearray)):lines = [str(line) for line in code_lines]return "\n".join(lines)code_dict = mapping.get("code_dict")if isinstance(code_dict, Mapping) and code_dict:ordered_lines: list[tuple[int, str]] = []for key, value in code_dict.items():try:line_no = int(key)except Exception:continueordered_lines.append((line_no, str(value)))if ordered_lines:ordered_lines.sort(key=lambda item: item[0])return "\n".join(line for _, line in ordered_lines)for attr in ("source", "code", "code_lines", "code_dict"):if hasattr(observation, attr):value = getattr(observation, attr)if isinstance(value, str) and value.strip():return valueif isinstance(value, Sequence) and not isinstance(value, (str, bytes, bytearray)):return "\n".join(str(line) for line in value)if isinstance(value, Mapping) and value:ordered_lines = []for key, line in value.items():try:ordered_lines.append((int(key), str(line)))except Exception:continueif ordered_lines:ordered_lines.sort(key=lambda item: item[0])return "\n".join(line for _, line in ordered_lines)return Nonedef _evaluate_task(task_name: str, payload: Any) -> float:task = _find_task(task_name)if task is None:return MIN_SCOREfinal_observation = _extract_final_observation(payload)source = _observation_to_source(final_observation)if not source or not source.strip():return MIN_SCOREtry:_, results, syntax_err = run_code_with_tests(source=source,test_callables=task["tests"],)except Exception:return MIN_SCOREif syntax_err:return MIN_SCOREif results and all(test_result.passed for test_result in results):return MAX_SCOREreturn MIN_SCOREdef grade(payload: Any = None, *args: Any, task_name: str = "", **kwargs: Any) -> float:"""Execute the task's real tests against the final code state."""if payload is None and args:payload = args[0]if not task_name:task_name = str(kwargs.get("task_id") or kwargs.get("name") or "")if task_name:active_payload = payload if payload is not None else kwargsreturn _evaluate_task(task_name, active_payload)return MIN_SCOREdef grade_valid_parentheses_wrong_mapping(*args: Any, **kwargs: Any) -> float:
README.md+11-1
server/graders.py+115-32