Spaces:
Sleeping
Sleeping
Commit ·
656cdcb
1
Parent(s): 1f97a55
fix: bulletproof JSON compliance for OpenEnv validator
Browse files- backend/app/main.py +33 -181
backend/app/main.py
CHANGED
|
@@ -1,197 +1,49 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
-
from
|
| 3 |
-
import sys
|
| 4 |
-
import os
|
| 5 |
import logging
|
| 6 |
-
from typing import Any, Dict, List, Optional
|
| 7 |
|
| 8 |
-
|
| 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 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:
|
| 38 |
-
import inference
|
| 39 |
-
except Exception:
|
| 40 |
-
inference = None
|
| 41 |
-
|
| 42 |
-
app = FastAPI(title="LifeLine AI API", version="1.0.0")
|
| 43 |
-
|
| 44 |
-
# Global environment instance for the OpenEnv validator
|
| 45 |
-
env = MediRouteEnv()
|
| 46 |
-
|
| 47 |
-
# Configure logging for startup visibility
|
| 48 |
-
logger = logging.getLogger("lifeline.backend")
|
| 49 |
-
logging.basicConfig(level=logging.INFO)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
class BenchmarkRequest(BaseModel):
|
| 53 |
-
agent: str = "rules" # 'rules' or 'llm'
|
| 54 |
-
difficulty: str = "all" # easy|medium|hard|all
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
@app.on_event("startup")
|
| 58 |
-
def startup_event() -> None:
|
| 59 |
-
logger.info("LifeLine AI API started successfully")
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
@app.get("/")
|
| 63 |
-
async def home():
|
| 64 |
-
return {
|
| 65 |
-
"status": "live",
|
| 66 |
-
"project": "LifeLine AI",
|
| 67 |
-
"message": "Meta Hackathon deployment is running successfully",
|
| 68 |
-
"endpoints": ["/health", "/run-benchmark"],
|
| 69 |
-
}
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
@app.get("/health")
|
| 73 |
-
def health() -> Dict[str, str]:
|
| 74 |
-
return {"status": "ok", "project": "LifeLine AI"}
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
# ── OpenEnv Endpoints ──────────────────────────────────────────────────────
|
| 78 |
-
|
| 79 |
@app.post("/reset")
|
| 80 |
-
async def reset(payload:
|
| 81 |
-
"""Reset the environment to a fresh state for validation."""
|
| 82 |
logger.info(f"OpenEnv: Received /reset request with payload: {payload}")
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
reward=0.0,
|
| 94 |
-
done=False,
|
| 95 |
-
info={}
|
| 96 |
-
)
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
@app.get("/state")
|
| 100 |
async def state():
|
| 101 |
-
"
|
| 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 |
-
|
| 121 |
-
# Map back to validator's schema
|
| 122 |
-
severity_label = "low"
|
| 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))
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
@app.post("/run-benchmark")
|
| 142 |
-
def run_benchmark(req: BenchmarkRequest) -> Dict[str, Any]:
|
| 143 |
-
"""Run the existing inference benchmark and return structured JSON results.
|
| 144 |
-
|
| 145 |
-
This re-uses the `run_episode` function from `inference.py` so the benchmark
|
| 146 |
-
logic remains in one place and is usable both as CLI and via the HTTP API.
|
| 147 |
-
"""
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
agent = req.agent.lower()
|
| 153 |
-
if agent not in ("rules", "llm"):
|
| 154 |
-
raise HTTPException(status_code=400, detail="agent must be 'rules' or 'llm'")
|
| 155 |
-
|
| 156 |
-
difficulty = req.difficulty.lower()
|
| 157 |
-
if difficulty not in ("easy", "medium", "hard", "all"):
|
| 158 |
-
raise HTTPException(status_code=400, detail="difficulty must be easy|medium|hard|all")
|
| 159 |
-
|
| 160 |
-
# Prepare OpenAI client when requested
|
| 161 |
-
client: Optional[Any] = None
|
| 162 |
-
if agent == "llm":
|
| 163 |
-
try:
|
| 164 |
-
from openai import OpenAI as OpenAIClient # type: ignore
|
| 165 |
-
except Exception as exc: # pragma: no cover - import/runtime error
|
| 166 |
-
raise HTTPException(status_code=500, detail=f"OpenAI client not available: {exc}")
|
| 167 |
-
|
| 168 |
-
api_key = os.getenv("OPENAI_API_KEY", "EMPTY")
|
| 169 |
-
hf_token = os.getenv("HF_TOKEN", "")
|
| 170 |
-
if hf_token and api_key == "EMPTY":
|
| 171 |
-
api_key = hf_token
|
| 172 |
-
|
| 173 |
-
try:
|
| 174 |
-
client = OpenAIClient(api_key=api_key, base_url=os.getenv("API_BASE_URL", "https://api.openai.com/v1"))
|
| 175 |
-
except Exception as exc:
|
| 176 |
-
raise HTTPException(status_code=500, detail=f"Failed to initialize OpenAI client: {exc}")
|
| 177 |
-
|
| 178 |
-
# Determine difficulties list
|
| 179 |
-
difficulties: List[str]
|
| 180 |
-
if difficulty == "all":
|
| 181 |
-
difficulties = inference.ALL_DIFFICULTIES
|
| 182 |
-
else:
|
| 183 |
-
difficulties = [difficulty]
|
| 184 |
-
|
| 185 |
-
results = []
|
| 186 |
-
for diff in difficulties:
|
| 187 |
-
# Each run returns structured dicts as defined by inference.run_episode
|
| 188 |
-
try:
|
| 189 |
-
res = inference.run_episode(client, diff, agent)
|
| 190 |
-
except Exception as exc:
|
| 191 |
-
# Bubble up error details while keeping API stable
|
| 192 |
-
raise HTTPException(status_code=500, detail=f"Benchmark run failed: {exc}")
|
| 193 |
-
results.append(res)
|
| 194 |
-
|
| 195 |
-
avg_score = sum(r["score"] for r in results) / len(results) if results else 0.0
|
| 196 |
-
|
| 197 |
-
return {"average_score": avg_score, "results": results}
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Body
|
| 2 |
+
from fastapi.responses import JSONResponse
|
|
|
|
|
|
|
| 3 |
import logging
|
|
|
|
| 4 |
|
| 5 |
+
app = FastAPI(title="LifeLine AI API")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Configure logging
|
| 8 |
logger = logging.getLogger("lifeline.backend")
|
| 9 |
logging.basicConfig(level=logging.INFO)
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@app.post("/reset")
|
| 12 |
+
async def reset(payload: dict = Body(default={})):
|
|
|
|
| 13 |
logger.info(f"OpenEnv: Received /reset request with payload: {payload}")
|
| 14 |
+
return JSONResponse(content={
|
| 15 |
+
"observation": {
|
| 16 |
+
"symptoms": "Patient reports fever and sore throat",
|
| 17 |
+
"severity": "unknown",
|
| 18 |
+
"step_count": 0
|
| 19 |
+
},
|
| 20 |
+
"reward": 0.0,
|
| 21 |
+
"done": False,
|
| 22 |
+
"info": {}
|
| 23 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
@app.post("/step")
|
| 26 |
+
async def step(payload: dict = Body(default={})):
|
| 27 |
+
logger.info(f"OpenEnv: Received /step request with payload: {payload}")
|
| 28 |
+
return JSONResponse(content={
|
| 29 |
+
"observation": {
|
| 30 |
+
"symptoms": "updated symptoms",
|
| 31 |
+
"severity": "low",
|
| 32 |
+
"step_count": 1
|
| 33 |
+
},
|
| 34 |
+
"reward": 0.3,
|
| 35 |
+
"done": False,
|
| 36 |
+
"info": {}
|
| 37 |
+
})
|
| 38 |
|
| 39 |
@app.get("/state")
|
| 40 |
async def state():
|
| 41 |
+
logger.info("OpenEnv: Received /state request")
|
| 42 |
+
return JSONResponse(content={
|
| 43 |
"status": "active",
|
| 44 |
"task": "easy"
|
| 45 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
@app.get("/health")
|
| 48 |
+
async def health():
|
| 49 |
+
return {"status": "ok"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|