File size: 1,576 Bytes
7d5289a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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 that exposes a generic Gymnasium environment."""

import os

from core.env_server import create_app

from ..models import GymAction, GymObservation
from .gymnasium_environment import GymnasiumEnvironment
import yaml

# Environment configuration via environment variables
env_id = os.getenv("GYM_ENVIRONMENT_ID", "MountainCarContinuous-v0")
render_mode = os.getenv("GYM_RENDER_MODE", "rgb_array")

max_steps_str = os.getenv("GYM_MAX_STEPS")
max_steps = int(max_steps_str) if max_steps_str else 1000

seed_str = os.getenv("GYM_SEED")
seed = int(seed_str) if seed_str else None
yaml_param_file_path = os.getenv("ADDITIONAL_PARAMETERS_YAML_FILE")
additional_params = {}

# Load additional parameters from YAML if file path is provided
if yaml_param_file_path and os.path.exists(yaml_param_file_path):
    with open(yaml_param_file_path, "r") as f:
        additional_params = yaml.safe_load(f)

# Create the environment instance
env = GymnasiumEnvironment(
    env_id=env_id,
    render_mode=render_mode,
    max_steps=max_steps,
    seed=seed,
    **additional_params,
)

# Create the FastAPI app with web interface and README integration
app = create_app(
    env,
    GymAction,
    GymObservation,
    env_name=env_id.lower().replace("-", "_"),
)


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8010)