Spaces:
Sleeping
Sleeping
Download server/app.py from darshsingh1/sql-debug-env: direct link, hf CLI and curl.
- Browser
- Download file 3.91 kB
-
https://huggingface.co/spaces/darshsingh1/sql-debug-env/resolve/main/server/app.py
- Command line
-
hf download hf://spaces/darshsingh1/sql-debug-env/server/app.py
-
curl -L -o app.py https://huggingface.co/spaces/darshsingh1/sql-debug-env/resolve/main/server/app.py
3.91 kB
| """ | |
| FastAPI server exposing the OpenENV SQL Debugging environment. | |
| Endpoints: | |
| POST /reset?task=<name> → SQLObservation (reset with optional task selection) | |
| POST /step → StepResult (submit SQLAction, get StepResult) | |
| GET /state → SQLObservation (current state, no side effects) | |
| GET /health → {"status":"ok"} (liveness probe) | |
| """ | |
| from fastapi import FastAPI, HTTPException, Query | |
| from fastapi.responses import JSONResponse | |
| from models import SQLAction, SQLObservation, StepResult | |
| from env import SQLEnvironment | |
| app = FastAPI( | |
| title="OpenENV — SQL Debugging Environment", | |
| description=( | |
| "An OpenEnv-compliant RL environment where an AI agent debugs and fixes " | |
| "broken SQL queries against an in-memory SQLite database." | |
| ), | |
| version="1.0.0", | |
| ) | |
| # Single global environment instance (one agent at a time) | |
| _env = SQLEnvironment() | |
| # --------------------------------------------------------------------------- | |
| # /health | |
| # --------------------------------------------------------------------------- | |
| def health(): | |
| """Returns 200 OK with status=ok if the server is running.""" | |
| return {"status": "ok"} | |
| # --------------------------------------------------------------------------- | |
| # /reset | |
| # --------------------------------------------------------------------------- | |
| VALID_TASKS = ["fix-syntax", "fix-logic", "fix-and-optimize"] | |
| def reset( | |
| task: str = Query( | |
| default="fix-syntax", | |
| description="Task name. One of: fix-syntax, fix-logic, fix-and-optimize", | |
| ) | |
| ): | |
| """ | |
| Reset the environment with the specified task and return the initial observation. | |
| If no task is provided, defaults to 'fix-syntax'. | |
| """ | |
| if task not in VALID_TASKS: | |
| raise HTTPException( | |
| status_code=400, | |
| detail=f"Invalid task '{task}'. Must be one of: {VALID_TASKS}", | |
| ) | |
| try: | |
| obs = _env.reset(task_name=task) | |
| return obs | |
| except ValueError as exc: | |
| raise HTTPException(status_code=400, detail=str(exc)) | |
| except RuntimeError as exc: | |
| raise HTTPException(status_code=500, detail=str(exc)) | |
| # --------------------------------------------------------------------------- | |
| # /step | |
| # --------------------------------------------------------------------------- | |
| def step(action: SQLAction): | |
| """ | |
| Submit a SQL query as the agent's action. Returns a StepResult with | |
| the new observation, reward, done flag, and info dict. | |
| """ | |
| try: | |
| result = _env.step(action) | |
| return result | |
| except RuntimeError as exc: | |
| raise HTTPException(status_code=400, detail=str(exc)) | |
| # --------------------------------------------------------------------------- | |
| # /state | |
| # --------------------------------------------------------------------------- | |
| def state(): | |
| """ | |
| Return the current observation without advancing the episode. | |
| Useful for agents that need to re-read the environment state. | |
| """ | |
| try: | |
| obs = _env.state() | |
| return obs | |
| except RuntimeError as exc: | |
| raise HTTPException(status_code=400, detail=str(exc)) | |
| # --------------------------------------------------------------------------- | |
| # Root redirect → docs | |
| # --------------------------------------------------------------------------- | |
| def root(): | |
| return JSONResponse({"message": "OpenENV SQL Debugging Environment. See /docs for API reference."}) | |
| def main(): | |
| import uvicorn | |
| uvicorn.run("server.app:app", host="0.0.0.0", port=7860) | |
| if __name__ == "__main__": | |
| main() | |