File size: 1,058 Bytes
ba6297d
 
 
 
fbe7607
 
ba6297d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbe7607
 
 
 
 
 
 
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
from fastapi import FastAPI
from pydantic import BaseModel
import sys
import os
import uvicorn

sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
from environment import CloudCostEnv

app = FastAPI()
_env = CloudCostEnv()

class StepRequest(BaseModel):
    action: int

@app.post("/reset")
async def reset():
    state = await _env.reset()
    if hasattr(state, 'model_dump'):
        return state.model_dump()
    return state

@app.post("/step")
async def step(req: StepRequest):
    state, reward, done = await _env.step(req.action)
    if hasattr(state, 'model_dump'):
        state_data = state.model_dump()
    else:
        state_data = state
    return {"state": state_data, "reward": reward, "done": done}

@app.get("/health")
async def health():
    return {"status": "ok"}

@app.get("/state")
async def get_state():
    state = await _env.state()
    if hasattr(state, 'model_dump'):
        return state.model_dump()
    return state

def main():
    uvicorn.run(app, host="0.0.0.0", port=7860)

if __name__ == "__main__":
    main()