TanujInsane commited on
Commit ·
46d7b20
1
Parent(s): 83330fe
Add recursive JSON sanitizer and wrap JSONResponse payloads
Browse files
app.py
CHANGED
|
@@ -4,6 +4,23 @@ from fastapi import FastAPI, Request
|
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
from environment import DocumentClassificationEnv
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def ensure_models():
|
| 8 |
"""Train and persist ML models before starting servers."""
|
| 9 |
from baseline_inference import load_or_train
|
|
@@ -36,8 +53,7 @@ async def reset_route(request: Request):
|
|
| 36 |
obs, _ = env.reset(seed=seed)
|
| 37 |
api_envs["current"] = env
|
| 38 |
|
| 39 |
-
|
| 40 |
-
return {"observation": obs_dict}
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
return JSONResponse({"error": str(e), "traceback": traceback.format_exc()}, status_code=500)
|
|
@@ -59,14 +75,7 @@ async def step_route(request: Request):
|
|
| 59 |
env = api_envs["current"]
|
| 60 |
obs, reward, done, _, info = env.step(action_val)
|
| 61 |
|
| 62 |
-
|
| 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)
|
|
@@ -77,7 +86,7 @@ async def state_route():
|
|
| 77 |
return JSONResponse(status_code=400, content={"error": "Environment not initialized"})
|
| 78 |
env = api_envs["current"]
|
| 79 |
state = env.state()
|
| 80 |
-
return state
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
import uvicorn
|
|
|
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
from environment import DocumentClassificationEnv
|
| 6 |
|
| 7 |
+
def sanitize(obj):
|
| 8 |
+
import numpy as np
|
| 9 |
+
if isinstance(obj, dict):
|
| 10 |
+
return {k: sanitize(v) for k, v in obj.items()}
|
| 11 |
+
elif isinstance(obj, (list, tuple)):
|
| 12 |
+
return [sanitize(i) for i in obj]
|
| 13 |
+
elif isinstance(obj, np.integer):
|
| 14 |
+
return int(obj)
|
| 15 |
+
elif isinstance(obj, np.floating):
|
| 16 |
+
return float(obj)
|
| 17 |
+
elif isinstance(obj, np.ndarray):
|
| 18 |
+
return obj.tolist()
|
| 19 |
+
elif isinstance(obj, np.bool_):
|
| 20 |
+
return bool(obj)
|
| 21 |
+
else:
|
| 22 |
+
return obj
|
| 23 |
+
|
| 24 |
def ensure_models():
|
| 25 |
"""Train and persist ML models before starting servers."""
|
| 26 |
from baseline_inference import load_or_train
|
|
|
|
| 53 |
obs, _ = env.reset(seed=seed)
|
| 54 |
api_envs["current"] = env
|
| 55 |
|
| 56 |
+
return JSONResponse(content=sanitize({"observation": dict(obs)}))
|
|
|
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
return JSONResponse({"error": str(e), "traceback": traceback.format_exc()}, status_code=500)
|
|
|
|
| 75 |
env = api_envs["current"]
|
| 76 |
obs, reward, done, _, info = env.step(action_val)
|
| 77 |
|
| 78 |
+
return JSONResponse(content=sanitize({"observation": dict(obs), "reward": reward, "done": done, "info": info}))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
return JSONResponse({"error": str(e), "traceback": traceback.format_exc()}, status_code=500)
|
|
|
|
| 86 |
return JSONResponse(status_code=400, content={"error": "Environment not initialized"})
|
| 87 |
env = api_envs["current"]
|
| 88 |
state = env.state()
|
| 89 |
+
return JSONResponse(content=sanitize(state))
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
import uvicorn
|