hydrosdk.modelversion module

show-inheritance

class hydrosdk.modelversion.DataProfileStatus(value)

An enumeration.

class hydrosdk.modelversion.DataUploadResponse(cluster, modelversion_id)

Handle processing status of the training data upload.

Example

Parameters
Return type

DataUploadResponse

Wait till data processing gets finished.

>>> from hydrosdk.cluster import Cluster
>>> cluster = Cluster("http-cluster-endpoint")
>>> model_version = ModelVersion.find(cluster, "my-model", 1)
>>> model_version.training_data = "s3://bucket/path/to/training-data.csv"
>>> data_upload_response = model_version.upload_training_data()
>>> try:
        data_upload_response.wait(retry=3, sleep=30)
    except TimeoutException as e:
        print(e)
    except DataUploadResponse.NotRegistered:
        id = data_upload_response.modelversion_id
        print(f"Unable to find training data for modelversion_id={id}")
    except DataUploadResponse.Failed:
        print(f"Failed to process training data")
exception Failed
exception NotRegistered
exception Unknown
wait(retry=12, sleep=30)

Wait till data processing gets finished.

Parameters
  • retry – Number of retries before giving up waiting.

  • sleep – Sleep interval between retries.

class hydrosdk.modelversion.ModelVersion(cluster, id, model_id, name, version, signature, status, image, runtime, is_external, metadata=None, install_command=None, training_data=None, monitoring_configuration=None, created=None, finished=None, applications=None)

A Model, registered within the cluster. ModelVersion represents one of the Models’ versions. (https://hydrosphere.io/serving-docs/latest/overview/concepts.html#models)

Example

Parameters
  • cluster (hydrosdk.cluster.Cluster) –

  • id (int) –

  • model_id (int) –

  • name (str) –

  • version (int) –

  • signature (hydro_serving_grpc.serving.contract.signature_pb2.ModelSignature) –

  • status (Optional[hydrosdk.modelversion.ModelVersionStatus]) –

  • image (Optional[hydrosdk.image.DockerImage]) –

  • runtime (Optional[hydrosdk.image.DockerImage]) –

  • is_external (bool) –

  • metadata (Optional[Dict[str, str]]) –

  • install_command (Optional[str]) –

  • training_data (Optional[str]) –

  • monitoring_configuration (Optional[hydrosdk.modelversion.MonitoringConfiguration]) –

  • created (Optional[str]) –

  • finished (Optional[str]) –

  • applications (Optional[List[str]]) –

List all modelversions, registered on the cluster.

>>> from hydrosdk.cluster import Cluster
>>> cluster = Cluster("http-cluster-endpoint")
>>> for model_version in ModelVersion.list(cluster):
        print(model_version)

Upload training data for a model_version.

>>> from hydrosdk.cluster import Cluster
>>> cluster = Cluster("http-cluster-endpoint")
>>> model_version = ModelVersion.find(cluster, "my-model", 1)
>>> model_version.lock_till_released()
>>> model_version.training_data = "s3://my-bucket/path/to/training-data.csv"
>>> data_upload_response = model_version.upload_traininf_data()

Assign custom monitoring metrics for a model_version.

>>> from hydrosdk.cluster import Cluster
>>> from hydrosdk.monitoring import ThresholdCmpOp
>>> cluster = Cluster("http-cluster-endpoint")
>>> model_version = ModelVersion.find(cluster, "my-model", 1)
>>> modelversion_metric = ModelVersion.find(cluster, "my-model-metric", 1)
>>> modelversion_metric = modmodelversion_metric.as_metric(1.4, ThresholdCmpOp.LESS_EQ)
>>> model_version.assign_metrics([modelversion_metric])

Check logs from a ModelVersion.

>>> from hydrosdk.cluster import Cluster
>>> cluster = Cluster("http-cluster-endpoint")
>>> model_version = ModelVersion.find(cluster, "my-model", 1)
>>> for event in model_version.logs():
        print(event.data)
Parameters
  • cluster (hydrosdk.cluster.Cluster) – active cluster

  • id (int) – id of the model_version assigned by the cluster

  • model_id (int) – id of the model assigned by the cluster

  • name (str) – a name of the model this ModelVersion belongs to

  • version (int) – a version of the model this ModelVersion belongs to

  • signature (hydro_serving_grpc.serving.contract.signature_pb2.ModelSignature) – a ModelSignature which specifies the name of the inference function, as well as its types and shapes for both inputs and outputs

  • status (Optional[hydrosdk.modelversion.ModelVersionStatus]) – a status of this ModelVersion, one of {Assembling, Released, Failed}

  • image (Optional[hydrosdk.image.DockerImage]) – DockerImage of a packed ModelVersion stored inside the cluster

  • runtime (Optional[hydrosdk.image.DockerImage]) – DockerImage of a runtime which was used to build an image

  • is_external (bool) – indicates whether model is running outside the cluster

  • metadata (Optional[Dict[str, str]]) – metadata used for describing a ModelVersion

  • install_command (Optional[str]) – a command which was run within a runtime to prepare a model_version environment

  • monitoring_configuration (Optional[hydrosdk.modelversion.MonitoringConfiguration]) – specified monitoring configuration for the model

  • created (Optional[str]) – specifies, when the model version was created

  • finished (Optional[str]) – specified, when the model has finished building

  • applications (Optional[List[str]]) – specifies, which applications are using this model version

  • training_data (Optional[str]) –

exception ReleaseFailed
as_metric(threshold, comparator)

Converts model to MetricModel.

Parameters
Returns

MetricModel

Return type

hydrosdk.monitoring.MetricModel

assign_metrics(metrics, wait=True)

Adds metrics to the model.

Parameters
Returns

self

build_logs()

Sends request, saves and returns a build logs iterator.

Returns

Iterator over sseclient.Event

Return type

Iterator[sseclient.Event]

static find(cluster, name, version)

Find a ModelVersion on the cluster by model name and a version.

Parameters
Returns

ModelVersion object

Return type

hydrosdk.modelversion.ModelVersion

static find_by_id(cluster, id)

Find a ModelVersion on the cluster by id.

Parameters
Returns

ModelVersion object

Return type

hydrosdk.modelversion.ModelVersion

static find_by_model_name(cluster, model_name)

Find all model versions on the cluster filtered by model_name, sorted in ascending order by version.

Parameters
Returns

list of ModelVersions with model_name

Return type

<staticmethod object at 0x7f608215c310>

static list(cluster)

List all model versions on the cluster.

Parameters

cluster (hydrosdk.cluster.Cluster) – active cluster

Returns

list of ModelVersions

Return type

List[hydrosdk.modelversion.ModelVersion]

lock_till_released(timeout=120)

Lock till the model completes assembling.

Parameters

timeout (int) – approximate value when to raise TimeoutException

Raises
to_proto()

Convert ModelVersion object into a ModelVersion proto message.

Returns

ModelVersion proto message

Return type

hydro_serving_grpc.serving.manager.entities_pb2.ModelVersion

upload_training_data()

Uploads training data for a given model_version.

Raises

ValueError if training data is not specified

Return type

hydrosdk.modelversion.DataUploadResponse

class hydrosdk.modelversion.ModelVersionBuilder(name, path)

A local instance of the model yet to be uploaded to the cluster. (https://hydrosphere.io/serving-docs/latest/overview/concepts.html#models)

Example

Parameters
  • name (str) –

  • path (str) –

Return type

ModelVersionBuilder

Create a local instance of the model and upload it to the cluster.

>>> from hydrosdk.cluster import Cluster
>>> from hydrosdk.image import DockerImage
>>> from hydrosdk.signature import SignatureBuilder, ProfilingType
>>> cluster = Cluster("http-cluster-endpoint")
>>> builder = ModelVersionBuilder("my-model", "/path/to/my-model")
>>> builder.with_runtime(DockerImage("hydrosphere/serving-runtime-python-3.7", tag="latest"))
>>> builder.with_payload(["src/func_main.py", "requirements.txt"])
>>> signature = SignatureBuilder("predict")             .with_input("x", int, "scalar", ProfilingType.NUMERICAL)             .with_output("y", float, "scalar", ProfilingType.NUMERICAL)             .build()
>>> builder.with_signature(signature)
>>> builder.with_install_command("pip install -r requirements.txt")
>>> builder.with_training_data("training-data.csv")
>>> model_version = builder.build(cluster)
>>> model_version.lock_till_released(timeout=120)
>>> data_upload_response = model_version.upload_training_data()
Parameters
  • name (str) – a name of the model

  • path (str) – a path to the root folder of the model

Return type

ModelVersionBuilder

build(cluster)

Upload local model instance to the cluster.

Parameters

cluster (hydrosdk.cluster.Cluster) – active cluster

Returns

ModelVersion object

Return type

hydrosdk.modelversion.ModelVersion

build_external(cluster)

Create an external ModelVersion on the cluster.

Parameters

cluster (hydrosdk.cluster.Cluster) – active cluster

Returns

ModelVersion object

Return type

hydrosdk.modelversion.ModelVersion

with_install_command(install_command)

Specify install command for the model.

Parameters

install_command (str) – a command to run within a runtime to prepare a model version environment.

Return type

hydrosdk.modelversion.ModelVersionBuilder

with_metadata(metadata)

Specify metadata for the model version.

Parameters

metadata (Dict[str, str]) – a metadata to describe the model version.

Return type

hydrosdk.modelversion.ModelVersionBuilder

with_monitoring_configuration(monitoring_configuration)

Specify monitoring configuration for the model version.

Parameters

monitoring_configuration (hydrosdk.modelversion.MonitoringConfiguration) – Specifies a configuration to be used when monitoring the model version.

Return type

hydrosdk.modelversion.ModelVersionBuilder

with_payload(payload)

Specify payload for the model version.

Parameters

payload (List[str]) – a list of paths to files (absolute or relative) with any additional resources that will be exported to the container.

Return type

hydrosdk.modelversion.ModelVersionBuilder

with_runtime(runtime)

Specify runtime for the model version.

Parameters

runtime (hydrosdk.image.DockerImage) – a docker image used to run model version’s code.

Return type

hydrosdk.modelversion.ModelVersionBuilder

with_signature(signature)

Specify signature for the model version.

Parameters

signature (hydro_serving_grpc.serving.contract.signature_pb2.ModelSignature) – ModelSignature, which specifies the name of the inference function, its types and shapes of both inputs and outputs.

Return type

hydrosdk.modelversion.ModelVersionBuilder

with_training_data(training_data)

Specify training data for the model version.

Parameters

training_data (str) – path (absolute, relative or an S3 URI) to a csv file with the training data

Return type

hydrosdk.modelversion.ModelVersionBuilder

class hydrosdk.modelversion.ModelVersionStatus(value)

Model building statuses.

hydrosdk.modelversion.resolve_paths(path, payload)

Appends each element of payload to the path and makes {resolved_path: payload_element} dict

Parameters
  • path (str) – absolute path

  • payload (List[str]) – list of relative paths

Returns

dict with {resolved_path: payload_element}

Return type

Dict[str, str]