openlayer.OpenlayerClient.add_model#

OpenlayerClient.add_model(model_config_file_path, model_package_dir=None, sample_data=None, force=False, project_id=None)#

Adds a model to a project’s staging area.

Parameters
model_config_file_pathstr

Path to the model configuration YAML file.

What’s on the model config file?

The model configuration YAML file must contain the following fields:

namestr

Name of the model.

architectureTypestr

The model’s framework. Must be one of the supported frameworks on ModelType.

classNamesList[str]

List of class names corresponding to the outputs of your predict function. E.g. ['positive', 'negative'].

featureNamesList[str], default []

List of input feature names. Only applicable if your task_type is TaskType.TabularClassification or TaskType.TabularRegression.

categoricalFeatureNamesList[str], default []

A list containing the names of all categorical features used by the model. E.g. ["Gender", "Geography"]. Only applicable if your task_type is TaskType.TabularClassification or TaskType.TabularRegression.

predictionThresholdfloat, default None

The threshold used to determine the predicted class. Only applicable if you are using a binary classifier and you provided the predictionScoresColumnName with the lists of class probabilities in your datasets (refer to add_dataframe).

If you provided predictionScoresColumnName but not predictionThreshold, the predicted class is defined by the argmax of the lists in predictionScoresColumnName.

metadataDict[str, any], default {}

Dictionary containing metadata about the model. This is the metadata that will be displayed on the Openlayer platform.

model_package_dirstr, default None

Path to the directory containing the model package. Only needed if you are interested in adding the model’s artifacts.

What’s inside model_package_dir?

The model package directory must contain the following files:

  • prediction_interface.py

    The prediction interface file.

  • model artifacts

    The model artifacts. This can be a single file, multiple files or a directory. The model artifacts must be compatible with the prediction interface file.

  • requirements.txt

    The requirements file. This file contains the dependencies needed to run the prediction interface file.

For instructions on how to create a model package, refer to the documentation.

sample_datapd.DataFrame, default None

Sample data that can be run through the model. Only needed if model_package_dir is not None. This data is used to ensure the model’s prediction interface is compatible with the Openlayer platform.

Important

The sample_data must be a dataframe with at least two rows.

forcebool

If add_model is called when there is already a model in the staging area, when force=True, the existing staged model will be overwritten by the new one. When force=False, the user will be prompted to confirm the overwrite.

Examples

See also

Our sample notebooks and tutorials.

First, instantiate the client:

>>> import openlayer
>>> client = openlayer.OpenlayerClient('YOUR_API_KEY_HERE')

Create a project if you don’t have one:

>>> from openlayer.tasks import TaskType
>>> project = client.create_project(
...     name="Churn Prediction",
...     task_type=TaskType.TabularClassification,
...     description="My first project!",
... )

If you already have a project created on the platform:

>>> project = client.load_project(name="Your project name")

If your project’s task type is tabular classification…

Let’s say your model expects to receive a dataset looks like the following as input:

>>> df
    CreditScore  Geography    Balance
0           618     France     321.92
1           714    Germany  102001.22
2           604      Spain   12333.15
..          ...        ...        ...

Then, you can add the model to the project with:

>>> project.add_model(
...     model_config_file_path="path/to/model/config",
...     model_package_dir="path/to/model/package")
...     sample_data=df.iloc[:5, :],
... )

After adding the model to the project, it is staged, waiting to be committed and pushed to the platform. You can check what’s on your staging area with status. If you want to push the model right away with a commit message, you can use the commit and push methods:

>>> project.commit("Initial model commit.")
>>> project.push()

If your task type is text classification…

Let’s say your dataset looks like the following:

>>> df
                              Text
0    I have had a long weekend
1    I'm in a fantastic mood today
2    Things are looking up
..                             ...

Then, you can add the model to the project with:

>>> project.add_model(
...     model_config_file_path="path/to/model/config",
...     model_package_dir="path/to/model/package")
...     sample_data=df.iloc[:5, :],
... )

After adding the model to the project, it is staged, waiting to be committed and pushed to the platform. You can check what’s on your staging area with status. If you want to push the model right away with a commit message, you can use the commit and push methods:

>>> project.commit("Initial model commit.")
>>> project.push()