Spaces:
Sleeping
Sleeping
Commit ·
c4a66be
1
Parent(s): 03b2a1c
revert to working server
Browse files- sql_env/server.py +115 -109
sql_env/server.py
CHANGED
|
@@ -1,114 +1,120 @@
|
|
| 1 |
"""
|
| 2 |
-
FastAPI HTTP wrapper for SQLCorrectionEnv
|
|
|
|
|
|
|
| 3 |
"""
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
from
|
| 9 |
-
from
|
| 10 |
-
import
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
|
| 114 |
def main():
|
|
|
|
| 1 |
"""
|
| 2 |
+
FastAPI HTTP wrapper for SQLCorrectionEnv.
|
| 3 |
+
|
| 4 |
+
Exposes the OpenEnv-required endpoints: /reset, /step, /state + /tasks for validator.
|
| 5 |
"""
|
| 6 |
+
|
| 7 |
+
from contextlib import asynccontextmanager
|
| 8 |
+
from typing import Optional
|
| 9 |
+
|
| 10 |
+
from fastapi import FastAPI, HTTPException
|
| 11 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
+
from pydantic import BaseModel
|
| 13 |
+
|
| 14 |
+
from sql_env import SQLAction, SQLCorrectionEnv
|
| 15 |
+
from sql_env.tasks import ALL_TASKS
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class ResetRequest(BaseModel):
|
| 19 |
+
difficulty: Optional[str] = "easy"
|
| 20 |
+
task_name: Optional[str] = None
|
| 21 |
+
task_index: Optional[int] = None
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class StepRequest(BaseModel):
|
| 25 |
+
corrected_query: str
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
env: Optional[SQLCorrectionEnv] = None
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@asynccontextmanager
|
| 32 |
+
async def lifespan(_: FastAPI):
|
| 33 |
+
global env
|
| 34 |
+
env = SQLCorrectionEnv(difficulty="easy")
|
| 35 |
+
yield
|
| 36 |
+
if env is not None:
|
| 37 |
+
await env.close()
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
app = FastAPI(
|
| 41 |
+
title="SQL Correction RL Environment",
|
| 42 |
+
description="OpenEnv-compliant environment for SQL query correction tasks.",
|
| 43 |
+
version="1.0.0",
|
| 44 |
+
lifespan=lifespan,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
app.add_middleware(
|
| 48 |
+
CORSMiddleware,
|
| 49 |
+
allow_origins=["*"],
|
| 50 |
+
allow_methods=["*"],
|
| 51 |
+
allow_headers=["*"],
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
@app.post("/reset")
|
| 56 |
+
async def reset(request: ResetRequest = ResetRequest()):
|
| 57 |
+
"""Reset the environment and return the initial observation."""
|
| 58 |
+
global env
|
| 59 |
+
difficulty = request.task_name or request.difficulty or "easy"
|
| 60 |
+
if difficulty not in {"easy", "medium", "hard"}:
|
| 61 |
+
raise HTTPException(status_code=400, detail="difficulty must be easy, medium, or hard")
|
| 62 |
+
|
| 63 |
+
env = SQLCorrectionEnv(
|
| 64 |
+
difficulty=difficulty,
|
| 65 |
+
task_index=request.task_index,
|
| 66 |
+
)
|
| 67 |
+
obs = await env.reset()
|
| 68 |
+
return obs.model_dump()
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
@app.post("/step")
|
| 72 |
+
async def step(request: StepRequest):
|
| 73 |
+
"""Take one step and return the new observation, reward, done flag, and info."""
|
| 74 |
+
global env
|
| 75 |
+
if env is None:
|
| 76 |
+
raise HTTPException(status_code=400, detail="Call /reset first.")
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
action = SQLAction(corrected_query=request.corrected_query)
|
| 80 |
+
result = await env.step(action)
|
| 81 |
+
return result.model_dump()
|
| 82 |
+
except RuntimeError as exc:
|
| 83 |
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
@app.post("/state")
|
| 87 |
+
async def state():
|
| 88 |
+
"""Return current environment state."""
|
| 89 |
+
global env
|
| 90 |
+
if env is None:
|
| 91 |
+
return {"status": "not_initialized"}
|
| 92 |
+
return await env.state()
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
@app.get("/health")
|
| 96 |
+
async def health():
|
| 97 |
+
return {"status": "ok", "service": "sql-correction-env"}
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
@app.get("/tasks")
|
| 101 |
+
async def list_tasks():
|
| 102 |
+
"""Return graded tasks by difficulty (RL validator format)."""
|
| 103 |
+
graded_tasks = {
|
| 104 |
+
diff: [task.__dict__ for task in tasks if task.grader is not None]
|
| 105 |
+
for diff, tasks in ALL_TASKS.items()
|
| 106 |
+
}
|
| 107 |
+
return graded_tasks # {"easy": [tasks], "medium": [tasks], "hard": [tasks]}
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
@app.get("/")
|
| 111 |
+
async def root():
|
| 112 |
+
return {
|
| 113 |
+
"name": "SQL Correction RL Environment",
|
| 114 |
+
"version": "1.0.0",
|
| 115 |
+
"endpoints": ["/reset", "/step", "/state", "/health", "/tasks"],
|
| 116 |
+
"tasks": ["easy", "medium", "hard"],
|
| 117 |
+
}
|
| 118 |
|
| 119 |
|
| 120 |
def main():
|