Spaces:
Sleeping
Sleeping
File size: 1,824 Bytes
bdf304a 00642d3 bdf304a 00642d3 bdf304a | 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 57 58 59 60 61 62 63 64 65 | 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)
@app.post("/reset", response_model=ObservationModel)
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))
@app.post("/step", response_model=StepResult)
def step(action: ActionModel):
try:
result = env.step(action)
return result
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/state", response_model=ObservationModel)
def state():
try:
return env._get_obs()
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/info")
def info():
return {
"task": env.task,
"max_steps": env.max_steps,
"current_step": env.current_step,
"soc": env.soc
}
|