bikes.utils.signers
Generate signatures for AI/ML models.
1"""Generate signatures for AI/ML models.""" 2 3# %% IMPORTS 4 5import abc 6import typing as T 7 8import mlflow 9import pydantic as pdt 10from mlflow.models import signature as ms 11 12from bikes.core import schemas 13 14# %% TYPES 15 16Signature: T.TypeAlias = ms.ModelSignature 17 18# %% SIGNERS 19 20 21class Signer(abc.ABC, pdt.BaseModel, strict=True, frozen=True, extra="forbid"): 22 """Base class for generating model signatures. 23 24 Allow switching between model signing strategies. 25 e.g., automatic inference, manual model signature, ... 26 27 https://mlflow.org/docs/latest/models.html#model-signature-and-input-example 28 """ 29 30 KIND: str 31 32 @abc.abstractmethod 33 def sign(self, inputs: schemas.Inputs, outputs: schemas.Outputs) -> Signature: 34 """Generate a model signature from its inputs/outputs. 35 36 Args: 37 inputs (schemas.Inputs): inputs data. 38 outputs (schemas.Outputs): outputs data. 39 40 Returns: 41 Signature: signature of the model. 42 """ 43 44 45class InferSigner(Signer): 46 """Generate model signatures from inputs/outputs data.""" 47 48 KIND: T.Literal["InferSigner"] = "InferSigner" 49 50 @T.override 51 def sign(self, inputs: schemas.Inputs, outputs: schemas.Outputs) -> Signature: 52 return mlflow.models.infer_signature(model_input=inputs, model_output=outputs) 53 54 55SignerKind = InferSigner
Signature: TypeAlias =
mlflow.models.signature.ModelSignature
class
Signer(abc.ABC, pydantic.main.BaseModel):
22class Signer(abc.ABC, pdt.BaseModel, strict=True, frozen=True, extra="forbid"): 23 """Base class for generating model signatures. 24 25 Allow switching between model signing strategies. 26 e.g., automatic inference, manual model signature, ... 27 28 https://mlflow.org/docs/latest/models.html#model-signature-and-input-example 29 """ 30 31 KIND: str 32 33 @abc.abstractmethod 34 def sign(self, inputs: schemas.Inputs, outputs: schemas.Outputs) -> Signature: 35 """Generate a model signature from its inputs/outputs. 36 37 Args: 38 inputs (schemas.Inputs): inputs data. 39 outputs (schemas.Outputs): outputs data. 40 41 Returns: 42 Signature: signature of the model. 43 """
Base class for generating model signatures.
Allow switching between model signing strategies. e.g., automatic inference, manual model signature, ...
https://mlflow.org/docs/latest/models.html#model-signature-and-input-example
@abc.abstractmethod
def
sign( self, inputs: pandera.typing.pandas.DataFrame[bikes.core.schemas.InputsSchema], outputs: pandera.typing.pandas.DataFrame[bikes.core.schemas.OutputsSchema]) -> mlflow.models.signature.ModelSignature:
33 @abc.abstractmethod 34 def sign(self, inputs: schemas.Inputs, outputs: schemas.Outputs) -> Signature: 35 """Generate a model signature from its inputs/outputs. 36 37 Args: 38 inputs (schemas.Inputs): inputs data. 39 outputs (schemas.Outputs): outputs data. 40 41 Returns: 42 Signature: signature of the model. 43 """
Generate a model signature from its inputs/outputs.
Arguments:
- inputs (schemas.Inputs): inputs data.
- outputs (schemas.Outputs): outputs data.
Returns:
Signature: signature of the model.
46class InferSigner(Signer): 47 """Generate model signatures from inputs/outputs data.""" 48 49 KIND: T.Literal["InferSigner"] = "InferSigner" 50 51 @T.override 52 def sign(self, inputs: schemas.Inputs, outputs: schemas.Outputs) -> Signature: 53 return mlflow.models.infer_signature(model_input=inputs, model_output=outputs)
Generate model signatures from inputs/outputs data.
@T.override
def
sign( self, inputs: pandera.typing.pandas.DataFrame[bikes.core.schemas.InputsSchema], outputs: pandera.typing.pandas.DataFrame[bikes.core.schemas.OutputsSchema]) -> mlflow.models.signature.ModelSignature:
51 @T.override 52 def sign(self, inputs: schemas.Inputs, outputs: schemas.Outputs) -> Signature: 53 return mlflow.models.infer_signature(model_input=inputs, model_output=outputs)
Generate a model signature from its inputs/outputs.
Arguments:
- inputs (schemas.Inputs): inputs data.
- outputs (schemas.Outputs): outputs data.
Returns:
Signature: signature of the model.
SignerKind =
<class 'InferSigner'>