Getting started
You can use the Label Studio Python SDK to make annotating data a more integrated part of your data science and machine learning pipelines. This software development kit (SDK) lets you call the Label Studio API directly from scripts using predefined classes and methods.
With the Label Studio Python SDK, you can perform the following tasks in a Python script:
- Authenticate to the Label Studio API
- Create a Label Studio project, including setting up a labeling configuration.
- Import tasks.
- Manage pre-annotated tasks and model predictions.
- Connect to a cloud storage provider, such as Amazon S3, Microsoft Azure, or Google Cloud Services (GCS), to retrieve unlabeled tasks and store annotated tasks.
- Manage labeling jobs by creating custom filters and ordering for tasks based on parameters that you specify.
- Export annotations from Label Studio.
Install
Install the Label Studio SDK using pip:
or
Authentication
In your Python script, do the following:
- Import the SDK.
- Define your API key and Label Studio URL (API key is available at Account page).
- Connect to the API.
Create a Project
After you connect to the Label Studio API, you can create a project in Label Studio using the SDK.
Specify the project title and the labeling configuration. Choose your labeling configuration based on the type of labeling that you wish to perform. See the available templates for Label Studio projects, or set a blank configuration with <View></View>
.
For example, create a text classification project in your Python code:
label_config
is XML string that represents the labeling interface with object and control tags.
For more about what you can do with the project module of the SDK, see the project module SDK reference.
Import Tasks
You can import tasks from your script using the Label Studio Python SDK client.
For a specific project created, you can import tasks in Label Studio JSON format or connect to cloud storage providers and import image, audio, or video files directly.
Add Model Predictions
You can add predictions to existing tasks in Label Studio in your Python script.
For an existing simple image classification project, you can do the following to add predictions of “Dog” for image tasks that you retrieve:
For another example, see the Jupyter notebook example of importing pre-annotated data.
Managing Labeling Jobs
You can also use the SDK to control how tasks appear in the data manager to annotators or reviewers. You can create custom filters and ordering for the tasks based on parameters that you specify with the SDK. This lets you have more granular control over which tasks in your dataset get labeled or reviewed, and in which order.
Create a batch of tasks to annotate
For example, you can create a filter to prepare tasks to be annotated. For example, if you want annotators to focus on tasks in the first 1000 tasks in a dataset that contain the word “possum” in the field “text” in the task data, do the following:
Most often it is very useful to create a view with annotated tasks to review:
To create a filtered tasks view, use the following code:
If will be displayed in the data manager as tab with Tasks Sample
name.
Export Annotations
Run the following code to export annotations from project’s tab
you created in the previous step:
Read more about export formats in the Label Studio SDK documentation.
SDK versions and compatibility
In June 2024, we released SDK 1.0. The previous SDK (version < 1) is deprecated and no longer supported. We recommend upgrading to the latest version.
If you still want to use the older version, you can install it using pip install "label-studio-sdk<1"
.
You can also check out an older branch version in the GitHub repository:
Or you can simply modify you code to change the import stream as follows:
If you’re looking for the documentation for the older version, you can find it here.
Advanced
Handling Errors
If you encounter an error while using the Label Studio Python SDK, you can catch the error and handle it in your script.
Annotations are exported in the format specified in the Label Studio JSON format.
Timeouts
By default, requests time out after 60 seconds. You can configure this with a timeout option at the client or request level.
Custom HTTP client
You can override the httpx client to customize it for your use-case. Some common use-cases include support for proxies and transports.