spk-22/Context_Aware_Content_Moderation_Environment_using_OpenEnv
0
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 Content Moderation Environment.9 10This module creates an HTTP server that exposes the ContentModerationEnvironment11over 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 31try:32 from openenv.core.env_server.http_server import create_app33except Exception as e: # pragma: no cover34 raise ImportError(35 "openenv is required for the web interface. Install dependencies with '\n uv sync\n'"36 ) from e37 38from models import ContentModerationAction, ContentModerationObservation39from server.content_moderation_environment import ContentModerationEnvironment40 41 42# Create the app with web interface and README integration43app = create_app(44 ContentModerationEnvironment,45 ContentModerationAction,46 ContentModerationObservation,47 env_name="content_moderation",48 max_concurrent_envs=1, # increase thisa number to allow more concurrent WebSocket sessions49)50 51 52def main(host: str = "0.0.0.0", port: int = 8000):53 """54 Entry point for direct execution via uv run or python -m.55 56 This function enables running the server without Docker:57 uv run --project . server58 uv run --project . server --port 800159 python -m content_moderation.server.app60 61 Args:62 host: Host address to bind to (default: "0.0.0.0")63 port: Port number to listen on (default: 8000)64 65 For production deployments, consider using uvicorn directly with66 multiple workers:67 uvicorn content_moderation.server.app:app --workers 468 """69 import uvicorn70 71 uvicorn.run(app, host=host, port=port)72 73 74if __name__ == "__main__":75 import argparse76 77 parser = argparse.ArgumentParser()78 parser.add_argument("--port", type=int, default=8000)79 args = parser.parse_args()80 main(port=args.port)81 