File size: 2,168 Bytes
8f3dcc9
 
 
 
 
 
 
 
 
 
 
ba81a8e
ec4d8ae
 
8f3dcc9
ba81a8e
8f3dcc9
 
 
 
7785332
8f3dcc9
 
 
ec4d8ae
7cc3853
 
8f3dcc9
 
 
 
7cc3853
8f3dcc9
ec4d8ae
8f3dcc9
 
 
 
42343e0
8f3dcc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec4d8ae
8f3dcc9
 
 
 
 
 
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
from __future__ import annotations

from typing import TYPE_CHECKING, Annotated

from fastapi import (
    APIRouter,
    HTTPException,
    Path,
)
import huggingface_hub

from speaches.api_models import (
    ListModelsResponse,
    Model,
)
from speaches.hf_utils import list_whisper_models

if TYPE_CHECKING:
    from huggingface_hub.hf_api import ModelInfo

router = APIRouter(tags=["models"])


@router.get("/v1/models")
def get_models() -> ListModelsResponse:
    whisper_models = list(list_whisper_models())
    return ListModelsResponse(data=whisper_models)


@router.get("/v1/models/{model_name:path}")
def get_model(
    # NOTE: `examples` doesn't work https://github.com/tiangolo/fastapi/discussions/10537
    model_name: Annotated[str, Path(example="Systran/faster-distil-whisper-large-v3")],
) -> Model:
    models = huggingface_hub.list_models(
        model_name=model_name, library="ctranslate2", tags="automatic-speech-recognition", cardData=True
    )
    models = list(models)
    models.sort(key=lambda model: model.downloads or -1, reverse=True)
    if len(models) == 0:
        raise HTTPException(status_code=404, detail="Model doesn't exists")
    exact_match: ModelInfo | None = None
    for model in models:
        if model.id == model_name:
            exact_match = model
            break
    if exact_match is None:
        raise HTTPException(
            status_code=404,
            detail=f"Model doesn't exists. Possible matches: {', '.join([model.id for model in models])}",
        )
    assert exact_match.created_at is not None
    assert exact_match.card_data is not None
    assert exact_match.card_data.language is None or isinstance(exact_match.card_data.language, str | list)
    if exact_match.card_data.language is None:
        language = []
    elif isinstance(exact_match.card_data.language, str):
        language = [exact_match.card_data.language]
    else:
        language = exact_match.card_data.language
    return Model(
        id=exact_match.id,
        created=int(exact_match.created_at.timestamp()),
        object_="model",
        owned_by=exact_match.id.split("/")[0],
        language=language,
    )