Spaces:
Sleeping
Sleeping
| """FastAPI application for the Python code review environment.""" | |
| from __future__ import annotations | |
| import os | |
| from fastapi import APIRouter, HTTPException | |
| from fastapi.responses import RedirectResponse | |
| from openenv.core.env_server.http_server import create_app | |
| from models import ( | |
| HealthResponse, | |
| PythonCodeReviewAction, | |
| PythonCodeReviewObservation, | |
| PythonCodeReviewState, | |
| TaskDescriptor, | |
| TaskGrade, | |
| ) | |
| from server.env import PythonCodeReviewEnvironment | |
| MAX_CONCURRENT_ENVS = int(os.getenv("MAX_CONCURRENT_ENVS", "16")) | |
| python_env = PythonCodeReviewEnvironment() | |
| app = create_app( | |
| PythonCodeReviewEnvironment, | |
| PythonCodeReviewAction, | |
| PythonCodeReviewObservation, | |
| max_concurrent_envs=MAX_CONCURRENT_ENVS, | |
| ) | |
| router = APIRouter(tags=["python-code-review"]) | |
| def root() -> RedirectResponse: | |
| """Redirect root to API documentation.""" | |
| return RedirectResponse(url="/docs") | |
| def health() -> HealthResponse: | |
| """Health check endpoint for deployment monitoring.""" | |
| return python_env.health() | |
| def list_tasks() -> list: | |
| """List all available deterministic tasks.""" | |
| return python_env.list_task_summaries() | |
| def get_task(task_id: str) -> object: | |
| """Get a specific task by ID.""" | |
| try: | |
| return python_env.get_task(task_id) | |
| except ValueError as exc: | |
| raise HTTPException(status_code=404, detail=str(exc)) from exc | |
| def grade_task(task_id: str, payload: PythonCodeReviewAction) -> TaskGrade: | |
| """Grade code submission for a task without running an episode.""" | |
| if payload.action_type != "edit_code" or not payload.code: | |
| raise HTTPException( | |
| status_code=400, | |
| detail="Requires action_type='edit_code' with code parameter." | |
| ) | |
| try: | |
| return python_env.grade_task_submission(task_id=task_id, code=payload.code) | |
| except ValueError as exc: | |
| raise HTTPException(status_code=404, detail=str(exc)) from exc | |
| def get_state_post() -> RedirectResponse: | |
| """Redirect POST /state to GET for compatibility.""" | |
| return RedirectResponse(url="/state", status_code=303) | |
| app.include_router(router) | |
| def main(host: str = "0.0.0.0", port: int = 8000) -> None: | |
| """Run the FastAPI application with uvicorn.""" | |
| import uvicorn | |
| uvicorn.run( | |
| app, | |
| host=os.getenv("HOST", host), | |
| port=int(os.getenv("PORT", str(port))), | |
| ) | |
| if __name__ == "__main__": | |
| main() | |