imag vector generation with dimentionality reduction

This commit is contained in:
sriram
2026-09-18 15:26:25 +05:30
parent afa0bfa743
commit d6296bd1f0
16 changed files with 1724 additions and 48 deletions

View File

@@ -14,10 +14,12 @@ import threading
from contextlib import asynccontextmanager
from pathlib import Path
from fastapi import FastAPI, HTTPException
from fastapi import FastAPI, HTTPException, Request
from fastapi.exceptions import RequestValidationError
from fastapi.encoders import jsonable_encoder
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from fastapi.responses import FileResponse, JSONResponse
from app.infrastructure.persistence import restore_bundled_assets
from app.infrastructure.settings import (
@@ -213,6 +215,27 @@ app.add_middleware(
allow_headers=["*"],
)
def _json_safe(value):
"""Replace floats JSON cannot carry (NaN, +/-inf) so an error can be sent."""
if isinstance(value, float) and (value != value or value in (float("inf"), float("-inf"))):
return str(value)
if isinstance(value, dict):
return {k: _json_safe(v) for k, v in value.items()}
if isinstance(value, (list, tuple)):
return [_json_safe(v) for v in value]
return value
@app.exception_handler(RequestValidationError)
async def _validation_error_as_422(request: Request, exc: RequestValidationError) -> JSONResponse:
"""FastAPI's own 422 body echoes the rejected input. Python's JSON parser
accepts `NaN` on the way in, pydantic rejects it, and the echo then fails
to serialise - so a client that sent one NaN in a float field got a 500
instead of the 422 that names the field. Found by the image-vector
search, where the body is 1024 floats; applies to every route."""
return JSONResponse(status_code=422, content={"detail": _json_safe(jsonable_encoder(exc.errors()))})
# A wrong origin list fails only in the browser, as an opaque "blocked by CORS"
# with a perfectly healthy 200 in the server log - so state the effective list
# at startup, where it can actually be compared against the frontend's URL.