Spaces:
Sleeping
Sleeping
File size: 6,390 Bytes
161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 a1415e8 161cb23 419a527 161cb23 7667cef 161cb23 a1415e8 161cb23 | 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 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | from __future__ import annotations
import asyncio
from collections.abc import AsyncIterator, Awaitable, Callable
from contextlib import asynccontextmanager
import logging
from typing import Any
import httpx
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, PlainTextResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, Field
from .config import Settings, load_settings
from .hygiene import SCORER_ID, SCORER_VERSION, score_dict
from .leaderboard import BOARD_FILE, SEED_REPOS, Leaderboard, RankBody, RankError
from .telemetry import RateLimiter, ScanBody, StatsStore, SyncedFile, Telemetry, preset_digests
log = logging.getLogger("space")
MAX_BODY_CHARS = 200_000
ROOT_NOTE = "codehygiene Space API is running. Build space/web to serve the UI here.\n"
SCAN_BATCHES_PER_MINUTE = 10
SCAN_BATCHES_PER_MINUTE_ALL = 600
RANKS_PER_MINUTE = 2
RANKS_PER_MINUTE_ALL = 20
STATS_HEADERS = {"Cache-Control": "public, max-age=60"}
class ScoreBody(BaseModel):
filename: str = ""
content: str = Field(max_length=MAX_BODY_CHARS)
threshold: float = Field(0.55, ge=0.0, le=1.0)
def caller_of(request: Request) -> str:
"""Address used only for the rate-limit bucket check, then discarded."""
forwarded = request.headers.get("x-forwarded-for", "").split(",")[0].strip()
return forwarded or (request.client.host if request.client else "")
def error_response(error: RankError) -> JSONResponse:
return JSONResponse(error.payload(), status_code=error.status)
def create_app(
settings: Settings | None = None,
github_transport: httpx.AsyncBaseTransport | None = None,
board: Leaderboard | None = None,
) -> FastAPI:
settings = settings or load_settings()
telemetry = Telemetry(excluded=preset_digests(settings.presets_path))
board = board or Leaderboard(settings, github_transport, telemetry=telemetry)
if board.telemetry is None:
board.telemetry = telemetry
dataset = (settings.stats_dataset_repo, settings.hf_token)
synced = (
SyncedFile(StatsStore(settings.stats_path, *dataset), telemetry),
SyncedFile(StatsStore(settings.leaderboard_path, *dataset, filename=BOARD_FILE), board),
)
scan_limit = RateLimiter(SCAN_BATCHES_PER_MINUTE)
scan_limit_all = RateLimiter(SCAN_BATCHES_PER_MINUTE_ALL)
rank_limit = RateLimiter(RANKS_PER_MINUTE)
rank_limit_all = RateLimiter(RANKS_PER_MINUTE_ALL)
async def sync_files() -> None:
while True:
await asyncio.sleep(settings.stats_sync_seconds)
for file in synced:
await file.flush()
@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
for file in synced:
await file.load()
tasks = [asyncio.create_task(sync_files())]
if settings.leaderboard_seed:
tasks.append(asyncio.create_task(board.maintain(SEED_REPOS, settings.leaderboard_seed_pause)))
yield
for task in tasks:
task.cancel()
await board.close()
for file in synced:
await file.flush()
app = FastAPI(title="codehygiene", lifespan=lifespan)
app.state.telemetry = telemetry
app.state.board = board
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
@app.middleware("http")
async def static_cache_headers(
request: Request, call_next: Callable[[Request], Awaitable[Any]]
) -> Any:
response = await call_next(request)
if request.method not in ("GET", "HEAD") or request.url.path.startswith("/api/"):
return response
if "cache-control" in response.headers:
return response
content_type = response.headers.get("content-type", "")
if content_type.startswith("text/html"):
response.headers["Cache-Control"] = "no-cache"
elif request.url.path.startswith("/fonts/") or "v=" in request.url.query:
response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
else:
response.headers["Cache-Control"] = "no-cache"
return response
@app.get("/api/health")
def health() -> dict[str, Any]:
return {
"status": "ok",
"scorer_version": SCORER_VERSION,
"scorer_id": SCORER_ID,
}
@app.get("/api/stats")
def stats() -> JSONResponse:
body = {
**telemetry.public(),
"leaderboard_entries": len(board.current()),
"scorer_version": SCORER_VERSION,
}
return JSONResponse(body, headers=STATS_HEADERS)
@app.get("/api/leaderboard")
def leaderboard() -> JSONResponse:
return JSONResponse(board.public(), headers=STATS_HEADERS)
@app.post("/api/leaderboard/rank", response_model=None)
async def leaderboard_rank(body: RankBody, request: Request) -> dict[str, Any] | JSONResponse:
if not (rank_limit.allow(caller_of(request)) and rank_limit_all.allow("*")):
return error_response(RankError(429, "busy", "Too many rank requests. Try again shortly."))
try:
return await board.rank(body.owner, body.repo)
except RankError as error:
return error_response(error)
except httpx.HTTPError as error:
log.warning("GitHub archive request failed: %s", type(error).__name__)
return error_response(RankError(502, "github_error", "GitHub could not be reached."))
@app.post("/api/telemetry/scan")
def telemetry_scan(body: ScanBody, request: Request) -> dict[str, int]:
allowed = scan_limit.allow(caller_of(request)) and scan_limit_all.allow("*")
if not allowed:
raise HTTPException(status_code=429, detail="Too many scan reports.")
return {"counted": telemetry.record_scan(body)}
@app.post("/api/score")
def score(body: ScoreBody) -> dict[str, Any]:
return score_dict(body.content, body.threshold)
if (settings.web_dist / "index.html").is_file():
app.mount("/", StaticFiles(directory=settings.web_dist, html=True), name="web")
else:
@app.get("/", response_class=PlainTextResponse)
def root_note() -> str:
return ROOT_NOTE
return app
app = create_app()
|