Skip to content

Entity and methods

Model

Bases: MaterialEntity

A class representing a model.

Source code in digitalhub/entities/model/entity/_base.py
class Model(MaterialEntity):
    """
    A class representing a model.
    """

    ENTITY_TYPE = EntityTypes.MODEL.value

    def __init__(
        self,
        project: str,
        name: str,
        uuid: str,
        kind: str,
        metadata: Metadata,
        spec: ModelSpec,
        status: ModelStatus,
        user: str | None = None,
    ) -> None:
        super().__init__(project, name, uuid, kind, metadata, spec, status, user)
        self.spec: ModelSpec
        self.status: ModelStatus

ModelModel

Bases: Model

Model model.

Source code in digitalhub/entities/model/entity/model.py
6
7
8
9
class ModelModel(Model):
    """
    Model model.
    """

ModelMlflow

Bases: Model

Mlflow model.

Source code in digitalhub/entities/model/entity/mlflow.py
class ModelMlflow(Model):
    """
    Mlflow model.
    """

from_mlflow_run(run_id)

Extract from mlflow run spec for Digitalhub Model.

Parameters:

Name Type Description Default
run_id str

The id of the mlflow run.

required

Returns:

Type Description
dict

The extracted spec.

Source code in digitalhub/entities/model/entity/mlflow.py
def from_mlflow_run(run_id: str) -> dict:
    """
    Extract from mlflow run spec for Digitalhub Model.

    Parameters
    ----------
    run_id : str
        The id of the mlflow run.

    Returns
    -------
    dict
        The extracted spec.
    """

    # Get MLFlow run
    client = mlflow.MlflowClient()
    run = client.get_run(run_id)

    # Extract spec
    data = run.data
    parameters = data.params
    metrics = data.metrics
    source_path = urlparse(run.info.artifact_uri).path
    model_uri = f"runs:/{run_id}/model"
    model = mlflow.pyfunc.load_model(model_uri=model_uri)
    model_config = model.model_config
    flavor = None
    for f in model.metadata.flavors:
        if f != "python_function":
            flavor = f
            break

    # Extract signature
    mlflow_signature = model.metadata.signature
    signature = Signature(
        inputs=mlflow_signature.inputs.to_json() if mlflow_signature.inputs else None,
        outputs=mlflow_signature.outputs.to_json() if mlflow_signature.outputs else None,
        params=mlflow_signature.params.to_json() if mlflow_signature.params else None,
    ).dict()

    # Extract datasets
    datasets = []
    if run.inputs and run.inputs.dataset_inputs:
        datasets = [
            Dataset(
                name=d.dataset.name,
                digest=d.dataset.digest,
                profile=d.dataset.profile,
                schema=d.dataset.schema,
                source=d.dataset.source,
                source_type=d.dataset.source_type,
            ).dict()
            for d in run.inputs.dataset_inputs
        ]

    # Create model params
    model_params = {}

    # source path
    model_params["source"] = source_path

    # common properties
    model_params["framework"] = flavor
    model_params["parameters"] = parameters
    model_params["metrics"] = metrics

    # specific to MLFlow
    model_params["flavor"] = flavor
    model_params["model_config"] = model_config
    model_params["input_datasets"] = datasets
    model_params["signature"] = signature

    return model_params

ModelSklearn

Bases: Model

SKLearn model.

Source code in digitalhub/entities/model/entity/sklearn.py
6
7
8
9
class ModelSklearn(Model):
    """
    SKLearn model.
    """

ModelHuggingface

Bases: Model

Huggingface model.

Source code in digitalhub/entities/model/entity/huggingface.py
6
7
8
9
class ModelHuggingface(Model):
    """
    Huggingface model.
    """

model_from_dict(obj)

Create a new object from dictionary.

Parameters:

Name Type Description Default
obj dict

Dictionary to create object from.

required

Returns:

Type Description
Model

Object instance.

Source code in digitalhub/entities/model/builder.py
def model_from_dict(obj: dict) -> Model:
    """
    Create a new object from dictionary.

    Parameters
    ----------
    obj : dict
        Dictionary to create object from.

    Returns
    -------
    Model
        Object instance.
    """
    kind = obj.get("kind")
    cls = _choose_model_type(kind)
    return cls.from_dict(obj)

model_from_parameters(project, name, kind, uuid=None, description=None, labels=None, embedded=True, path=None, **kwargs)

Create a new object.

Parameters:

Name Type Description Default
project str

Project name.

required
name str

Object name.

required
kind str

Kind the object.

required
uuid str

ID of the object (UUID4, e.g. 40f25c4b-d26b-4221-b048-9527aff291e2).

None
labels list[str]

List of labels.

None
description str

Description of the object (human readable).

None
embedded bool

Flag to determine if object spec must be embedded in project spec.

True
path str

Object path on local file system or remote storage. It is also the destination path of upload() method.

None
**kwargs dict

Spec keyword arguments.

{}

Returns:

Type Description
Model

Object instance.

Source code in digitalhub/entities/model/builder.py
def model_from_parameters(
    project: str,
    name: str,
    kind: str,
    uuid: str | None = None,
    description: str | None = None,
    labels: list[str] | None = None,
    embedded: bool = True,
    path: str | None = None,
    **kwargs,
) -> Model:
    """
    Create a new object.

    Parameters
    ----------
    project : str
        Project name.
    name : str
        Object name.
    kind : str
        Kind the object.
    uuid : str
        ID of the object (UUID4, e.g. 40f25c4b-d26b-4221-b048-9527aff291e2).
    labels : list[str]
        List of labels.
    description : str
        Description of the object (human readable).
    embedded : bool
        Flag to determine if object spec must be embedded in project spec.
    path : str
        Object path on local file system or remote storage. It is also the destination path of upload() method.
    **kwargs : dict
        Spec keyword arguments.

    Returns
    -------
    Model
        Object instance.
    """
    if path is None:
        raise EntityError("Path must be provided.")
    name = build_name(name)
    uuid = build_uuid(uuid)
    metadata = build_metadata(
        kind,
        project=project,
        name=name,
        version=uuid,
        description=description,
        labels=labels,
        embedded=embedded,
    )
    spec = build_spec(
        kind,
        path=path,
        **kwargs,
    )
    status = build_status(kind)
    cls = _choose_model_type(kind)
    return cls(
        project=project,
        name=name,
        uuid=uuid,
        kind=kind,
        metadata=metadata,
        spec=spec,
        status=status,
    )