- Dockerfile +5 -1
- backend/main.py +106 -331
- grader/__init__.py +3 -0
- grader/clm_graders.py +80 -0
- inference.py +104 -59
- models.py +21 -14
- openenv.yaml +25 -49
- server/__init__.py +1 -0
- server/app.py +6 -3
Dockerfile
CHANGED
|
@@ -6,8 +6,12 @@ COPY backend/requirements.txt .
|
|
| 6 |
RUN pip install uv && uv pip install --system --no-cache -r requirements.txt
|
| 7 |
|
| 8 |
COPY backend/ /app/backend/
|
|
|
|
|
|
|
| 9 |
COPY models.py /app/models.py
|
|
|
|
|
|
|
| 10 |
|
| 11 |
EXPOSE 7860
|
| 12 |
|
| 13 |
-
CMD ["uvicorn", "
|
|
|
|
| 6 |
RUN pip install uv && uv pip install --system --no-cache -r requirements.txt
|
| 7 |
|
| 8 |
COPY backend/ /app/backend/
|
| 9 |
+
COPY server/ /app/server/
|
| 10 |
+
COPY grader/ /app/grader/
|
| 11 |
COPY models.py /app/models.py
|
| 12 |
+
COPY inference.py /app/inference.py
|
| 13 |
+
COPY openenv.yaml /app/openenv.yaml
|
| 14 |
|
| 15 |
EXPOSE 7860
|
| 16 |
|
| 17 |
+
CMD ["uvicorn", "server.app:app", "--host", "0.0.0.0", "--port", "7860"]
|
backend/main.py
CHANGED
|
@@ -1,178 +1,3 @@
|
|
| 1 |
-
# import os
|
| 2 |
-
# import sys
|
| 3 |
-
# from typing import Any, Dict, List, Optional
|
| 4 |
-
|
| 5 |
-
# sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 6 |
-
|
| 7 |
-
# from fastapi import FastAPI
|
| 8 |
-
# from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
-
# from pydantic import Field
|
| 10 |
-
|
| 11 |
-
# from openenv.core.env_server.interfaces import Environment
|
| 12 |
-
# from openenv.core.env_server.types import (
|
| 13 |
-
# Action as OEAction,
|
| 14 |
-
# Observation as OEObservation,
|
| 15 |
-
# State as OEState,
|
| 16 |
-
# EnvironmentMetadata,
|
| 17 |
-
# )
|
| 18 |
-
# from openenv.core.env_server.http_server import HTTPEnvServer
|
| 19 |
-
|
| 20 |
-
# from models import (
|
| 21 |
-
# Action as ModelAction,
|
| 22 |
-
# Observation as ModelObservation,
|
| 23 |
-
# generate_tasks,
|
| 24 |
-
# deterministic_grader,
|
| 25 |
-
# CLMEnvironment,
|
| 26 |
-
# )
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# # ── OpenEnv-compatible Action / Observation / State models ──────────────────
|
| 30 |
-
|
| 31 |
-
# class CLMAction(OEAction):
|
| 32 |
-
# """Action for the Cognitive Load Manager environment."""
|
| 33 |
-
# type: str = Field(description="Action type: work, break, switch, or delay")
|
| 34 |
-
# task_id: Optional[str] = Field(default=None, description="Task ID to act on")
|
| 35 |
-
|
| 36 |
-
# model_config = {"extra": "allow"}
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
# class CLMObservation(OEObservation):
|
| 40 |
-
# """Observation from the Cognitive Load Manager environment."""
|
| 41 |
-
# tasks: List[Dict[str, Any]] = Field(default_factory=list)
|
| 42 |
-
# visible_state: Dict[str, Any] = Field(default_factory=dict)
|
| 43 |
-
# time_step: int = Field(default=0)
|
| 44 |
-
|
| 45 |
-
# model_config = {"extra": "allow"}
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
# class CLMState(OEState):
|
| 49 |
-
# """State for the Cognitive Load Manager environment."""
|
| 50 |
-
# energy: float = Field(default=1.0)
|
| 51 |
-
# stress: float = Field(default=0.0)
|
| 52 |
-
# fatigue: float = Field(default=0.0)
|
| 53 |
-
# current_task_id: Optional[str] = Field(default=None)
|
| 54 |
-
# tasks: List[Dict[str, Any]] = Field(default_factory=list)
|
| 55 |
-
|
| 56 |
-
# model_config = {"extra": "allow"}
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# # ── OpenEnv Environment wrapper ─────────────────────────────────────────────
|
| 60 |
-
|
| 61 |
-
# class CLMEnvWrapper(Environment):
|
| 62 |
-
# """
|
| 63 |
-
# Cognitive Load Manager wrapped as an OpenEnv-compliant environment.
|
| 64 |
-
|
| 65 |
-
# Three difficulty levels via the task_id reset parameter:
|
| 66 |
-
# - easy: 2 tasks, no deadlines
|
| 67 |
-
# - medium: 5 tasks with deadlines
|
| 68 |
-
# - hard: 8 tasks with tight deadlines
|
| 69 |
-
# """
|
| 70 |
-
|
| 71 |
-
# SUPPORTS_CONCURRENT_SESSIONS = True
|
| 72 |
-
|
| 73 |
-
# def __init__(self):
|
| 74 |
-
# super().__init__()
|
| 75 |
-
# level = os.getenv("CLM_LEVEL", "easy")
|
| 76 |
-
# tasks = generate_tasks(level)
|
| 77 |
-
# self._env = CLMEnvironment(tasks=tasks, max_steps=50)
|
| 78 |
-
# self._final_score: float = 0.0
|
| 79 |
-
|
| 80 |
-
# def _to_oe_obs(self, obs: ModelObservation, done: bool = False, reward: Optional[float] = None, info: Optional[dict] = None) -> CLMObservation:
|
| 81 |
-
# return CLMObservation(
|
| 82 |
-
# tasks=[t.model_dump() for t in obs.tasks],
|
| 83 |
-
# visible_state=obs.visible_state.model_dump(),
|
| 84 |
-
# time_step=obs.time_step,
|
| 85 |
-
# done=done,
|
| 86 |
-
# reward=reward,
|
| 87 |
-
# metadata=info or {},
|
| 88 |
-
# )
|
| 89 |
-
|
| 90 |
-
# def reset(self, seed: Optional[int] = None, episode_id: Optional[str] = None, task_id: str = "easy", **kwargs) -> CLMObservation:
|
| 91 |
-
# if task_id not in ("easy", "medium", "hard"):
|
| 92 |
-
# task_id = "easy"
|
| 93 |
-
# tasks = generate_tasks(task_id)
|
| 94 |
-
# self._env = CLMEnvironment(tasks=tasks, max_steps=50)
|
| 95 |
-
# self._final_score = 0.0
|
| 96 |
-
# obs = self._env.reset()
|
| 97 |
-
# return self._to_oe_obs(obs)
|
| 98 |
-
|
| 99 |
-
# def step(self, action: CLMAction, timeout_s: Optional[float] = None, **kwargs) -> CLMObservation:
|
| 100 |
-
# model_action = ModelAction(type=action.type, task_id=action.task_id)
|
| 101 |
-
# obs, reward, done, info = self._env.step(model_action)
|
| 102 |
-
# if done:
|
| 103 |
-
# self._final_score = deterministic_grader(
|
| 104 |
-
# self._env.state.tasks,
|
| 105 |
-
# self._env.state.time_step,
|
| 106 |
-
# self._env.state.energy,
|
| 107 |
-
# )
|
| 108 |
-
# info["final_score"] = self._final_score
|
| 109 |
-
# return self._to_oe_obs(obs, done=done, reward=float(reward), info=info)
|
| 110 |
-
|
| 111 |
-
# @property
|
| 112 |
-
# def state(self) -> CLMState:
|
| 113 |
-
# raw = self._env.state_dict()
|
| 114 |
-
# return CLMState(
|
| 115 |
-
# energy=raw.get("energy", 1.0),
|
| 116 |
-
# stress=raw.get("stress", 0.0),
|
| 117 |
-
# fatigue=raw.get("fatigue", 0.0),
|
| 118 |
-
# current_task_id=raw.get("current_task_id"),
|
| 119 |
-
# tasks=raw.get("tasks", []),
|
| 120 |
-
# step_count=raw.get("time_step", 0),
|
| 121 |
-
# )
|
| 122 |
-
|
| 123 |
-
# def get_metadata(self) -> EnvironmentMetadata:
|
| 124 |
-
# return EnvironmentMetadata(
|
| 125 |
-
# name="cognitive-load-manager",
|
| 126 |
-
# description=(
|
| 127 |
-
# "Cognitive Load Manager (CLM) simulates human cognitive load "
|
| 128 |
-
# "(energy, stress, fatigue) while managing tasks with deadlines. "
|
| 129 |
-
# "Three difficulty levels: easy (2 tasks, no deadlines), "
|
| 130 |
-
# "medium (5 tasks with deadlines), hard (8 tasks with tight deadlines)."
|
| 131 |
-
# ),
|
| 132 |
-
# version="1.0.0",
|
| 133 |
-
# author="Team Innovators",
|
| 134 |
-
# )
|
| 135 |
-
|
| 136 |
-
# def close(self) -> None:
|
| 137 |
-
# pass
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
# # ── Build FastAPI app via OpenEnv HTTPEnvServer ──────────────────────────────
|
| 141 |
-
|
| 142 |
-
# def build_app() -> FastAPI:
|
| 143 |
-
# server = HTTPEnvServer(
|
| 144 |
-
# env=CLMEnvWrapper,
|
| 145 |
-
# action_cls=CLMAction,
|
| 146 |
-
# observation_cls=CLMObservation,
|
| 147 |
-
# max_concurrent_envs=10,
|
| 148 |
-
# )
|
| 149 |
-
|
| 150 |
-
# _app = FastAPI(
|
| 151 |
-
# title="Cognitive Load Manager (CLM) Environment API",
|
| 152 |
-
# version="1.0.0",
|
| 153 |
-
# description=(
|
| 154 |
-
# "OpenEnv-compliant environment for the Meta PyTorch Hackathon. "
|
| 155 |
-
# "Simulates cognitive load management with three difficulty levels."
|
| 156 |
-
# ),
|
| 157 |
-
# )
|
| 158 |
-
|
| 159 |
-
# _app.add_middleware(
|
| 160 |
-
# CORSMiddleware,
|
| 161 |
-
# allow_origins=["*"],
|
| 162 |
-
# allow_credentials=True,
|
| 163 |
-
# allow_methods=["*"],
|
| 164 |
-
# allow_headers=["*"],
|
| 165 |
-
# )
|
| 166 |
-
|
| 167 |
-
# server.register_routes(_app)
|
| 168 |
-
# return _app
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
# app = build_app()
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
import uuid
|
| 176 |
import os
|
| 177 |
import sys
|
| 178 |
from typing import Dict, Any, Optional, List
|
|
@@ -203,180 +28,85 @@ from openenv.core.env_server.types import (
|
|
| 203 |
)
|
| 204 |
from openenv.core.env_server.http_server import HTTPEnvServer
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
title="Cognitive Load Manager (CLM) Environment API",
|
| 213 |
-
version="1.0.0"
|
| 214 |
-
)
|
| 215 |
-
|
| 216 |
-
app.add_middleware(
|
| 217 |
-
CORSMiddleware,
|
| 218 |
-
allow_origins=["*"],
|
| 219 |
-
allow_credentials=True,
|
| 220 |
-
allow_methods=["*"],
|
| 221 |
-
allow_headers=["*"],
|
| 222 |
)
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
|
| 228 |
-
# ── Request / Response Models ────────────────────────────────────────────────
|
| 229 |
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
class StepRequest(BaseModel):
|
| 242 |
-
session_id: str = "default"
|
| 243 |
-
action: Optional[Action] = None
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
class StepResponse(BaseModel):
|
| 247 |
-
observation: Any
|
| 248 |
-
reward: float
|
| 249 |
-
done: bool
|
| 250 |
-
info: Dict[str, Any]
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
# ── Routes ──────────────────────────────────────────────────────────────────
|
| 254 |
-
# Add the home route with details of all the other routes
|
| 255 |
-
@app.get("/")
|
| 256 |
-
def read_root():
|
| 257 |
-
routes = []
|
| 258 |
-
for route in app.routes:
|
| 259 |
-
route_info = {
|
| 260 |
-
"path": route.path,
|
| 261 |
-
"name": getattr(route, "name", "")
|
| 262 |
-
}
|
| 263 |
-
if hasattr(route, "methods"):
|
| 264 |
-
route_info["methods"] = list(route.methods)
|
| 265 |
-
routes.append(route_info)
|
| 266 |
-
|
| 267 |
-
return {
|
| 268 |
-
"message": "Cognitive Load Manager is running 🚀",
|
| 269 |
-
"routes": routes
|
| 270 |
-
}
|
| 271 |
-
|
| 272 |
-
@app.post("/reset", response_model=ResetResponse)
|
| 273 |
-
def reset_env(req: Optional[ResetRequest] = None):
|
| 274 |
-
if req is None:
|
| 275 |
-
req = ResetRequest()
|
| 276 |
-
|
| 277 |
-
if req.level not in ["easy", "medium", "hard"]:
|
| 278 |
-
raise HTTPException(status_code=400, detail="Invalid level")
|
| 279 |
-
|
| 280 |
-
if req.task_id not in ["easy", "medium", "hard"]:
|
| 281 |
-
raise HTTPException(status_code=400, detail="Invalid task_id")
|
| 282 |
-
|
| 283 |
-
# FIX: choose ONE (task_id is better)
|
| 284 |
-
tasks = generate_tasks(req.task_id)
|
| 285 |
-
|
| 286 |
-
env = CLMEnvironment(tasks=tasks, max_steps=50)
|
| 287 |
-
obs = env.reset()
|
| 288 |
-
|
| 289 |
-
sess_id = req.session_id or str(uuid.uuid4())
|
| 290 |
-
sessions[sess_id] = env
|
| 291 |
-
|
| 292 |
-
return ResetResponse(session_id=sess_id, observation=obs)
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
@app.post("/step", response_model=StepResponse)
|
| 296 |
-
def step_env(req: Optional[StepRequest] = None):
|
| 297 |
-
if req is None:
|
| 298 |
-
req = StepRequest()
|
| 299 |
-
|
| 300 |
-
if req.action is None:
|
| 301 |
-
req.action = Action(type="work")
|
| 302 |
-
|
| 303 |
-
if req.session_id not in sessions:
|
| 304 |
-
tasks = generate_tasks("easy")
|
| 305 |
env = CLMEnvironment(tasks=tasks, max_steps=50)
|
| 306 |
env.reset()
|
| 307 |
-
sessions[req.session_id] = env
|
| 308 |
-
|
| 309 |
-
env = sessions[req.session_id]
|
| 310 |
-
|
| 311 |
-
obs, reward, done, info = env.step(req.action)
|
| 312 |
-
|
| 313 |
-
if done:
|
| 314 |
score = deterministic_grader(
|
| 315 |
env.state.tasks,
|
| 316 |
env.state.time_step,
|
| 317 |
-
env.state.energy
|
| 318 |
)
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
@app.get("/state")
|
| 330 |
-
def get_state(session_id: Optional[str] = "default"):
|
| 331 |
-
if session_id not in sessions:
|
| 332 |
-
tasks = generate_tasks("easy")
|
| 333 |
-
env = CLMEnvironment(tasks=tasks, max_steps=50)
|
| 334 |
-
env.reset()
|
| 335 |
-
sessions[session_id] = env
|
| 336 |
-
|
| 337 |
-
return sessions[session_id].state_dict()
|
| 338 |
|
| 339 |
|
| 340 |
-
#
|
| 341 |
-
# ── PART 2: OPENENV COMPATIBLE WRAPPER ───────────────────────────────────────
|
| 342 |
-
# =============================================================================
|
| 343 |
|
| 344 |
class CLMAction(OEAction):
|
| 345 |
-
type: str = Field(description="work, break, switch, delay")
|
| 346 |
-
task_id: Optional[str] = None
|
| 347 |
-
|
| 348 |
model_config = {"extra": "allow"}
|
| 349 |
|
| 350 |
|
| 351 |
class CLMObservation(OEObservation):
|
| 352 |
tasks: List[Dict[str, Any]] = Field(default_factory=list)
|
| 353 |
visible_state: Dict[str, Any] = Field(default_factory=dict)
|
| 354 |
-
time_step: int = 0
|
| 355 |
-
|
| 356 |
model_config = {"extra": "allow"}
|
| 357 |
|
| 358 |
|
| 359 |
class CLMState(OEState):
|
| 360 |
-
energy: float = 1.0
|
| 361 |
-
stress: float = 0.0
|
| 362 |
-
fatigue: float = 0.0
|
| 363 |
-
current_task_id: Optional[str] = None
|
| 364 |
tasks: List[Dict[str, Any]] = Field(default_factory=list)
|
| 365 |
-
|
| 366 |
model_config = {"extra": "allow"}
|
| 367 |
|
| 368 |
|
| 369 |
class CLMEnvWrapper(Environment):
|
| 370 |
-
|
| 371 |
SUPPORTS_CONCURRENT_SESSIONS = True
|
| 372 |
|
| 373 |
def __init__(self):
|
| 374 |
super().__init__()
|
| 375 |
tasks = generate_tasks("easy")
|
| 376 |
self._env = CLMEnvironment(tasks=tasks, max_steps=50)
|
| 377 |
-
self._final_score =
|
| 378 |
|
| 379 |
-
def
|
|
|
|
| 380 |
return CLMObservation(
|
| 381 |
tasks=[t.model_dump() for t in obs.tasks],
|
| 382 |
visible_state=obs.visible_state.model_dump(),
|
|
@@ -386,30 +116,30 @@ class CLMEnvWrapper(Environment):
|
|
| 386 |
metadata=info or {},
|
| 387 |
)
|
| 388 |
|
| 389 |
-
def reset(self,
|
|
|
|
| 390 |
if task_id not in ("easy", "medium", "hard"):
|
| 391 |
task_id = "easy"
|
| 392 |
-
|
| 393 |
tasks = generate_tasks(task_id)
|
| 394 |
self._env = CLMEnvironment(tasks=tasks, max_steps=50)
|
| 395 |
-
|
| 396 |
obs = self._env.reset()
|
| 397 |
-
return self.
|
| 398 |
-
|
| 399 |
-
def step(self, action: CLMAction, **kwargs):
|
| 400 |
-
model_action = Action(type=action.type, task_id=action.task_id)
|
| 401 |
|
|
|
|
|
|
|
| 402 |
obs, reward, done, info = self._env.step(model_action)
|
| 403 |
|
| 404 |
if done:
|
| 405 |
-
|
| 406 |
self._env.state.tasks,
|
| 407 |
self._env.state.time_step,
|
| 408 |
self._env.state.energy,
|
| 409 |
)
|
|
|
|
| 410 |
info["final_score"] = self._final_score
|
| 411 |
-
|
| 412 |
-
return self.
|
| 413 |
|
| 414 |
@property
|
| 415 |
def state(self):
|
|
@@ -435,15 +165,60 @@ class CLMEnvWrapper(Environment):
|
|
| 435 |
pass
|
| 436 |
|
| 437 |
|
| 438 |
-
#
|
| 439 |
-
|
| 440 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
|
| 442 |
-
server = HTTPEnvServer(
|
| 443 |
-
env=CLMEnvWrapper,
|
| 444 |
-
action_cls=CLMAction,
|
| 445 |
-
observation_cls=CLMObservation,
|
| 446 |
-
max_concurrent_envs=10,
|
| 447 |
-
)
|
| 448 |
|
| 449 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
from typing import Dict, Any, Optional, List
|
|
|
|
| 28 |
)
|
| 29 |
from openenv.core.env_server.http_server import HTTPEnvServer
|
| 30 |
|
| 31 |
+
from models import (
|
| 32 |
+
Action as ModelAction,
|
| 33 |
+
Observation as ModelObservation,
|
| 34 |
+
generate_tasks,
|
| 35 |
+
deterministic_grader,
|
| 36 |
+
CLMEnvironment,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
+
_SCORE_MIN = 0.01
|
| 40 |
+
_SCORE_MAX = 0.99
|
|
|
|
| 41 |
|
|
|
|
| 42 |
|
| 43 |
+
def _safe_score(raw: float) -> float:
|
| 44 |
+
"""Clamp to strictly open interval (0, 1). Never returns 0.0 or 1.0."""
|
| 45 |
+
try:
|
| 46 |
+
s = float(raw)
|
| 47 |
+
except (TypeError, ValueError):
|
| 48 |
+
return _SCORE_MIN
|
| 49 |
+
return round(max(_SCORE_MIN, min(_SCORE_MAX, s)), 4)
|
| 50 |
|
| 51 |
|
| 52 |
+
def _grade_task(difficulty: str) -> dict:
|
| 53 |
+
"""Run deterministic grader on a fresh environment for the given difficulty."""
|
| 54 |
+
try:
|
| 55 |
+
tasks = generate_tasks(difficulty)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
env = CLMEnvironment(tasks=tasks, max_steps=50)
|
| 57 |
env.reset()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
score = deterministic_grader(
|
| 59 |
env.state.tasks,
|
| 60 |
env.state.time_step,
|
| 61 |
+
env.state.energy,
|
| 62 |
)
|
| 63 |
+
score = _safe_score(score)
|
| 64 |
+
except Exception:
|
| 65 |
+
score = _SCORE_MIN
|
| 66 |
+
return {
|
| 67 |
+
"task_id": difficulty,
|
| 68 |
+
"reward": score,
|
| 69 |
+
"score": score,
|
| 70 |
+
"done": False,
|
| 71 |
+
"grader_message": f"CLM deterministic grader for difficulty={difficulty}",
|
| 72 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
+
# ── OpenEnv-compatible Action / Observation / State models ──────────────────
|
|
|
|
|
|
|
| 76 |
|
| 77 |
class CLMAction(OEAction):
|
| 78 |
+
type: str = Field(description="Action type: work, break, switch, or delay")
|
| 79 |
+
task_id: Optional[str] = Field(default=None, description="Task ID to act on")
|
|
|
|
| 80 |
model_config = {"extra": "allow"}
|
| 81 |
|
| 82 |
|
| 83 |
class CLMObservation(OEObservation):
|
| 84 |
tasks: List[Dict[str, Any]] = Field(default_factory=list)
|
| 85 |
visible_state: Dict[str, Any] = Field(default_factory=dict)
|
| 86 |
+
time_step: int = Field(default=0)
|
|
|
|
| 87 |
model_config = {"extra": "allow"}
|
| 88 |
|
| 89 |
|
| 90 |
class CLMState(OEState):
|
| 91 |
+
energy: float = Field(default=1.0)
|
| 92 |
+
stress: float = Field(default=0.0)
|
| 93 |
+
fatigue: float = Field(default=0.0)
|
| 94 |
+
current_task_id: Optional[str] = Field(default=None)
|
| 95 |
tasks: List[Dict[str, Any]] = Field(default_factory=list)
|
|
|
|
| 96 |
model_config = {"extra": "allow"}
|
| 97 |
|
| 98 |
|
| 99 |
class CLMEnvWrapper(Environment):
|
|
|
|
| 100 |
SUPPORTS_CONCURRENT_SESSIONS = True
|
| 101 |
|
| 102 |
def __init__(self):
|
| 103 |
super().__init__()
|
| 104 |
tasks = generate_tasks("easy")
|
| 105 |
self._env = CLMEnvironment(tasks=tasks, max_steps=50)
|
| 106 |
+
self._final_score: float = _SCORE_MIN
|
| 107 |
|
| 108 |
+
def _to_oe_obs(self, obs: ModelObservation, done: bool = False,
|
| 109 |
+
reward: Optional[float] = None, info: Optional[dict] = None) -> CLMObservation:
|
| 110 |
return CLMObservation(
|
| 111 |
tasks=[t.model_dump() for t in obs.tasks],
|
| 112 |
visible_state=obs.visible_state.model_dump(),
|
|
|
|
| 116 |
metadata=info or {},
|
| 117 |
)
|
| 118 |
|
| 119 |
+
def reset(self, seed: Optional[int] = None, episode_id: Optional[str] = None,
|
| 120 |
+
task_id: str = "easy", **kwargs) -> CLMObservation:
|
| 121 |
if task_id not in ("easy", "medium", "hard"):
|
| 122 |
task_id = "easy"
|
|
|
|
| 123 |
tasks = generate_tasks(task_id)
|
| 124 |
self._env = CLMEnvironment(tasks=tasks, max_steps=50)
|
| 125 |
+
self._final_score = _SCORE_MIN
|
| 126 |
obs = self._env.reset()
|
| 127 |
+
return self._to_oe_obs(obs)
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
def step(self, action: CLMAction, timeout_s: Optional[float] = None, **kwargs) -> CLMObservation:
|
| 130 |
+
model_action = ModelAction(type=action.type, task_id=action.task_id)
|
| 131 |
obs, reward, done, info = self._env.step(model_action)
|
| 132 |
|
| 133 |
if done:
|
| 134 |
+
raw_score = deterministic_grader(
|
| 135 |
self._env.state.tasks,
|
| 136 |
self._env.state.time_step,
|
| 137 |
self._env.state.energy,
|
| 138 |
)
|
| 139 |
+
self._final_score = _safe_score(raw_score)
|
| 140 |
info["final_score"] = self._final_score
|
| 141 |
+
safe_reward = _safe_score(float(reward))
|
| 142 |
+
return self._to_oe_obs(obs, done=done, reward=safe_reward, info=info)
|
| 143 |
|
| 144 |
@property
|
| 145 |
def state(self):
|
|
|
|
| 165 |
pass
|
| 166 |
|
| 167 |
|
| 168 |
+
# ── Build FastAPI app ────────────────────────────────────────────────────────
|
| 169 |
+
|
| 170 |
+
def build_app() -> FastAPI:
|
| 171 |
+
server = HTTPEnvServer(
|
| 172 |
+
env=CLMEnvWrapper,
|
| 173 |
+
action_cls=CLMAction,
|
| 174 |
+
observation_cls=CLMObservation,
|
| 175 |
+
max_concurrent_envs=10,
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
_app = FastAPI(
|
| 179 |
+
title="Cognitive Load Manager (CLM) Environment API",
|
| 180 |
+
version="1.0.0",
|
| 181 |
+
description=(
|
| 182 |
+
"OpenEnv-compliant environment for the Meta PyTorch Hackathon. "
|
| 183 |
+
"Simulates cognitive load management with three difficulty levels."
|
| 184 |
+
),
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
_app.add_middleware(
|
| 188 |
+
CORSMiddleware,
|
| 189 |
+
allow_origins=["*"],
|
| 190 |
+
allow_credentials=True,
|
| 191 |
+
allow_methods=["*"],
|
| 192 |
+
allow_headers=["*"],
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
server.register_routes(_app)
|
| 196 |
+
|
| 197 |
+
# ── Grade endpoints (required by hackathon Phase 2 validator) ────────────
|
| 198 |
+
# Validator calls GET /grader and GET /grade/{task_id} to score each task.
|
| 199 |
+
# Scores must be strictly in (0.01, 0.99) — never 0.0 or 1.0.
|
| 200 |
+
|
| 201 |
+
@_app.get("/grader", tags=["Grader"])
|
| 202 |
+
async def get_grader_score():
|
| 203 |
+
"""General grader endpoint — returns score for 'easy' difficulty."""
|
| 204 |
+
return _grade_task("easy")
|
| 205 |
+
|
| 206 |
+
@_app.get("/grade/easy", tags=["Grader"])
|
| 207 |
+
async def grade_easy():
|
| 208 |
+
"""Grade the 'easy' task (2 tasks, no deadlines)."""
|
| 209 |
+
return _grade_task("easy")
|
| 210 |
+
|
| 211 |
+
@_app.get("/grade/medium", tags=["Grader"])
|
| 212 |
+
async def grade_medium():
|
| 213 |
+
"""Grade the 'medium' task (5 tasks with deadlines)."""
|
| 214 |
+
return _grade_task("medium")
|
| 215 |
+
|
| 216 |
+
@_app.get("/grade/hard", tags=["Grader"])
|
| 217 |
+
async def grade_hard():
|
| 218 |
+
"""Grade the 'hard' task (8 tasks with tight deadlines)."""
|
| 219 |
+
return _grade_task("hard")
|
| 220 |
+
|
| 221 |
+
return _app
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
+
app = build_app()
|
grader/__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from grader.clm_graders import EasyGrader, MediumGrader, HardGrader
|
| 2 |
+
|
| 3 |
+
__all__ = ["EasyGrader", "MediumGrader", "HardGrader"]
|
grader/clm_graders.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Class-based graders for CLM tasks — matches auto-dev's BaseGrader interface.
|
| 3 |
+
|
| 4 |
+
The hackathon validator:
|
| 5 |
+
1. Reads openenv.yaml to find grader: "grader.clm_graders:EasyGrader"
|
| 6 |
+
2. Imports the module: from grader.clm_graders import EasyGrader
|
| 7 |
+
3. Instantiates the class: g = EasyGrader()
|
| 8 |
+
4. Calls grade(): score, done, msg = g.grade(...)
|
| 9 |
+
5. Checks 0 < score < 1
|
| 10 |
+
|
| 11 |
+
Scores are ALWAYS strictly in (0.01, 0.99) — never 0.0 or 1.0.
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import sys
|
| 15 |
+
import os
|
| 16 |
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 17 |
+
|
| 18 |
+
from models import generate_tasks, deterministic_grader, CLMEnvironment
|
| 19 |
+
|
| 20 |
+
_SCORE_MIN = 0.01
|
| 21 |
+
_SCORE_MAX = 0.99
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _safe(raw) -> float:
|
| 25 |
+
"""Clamp to strictly open interval (0.01, 0.99). Never returns 0.0 or 1.0."""
|
| 26 |
+
try:
|
| 27 |
+
val = float(raw)
|
| 28 |
+
except (TypeError, ValueError):
|
| 29 |
+
return _SCORE_MIN
|
| 30 |
+
return round(max(_SCORE_MIN, min(_SCORE_MAX, val)), 4)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def _compute_grade(difficulty: str) -> tuple[float, bool, str]:
|
| 34 |
+
"""Run the deterministic grader on a fresh env for the given difficulty."""
|
| 35 |
+
try:
|
| 36 |
+
tasks = generate_tasks(difficulty)
|
| 37 |
+
env = CLMEnvironment(tasks=tasks, max_steps=50)
|
| 38 |
+
env.reset()
|
| 39 |
+
raw = deterministic_grader(
|
| 40 |
+
env.state.tasks,
|
| 41 |
+
env.state.time_step,
|
| 42 |
+
env.state.energy,
|
| 43 |
+
)
|
| 44 |
+
score = _safe(raw)
|
| 45 |
+
except Exception:
|
| 46 |
+
score = _SCORE_MIN
|
| 47 |
+
return score, score >= 0.5, f"CLM {difficulty} grade: {score:.4f}"
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class EasyGrader:
|
| 51 |
+
"""Grader for the 'easy' CLM task (2 tasks, no deadlines)."""
|
| 52 |
+
|
| 53 |
+
def grade(self, *args, **kwargs) -> tuple[float, bool, str]:
|
| 54 |
+
return _compute_grade("easy")
|
| 55 |
+
|
| 56 |
+
def __call__(self, *args, **kwargs) -> float:
|
| 57 |
+
score, _, _ = _compute_grade("easy")
|
| 58 |
+
return score
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
class MediumGrader:
|
| 62 |
+
"""Grader for the 'medium' CLM task (5 tasks with deadlines)."""
|
| 63 |
+
|
| 64 |
+
def grade(self, *args, **kwargs) -> tuple[float, bool, str]:
|
| 65 |
+
return _compute_grade("medium")
|
| 66 |
+
|
| 67 |
+
def __call__(self, *args, **kwargs) -> float:
|
| 68 |
+
score, _, _ = _compute_grade("medium")
|
| 69 |
+
return score
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class HardGrader:
|
| 73 |
+
"""Grader for the 'hard' CLM task (8 tasks with tight deadlines)."""
|
| 74 |
+
|
| 75 |
+
def grade(self, *args, **kwargs) -> tuple[float, bool, str]:
|
| 76 |
+
return _compute_grade("hard")
|
| 77 |
+
|
| 78 |
+
def __call__(self, *args, **kwargs) -> float:
|
| 79 |
+
score, _, _ = _compute_grade("hard")
|
| 80 |
+
return score
|
inference.py
CHANGED
|
@@ -16,7 +16,7 @@ except ImportError:
|
|
| 16 |
|
| 17 |
from openai import OpenAI
|
| 18 |
|
| 19 |
-
# ──
|
| 20 |
# The hackathon validator INJECTS API_BASE_URL and API_KEY into the environment.
|
| 21 |
# We MUST use those values directly — never override them with HF_TOKEN or defaults.
|
| 22 |
API_BASE_URL = os.environ.get("API_BASE_URL", "https://router.huggingface.co/v1")
|
|
@@ -26,7 +26,7 @@ if not API_KEY:
|
|
| 26 |
API_KEY = "missing"
|
| 27 |
|
| 28 |
MODEL_NAME = os.environ.get("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
|
| 29 |
-
ENV_BASE_URL = os.environ.get("ENV_BASE_URL", "http://localhost:
|
| 30 |
|
| 31 |
print("DEBUG BASE URL:", API_BASE_URL, flush=True)
|
| 32 |
print("DEBUG MODEL:", MODEL_NAME, flush=True)
|
|
@@ -50,98 +50,138 @@ def post_json(url: str, payload: dict) -> dict:
|
|
| 50 |
req = urllib.request.Request(
|
| 51 |
url, data=data, headers={"Content-Type": "application/json"}
|
| 52 |
)
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
# ── LOGGING ────────────────────────────────────────────────────
|
| 58 |
def log_start(task: str, env: str, model: str) -> None:
|
| 59 |
print(f"[START] task={task} env={env} model={model}", flush=True)
|
| 60 |
|
|
|
|
| 61 |
def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
|
| 62 |
print(
|
| 63 |
-
f"[STEP] step={step} action={action} reward={reward:.2f}
|
|
|
|
| 64 |
flush=True,
|
| 65 |
)
|
| 66 |
|
|
|
|
| 67 |
def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
|
|
|
|
| 68 |
print(
|
| 69 |
-
f"[END] success={str(success).lower()} steps={steps}
|
|
|
|
| 70 |
flush=True,
|
| 71 |
)
|
| 72 |
|
| 73 |
|
| 74 |
# ── MAIN ───────────────────────────────────────────────────────
|
| 75 |
def main():
|
| 76 |
-
|
| 77 |
task_id = os.environ.get("CLM_LEVEL", "hard")
|
| 78 |
|
| 79 |
log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
done = False
|
| 86 |
step = 0
|
| 87 |
-
rewards = []
|
| 88 |
-
history = []
|
|
|
|
| 89 |
|
|
|
|
| 90 |
while not done and step < MAX_STEPS:
|
| 91 |
step += 1
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
{
|
| 116 |
-
"role": "user",
|
| 117 |
-
"content": json.dumps(observation),
|
| 118 |
-
},
|
| 119 |
-
],
|
| 120 |
-
temperature=0.1,
|
| 121 |
-
max_tokens=120,
|
| 122 |
)
|
| 123 |
|
| 124 |
-
|
|
|
|
| 125 |
|
| 126 |
-
#
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
action = {"type": "delay"}
|
| 134 |
-
else:
|
| 135 |
-
action = {"type": "delay"}
|
| 136 |
|
| 137 |
# Validate action type
|
| 138 |
valid_types = {"work", "break", "switch", "delay"}
|
| 139 |
if action.get("type") not in valid_types:
|
| 140 |
action = {"type": "delay"}
|
| 141 |
|
| 142 |
-
action_str = json.dumps(action)
|
| 143 |
|
| 144 |
-
# ── ENV STEP ──
|
| 145 |
try:
|
| 146 |
step_data = post_json(
|
| 147 |
f"{ENV_BASE_URL}/step",
|
|
@@ -150,16 +190,21 @@ def main():
|
|
| 150 |
observation = step_data["observation"]
|
| 151 |
reward = float(step_data.get("reward", 0.0))
|
| 152 |
done = bool(step_data.get("done", False))
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
| 156 |
|
| 157 |
rewards.append(reward)
|
| 158 |
-
history.append(action_str)
|
| 159 |
|
| 160 |
-
log_step(step, action_str, reward, done,
|
| 161 |
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
| 163 |
success = score >= SUCCESS_SCORE_THRESHOLD
|
| 164 |
|
| 165 |
log_end(success, step, score, rewards)
|
|
|
|
| 16 |
|
| 17 |
from openai import OpenAI
|
| 18 |
|
| 19 |
+
# ── Credentials ───────────────────────────────────────────────────────────────
|
| 20 |
# The hackathon validator INJECTS API_BASE_URL and API_KEY into the environment.
|
| 21 |
# We MUST use those values directly — never override them with HF_TOKEN or defaults.
|
| 22 |
API_BASE_URL = os.environ.get("API_BASE_URL", "https://router.huggingface.co/v1")
|
|
|
|
| 26 |
API_KEY = "missing"
|
| 27 |
|
| 28 |
MODEL_NAME = os.environ.get("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
|
| 29 |
+
ENV_BASE_URL = os.environ.get("ENV_BASE_URL", "http://localhost:7860")
|
| 30 |
|
| 31 |
print("DEBUG BASE URL:", API_BASE_URL, flush=True)
|
| 32 |
print("DEBUG MODEL:", MODEL_NAME, flush=True)
|
|
|
|
| 50 |
req = urllib.request.Request(
|
| 51 |
url, data=data, headers={"Content-Type": "application/json"}
|
| 52 |
)
|
| 53 |
+
try:
|
| 54 |
+
with urllib.request.urlopen(req, timeout=30) as res:
|
| 55 |
+
return json.loads(res.read().decode("utf-8"))
|
| 56 |
+
except urllib.error.HTTPError as e:
|
| 57 |
+
raise Exception(f"HTTP {e.code}: {e.read().decode('utf-8')[:200]}")
|
| 58 |
|
| 59 |
|
| 60 |
# ── LOGGING ────────────────────────────────────────────────────
|
| 61 |
def log_start(task: str, env: str, model: str) -> None:
|
| 62 |
print(f"[START] task={task} env={env} model={model}", flush=True)
|
| 63 |
|
| 64 |
+
|
| 65 |
def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
|
| 66 |
print(
|
| 67 |
+
f"[STEP] step={step} action={action} reward={reward:.2f} "
|
| 68 |
+
f"done={str(done).lower()} error={error or 'null'}",
|
| 69 |
flush=True,
|
| 70 |
)
|
| 71 |
|
| 72 |
+
|
| 73 |
def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
|
| 74 |
+
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
|
| 75 |
print(
|
| 76 |
+
f"[END] success={str(success).lower()} steps={steps} "
|
| 77 |
+
f"score={score:.3f} rewards={rewards_str}",
|
| 78 |
flush=True,
|
| 79 |
)
|
| 80 |
|
| 81 |
|
| 82 |
# ── MAIN ───────────────────────────────────────────────────────
|
| 83 |
def main():
|
|
|
|
| 84 |
task_id = os.environ.get("CLM_LEVEL", "hard")
|
| 85 |
|
| 86 |
log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)
|
| 87 |
|
| 88 |
+
# ── 1. Reset environment ─────────────────────────────────────
|
| 89 |
+
try:
|
| 90 |
+
data = post_json(f"{ENV_BASE_URL}/reset", {"task_id": task_id})
|
| 91 |
+
session_id = data.get("session_id", "default")
|
| 92 |
+
observation = data["observation"]
|
| 93 |
+
except Exception as e:
|
| 94 |
+
log_step(step=0, action="reset", reward=0.0, done=True, error=str(e)[:80])
|
| 95 |
+
log_end(success=False, steps=0, score=0.0, rewards=[])
|
| 96 |
+
return
|
| 97 |
|
| 98 |
done = False
|
| 99 |
step = 0
|
| 100 |
+
rewards: List[float] = []
|
| 101 |
+
history: List[str] = []
|
| 102 |
+
info: dict = {}
|
| 103 |
|
| 104 |
+
# ── 2. Agent loop ────────────────────────────────────────────
|
| 105 |
while not done and step < MAX_STEPS:
|
| 106 |
step += 1
|
| 107 |
|
| 108 |
+
history_str = "\n".join(history[-5:]) if history else "No previous actions."
|
| 109 |
+
|
| 110 |
+
system_prompt = (
|
| 111 |
+
"You are an AI task scheduler managing human cognitive load.\n"
|
| 112 |
+
"You MUST respond with ONLY a JSON object (no markdown, no explanation).\n\n"
|
| 113 |
+
"ACTION FORMAT: {\"type\": \"<action>\", \"task_id\": \"<id or null>\"}\n"
|
| 114 |
+
"Valid types:\n"
|
| 115 |
+
" - \"work\" : work on task_id (requires task_id)\n"
|
| 116 |
+
" - \"break\" : rest to recover energy (task_id: null)\n"
|
| 117 |
+
" - \"switch\": switch to a different task_id (requires task_id)\n"
|
| 118 |
+
" - \"delay\" : wait/do nothing (task_id: null)\n\n"
|
| 119 |
+
"STRATEGY:\n"
|
| 120 |
+
"1. If fatigue_level is 'high' OR stress_warning is true → {\"type\": \"break\", \"task_id\": null}\n"
|
| 121 |
+
"2. If fatigue_level is 'medium' and stress is manageable → {\"type\": \"work\", \"task_id\": \"<earliest deadline incomplete task>\"}\n"
|
| 122 |
+
"3. Otherwise → {\"type\": \"work\", \"task_id\": \"<earliest deadline incomplete task>\"}\n"
|
| 123 |
+
"4. Pick incomplete tasks (progress < 1.0) with the earliest deadline first.\n"
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
user_prompt = (
|
| 127 |
+
f"Previous 5 steps:\n{history_str}\n\n"
|
| 128 |
+
f"Current observation:\n{json.dumps(observation, indent=2)}\n\n"
|
| 129 |
+
"What is your next action JSON?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
)
|
| 131 |
|
| 132 |
+
action: Optional[dict] = None
|
| 133 |
+
error_msg: Optional[str] = None
|
| 134 |
|
| 135 |
+
# ── LLM call through the validator proxy ─────────────────
|
| 136 |
+
try:
|
| 137 |
+
completion = client.chat.completions.create(
|
| 138 |
+
model=MODEL_NAME,
|
| 139 |
+
messages=[
|
| 140 |
+
{"role": "system", "content": system_prompt},
|
| 141 |
+
{"role": "user", "content": user_prompt},
|
| 142 |
+
],
|
| 143 |
+
temperature=0.1,
|
| 144 |
+
max_tokens=150,
|
| 145 |
+
)
|
| 146 |
+
text = (completion.choices[0].message.content or "").strip()
|
| 147 |
+
|
| 148 |
+
# Strip markdown fences if present
|
| 149 |
+
if text.startswith("```json"):
|
| 150 |
+
text = text[7:]
|
| 151 |
+
if text.startswith("```"):
|
| 152 |
+
text = text[3:]
|
| 153 |
+
if text.endswith("```"):
|
| 154 |
+
text = text[:-3]
|
| 155 |
+
text = text.strip()
|
| 156 |
+
|
| 157 |
+
# Extract JSON
|
| 158 |
+
s = text.find("{")
|
| 159 |
+
e = text.rfind("}")
|
| 160 |
+
if s != -1 and e != -1:
|
| 161 |
+
action = json.loads(text[s : e + 1])
|
| 162 |
+
except Exception as ex:
|
| 163 |
+
error_msg = str(ex)[:80]
|
| 164 |
+
|
| 165 |
+
# ── Heuristic fallback (only if LLM call failed / unparseable) ───
|
| 166 |
+
if not action:
|
| 167 |
+
tasks = observation.get("tasks", [])
|
| 168 |
+
incomp = [t for t in tasks if t.get("progress", 0.0) < 1.0]
|
| 169 |
+
fs = observation.get("visible_state", {})
|
| 170 |
+
if fs.get("fatigue_level") in ("high", "medium") or fs.get("stress_warning"):
|
| 171 |
+
action = {"type": "break"}
|
| 172 |
+
elif incomp:
|
| 173 |
+
action = {"type": "work", "task_id": incomp[0]["id"]}
|
| 174 |
+
else:
|
| 175 |
action = {"type": "delay"}
|
|
|
|
|
|
|
| 176 |
|
| 177 |
# Validate action type
|
| 178 |
valid_types = {"work", "break", "switch", "delay"}
|
| 179 |
if action.get("type") not in valid_types:
|
| 180 |
action = {"type": "delay"}
|
| 181 |
|
| 182 |
+
action_str = json.dumps(action, separators=(",", ":"))
|
| 183 |
|
| 184 |
+
# ── ENV STEP ─────────────────────────────────────────────
|
| 185 |
try:
|
| 186 |
step_data = post_json(
|
| 187 |
f"{ENV_BASE_URL}/step",
|
|
|
|
| 190 |
observation = step_data["observation"]
|
| 191 |
reward = float(step_data.get("reward", 0.0))
|
| 192 |
done = bool(step_data.get("done", False))
|
| 193 |
+
info = step_data.get("info", {})
|
| 194 |
+
except Exception as ex:
|
| 195 |
+
reward = 0.0
|
| 196 |
+
done = True
|
| 197 |
+
error_msg = error_msg or str(ex)[:80]
|
| 198 |
|
| 199 |
rewards.append(reward)
|
| 200 |
+
history.append(f"Step {step}: {action_str} -> reward={reward:.2f}")
|
| 201 |
|
| 202 |
+
log_step(step=step, action=action_str, reward=reward, done=done, error=error_msg)
|
| 203 |
|
| 204 |
+
# ── 3. Final scoring ─────────────────────────────────────────
|
| 205 |
+
score = float(info.get("final_score", 0.0))
|
| 206 |
+
if score == 0.0 and rewards:
|
| 207 |
+
score = sum(rewards) / len(rewards)
|
| 208 |
success = score >= SUCCESS_SCORE_THRESHOLD
|
| 209 |
|
| 210 |
log_end(success, step, score, rewards)
|
models.py
CHANGED
|
@@ -68,6 +68,7 @@ def grader(trajectory: dict) -> float:
|
|
| 68 |
|
| 69 |
Wraps deterministic_grader for use with the openenv-core task evaluation
|
| 70 |
framework. The trajectory dict should contain keys: tasks, time_step, energy.
|
|
|
|
| 71 |
"""
|
| 72 |
raw_tasks = trajectory.get("tasks", [])
|
| 73 |
time_step_val = trajectory.get("time_step", 50)
|
|
@@ -78,29 +79,35 @@ def grader(trajectory: dict) -> float:
|
|
| 78 |
|
| 79 |
def deterministic_grader(tasks: list[Task], time_step: int, final_energy: float) -> float:
|
| 80 |
"""
|
| 81 |
-
A deterministic grader returning 0.0
|
| 82 |
- completion rate
|
| 83 |
-
- deadline adherence
|
| 84 |
- energy efficiency
|
|
|
|
|
|
|
|
|
|
| 85 |
"""
|
|
|
|
| 86 |
if not tasks:
|
| 87 |
-
return 0.
|
| 88 |
-
|
| 89 |
completion_rate = sum(t.progress for t in tasks) / len(tasks)
|
| 90 |
-
|
| 91 |
-
#
|
| 92 |
missed_deadlines = 0
|
| 93 |
for t in tasks:
|
| 94 |
if t.deadline and time_step > t.deadline and t.progress < 1.0:
|
| 95 |
missed_deadlines += 1
|
| 96 |
-
|
| 97 |
deadline_penalty = min(0.3, missed_deadlines * 0.1)
|
| 98 |
-
|
| 99 |
-
#
|
| 100 |
-
energy_score = max(0.0, (final_energy - 0.1) * 0.
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
| 104 |
|
| 105 |
|
| 106 |
# ==========================================
|
|
@@ -201,7 +208,7 @@ class CLMEnvironment:
|
|
| 201 |
else:
|
| 202 |
reward += 1.0
|
| 203 |
|
| 204 |
-
reward = max(0.
|
| 205 |
|
| 206 |
return self._get_observation(), reward, done, self.state.model_dump()
|
| 207 |
|
|
|
|
| 68 |
|
| 69 |
Wraps deterministic_grader for use with the openenv-core task evaluation
|
| 70 |
framework. The trajectory dict should contain keys: tasks, time_step, energy.
|
| 71 |
+
Score is always strictly in the open interval (0.01, 0.99) — never 0.0 or 1.0.
|
| 72 |
"""
|
| 73 |
raw_tasks = trajectory.get("tasks", [])
|
| 74 |
time_step_val = trajectory.get("time_step", 50)
|
|
|
|
| 79 |
|
| 80 |
def deterministic_grader(tasks: list[Task], time_step: int, final_energy: float) -> float:
|
| 81 |
"""
|
| 82 |
+
A deterministic grader returning a score strictly in (0.01, 0.99) based on:
|
| 83 |
- completion rate
|
| 84 |
+
- deadline adherence
|
| 85 |
- energy efficiency
|
| 86 |
+
|
| 87 |
+
Score is NEVER exactly 0.0 or 1.0 — always strictly between 0 and 1
|
| 88 |
+
to satisfy openenv Phase 2 validation requirements.
|
| 89 |
"""
|
| 90 |
+
# Guard: no tasks → minimal score (not zero)
|
| 91 |
if not tasks:
|
| 92 |
+
return 0.01
|
| 93 |
+
|
| 94 |
completion_rate = sum(t.progress for t in tasks) / len(tasks)
|
| 95 |
+
|
| 96 |
+
# Penalty for missed deadlines
|
| 97 |
missed_deadlines = 0
|
| 98 |
for t in tasks:
|
| 99 |
if t.deadline and time_step > t.deadline and t.progress < 1.0:
|
| 100 |
missed_deadlines += 1
|
| 101 |
+
|
| 102 |
deadline_penalty = min(0.3, missed_deadlines * 0.1)
|
| 103 |
+
|
| 104 |
+
# Energy efficiency bonus (capped so total can't reach 1.0)
|
| 105 |
+
energy_score = max(0.0, (final_energy - 0.1) * 0.18)
|
| 106 |
+
|
| 107 |
+
raw = completion_rate * 0.78 - deadline_penalty + energy_score
|
| 108 |
+
|
| 109 |
+
# Strictly clamp to open interval (0.01, 0.99) — never 0.0 or 1.0
|
| 110 |
+
return round(max(0.01, min(0.99, raw)), 4)
|
| 111 |
|
| 112 |
|
| 113 |
# ==========================================
|
|
|
|
| 208 |
else:
|
| 209 |
reward += 1.0
|
| 210 |
|
| 211 |
+
reward = max(0.01, min(0.99, float(reward)))
|
| 212 |
|
| 213 |
return self._get_observation(), reward, done, self.state.model_dump()
|
| 214 |
|
openenv.yaml
CHANGED
|
@@ -1,63 +1,39 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
description: Cognitive Load Manager (CLM) simulates human cognitive load (energy, stress, fatigue) while managing tasks with deadlines.
|
| 3 |
version: "1.0.0"
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
properties:
|
| 13 |
-
id: { type: string }
|
| 14 |
-
difficulty: { type: string }
|
| 15 |
-
progress: { type: number }
|
| 16 |
-
deadline: { type: number, nullable: true }
|
| 17 |
-
visible_state:
|
| 18 |
-
type: object
|
| 19 |
-
properties:
|
| 20 |
-
fatigue_level: { type: string }
|
| 21 |
-
stress_warning: { type: boolean }
|
| 22 |
-
time_step: { type: integer }
|
| 23 |
-
action:
|
| 24 |
-
type: object
|
| 25 |
-
properties:
|
| 26 |
-
type: { type: string, enum: ["work", "break", "switch", "delay"] }
|
| 27 |
-
task_id: { type: string, nullable: true }
|
| 28 |
-
reward:
|
| 29 |
-
type: number
|
| 30 |
-
graders:
|
| 31 |
-
type: object
|
| 32 |
-
properties:
|
| 33 |
-
deterministic_grader:
|
| 34 |
-
type: object
|
| 35 |
-
properties:
|
| 36 |
-
description: { type: string }
|
| 37 |
-
fn: { type: string }
|
| 38 |
-
graders:
|
| 39 |
-
deterministic_grader:
|
| 40 |
-
description: "Evaluates agent performance based on task completion, deadline adherence, and energy efficiency"
|
| 41 |
-
fn: "models.grader"
|
| 42 |
tasks:
|
| 43 |
- id: easy
|
| 44 |
difficulty: easy
|
| 45 |
description: "2 easy tasks with no deadlines. Agent must complete both tasks without burning out."
|
| 46 |
max_steps: 50
|
| 47 |
-
grader:
|
| 48 |
-
|
| 49 |
-
description: "Evaluates agent performance based on task completion, deadline adherence, and energy efficiency"
|
| 50 |
- id: medium
|
| 51 |
difficulty: medium
|
| 52 |
description: "5 medium tasks with deadlines. Agent must balance speed and energy to meet deadlines."
|
| 53 |
max_steps: 50
|
| 54 |
-
grader:
|
| 55 |
-
|
| 56 |
-
description: "Evaluates agent performance based on task completion, deadline adherence, and energy efficiency"
|
| 57 |
- id: hard
|
| 58 |
difficulty: hard
|
| 59 |
-
description: "8 hard tasks with tight deadlines and hidden fatigue mechanics.
|
| 60 |
max_steps: 50
|
| 61 |
-
grader:
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
spec_version: 1
|
| 2 |
+
name: cognitive-load-manager
|
| 3 |
+
type: space
|
| 4 |
+
runtime: fastapi
|
| 5 |
+
app: server.app:app
|
| 6 |
+
port: 7860
|
| 7 |
description: Cognitive Load Manager (CLM) simulates human cognitive load (energy, stress, fatigue) while managing tasks with deadlines.
|
| 8 |
version: "1.0.0"
|
| 9 |
+
|
| 10 |
+
endpoints:
|
| 11 |
+
health: /health
|
| 12 |
+
reset: /reset
|
| 13 |
+
step: /step
|
| 14 |
+
state: /state
|
| 15 |
+
grade: /grader
|
| 16 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
tasks:
|
| 18 |
- id: easy
|
| 19 |
difficulty: easy
|
| 20 |
description: "2 easy tasks with no deadlines. Agent must complete both tasks without burning out."
|
| 21 |
max_steps: 50
|
| 22 |
+
grader: "grader.clm_graders:EasyGrader"
|
| 23 |
+
|
|
|
|
| 24 |
- id: medium
|
| 25 |
difficulty: medium
|
| 26 |
description: "5 medium tasks with deadlines. Agent must balance speed and energy to meet deadlines."
|
| 27 |
max_steps: 50
|
| 28 |
+
grader: "grader.clm_graders:MediumGrader"
|
| 29 |
+
|
|
|
|
| 30 |
- id: hard
|
| 31 |
difficulty: hard
|
| 32 |
+
description: "8 hard tasks with tight deadlines and hidden fatigue mechanics."
|
| 33 |
max_steps: 50
|
| 34 |
+
grader: "grader.clm_graders:HardGrader"
|
| 35 |
+
|
| 36 |
+
scoring:
|
| 37 |
+
reward_range: [0.01, 0.99]
|
| 38 |
+
success_threshold: 0.5
|
| 39 |
+
score_formula: deterministic_grader
|
server/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# Server module initialization
|
server/app.py
CHANGED
|
@@ -2,11 +2,14 @@ import uvicorn
|
|
| 2 |
import sys
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
sys.path.
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def main():
|
| 9 |
-
uvicorn.run(
|
|
|
|
| 10 |
|
| 11 |
if __name__ == "__main__":
|
| 12 |
main()
|
|
|
|
| 2 |
import sys
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 6 |
+
|
| 7 |
+
from backend.main import app # app is now importable as server.app:app
|
| 8 |
+
|
| 9 |
|
| 10 |
def main():
|
| 11 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 12 |
+
|
| 13 |
|
| 14 |
if __name__ == "__main__":
|
| 15 |
main()
|