Spaces:
Sleeping
Sleeping
Update inference.py
Browse filespull request to correct routing
- inference.py +79 -5
inference.py
CHANGED
|
@@ -1,14 +1,88 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
|
| 3 |
import uvicorn
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
from
|
| 6 |
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
if __name__ == "__main__":
|
| 14 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from typing import Any, Optional
|
| 3 |
|
| 4 |
import uvicorn
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
|
| 9 |
+
from env.environment import CareerEnv
|
| 10 |
|
| 11 |
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
# Add CORS middleware for cross-origin requests
|
| 15 |
+
app.add_middleware(
|
| 16 |
+
CORSMiddleware,
|
| 17 |
+
allow_origins=["*"],
|
| 18 |
+
allow_credentials=True,
|
| 19 |
+
allow_methods=["*"],
|
| 20 |
+
allow_headers=["*"],
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
env = CareerEnv()
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class ResetRequest(BaseModel):
|
| 27 |
+
user_skills: Optional[list[str]] = None
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class StepRequest(BaseModel):
|
| 31 |
+
action: dict[str, Any]
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@app.get("/")
|
| 35 |
+
def healthcheck() -> dict[str, str]:
|
| 36 |
+
return {"status": "ok"}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@app.post("/reset")
|
| 40 |
+
def reset(payload: ResetRequest) -> dict[str, Any]:
|
| 41 |
+
user_skills = payload.user_skills
|
| 42 |
+
env.reset(user_skills=user_skills)
|
| 43 |
+
return {
|
| 44 |
+
"observation": {"message": "env reset"},
|
| 45 |
+
"reward": 0,
|
| 46 |
+
"done": False,
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
@app.post("/step")
|
| 51 |
+
def step(payload: StepRequest) -> dict[str, Any]:
|
| 52 |
+
action = payload.action
|
| 53 |
+
observation, reward, done, info = env.step(action)
|
| 54 |
+
return {
|
| 55 |
+
"observation": observation,
|
| 56 |
+
"reward": reward,
|
| 57 |
+
"done": done,
|
| 58 |
+
"info": info,
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# Keep legacy endpoints for backward compatibility
|
| 63 |
+
@app.post("/openenv/reset")
|
| 64 |
+
def reset_legacy(payload: ResetRequest) -> dict[str, Any]:
|
| 65 |
+
user_skills = payload.user_skills
|
| 66 |
+
env.reset(user_skills=user_skills)
|
| 67 |
+
return {
|
| 68 |
+
"observation": {"message": "env reset"},
|
| 69 |
+
"reward": 0,
|
| 70 |
+
"done": False,
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
@app.post("/openenv/step")
|
| 75 |
+
def step_legacy(payload: StepRequest) -> dict[str, Any]:
|
| 76 |
+
action = payload.action
|
| 77 |
+
observation, reward, done, info = env.step(action)
|
| 78 |
+
return {
|
| 79 |
+
"observation": observation,
|
| 80 |
+
"reward": reward,
|
| 81 |
+
"done": done,
|
| 82 |
+
"info": info,
|
| 83 |
+
}
|
| 84 |
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
+
port = int(os.environ.get("PORT", "7860"))
|
| 88 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|