Spaces:
Sleeping
Sleeping
Delete server/main.py
Browse files- server/main.py +0 -140
server/main.py
DELETED
|
@@ -1,140 +0,0 @@
|
|
| 1 |
-
import asyncio
|
| 2 |
-
|
| 3 |
-
from fastapi import FastAPI, HTTPException
|
| 4 |
-
|
| 5 |
-
from server.environment import SQLQualityEnvironment
|
| 6 |
-
from server.models import (
|
| 7 |
-
HealthResponse,
|
| 8 |
-
ResetRequest,
|
| 9 |
-
SQLAction,
|
| 10 |
-
SQLObservation,
|
| 11 |
-
SQLState,
|
| 12 |
-
StepResponse,
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
app = FastAPI(
|
| 16 |
-
title="SQL Quality Audit Agent Environment",
|
| 17 |
-
description="OpenEnv-compatible environment for training AI agents on SQL data quality tasks",
|
| 18 |
-
version="0.1.0",
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
# Module-level singleton — ONE instance shared across all requests
|
| 22 |
-
_env = SQLQualityEnvironment()
|
| 23 |
-
|
| 24 |
-
# HTTP-level request lock to serialize concurrent requests
|
| 25 |
-
_request_lock = asyncio.Lock()
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
@app.get("/health", response_model=HealthResponse)
|
| 29 |
-
async def health() -> HealthResponse:
|
| 30 |
-
"""Health check endpoint for HF Space ping validation.
|
| 31 |
-
|
| 32 |
-
Returns:
|
| 33 |
-
dict: Status response indicating the server is running
|
| 34 |
-
"""
|
| 35 |
-
return HealthResponse(status="ok")
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
@app.post("/ping", response_model=HealthResponse)
|
| 39 |
-
async def ping() -> HealthResponse:
|
| 40 |
-
"""Ping endpoint for submission validator — accepts empty body.
|
| 41 |
-
|
| 42 |
-
Returns:
|
| 43 |
-
dict: Status response indicating the server is responsive
|
| 44 |
-
"""
|
| 45 |
-
return HealthResponse(status="ok")
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
@app.post("/reset", response_model=SQLObservation)
|
| 49 |
-
async def reset(request: ResetRequest) -> SQLObservation:
|
| 50 |
-
"""Reset environment and return initial observation for specified task.
|
| 51 |
-
|
| 52 |
-
Args:
|
| 53 |
-
request: ResetRequest containing task_id and optional deterministic seed
|
| 54 |
-
|
| 55 |
-
Returns:
|
| 56 |
-
SQLObservation: Initial state of database for requested task
|
| 57 |
-
|
| 58 |
-
Raises:
|
| 59 |
-
HTTPException 422: If task_id is not a valid task identifier
|
| 60 |
-
HTTPException 500: If environment initialization fails
|
| 61 |
-
"""
|
| 62 |
-
async with _request_lock:
|
| 63 |
-
VALID_TASKS = {
|
| 64 |
-
"null_audit",
|
| 65 |
-
"type_repair",
|
| 66 |
-
"dedup_bug",
|
| 67 |
-
"fk_integrity",
|
| 68 |
-
"schema_drift",
|
| 69 |
-
}
|
| 70 |
-
if request.task_id not in VALID_TASKS:
|
| 71 |
-
raise HTTPException(
|
| 72 |
-
status_code=422,
|
| 73 |
-
detail=f"Invalid task_id '{request.task_id}'. Must be one of: {VALID_TASKS}",
|
| 74 |
-
)
|
| 75 |
-
try:
|
| 76 |
-
return _env.reset(request.task_id, seed=request.seed)
|
| 77 |
-
except Exception as e:
|
| 78 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
@app.post("/step", response_model=StepResponse)
|
| 82 |
-
async def step(action: SQLAction) -> StepResponse:
|
| 83 |
-
"""Submit an action and receive observation, reward, done flag, and info.
|
| 84 |
-
|
| 85 |
-
Args:
|
| 86 |
-
action: SQLAction containing task_id and fix_query or answer
|
| 87 |
-
|
| 88 |
-
Returns:
|
| 89 |
-
StepResponse: Updated observation, reward, done status, and info
|
| 90 |
-
|
| 91 |
-
Raises:
|
| 92 |
-
HTTPException 422: If action is invalid or episode is already done
|
| 93 |
-
HTTPException 500: If environment step execution fails
|
| 94 |
-
"""
|
| 95 |
-
async with _request_lock:
|
| 96 |
-
try:
|
| 97 |
-
return _env.step(action)
|
| 98 |
-
except HTTPException:
|
| 99 |
-
raise
|
| 100 |
-
except ValueError as e:
|
| 101 |
-
raise HTTPException(status_code=422, detail=str(e))
|
| 102 |
-
except Exception as e:
|
| 103 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
@app.get("/state", response_model=SQLState)
|
| 107 |
-
async def state() -> SQLState:
|
| 108 |
-
"""Get current episode state.
|
| 109 |
-
|
| 110 |
-
Returns:
|
| 111 |
-
SQLState: Current episode information including task_id, episode_id, attempt_number, cumulative_reward, and done status
|
| 112 |
-
|
| 113 |
-
Raises:
|
| 114 |
-
HTTPException 422: If no active episode exists
|
| 115 |
-
"""
|
| 116 |
-
try:
|
| 117 |
-
return _env.state()
|
| 118 |
-
except ValueError as e:
|
| 119 |
-
raise HTTPException(status_code=422, detail=str(e))
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
def run_server() -> None:
|
| 123 |
-
"""
|
| 124 |
-
Entry point for 'uv run server' command.
|
| 125 |
-
|
| 126 |
-
Uses PORT environment variable if set, otherwise defaults to 7860
|
| 127 |
-
(matching the Dockerfile EXPOSE and HF Spaces convention).
|
| 128 |
-
Override locally: PORT=8000 uv run server
|
| 129 |
-
"""
|
| 130 |
-
import os
|
| 131 |
-
|
| 132 |
-
import uvicorn
|
| 133 |
-
|
| 134 |
-
port = int(os.getenv("PORT", "7860"))
|
| 135 |
-
uvicorn.run("server.main:app", host="0.0.0.0", port=port, reload=False)
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
def main() -> None:
|
| 139 |
-
"""Primary CLI entrypoint expected by OpenEnv validators."""
|
| 140 |
-
run_server()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|