File size: 1,820 Bytes
f16b727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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))