Spaces:
Sleeping
Sleeping
Commit Β·
1f97a55
1
Parent(s): 0d9a3e2
fix: full compliance with Phase 1 OpenEnv validator schema
Browse files- backend/app/main.py +28 -30
backend/app/main.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import sys
|
| 4 |
import os
|
|
@@ -27,11 +27,11 @@ class ObservationSchema(BaseModel):
|
|
| 27 |
severity: str
|
| 28 |
step_count: int
|
| 29 |
|
| 30 |
-
class
|
| 31 |
observation: ObservationSchema
|
| 32 |
-
reward: float
|
| 33 |
-
done: bool
|
| 34 |
-
info:
|
| 35 |
|
| 36 |
# Import the existing inference runner so we can reuse run_episode
|
| 37 |
try:
|
|
@@ -77,24 +77,22 @@ def health() -> Dict[str, str]:
|
|
| 77 |
# ββ OpenEnv Endpoints ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 78 |
|
| 79 |
@app.post("/reset")
|
| 80 |
-
async def reset():
|
| 81 |
"""Reset the environment to a fresh state for validation."""
|
| 82 |
-
logger.info("OpenEnv: Received /reset request")
|
| 83 |
# Reset internal env
|
| 84 |
obs = env.reset(difficulty="easy")
|
| 85 |
|
| 86 |
# Map internal observation to validator's expected schema
|
| 87 |
-
|
| 88 |
-
severity_label = "low"
|
| 89 |
-
if obs.severity_score >= 0.7: severity_label = "high"
|
| 90 |
-
elif obs.severity_score >= 0.4: severity_label = "moderate"
|
| 91 |
-
|
| 92 |
-
return ResetResponse(
|
| 93 |
observation=ObservationSchema(
|
| 94 |
symptoms=obs.symptoms,
|
| 95 |
-
severity=
|
| 96 |
step_count=0
|
| 97 |
-
)
|
|
|
|
|
|
|
|
|
|
| 98 |
)
|
| 99 |
|
| 100 |
|
|
@@ -103,20 +101,20 @@ async def state():
|
|
| 103 |
"""Return the current snapshot status."""
|
| 104 |
return {
|
| 105 |
"status": "active",
|
| 106 |
-
"
|
| 107 |
}
|
| 108 |
|
| 109 |
|
| 110 |
@app.post("/step")
|
| 111 |
-
async def step(
|
| 112 |
"""Advance the environment using the validator's action dictionary."""
|
| 113 |
-
logger.info(f"OpenEnv: Received /step request with
|
| 114 |
|
| 115 |
try:
|
| 116 |
# Construct internal Action model from dict
|
| 117 |
internal_action = Action(
|
| 118 |
-
action_type=
|
| 119 |
-
target=
|
| 120 |
)
|
| 121 |
result = env.step(internal_action)
|
| 122 |
|
|
@@ -125,16 +123,16 @@ async def step(action: dict):
|
|
| 125 |
if result.observation.severity_score >= 0.7: severity_label = "high"
|
| 126 |
elif result.observation.severity_score >= 0.4: severity_label = "moderate"
|
| 127 |
|
| 128 |
-
return
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
except Exception as e:
|
| 139 |
logger.error(f"OpenEnv step failed: {e}")
|
| 140 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Body
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import sys
|
| 4 |
import os
|
|
|
|
| 27 |
severity: str
|
| 28 |
step_count: int
|
| 29 |
|
| 30 |
+
class StepResponse(BaseModel):
|
| 31 |
observation: ObservationSchema
|
| 32 |
+
reward: float
|
| 33 |
+
done: bool
|
| 34 |
+
info: Dict[str, Any]
|
| 35 |
|
| 36 |
# Import the existing inference runner so we can reuse run_episode
|
| 37 |
try:
|
|
|
|
| 77 |
# ββ OpenEnv Endpoints ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 78 |
|
| 79 |
@app.post("/reset")
|
| 80 |
+
async def reset(payload: Dict[str, Any] = Body(default={})):
|
| 81 |
"""Reset the environment to a fresh state for validation."""
|
| 82 |
+
logger.info(f"OpenEnv: Received /reset request with payload: {payload}")
|
| 83 |
# Reset internal env
|
| 84 |
obs = env.reset(difficulty="easy")
|
| 85 |
|
| 86 |
# Map internal observation to validator's expected schema
|
| 87 |
+
return StepResponse(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
observation=ObservationSchema(
|
| 89 |
symptoms=obs.symptoms,
|
| 90 |
+
severity="unknown", # Phase 1 initial state requirement
|
| 91 |
step_count=0
|
| 92 |
+
),
|
| 93 |
+
reward=0.0,
|
| 94 |
+
done=False,
|
| 95 |
+
info={}
|
| 96 |
)
|
| 97 |
|
| 98 |
|
|
|
|
| 101 |
"""Return the current snapshot status."""
|
| 102 |
return {
|
| 103 |
"status": "active",
|
| 104 |
+
"task": "easy"
|
| 105 |
}
|
| 106 |
|
| 107 |
|
| 108 |
@app.post("/step")
|
| 109 |
+
async def step(payload: Dict[str, Any] = Body(default={})):
|
| 110 |
"""Advance the environment using the validator's action dictionary."""
|
| 111 |
+
logger.info(f"OpenEnv: Received /step request with payload: {payload}")
|
| 112 |
|
| 113 |
try:
|
| 114 |
# Construct internal Action model from dict
|
| 115 |
internal_action = Action(
|
| 116 |
+
action_type=payload.get("action_type", "analyze_symptoms"),
|
| 117 |
+
target=payload.get("target")
|
| 118 |
)
|
| 119 |
result = env.step(internal_action)
|
| 120 |
|
|
|
|
| 123 |
if result.observation.severity_score >= 0.7: severity_label = "high"
|
| 124 |
elif result.observation.severity_score >= 0.4: severity_label = "moderate"
|
| 125 |
|
| 126 |
+
return StepResponse(
|
| 127 |
+
observation=ObservationSchema(
|
| 128 |
+
symptoms=result.observation.symptoms,
|
| 129 |
+
severity=severity_label,
|
| 130 |
+
step_count=result.info.get("step", 1)
|
| 131 |
+
),
|
| 132 |
+
reward=result.reward,
|
| 133 |
+
done=result.done,
|
| 134 |
+
info=result.info
|
| 135 |
+
)
|
| 136 |
except Exception as e:
|
| 137 |
logger.error(f"OpenEnv step failed: {e}")
|
| 138 |
raise HTTPException(status_code=500, detail=str(e))
|