botezx's picture
Grok Bot
FlagTLDR 0.5.0 (snapshot of local f98bb37)
043267b
Raw History Blame Contribute Delete
1.09 kB
from __future__ import annotations
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from . import __version__
from .backends import Backend, get_backend
from .spec import ClassifyRequest, ClassifyResponse
def create_app(backend: Backend | None = None) -> FastAPI:
app = FastAPI(title="X Addons classifier", version=__version__)
# Only browser extensions (and local tools) may call this local service.
app.add_middleware(
CORSMiddleware,
allow_origin_regex=r"^(chrome-extension|moz-extension|safari-web-extension)://.*$",
allow_methods=["POST", "GET"],
allow_headers=["content-type"],
)
app.state.backend = backend or get_backend("rules")
@app.get("/health")
async def health():
return {"ok": True, "backend": app.state.backend.name, "version": __version__}
@app.post("/v1/classify", response_model=ClassifyResponse)
async def classify(req: ClassifyRequest):
return ClassifyResponse(results=await app.state.backend.classify(req.labelset, req.posts))
return app