Spaces:
Sleeping
Sleeping
Commit Β·
0d9a3e2
1
Parent(s): 99cd67c
fix: exact ResetResponse schema alignment for Phase 1 validator
Browse files- backend/app/main.py +79 -29
backend/app/main.py
CHANGED
|
@@ -1,18 +1,37 @@
|
|
| 1 |
-
from
|
| 2 |
-
|
|
|
|
| 3 |
import os
|
| 4 |
import logging
|
| 5 |
from typing import Any, Dict, List, Optional
|
| 6 |
|
| 7 |
-
from fastapi import FastAPI, HTTPException
|
| 8 |
-
from pydantic import BaseModel
|
| 9 |
-
|
| 10 |
-
import sys
|
| 11 |
# Ensure project root is in path for environment and models
|
| 12 |
sys.path.append(os.getcwd())
|
| 13 |
|
| 14 |
from environment import MediRouteEnv
|
| 15 |
-
from models import Action
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Import the existing inference runner so we can reuse run_episode
|
| 18 |
try:
|
|
@@ -57,39 +76,70 @@ def health() -> Dict[str, str]:
|
|
| 57 |
|
| 58 |
# ββ OpenEnv Endpoints ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
-
@app.
|
| 65 |
-
async def
|
| 66 |
-
"""
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
|
| 72 |
@app.post("/step")
|
| 73 |
-
async def step(action:
|
| 74 |
-
"""Advance the environment
|
| 75 |
-
logger.info(f"OpenEnv: step with action
|
|
|
|
| 76 |
try:
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
logger.error(f"OpenEnv step failed: {e}")
|
| 81 |
raise HTTPException(status_code=500, detail=str(e))
|
| 82 |
|
| 83 |
|
| 84 |
-
@app.get("/state")
|
| 85 |
-
async def get_state() -> Observation:
|
| 86 |
-
"""Return the current snapshot of the environment's observation."""
|
| 87 |
-
try:
|
| 88 |
-
return env.state()
|
| 89 |
-
except Exception as e:
|
| 90 |
-
raise HTTPException(status_code=400, detail=str(e))
|
| 91 |
-
|
| 92 |
-
|
| 93 |
@app.post("/run-benchmark")
|
| 94 |
def run_benchmark(req: BenchmarkRequest) -> Dict[str, Any]:
|
| 95 |
"""Run the existing inference benchmark and return structured JSON results.
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import sys
|
| 4 |
import os
|
| 5 |
import logging
|
| 6 |
from typing import Any, Dict, List, Optional
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# Ensure project root is in path for environment and models
|
| 9 |
sys.path.append(os.getcwd())
|
| 10 |
|
| 11 |
from environment import MediRouteEnv
|
| 12 |
+
from models import Action
|
| 13 |
+
|
| 14 |
+
app = FastAPI(title="LifeLine AI API", version="1.0.0")
|
| 15 |
+
|
| 16 |
+
# Global environment instance
|
| 17 |
+
env = MediRouteEnv()
|
| 18 |
+
|
| 19 |
+
# Configure logging
|
| 20 |
+
logger = logging.getLogger("lifeline.backend")
|
| 21 |
+
logging.basicConfig(level=logging.INFO)
|
| 22 |
+
|
| 23 |
+
# ββ Validator-specific Models ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 24 |
+
|
| 25 |
+
class ObservationSchema(BaseModel):
|
| 26 |
+
symptoms: str
|
| 27 |
+
severity: str
|
| 28 |
+
step_count: int
|
| 29 |
+
|
| 30 |
+
class ResetResponse(BaseModel):
|
| 31 |
+
observation: ObservationSchema
|
| 32 |
+
reward: float = 0.0
|
| 33 |
+
done: bool = False
|
| 34 |
+
info: dict = {}
|
| 35 |
|
| 36 |
# Import the existing inference runner so we can reuse run_episode
|
| 37 |
try:
|
|
|
|
| 76 |
|
| 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 |
+
# Map severity_score (float) back to string labels for validator
|
| 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=severity_label,
|
| 96 |
+
step_count=0
|
| 97 |
+
)
|
| 98 |
+
)
|
| 99 |
|
| 100 |
|
| 101 |
+
@app.get("/state")
|
| 102 |
+
async def state():
|
| 103 |
+
"""Return the current snapshot status."""
|
| 104 |
+
return {
|
| 105 |
+
"status": "active",
|
| 106 |
+
"current_task": "easy"
|
| 107 |
+
}
|
| 108 |
|
| 109 |
|
| 110 |
@app.post("/step")
|
| 111 |
+
async def step(action: dict):
|
| 112 |
+
"""Advance the environment using the validator's action dictionary."""
|
| 113 |
+
logger.info(f"OpenEnv: Received /step request with action: {action}")
|
| 114 |
+
|
| 115 |
try:
|
| 116 |
+
# Construct internal Action model from dict
|
| 117 |
+
internal_action = Action(
|
| 118 |
+
action_type=action.get("action_type", "analyze_symptoms"),
|
| 119 |
+
target=action.get("target")
|
| 120 |
+
)
|
| 121 |
+
result = env.step(internal_action)
|
| 122 |
+
|
| 123 |
+
# Map back to validator's schema
|
| 124 |
+
severity_label = "low"
|
| 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 |
+
"observation": {
|
| 130 |
+
"symptoms": result.observation.symptoms,
|
| 131 |
+
"severity": severity_label,
|
| 132 |
+
"step_count": result.info.get("step", 1)
|
| 133 |
+
},
|
| 134 |
+
"reward": result.reward,
|
| 135 |
+
"done": result.done,
|
| 136 |
+
"info": result.info
|
| 137 |
+
}
|
| 138 |
except Exception as e:
|
| 139 |
logger.error(f"OpenEnv step failed: {e}")
|
| 140 |
raise HTTPException(status_code=500, detail=str(e))
|
| 141 |
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
@app.post("/run-benchmark")
|
| 144 |
def run_benchmark(req: BenchmarkRequest) -> Dict[str, Any]:
|
| 145 |
"""Run the existing inference benchmark and return structured JSON results.
|