knowledge-engine / api /v1 /archive.py
m97j's picture
feat: authenticated archive inference with bounded CPU execution
f16b727 verified
Raw History Blame Contribute Delete
1.82 kB
"""Server-to-server APIs; private drafts are never added to the public demo."""
from fastapi import APIRouter, Depends, HTTPException, Request
from api.archive_security import archive_bearer
from api.schemas.archive import EmbeddingsRequest, EmbeddingsResponse, TranslationRequest, TranslationResponse
from core.inference_runner import InferenceBusy, InferenceTimedOut
from services.archive_inference import ProviderUnavailable
router = APIRouter(prefix="/archive", tags=["Authenticated Archive Inference"],
dependencies=[Depends(archive_bearer)])
async def run(request, operation):
try:
return await request.app.state.inference_runner.run(operation)
except InferenceBusy:
raise HTTPException(429, "Inference queue is full", headers={"Retry-After": "5"})
except InferenceTimedOut:
raise HTTPException(504, "Inference timed out")
except ProviderUnavailable:
raise HTTPException(503, "Translation provider is not configured")
except ValueError:
raise HTTPException(422, "Input is not supported by the configured model")
except Exception:
# No exception strings: model/provider errors may include private input.
raise HTTPException(502, "Inference failed")
@router.post("/embeddings", response_model=EmbeddingsResponse)
async def embeddings(body: EmbeddingsRequest, request: Request):
service = request.app.state.archive_inference_service
return await run(request, lambda: service.embed(body.texts, body.model))
@router.post("/translate", response_model=TranslationResponse)
async def translate(body: TranslationRequest, request: Request):
service = request.app.state.archive_inference_service
return await run(request, lambda: service.translate(body.texts, body.source_locale, body.target_locale))