Spaces:
Sleeping
Sleeping
File size: 2,049 Bytes
30bdd62 578508e 30bdd62 578508e 30bdd62 578508e 30bdd62 578508e 30bdd62 | 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 66 67 68 69 70 71 72 73 74 75 76 77 | import json
import os
try:
from openenv.core.env_server.http_server import create_app
except ImportError as e:
raise ImportError("openenv-core>=0.2.0 is required for the server.") from e
from fastapi import Request
from fastapi.responses import JSONResponse
from models.schemas import GridAction
from server.ecogrid_environment import ServerEcoGridEnv, ServerObservation
app = create_app(
ServerEcoGridEnv,
GridAction,
ServerObservation,
env_name="eco-grid-openenv",
max_concurrent_envs=10,
)
@app.middleware("http")
async def normalize_step_payload(request: Request, call_next):
"""Allow /step payloads with either wrapped or direct action JSON."""
if request.method == "POST" and request.url.path == "/step":
body = await request.body()
if body:
try:
payload = json.loads(body)
except json.JSONDecodeError:
return JSONResponse(status_code=422, content={"detail": "Invalid JSON body"})
if isinstance(payload, dict) and "action" not in payload:
wrapped = json.dumps({"action": payload}).encode("utf-8")
request._body = wrapped
async def _receive():
return {"type": "http.request", "body": wrapped, "more_body": False}
request._receive = _receive
return await call_next(request)
@app.get("/", include_in_schema=False)
def root():
"""Landing route for judges/operators."""
return JSONResponse(
{
"name": "eco-grid-openenv",
"status": "ok",
"docs": "/docs",
"health": "/health",
"schema": "/schema",
"version": "/version",
}
)
@app.get("/version", include_in_schema=False)
def version():
return {"version": "1.1.0-stabilized"}
def main():
import uvicorn
host = os.getenv("HOST", "0.0.0.0")
port = int(os.getenv("PORT", "7860"))
uvicorn.run(app, host=host, port=port)
if __name__ == "__main__":
main()
|