Spaces:
Sleeping
Sleeping
| import uuid | |
| from fastapi import FastAPI, HTTPException | |
| import uvicorn | |
| from environment.actions import ContextCorruptionAction, EpisodeObservation | |
| from environment.env import ContextCorruptionEnv | |
| app = FastAPI(title="ContextCorruption-Env") | |
| # session_id -> env instance | |
| _sessions: dict[str, ContextCorruptionEnv] = {} | |
| _MAX_SESSIONS = 64 | |
| def reset(session_id: str | None = None): | |
| if session_id is None: | |
| if len(_sessions) >= _MAX_SESSIONS: | |
| raise HTTPException(status_code=503, detail="Max concurrent sessions reached") | |
| session_id = str(uuid.uuid4()) | |
| if session_id not in _sessions: | |
| _sessions[session_id] = ContextCorruptionEnv() | |
| obs = _sessions[session_id].reset() | |
| return obs | |
| def step(session_id: str, action: ContextCorruptionAction): | |
| if session_id not in _sessions: | |
| raise HTTPException(status_code=404, detail="Session not found") | |
| obs = _sessions[session_id].step(action) | |
| if obs.episode_done: | |
| del _sessions[session_id] | |
| return obs | |
| def state(session_id: str): | |
| if session_id not in _sessions: | |
| raise HTTPException(status_code=404, detail="Session not found") | |
| return _sessions[session_id].state() | |
| def close_session(session_id: str): | |
| _sessions.pop(session_id, None) | |
| return {"status": "closed"} | |
| def health(): | |
| return {"status": "ok", "active_sessions": len(_sessions)} | |
| if __name__ == "__main__": | |
| uvicorn.run("environment.server:app", host="0.0.0.0", port=8000, reload=False) | |