Spaces:
Sleeping
Sleeping
File size: 12,573 Bytes
510ab6f bf60b3f 8a41687 510ab6f 676b5ae 510ab6f 676b5ae 510ab6f 8a41687 510ab6f 4e40d19 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | """
FlexTime β FastAPI Application
OpenEnv-compliant workforce scheduling environment.
All required endpoints: /reset /step /state /tasks /grader /baseline
Plus: /health /info and interactive UI at /
"""
from __future__ import annotations
import os
from datetime import datetime, timezone
from pathlib import Path
from fastapi import FastAPI, HTTPException, Query
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from server.engine import FlexTimeEnv, TASK_CONFIGS
from server.models import (
Action, Observation, ResetRequest, StepResult,
AddEmployeeRequest, EditEmployeeRequest, AddShiftRequest, LeaveRequest
)
# ββ App βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
app = FastAPI(
title="FlexTime",
description=(
"π **FlexTime** β Real-world AI Workforce Scheduling OpenEnv Environment\n\n"
"Agents learn to assign employees to shifts while satisfying hard constraints "
"(skill matching, availability, max hours) and soft objectives "
"(fairness, preferences, demand coverage).\n\n"
"**3 Tasks:** Easy β Medium β Hard with full programmatic graders (0.0β1.0).\n\n"
"Compliant with the [OpenEnv specification](https://github.com/openenv/openenv)."
),
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# Resolve static dir relative to this file so it works in Docker too
_STATIC_DIR = Path(__file__).parent / "static"
# Serve static assets (style.css, app.js) at /static/*
app.mount("/static", StaticFiles(directory=_STATIC_DIR), name="static")
# Global env instance (stateful single-session for HF Spaces)
_env = FlexTimeEnv()
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ROOT β Interactive UI
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
async def root():
"""Serve the FlexTime interactive demo UI."""
html_path = _STATIC_DIR / "index.html"
return HTMLResponse(content=html_path.read_text(encoding="utf-8"))
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# HEALTH β Required for HF Spaces ping (must return 200)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.get("/health", tags=["Health"])
async def health():
"""Health check β HF Spaces pings this. Must return 200."""
return {"status": "ok", "service": "FlexTime", "version": "1.0.0"}
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# OPENENV CORE ENDPOINTS
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.post(
"/reset",
response_model=Observation,
summary="Reset the environment",
description=(
"Initialize or reset for a given task. Returns the initial Observation. "
"Must be called before step(). Optionally pass a seed for reproducibility."
),
tags=["OpenEnv Core"],
)
async def reset(body: ResetRequest = None):
"""POST /reset β OpenEnv spec required endpoint."""
try:
task_id = body.task_id if body and body.task_id else "task_medium"
seed = body.seed if body else None
obs = _env.reset(task_id=task_id, seed=seed)
return obs
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post(
"/step",
response_model=StepResult,
summary="Step the environment",
description=(
"Apply one action. Returns new Observation, shaped Reward with component "
"breakdown, done flag, and info dict. Call reset() first."
),
tags=["OpenEnv Core"],
)
async def step(action: Action):
"""POST /step β OpenEnv spec required endpoint."""
try:
return _env.step(action)
except RuntimeError as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get(
"/state",
response_model=Observation,
summary="Get current state",
description="Returns the current Observation without modifying state.",
tags=["OpenEnv Core"],
)
async def state():
"""GET /state β OpenEnv spec required endpoint."""
try:
return _env.state()
except RuntimeError as e:
raise HTTPException(status_code=400, detail=str(e))
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# REQUIRED ADDITIONAL ENDPOINTS (per hackathon spec)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.get(
"/tasks",
summary="List tasks and action schema",
description=(
"Returns all available tasks with id, name, difficulty, description, "
"target_score, max_steps, and the complete JSON action_schema required "
"for POST /step. Required by the hackathon spec."
),
tags=["OpenEnv Extended"],
)
async def tasks():
"""GET /tasks β Returns task list + action schema (hackathon required)."""
action_schema = Action.model_json_schema()
task_list = []
for task_id, cfg in TASK_CONFIGS.items():
task_list.append({
"id": task_id,
"name": cfg["name"],
"difficulty": cfg["difficulty"],
"description": cfg["description"],
"max_steps": cfg["max_steps"],
"target_score": cfg["target_score"],
"n_employees": cfg["n_employees"],
"n_shifts": cfg["n_shifts"],
"action_schema": action_schema, # full JSON schema of Action model
})
return {
"tasks": task_list,
"total": len(task_list),
"action_schema": action_schema, # also at top level for convenience
}
@app.get(
"/grader",
summary="Grade the current episode",
description=(
"Compute the final normalized score (0.0β1.0) for the current episode. "
"Returns a full breakdown of sub-scores and pass/fail status. "
"Can be called at any time β does not require done=True. "
"Required by the hackathon spec."
),
tags=["OpenEnv Extended"],
)
async def grader():
"""GET /grader β Returns grader score (hackathon required)."""
try:
return _env.grade()
except RuntimeError as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post(
"/baseline",
summary="Run baseline inference on all 3 tasks",
description=(
"Triggers the built-in greedy baseline agent against all 3 tasks and "
"returns reproducible scores. Set use_llm=true to use the OpenAI API "
"(requires OPENAI_API_KEY environment variable). "
"Required by the hackathon spec."
),
tags=["OpenEnv Extended"],
)
async def baseline(
use_llm: bool = Query(False, description="Use OpenAI LLM agent (requires OPENAI_API_KEY)"),
):
"""POST /baseline β Runs baseline agent, returns scores (hackathon required)."""
try:
from scripts.baseline import run_baseline
results = await run_baseline(use_llm=use_llm)
return results
except Exception as e:
raise HTTPException(status_code=500, detail=f"Baseline error: {str(e)}")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# SAAS / DYNAMIC UI ENDPOINTS
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.post("/add_employee", response_model=Observation, tags=["SaaS"])
async def add_employee(body: AddEmployeeRequest):
"""Dynamically add an employee. Mid-episode structural change."""
try:
return _env.add_employee(body.model_dump())
except RuntimeError as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post("/edit_employee", response_model=Observation, tags=["SaaS"])
async def edit_employee(body: EditEmployeeRequest):
"""Dynamically edit an employee."""
try:
return _env.edit_employee(body.employee_id, body.model_dump(exclude_unset=True))
except (RuntimeError, ValueError) as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post("/add_shift", response_model=Observation, tags=["SaaS"])
async def add_shift(body: AddShiftRequest):
"""Dynamically add a shift. Mid-episode structural change."""
try:
return _env.add_shift(body.model_dump())
except RuntimeError as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post("/leave", response_model=Observation, tags=["SaaS"])
async def leave_request(body: LeaveRequest):
"""Dynamically take an employee offline and drop their shifts."""
try:
return _env.apply_leave(body.employee_id, body.from_day, body.to_day)
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post("/scenario/{scenario_type}", response_model=Observation, tags=["SaaS"])
async def apply_scenario(scenario_type: str):
"""Mutate environment based on a scenario."""
if scenario_type not in ["shortage", "surge", "holiday"]:
raise HTTPException(status_code=400, detail="Invalid scenario")
try:
return _env.apply_scenario(scenario_type)
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# INFO
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.get("/info", summary="Environment metadata", tags=["Info"])
async def info():
"""Returns FlexTime metadata and full endpoint map."""
return {
"name": "FlexTime",
"version": "1.0.0",
"description": "Real-world AI workforce scheduling OpenEnv environment",
"openenv_compliant": True,
"real_world_domain": "Workforce Scheduling / Operations Research",
"tasks": list(TASK_CONFIGS.keys()),
"action_types": ["assign", "remove", "swap", "noop"],
"reward_range": [-1.0, 1.0],
"reward_type": "dense_shaped",
"constraints": {
"hard": ["no_overlap", "skill_match", "max_hours", "availability"],
"soft": ["fair_distribution", "preferences", "min_rest_gap", "consecutive_days"],
},
"endpoints": {
"reset": "POST /reset",
"step": "POST /step",
"state": "GET /state",
"tasks": "GET /tasks",
"grader": "GET /grader",
"baseline": "POST /baseline",
"health": "GET /health",
"docs": "GET /docs",
},
"baseline_scores": {
"task_easy": {"agent": "GreedyBaseline", "score": 0.95, "seed": 42},
"task_medium": {"agent": "GreedyBaseline", "score": 0.72, "seed": 42},
"task_hard": {"agent": "GreedyBaseline", "score": 0.48, "seed": 42},
},
}
def main():
import uvicorn
uvicorn.run("server.app:app", host="0.0.0.0", port=7860, reload=False)
if __name__ == "__main__":
main()
|