Team Ai
Apppublic

SidhaGarg/Cloud-DevOps-RLEnv

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
app.py104 linesDownload Raw Back to server
1# Copyright (c) Meta Platforms, Inc. and affiliates.2# All rights reserved.3#4# This source code is licensed under the BSD-style license found in the5# LICENSE file in the root directory of this source tree.6 7"""8FastAPI application for the Cloud Devops Env Environment.9 10This module creates an HTTP server that exposes the CloudDevopsEnvironment11over HTTP and WebSocket endpoints, compatible with EnvClient.12 13Endpoints:14    - POST /reset: Reset the environment15    - POST /step: Execute an action16    - GET /state: Get current environment state17    - GET /schema: Get action/observation schemas18    - WS /ws: WebSocket endpoint for persistent sessions19 20Usage:21    # Development (with auto-reload):22    uvicorn server.app:app --reload --host 0.0.0.0 --port 800023 24    # Production:25    uvicorn server.app:app --host 0.0.0.0 --port 8000 --workers 426 27    # Or run directly:28    python -m server.app29"""30 31import os32from pathlib import Path33 34# Default to enabling the OpenEnv web interface for local development.35# You can still disable it explicitly: ENABLE_WEB_INTERFACE=false36os.environ.setdefault("ENABLE_WEB_INTERFACE", "true")37_BASE_DIR = Path(__file__).resolve().parent.parent38_WEB_README = _BASE_DIR / "WEB_README.md"39# Force a clean renderer source file for /web; fallback only if missing.40os.environ["ENV_README_PATH"] = str(41    _WEB_README if _WEB_README.exists() else (_BASE_DIR / "README.md")42)43 44try:45    from openenv.core.env_server.http_server import create_app46except Exception as e:  # pragma: no cover47    raise ImportError(48        "openenv is required for the web interface. Install dependencies with '\n    uv sync\n'"49    ) from e50 51try:52    from ..models import CloudDevopsAction, CloudDevopsObservation53    from .cloud_devops_env_environment import CloudDevopsEnvironment54except (ModuleNotFoundError, ImportError):55    from models import CloudDevopsAction, CloudDevopsObservation56    from server.cloud_devops_env_environment import CloudDevopsEnvironment57 58 59# Create the app with web interface and README integration60app = create_app(61    CloudDevopsEnvironment,62    CloudDevopsAction,63    CloudDevopsObservation,64    env_name="cloud_devops_env",65    max_concurrent_envs=1,  # increase this number to allow more concurrent WebSocket sessions66)67 68 69def main(host: str | None = None, port: int | None = None):70    """71    Entry point for direct execution via uv run or python -m.72 73    This function enables running the server without Docker:74        uv run --project . server75        uv run --project . server --port 800176        python -m cloud_devops_env.server.app77 78    Args:79        host: Host address to bind to. If not provided, CLI args are parsed.80        port: Port number to listen on. If not provided, CLI args are parsed.81 82    For production deployments, consider using uvicorn directly with83    multiple workers:84        uvicorn cloud_devops_env.server.app:app --workers 485    """86    import argparse87    import uvicorn88 89    # Console-script entry points invoke main() with no parameters, so parse90    # CLI flags here to make `server --host ... --port ...` work as expected.91    if host is None and port is None:92        parser = argparse.ArgumentParser(add_help=False)93        parser.add_argument("--host", type=str, default="0.0.0.0")94        parser.add_argument("--port", type=int, default=8000)95        args, _ = parser.parse_known_args()96        host = args.host97        port = args.port98 99    uvicorn.run(app, host=host or "0.0.0.0", port=port or 8000)100 101 102if __name__ == "__main__":103    main()104