openlayer.Project.add_model#
- Project.add_model(*args, **kwargs)#
Adds a model to a project’s staging area.
This is the method for every model upload, regardless of whether you want to add a shell model, a full model, or a direct-to-API model (for LLMs-only).
Refer to the Knowledge base guide on model upload to learn more about the differences between these options.
- Parameters:
- model_configDict[str, any]
Dictionary containing the model configuration. This is not needed if
model_config_file_path
is provided.What’s in the model config dict?
The model configuration depends on the project’s
tasks.TaskType
. Refer to the How to write model configs guide for details.- model_config_file_pathstr
Path to the model configuration YAML file. This is not needed if
model_config
is provided.What’s in the model config file?
The model configuration YAML depends on the project’s
tasks.TaskType
. Refer to the How to write model configs guide for details.- model_package_dirstr, default None
Path to the directory containing the model package. Only needed if you are interested in adding a full model.
What’s in the 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, whenforce=True
, the existing staged model will be overwritten by the new one. Whenforce=False
, the user will be prompted to confirm the overwrite.
Examples
Related guide: How to upload datasets and models for development.
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")
Let’s say you have a tabular classification project and your dataset looks like the following:
>>> df CreditScore Geography Balance PredictionScores 0 618 France 321.92 [0.1, 0.9] 1 714 Germany 102001.22 [0.7, 0.3] 2 604 Spain 12333.15 [0.2, 0.8] .. ... ... ...
If you want to add a shell model…
Prepare the model config:
>>> model_config = { ... "metadata": { # Can add anything here, as long as it is a dict ... "model_type": "Gradient Boosting Classifier", ... "regularization": "None", ... "encoder_used": "One Hot", ... }, ... "classNames": class_names, ... "featureNames": feature_names, ... "categoricalFeatureNames": categorical_feature_names, ... }
What’s in the model config?
The model configuration depends on the project’s
tasks.TaskType
. Refer to the How to write model configs guides for details.Then, you can add the model to the project with:
>>> project.add_model( ... model_config=model_config, ... )
If you want to add a full model…
Prepare the model config and the model package directory. Refer to the Examples gallery GitHub repository for code examples.
You can then add the model to the project with:
Then, you can add the model to the project with:
>>> project.add_model( ... model_config=model_config, ... model_package_dir="path/to/model/package") ... sample_data=df.loc[: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 thecommit
andpush
methods:>>> project.commit("Initial model commit.") >>> project.push()