Team Ai
Apppublic

cactus183/patchbench-dev

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
app.py72 linesDownload Raw Back to server
1from typing import Any, Optional2 3from fastapi import FastAPI, HTTPException, Request4from pydantic import BaseModel5 6from patchbench.environment import PatchBenchEnv7from patchbench.models import PatchBenchAction, PatchBenchObservation8 9app = FastAPI(10    title="PatchBench",11    description="OpenEnv environment for bug-fixing tasks with pytest-based grading",12    version="1.0.0",13)14 15env = PatchBenchEnv()16 17 18@app.get("/")19def root() -> dict[str, Any]:20    return {21        "name": "patchbench",22        "description": "Bug-fixing RL environment with pytest-based grading",23        "tasks": env.task_ids,24        "endpoints": ["/health", "/reset", "/step", "/state"],25    }26 27 28@app.get("/health")29def health() -> dict[str, str]:30    return {"status": "ok"}31 32 33@app.post("/reset", response_model=PatchBenchObservation)34async def reset(request: Request) -> PatchBenchObservation:35    try:36        try:37            body = await request.json()38        except Exception:39            body = {}40        if body is None:41            body = {}42        seed = body.get("seed") if isinstance(body, dict) else None43        task_id = body.get("task_id") if isinstance(body, dict) else None44        return env.reset(seed=seed, task_id=task_id)45    except Exception as exc:46        raise HTTPException(status_code=500, detail=f"reset_error: {exc}")47 48 49@app.post("/step", response_model=PatchBenchObservation)50async def step(request: Request) -> PatchBenchObservation:51    try:52        try:53            body = await request.json()54        except Exception:55            raise HTTPException(status_code=400, detail="step requires JSON body with action.patched_code")56        if not isinstance(body, dict) or "action" not in body:57            raise HTTPException(status_code=400, detail="step requires action field in body")58        action_data = body["action"]59        if not isinstance(action_data, dict) or "patched_code" not in action_data:60            raise HTTPException(status_code=400, detail="action must contain patched_code")61        action = PatchBenchAction(patched_code=action_data["patched_code"])62        return env.step(action)63    except HTTPException:64        raise65    except Exception as exc:66        raise HTTPException(status_code=500, detail=f"step_error: {exc}")67 68 69@app.get("/state")70def state() -> dict[str, Any]:71    return env.state72