"""FastAPI application entrypoint for the ATC optimization environment.""" from __future__ import annotations import asyncio import base64 import json from pathlib import Path from typing import Any, Dict from fastapi import HTTPException from fastapi.responses import HTMLResponse try: from openenv.core.env_server.http_server import create_app except ImportError as exc: # pragma: no cover raise ImportError( "openenv is required to run this environment. Install dependencies first." ) from exc try: from ..models import ATCOptimizationAction, ATCOptimizationObservation from . import ui_runner from .atc_environment import ATCOptimizationEnvironment except ImportError: from models import ATCOptimizationAction, ATCOptimizationObservation from server import ui_runner from server.atc_environment import ATCOptimizationEnvironment UI_TEMPLATE_PATH = Path(__file__).with_name("ui_console.html") PLANE_IMAGE_PATH = Path(__file__).with_name("ui_aircraft.png") app = create_app( ATCOptimizationEnvironment, ATCOptimizationAction, ATCOptimizationObservation, env_name="atc_env", max_concurrent_envs=8, ) def _render_task_strips() -> str: return "\n".join( ( f'
' f'
{task["title"]}' f'{task["difficulty"]}
' f'
{task["summary"]}
' f'
Baseline {task["random_baseline"]:.2f}' 'Awaiting run
' "
" ) for task in ui_runner.UI_TASKS ) def _render_index_html() -> str: template = UI_TEMPLATE_PATH.read_text(encoding="utf-8") model_options = "\n".join( f'' for model in ui_runner.MODEL_OPTIONS ) plane_image_bytes = PLANE_IMAGE_PATH.read_bytes() plane_image_src = ( "data:image/png;base64," + base64.b64encode(plane_image_bytes).decode("ascii") ) return ( template.replace("__MODEL_OPTIONS__", model_options) .replace("__PLANE_IMAGE_SRC__", plane_image_src) .replace("__TASK_DATA__", json.dumps(ui_runner.UI_TASKS)) .replace("__TASK_STRIPS__", _render_task_strips()) ) @app.get("/", response_class=HTMLResponse, include_in_schema=False) async def index() -> HTMLResponse: return HTMLResponse(_render_index_html()) @app.post("/ui/run-inference", include_in_schema=False) async def run_inference_ui( payload: ui_runner.InferenceRunRequest, ) -> Dict[str, Any]: """Run user-triggered inference from the Space UI.""" async with ui_runner.INFERENCE_LOCK: try: return await asyncio.to_thread(ui_runner.run_requested_inference, payload) except ValueError as exc: raise HTTPException(status_code=400, detail=str(exc)) from exc except Exception as exc: # pragma: no cover - defensive guard raise HTTPException( status_code=500, detail=f"Inference run failed: {exc}", ) from exc def main(host: str = "0.0.0.0", port: int = 8000) -> None: """Run the FastAPI server directly.""" import uvicorn uvicorn.run(app, host=host, port=port) if __name__ == "__main__": main()