skip to Main Content

Getting Started with Comet REST API

Photo by Campaign Creators on Unsplash

Comet is an experimentation platform that allows you to track, monitor, and compare your Machine Learning experiments. You can write your code in your preferred language, and then compare the results of your experiments in the Comet interface. Originally, I had tested Comet using Python, but in some of my recent articles, I have described how to easily work with Comet using Rand Java as well.

In this article, I focus on REST API. Yes, because Comet also provides a very useful REST API service to interact with!

The article is organized as follows:

  • Overview of the Comet REST API
  • A practical example

1. Overview of the Comet REST API

The Comet REST API service allows you to access workspaces, projects, experiments, models, and so on. To set up a connection with the Comet REST API service, you must first configure the HTTP Authorization Header with your Comet API key. In the next section, you will see how to build a REST API call using the curl command.

Comet provides the following basic URL to access the REST API service:

https://www.comet.ml/api/rest/v2/

Depending on what you pass as the next parameter to the basic URL, you will get different Comet objects.

For example, to access all the workspaces, you can use the workspacesparameter, as follows:

https://www.comet.ml/api/rest/v2/workspaces

You can also access all the projects within a workspace, using the projectsparameter and the workspace name:

https://www.comet.ml/api/rest/v2/projects?workspaceName=aWorkspace

To access the experiments within a project, you can use the experimentparameter, as well as the projectId. If you do not want to access the archived experiments, you need to set up the keyword archived to false:

https://www.comet.ml/api/rest/v2/experiments?projectId=aProjectId&archived=false

You can even access models contained in the registry, using the registry-model parameter, as well as the workspace name:

https://www.comet.ml/api/rest/v2/registry-model?workspaceName=aWorkspace

To download a model from the registry, you can use the registry-model parameter, as well as the workspace name, the model name, and its version:

https://www.comet.ml/api/rest/v2/registry-model/item/download?workspaceName=AWorkspace&modelName=AModel&version=AVersion

The Comet REST API service also permits you to access other objects. The list of all the calls provided by the Comet API is available here.

Different teams have different needs. Comet’s got you covered. Learn how the team at Uber use Comet’s experiment management to perform real-time model tuning.

2. A practical example

Let’s suppose that you’ve built an experiment and tested different models. You’ve chosen the best of these models and registered it in the Comet Registry. You can find a detailed description of how to perform these operations in my previous article, entitled How to Use the Comet Registry to Track Your Machine Learning Models.

Now you want to use the Comet Registry as the official repository for your model. This could be very useful when you move your application to production because you could use Comet to always store the best and last model. Thanks to the use of the Comet Registry, you will no longer have to remember which model is the best, because you will keep everything under control in the Comet Registry. For example, if a new experiment proves that one model is better than the current one, it will be sufficient to register it in Comet Registry, and simply replace the previous model with the new one. Through the REST API, then you can always download the most updated model.

To download the model, you can adopt two different strategies.

In the first strategy, you can use the Comet interface. If you access the Comet Registry, you can either: 1) download the most recent version of the model through the ‘Get Model’ button, or; 2) download any particular version by accessing the download icon, as shown in the following illustration:

Image by Author

This strategy, however, requires you to manually download the model version you are interested in. Thus, every time you want to download the model, you need to access the Comet interface.

In the second strategy, you can write code in your preferred language to download the model from the registry automatically by using the Comet Python API.

Here I combine REST API and bash scripting to download the model.

First, create a file named get_model.sh. Then, edit as follows:

Define some variables containing your Comet API and your workspace:

#!/bin/shCOMET_API_KEY='YOUR_COMET_API_KEY'
COMET_WORKSPACE='YOUR_COMET_WORKSPACE'

Now define two additional variables containing the model name and the model version you want to download:

MODEL_NAME='YOUR_MODEL_NAME'
MODEL_VERSION='YOUR_MODEL_VERSION'

Finally, use the curl command to download the model:

curl -o model.zip -s "https://www.comet.ml/api/rest/v2/registry-model/item/download?workspaceName=$COMET_WORKSPACE&modelName=$MODEL_NAME&version=$MODEL_VERSION" -H "Authorization: $COMET_API_KEY"

The downloaded model is stored in a file named model.zip. To restore this file to the original model, simply unzip it.

And that’s all — quick and simple!

Summary

Congratulations! You have just learned the Comet REST API and how to use it to download a model from the Comet Model Registry.

You can use Comet REST API for different purposes, including the integration of Comet with a CI/CD pipeline. You can find more information about it here.

Happy Coding! Happy Comet!

Angelica Lo Duca

Angelica Lo Duca

Back To Top