Skip to content

Getting Started

Please follow the installation procedure and then try to run one of the snippets below. You will need to fill in your own API token (YOUR_API_TOKEN). Documentation on creating an API token can be found here.

Authenticate to the Core Api

import ubiops

# Set environment variables
# - UBIOPS_API_TOKEN: "Token <YOUR_API_TOKEN>"
# - UBIOPS_API_HOST: optional - default to "https://api.ubiops.com/v2.1"
core_api = ubiops.CoreApi()

api_response = core_api.service_status()
print(api_response)

# Close the connection
core_api.api_client.close()
import ubiops

configuration = ubiops.Configuration()
# Configure API token authorization
configuration.api_key['Authorization'] = "Token <YOUR_API_TOKEN>"
# Defining host is optional and default to "https://api.ubiops.com/v2.1"
configuration.host = "https://api.ubiops.com/v2.1"

api_client = ubiops.ApiClient(configuration)
core_api = ubiops.CoreApi(api_client)

api_response = core_api.service_status()
print(api_response)

# Close the connection
api_client.close()

Authenticate for Training features

import ubiops

# Set environment variables
# - UBIOPS_API_TOKEN: "Token <YOUR_API_TOKEN>"
# - UBIOPS_API_HOST: optional - default to "https://api.ubiops.com/v2.1"
training = ubiops.Training()

project_name = 'project_name_example' # str
experiment_name = 'experiment_name_example' # str
timeout = 56 # int (optional)

# Create run
api_response = training.experiment_runs_create(
    project_name=project_name,
    experiment_name=experiment_name,
    data=ubiops.ExperimentRunCreate(
        name="my-run",
        training_code="./train.py"
    ),
    timeout=timeout
)
print(api_response)

# Close the connection
training.api_client.close()
import ubiops

configuration = ubiops.Configuration()
# Configure API token authorization
configuration.api_key['Authorization'] = "Token <YOUR_API_TOKEN>"
# Defining host is optional and default to "https://api.ubiops.com/v2.1"
configuration.host = "https://api.ubiops.com/v2.1"

api_client = ubiops.ApiClient(configuration)
training = ubiops.Training(api_client)

project_name = 'project_name_example' # str
experiment_name = 'experiment_name_example' # str
timeout = 56 # int (optional)

# Create run
api_response = training.experiment_runs_create(
    project_name=project_name,
    experiment_name=experiment_name,
    data=ubiops.ExperimentRunCreate(
        name="my-run",
        training_code="./train.py"
    ),
    timeout=timeout
)
print(api_response)

# Close the connection
api_client.close()

Authenticate to the Metric Client

When writing custom metrics from your deployment inside UbiOps, you can easily initiate a MetricClient, which gets automatically permission to write metrics. For local testing, you need to create an API token with metric permissions and either pass it using system environment variables or use the authorization parameters.

from ubiops.utils.metrics import MetricClient

# Set environment variables (these are set automatically for you when running inside UbiOps)
# - INT_API_TOKEN: "Token <YOUR_API_TOKEN>"
# - INT_API_URL: "https://api.ubiops.com/v2.1"

project_name = 'project_name_example' # str

metric_client = MetricClient(project_name=project_name)
metric_client.start()

metric_client.log_metric(metric_name="custom.metric", labels={"test": "test"}, value=1.0)

metric_client.stop()
import ubiops
from ubiops.utils.metrics import MetricClient

configuration = ubiops.Configuration()
# Configure API token authorization
configuration.api_key['Authorization'] = 'Token <YOUR_API_TOKEN>'

# Defining host is optional and default to https://api.ubiops.com/v2.1
configuration.host = "https://api.ubiops.com/v2.1"
# Enter a context with an instance of the API client
api_client = ubiops.ApiClient(configuration)

project_name = 'project_name_example' # str

metric_client = MetricClient(project_name=project_name, api_client=api_client)
metric_client.start()

metric_client.log_metric(metric_name="custom.metric", labels={"test": "test"}, value=1.0)

metric_client.stop()