Spaces:
Sleeping
Sleeping
File size: 14,592 Bytes
22805ee 12f3696 22805ee 27f6a5e 22805ee e37d64c 3e31f95 13d9548 12f3696 27f6a5e 12f3696 22805ee 27f6a5e 22805ee 27f6a5e 22805ee 27f6a5e 22805ee 27f6a5e 22805ee 27f6a5e 22805ee 27f6a5e 22805ee 27f6a5e 22805ee 27f6a5e 22805ee 27f6a5e 22805ee e37d64c 22805ee e37d64c 3e31f95 e37d64c 27f6a5e 3e31f95 27f6a5e 3e31f95 27f6a5e 3e31f95 13d9548 3e31f95 27f6a5e 13d9548 3e31f95 27f6a5e 22805ee 12f3696 22805ee | 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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | # Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
"""
FastAPI application for the Satellite Environment.
This module creates an HTTP server that exposes the SatelliteEnvironment
over HTTP and WebSocket endpoints, compatible with EnvClient.
Endpoints:
- POST /reset: Reset the environment
- POST /step: Execute an action
- GET /state: Get current environment state
- GET /schema: Get action/observation schemas
- WS /ws: WebSocket endpoint for persistent sessions
Usage:
# Development (with auto-reload):
uvicorn server.app:app --reload --host 0.0.0.0 --port 8000
# Production:
uvicorn server.app:app --host 0.0.0.0 --port 8000 --workers 4
# Or run directly:
python -m server.app
"""
try:
from openenv.core.env_server.http_server import create_app
except Exception as e: # pragma: no cover
raise ImportError(
"openenv-core[core] is required for the web interface. Install dependencies with "
"'\n uv pip install openenv-core[core] fastapi uvicorn\n'"
) from e
import importlib
from pathlib import Path
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.openapi.utils import get_openapi
from fastapi.staticfiles import StaticFiles
# 1) Try absolute imports first (this handles the Docker layout where /app/env
# is on PYTHONPATH and modules like `models` and `server` are top-level).
try:
from models import SatelliteAction, SatelliteObservation
from server.satellite_environment import SatelliteEnvironment
from server.ui_demo import SatelliteUIDemo
except Exception: # pragma: no cover - tolerate multiple packaging layouts
# 2) Try package-relative imports (when running as `satellite.server.app`).
try:
from ..models import SatelliteAction, SatelliteObservation
from .satellite_environment import SatelliteEnvironment
from .ui_demo import SatelliteUIDemo
except Exception:
# 3) Try common installed package names (e.g., `satellite` or `env`).
SatelliteAction = None
SatelliteObservation = None
SatelliteEnvironment = None
SatelliteUIDemo = None
for pkg in ("satellite", "env"):
try:
mod_models = importlib.import_module(f"{pkg}.models")
mod_server_env = importlib.import_module(f"{pkg}.server.satellite_environment")
mod_ui_demo = importlib.import_module(f"{pkg}.server.ui_demo")
SatelliteAction = getattr(mod_models, "SatelliteAction")
SatelliteObservation = getattr(mod_models, "SatelliteObservation")
SatelliteEnvironment = getattr(mod_server_env, "SatelliteEnvironment")
SatelliteUIDemo = getattr(mod_ui_demo, "SatelliteUIDemo")
break
except Exception:
continue
if SatelliteAction is None:
# 4) Last resort: file-based fallback (load sibling files directly).
try:
import importlib.util
from pathlib import Path
here = Path(__file__).resolve().parent
pkg_root = here.parent
models_path = pkg_root / "models.py"
server_env_path = here / "satellite_environment.py"
ui_demo_path = here / "ui_demo.py"
if models_path.exists() and server_env_path.exists() and ui_demo_path.exists():
spec_models = importlib.util.spec_from_file_location(
"satellite_models_fallback", str(models_path)
)
mod_models = importlib.util.module_from_spec(spec_models) # type: ignore[arg-type]
spec_models.loader.exec_module(mod_models) # type: ignore[attr-defined]
spec_server_env = importlib.util.spec_from_file_location(
"satellite_server_env_fallback", str(server_env_path)
)
mod_server_env = importlib.util.module_from_spec(spec_server_env) # type: ignore[arg-type]
spec_server_env.loader.exec_module(mod_server_env) # type: ignore[attr-defined]
spec_ui_demo = importlib.util.spec_from_file_location(
"satellite_ui_demo_fallback", str(ui_demo_path)
)
mod_ui_demo = importlib.util.module_from_spec(spec_ui_demo) # type: ignore[arg-type]
spec_ui_demo.loader.exec_module(mod_ui_demo) # type: ignore[attr-defined]
SatelliteAction = getattr(mod_models, "SatelliteAction")
SatelliteObservation = getattr(mod_models, "SatelliteObservation")
SatelliteEnvironment = getattr(mod_server_env, "SatelliteEnvironment")
SatelliteUIDemo = getattr(mod_ui_demo, "SatelliteUIDemo")
else:
raise
except Exception:
raise
# Build the OpenEnv API app first, then expose its non-UI routes from our
# top-level FastAPI app so Hugging Face Spaces can use the custom frontend at `/`.
openenv_app = create_app(
SatelliteEnvironment,
SatelliteAction,
SatelliteObservation,
env_name="satellite",
max_concurrent_envs=1, # increase this number to allow more concurrent WebSocket sessions
)
app = FastAPI(
title="Satellite Mission Tracking",
version="0.1.0",
description="Custom satellite mission frontend with mounted OpenEnv API routes.",
)
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:4173",
"http://127.0.0.1:4173",
"http://localhost:5173",
"http://127.0.0.1:5173",
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
for route in openenv_app.router.routes:
if getattr(route, "path", None) in {"/", "/docs", "/redoc", "/openapi.json"}:
continue
app.router.routes.append(route)
ui_demo = SatelliteUIDemo()
frontend_dist = Path(__file__).resolve().parent.parent / "frontend" / "dist"
if frontend_dist.exists():
assets_dir = frontend_dist / "assets"
if assets_dir.exists():
app.mount("/assets", StaticFiles(directory=str(assets_dir)), name="assets")
app.mount("/web/assets", StaticFiles(directory=str(assets_dir)), name="web-assets")
@app.get("/", include_in_schema=False)
def serve_root_index():
index_path = frontend_dist / "index.html"
if not index_path.exists():
raise HTTPException(status_code=404, detail="Frontend build not found. Build /frontend first.")
return FileResponse(index_path)
@app.get("/web", include_in_schema=False)
def serve_web_index():
index_path = frontend_dist / "index.html"
if not index_path.exists():
raise HTTPException(status_code=404, detail="Frontend build not found. Build /frontend first.")
return FileResponse(index_path)
@app.get("/web/{full_path:path}", include_in_schema=False)
def serve_web_app(full_path: str):
candidate = frontend_dist / full_path
index_path = frontend_dist / "index.html"
if candidate.exists() and candidate.is_file():
return FileResponse(candidate)
if index_path.exists():
return FileResponse(index_path)
raise HTTPException(status_code=404, detail="Frontend build not found. Build /frontend first.")
@app.get("/{full_path:path}", include_in_schema=False)
def serve_frontend_app(full_path: str):
if full_path.startswith(("api/", "docs", "openapi.json", "redoc", "ws", "reset", "step", "state", "schema", "health")):
raise HTTPException(status_code=404, detail="Not found")
candidate = frontend_dist / full_path
index_path = frontend_dist / "index.html"
if candidate.exists() and candidate.is_file():
return FileResponse(candidate)
if index_path.exists():
return FileResponse(index_path)
raise HTTPException(status_code=404, detail="Frontend build not found. Build /frontend first.")
@app.get("/api/ui/demo")
def ui_demo_snapshot():
return ui_demo.snapshot()
@app.post("/api/ui/demo/reset")
def ui_demo_reset(task_name: str = "medium"):
if task_name not in {"easy", "medium", "hard"}:
raise HTTPException(status_code=400, detail="task_name must be easy, medium, or hard")
return ui_demo.reset(task_name)
@app.post("/api/ui/demo/step")
def ui_demo_step():
return ui_demo.step()
def _with_examples() -> None:
"""Inject endpoint examples into Swagger / OpenAPI docs."""
step_action = {
"action": {
"satellite_actions": {
"0": "capture",
"1": "maintain",
"2": "idle",
}
},
"timeout_s": 30,
}
reset_request = {"task_name": "easy", "timeout_s": 30}
observation_example = {
"satellites": [
{
"id": 0,
"position": [7000.0, 0.0, 0.0],
"battery": 82.5,
"storage": 35.0,
"last_action": "capture",
},
{
"id": 1,
"position": [0.0, 7000.0, 0.0],
"battery": 91.0,
"storage": 10.0,
"last_action": "maintain",
},
],
"time_step": 1,
"ground_stations": [[28.6139, 77.2090], [34.0522, -118.2437]],
"weather_conditions": {"region_0": 0.2, "region_1": 0.6},
"pending_tasks": [
{
"id": "img_001",
"type": "capture",
"target": "region_0",
"priority": 0.9,
}
],
"total_reward": 4.5,
"done": False,
"reward": 4.5,
"metadata": {
"task_name": "easy",
"reward_components": {"capture": 5.0, "idle_penalty": -0.5},
},
}
step_response = {
"observation": observation_example,
"reward": 4.5,
"done": False,
"info": {
"task_name": "easy",
"reward_components": {"capture": 5.0, "idle_penalty": -0.5},
},
}
state_response = {
"episode_id": "demo-episode-123",
"step_count": 1,
}
schema_response = {
"action_schema": {
"title": "SatelliteAction",
"type": "object",
"properties": {
"satellite_actions": {
"type": "object",
"additionalProperties": {
"enum": ["capture", "downlink", "maintain", "idle"]
},
}
},
},
"observation_schema": {
"title": "SatelliteObservation",
"type": "object",
"properties": {
"satellites": {"type": "array"},
"time_step": {"type": "integer"},
"ground_stations": {"type": "array"},
"weather_conditions": {"type": "object"},
"pending_tasks": {"type": "array"},
"total_reward": {"type": "number"},
"done": {"type": "boolean"},
"reward": {"type": "number"},
"metadata": {"type": "object"},
},
},
}
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
schema = get_openapi(
title=app.title or "Satellite OpenEnv API",
version=app.version or "0.1.0",
description=app.description or "HTTP API for the Satellite OpenEnv environment.",
routes=app.routes,
)
paths = schema.setdefault("paths", {})
reset_post = paths.get("/reset", {}).get("post")
if reset_post:
content = reset_post.setdefault("requestBody", {}).setdefault("content", {})
content.setdefault("application/json", {})["example"] = reset_request
responses = reset_post.setdefault("responses", {})
ok = responses.setdefault("200", {}).setdefault("content", {})
ok.setdefault("application/json", {})["example"] = observation_example
step_post = paths.get("/step", {}).get("post")
if step_post:
content = step_post.setdefault("requestBody", {}).setdefault("content", {})
content.setdefault("application/json", {})["example"] = step_action
responses = step_post.setdefault("responses", {})
ok = responses.setdefault("200", {}).setdefault("content", {})
ok.setdefault("application/json", {})["example"] = step_response
state_get = paths.get("/state", {}).get("get")
if state_get:
responses = state_get.setdefault("responses", {})
ok = responses.setdefault("200", {}).setdefault("content", {})
ok.setdefault("application/json", {})["example"] = state_response
schema_get = paths.get("/schema", {}).get("get")
if schema_get:
responses = schema_get.setdefault("responses", {})
ok = responses.setdefault("200", {}).setdefault("content", {})
ok.setdefault("application/json", {})["example"] = schema_response
app.openapi_schema = schema
return app.openapi_schema
app.openapi = custom_openapi
_with_examples()
def run_server(host: str = "0.0.0.0", port: int = 8000):
"""
Entry point for direct execution via uv run or python -m.
This function enables running the server without Docker:
uv run --project . server
uv run --project . server --port 8001
python -m satellite.server.app
Args:
host: Host address to bind to (default: "0.0.0.0")
port: Port number to listen on (default: 8000)
For production deployments, consider using uvicorn directly with
multiple workers:
uvicorn satellite.server.app:app --workers 4
"""
import uvicorn
uvicorn.run(app, host=host, port=port)
def main():
"""CLI-compatible main entry point for OpenEnv validation and local runs."""
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--host", default="0.0.0.0")
parser.add_argument("--port", type=int, default=8000)
args = parser.parse_args()
run_server(host=args.host, port=args.port)
if __name__ == "__main__":
main()
|