Spaces:
Sleeping
Sleeping
File size: 2,486 Bytes
57c1397 | 1 2 3 4 5 6 7 8 9 10 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 | """FastAPI application for the WebSec Repair environment."""
from __future__ import annotations
import argparse
from fastapi import HTTPException
try:
from openenv.core.env_server.http_server import create_app
from ..models import BaselineCatalog, GraderReport, TaskCatalog, WebSecRepairAction, WebSecRepairObservation
from .challenge import grade_task, list_baselines, list_tasks
from .websec_repair_environment import WebSecRepairEnvironment
except ImportError:
from openenv.core.env_server.http_server import create_app
from models import ( # type: ignore
BaselineCatalog,
GraderReport,
TaskCatalog,
WebSecRepairAction,
WebSecRepairObservation,
)
from server.challenge import grade_task, list_baselines, list_tasks # type: ignore
from server.websec_repair_environment import WebSecRepairEnvironment # type: ignore
app = create_app(
WebSecRepairEnvironment,
WebSecRepairAction,
WebSecRepairObservation,
env_name="websec_repair_env",
max_concurrent_envs=1,
)
@app.get("/tasks", response_model=TaskCatalog, tags=["challenge"])
def tasks() -> TaskCatalog:
"""List all deterministic vulnerability repair tasks."""
return list_tasks()
@app.get("/grader", response_model=GraderReport, tags=["challenge"])
def grader(task_id: str | None = None) -> GraderReport:
"""Return the current grader state, optionally filtered to a task id."""
try:
return grade_task(task_id=task_id)
except ValueError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
@app.get("/baseline", response_model=BaselineCatalog, tags=["challenge"])
def baseline(task_id: str | None = None) -> BaselineCatalog:
"""Return all baseline trajectories or one filtered task baseline."""
try:
return list_baselines(task_id=task_id)
except ValueError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
def serve(host: str = "0.0.0.0", port: int = 8000) -> None:
"""Run the FastAPI app with uvicorn."""
import uvicorn
uvicorn.run(app, host=host, port=port)
def main() -> None:
"""CLI entry point exposed via the project script."""
parser = argparse.ArgumentParser()
parser.add_argument("--host", default="0.0.0.0")
parser.add_argument("--port", type=int, default=8000)
args = parser.parse_args()
serve(host=args.host, port=args.port)
if __name__ == "__main__":
main()
|