Spaces:
Sleeping
Sleeping
| import os | |
| import sys | |
| from fastapi import FastAPI, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| # Ensure the repo root is importable | |
| _ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) | |
| if _ROOT not in sys.path: | |
| sys.path.insert(0, _ROOT) | |
| from openenv.models import ActionModel, ObservationModel, StepResult, ResetConfig | |
| from openenv.server.env import BESSEnvironment | |
| app = FastAPI( | |
| title="BESS-RL Platform", | |
| version="2.0.0", | |
| description="OpenEnv simulation + React frontend API (SAC Agent)" | |
| ) | |
| # Allow React dev-server (port 5173) and nginx (port 3000) to call the API | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Global environment instance (shared across requests) | |
| data_path = os.path.join(os.path.dirname(__file__), "..", "..", "data", "pjm_data.csv") | |
| env = BESSEnvironment(data_path=data_path) | |
| def reset(config: ResetConfig): | |
| try: | |
| obs = env.reset(seed=config.seed, task=config.task) | |
| return obs | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| def step(action: ActionModel): | |
| try: | |
| result = env.step(action) | |
| return result | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| def state(): | |
| try: | |
| return env._get_obs() | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| def info(): | |
| return { | |
| "task": env.task, | |
| "max_steps": env.max_steps, | |
| "current_step": env.current_step, | |
| "soc": env.soc | |
| } | |