Skip to content

pixano_inference.models.capabilities

The capabilities served over HTTP, each described once.

A capability ties together a model base class, the Input/Output types of its predict, the request and response of its route, its default timeout and, when it accepts raw uploads, the multipart field of its /binary route. :data:CAPABILITIES is the single place where that is declared: the routes, the capability lookup of a model class and the default timeouts are derived from it. Adding a capability means adding one entry here (plus its base class and schemas).

BinaryUpload(file_field, payload_key) dataclass

Multipart upload accepted by the /binary route of a capability.

Attributes:

Name Type Description
file_field str

Name of the multipart part(s) carrying the raw media. "image" is a single file; any other name is a list of files.

payload_key str

Field of the request model that receives the uploaded bytes.

CapabilitySpec(model_base, input_type, output_type, request_type, response_type, default_timeout_s=60.0, binary=None) dataclass

Everything the server needs to know about one capability.

Attributes:

Name Type Description
model_base type[InferenceModel]

Base class a model subclasses to implement the capability. Its capability_name is the name of the capability.

input_type type[BaseModel]

Type predict receives.

output_type type[BaseModel]

Type predict returns.

request_type type[BaseRequest]

Body of the route; it exposes model and to_input().

response_type type[BaseResponse]

Response envelope of the route, whose data is output_type.

default_timeout_s float

Inference timeout when the deployment does not set one.

binary BinaryUpload | None

Upload accepted by the /binary route, or None when there is no such route.

name property

Name of the capability: the route segment and the value stored in deployment configs.

__post_init__()

Reject a base class that does not name its capability.

Source code in pixano_inference/models/capabilities.py
def __post_init__(self) -> None:
    """Reject a base class that does not name its capability."""
    if not self.model_base.capability_name:
        raise ValueError(f"{self.model_base.__name__} does not define 'capability_name'.")

capability_of(model_cls)

Return the capability a model class implements.

Raises:

Type Description
ValueError

If the class subclasses none of the capability base classes.

Source code in pixano_inference/models/capabilities.py
def capability_of(model_cls: type[InferenceModel]) -> CapabilitySpec:
    """Return the capability a model class implements.

    Raises:
        ValueError: If the class subclasses none of the capability base classes.
    """
    for spec in CAPABILITIES:
        if issubclass(model_cls, spec.model_base):
            return spec

    supported = ", ".join(base.__name__ for base in HTTP_CAPABILITY_BASES)
    raise ValueError(
        f"Model class '{model_cls.__name__}' is not supported by the HTTP inference API. "
        f"Supported base classes: {supported}."
    )

find_capability(name)

Return the capability called name, or None if there is no such capability.

Source code in pixano_inference/models/capabilities.py
def find_capability(name: str) -> CapabilitySpec | None:
    """Return the capability called *name*, or ``None`` if there is no such capability."""
    return _CAPABILITIES_BY_NAME.get(name)

get_capability(name)

Return the capability called name.

Raises:

Type Description
KeyError

If there is no such capability.

Source code in pixano_inference/models/capabilities.py
def get_capability(name: str) -> CapabilitySpec:
    """Return the capability called *name*.

    Raises:
        KeyError: If there is no such capability.
    """
    spec = find_capability(name)
    if spec is None:
        raise KeyError(f"Unknown capability '{name}'. Known capabilities: {', '.join(_CAPABILITIES_BY_NAME)}.")
    return spec

infer_http_capability(model_cls)

Infer the HTTP capability implemented by a model class.

Source code in pixano_inference/models/capabilities.py
def infer_http_capability(model_cls: type[InferenceModel]) -> str:
    """Infer the HTTP capability implemented by a model class."""
    return capability_of(model_cls).name