CoolFace
Apppublic

openenv/tbench2

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
app.py108 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 Tbench2 Env Environment.9 10This module creates an HTTP server that exposes the Tbench2Environment11over 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 os32 33 34try:35    from openenv.core.env_server.http_server import create_app36 37    # In-repo imports38    from tbench2_env.models import Tbench2Action, Tbench2Observation39 40    from .tbench2_env_environment import Tbench2DockerEnvironment, Tbench2Environment41except Exception as e:  # pragma: no cover42    from models import Tbench2Action, Tbench2Observation43 44    # Standalone imports (when environment is standalone with openenv from pip)45    from openenv.core.env_server.http_server import create_app46    from server.tbench2_env_environment import (47        Tbench2DockerEnvironment,48        Tbench2Environment,49    )50 51    _IMPORT_ERROR = e52 53 54# Determine which environment class to use based on TB2_MODE55_TB2_MODE = os.getenv("TB2_MODE", "local").lower()56 57if _TB2_MODE == "docker":58    _DEFAULT_ENVIRONMENT = Tbench2DockerEnvironment59    _ENV_SUFFIX = " (Docker mode)"60elif _TB2_MODE == "auto":61    # Auto-detect: try Docker, fall back to local62    _DEFAULT_ENVIRONMENT = Tbench2Environment63    _ENV_SUFFIX = " (auto-detect mode)"64else:65    _DEFAULT_ENVIRONMENT = Tbench2Environment66    _ENV_SUFFIX = " (local mode)"67 68 69# Create the app with web interface and README integration70app = create_app(71    _DEFAULT_ENVIRONMENT,72    Tbench2Action,73    Tbench2Observation,74    env_name="tbench2_env" + _ENV_SUFFIX,75    max_concurrent_envs=1,  # increase this number to allow more concurrent WebSocket sessions76)77 78 79def main(host: str = "0.0.0.0", port: int = 8000):80    """81    Entry point for direct execution via uv run or python -m.82 83    This function enables running the server without Docker:84        uv run --project . server85        uv run --project . server --port 800186        python -m tbench2_env.server.app87 88    Args:89        host: Host address to bind to (default: "0.0.0.0")90        port: Port number to listen on (default: 8000)91 92    For production deployments, consider using uvicorn directly with93    multiple workers:94        uvicorn tbench2_env.server.app:app --workers 495    """96    import uvicorn97 98    uvicorn.run(app, host=host, port=port)99 100 101if __name__ == "__main__":102    import argparse103 104    parser = argparse.ArgumentParser()105    parser.add_argument("--port", type=int, default=8000)106    args = parser.parse_args()107    main(port=args.port)108