TanujInsane commited on
Commit
83330fe
·
1 Parent(s): 9ba83b5

Restore env instantiation and add robust try-except error handling

Browse files
Files changed (1) hide show
  1. app.py +42 -41
app.py CHANGED
@@ -19,56 +19,57 @@ ensure_models()
19
  app = FastAPI()
20
  api_envs = {}
21
 
22
- from pydantic import BaseModel
23
- from typing import Optional
24
-
25
- class ResetRequest(BaseModel):
26
- seed: Optional[int] = None
27
- difficulty: Optional[str] = "easy"
28
 
29
  @app.api_route("/reset", methods=["GET", "POST"])
30
- def reset(body: Optional[ResetRequest] = None):
31
  try:
32
- seed = body.seed if body else None
33
- difficulty = body.difficulty if body else "easy"
34
-
35
- # Do NOT call any external env or model here
36
- # Keep it simple and safe
37
-
38
- return {
39
- "status": "ok",
40
- "seed": seed,
41
- "difficulty": difficulty
42
- }
43
-
 
 
 
44
  except Exception as e:
45
- return {
46
- "status": "ok",
47
- "error": str(e)
48
- }
49
 
50
  @app.post("/step")
51
  async def step_route(request: Request):
52
  try:
53
- data = await request.json()
54
- except Exception:
55
- data = {}
56
- action = data.get("action", 0)
57
- if "current" not in api_envs:
58
- return JSONResponse(status_code=400, content={"error": "Environment not initialized"})
59
-
60
- env = api_envs["current"]
61
- obs, reward, done, _, info = env.step(action)
62
-
63
- if "features" in obs and hasattr(obs["features"], "tolist"):
64
- obs["features"] = obs["features"].tolist()
65
 
66
- return {
67
- "observation": obs,
68
- "reward": float(reward),
69
- "done": bool(done),
70
- "info": info
71
- }
 
 
 
 
 
72
 
73
  @app.get("/state")
74
  async def state_route():
 
19
  app = FastAPI()
20
  api_envs = {}
21
 
22
+ import traceback
 
 
 
 
 
23
 
24
  @app.api_route("/reset", methods=["GET", "POST"])
25
+ async def reset_route(request: Request):
26
  try:
27
+ try:
28
+ data = await request.json()
29
+ except Exception:
30
+ data = {}
31
+ data = data or {}
32
+ difficulty = data.get("difficulty", "easy") if data else "easy"
33
+ seed = data.get("seed", 42) if data else 42
34
+
35
+ env = DocumentClassificationEnv(task_difficulty=difficulty, seed=seed)
36
+ obs, _ = env.reset(seed=seed)
37
+ api_envs["current"] = env
38
+
39
+ obs_dict = {k: (v.tolist() if hasattr(v, "tolist") else v) for k, v in obs.items()}
40
+ return {"observation": obs_dict}
41
+
42
  except Exception as e:
43
+ return JSONResponse({"error": str(e), "traceback": traceback.format_exc()}, status_code=500)
44
+
 
 
45
 
46
  @app.post("/step")
47
  async def step_route(request: Request):
48
  try:
49
+ try:
50
+ data = await request.json()
51
+ except Exception:
52
+ data = {}
53
+ data = data or {}
54
+ action_val = data.get("action", 0)
55
+
56
+ if "current" not in api_envs:
57
+ return JSONResponse(status_code=400, content={"error": "Environment not initialized"})
58
+
59
+ env = api_envs["current"]
60
+ obs, reward, done, _, info = env.step(action_val)
61
 
62
+ obs_dict = {k: (v.tolist() if hasattr(v, "tolist") else v) for k, v in obs.items()}
63
+
64
+ return {
65
+ "observation": obs_dict,
66
+ "reward": float(reward),
67
+ "done": bool(done),
68
+ "info": info
69
+ }
70
+
71
+ except Exception as e:
72
+ return JSONResponse({"error": str(e), "traceback": traceback.format_exc()}, status_code=500)
73
 
74
  @app.get("/state")
75
  async def state_route():