Thanks to visit codestin.com
Credit goes to api.labelstud.io

Import Tasks

You can import tasks in Label Studio JSON format or connect to cloud storage providers and import image, audio, or video files directly.

Note the following:

  • You must know your project ID. You can find this in the URL when viewing a Label Studio project (e.g. https://app.humansignal.com/projects/<PROJECT_ID>) or you can use the API.
  • This example imports data using JSON format. The key for the data field should match the variable you used in your labeling config.
  • For information on finding your API key and base URL, see Authenticate and Connect to the API.

For example, when we created the project, we specified the following labeling config:

1<View>
2 <Header value="Choose text sentiment:"/>
3 <Text name="text" value="$text"/>
4 <Choices name="sentiment" toName="text" choice="single">
5 <Choice value="Positive"/>
6 <Choice value="Negative"/>
7 <Choice value="Neutral"/>
8 </Choices>
9</View>

So our imported data must use text so that the value=$text variable can find it.

Create a single task

You can import a single labeling task into your project using the tasks.create method:

1# Define the URL where Label Studio is accessible
2LABEL_STUDIO_URL = 'YOUR_BASE_URL'
3# API key is available at the Account & Settings page in Label Studio UI
4LABEL_STUDIO_API_KEY = 'YOUR_API_KEY'
5# Replace with your project ID
6PROJECT_ID=1
7
8from label_studio_sdk import LabelStudio
9
10client = LabelStudio(base_url=LABEL_STUDIO_URL, api_key=LABEL_STUDIO_API_KEY)
11
12task = client.tasks.create(
13 project=PROJECT_ID,
14 data={"text": "I absolutely love this!"} # key 'text' matches value="$text" in your config
15)
16print("New task ID:", task.id)

Create multiple tasks

To create multiple tasks at once in a project, use the import_tasks method:

1LABEL_STUDIO_URL = 'YOUR_BASE_URL'
2LABEL_STUDIO_API_KEY = 'YOUR_API_KEY'
3PROJECT_ID=1 # replace with your project ID
4
5from label_studio_sdk import LabelStudio
6
7client = LabelStudio(base_url=LABEL_STUDIO_URL, api_key=LABEL_STUDIO_API_KEY)
8
9# Tasks to import — each item must match variables used in your labeling config (e.g., $text)
10tasks = [
11 {"text": "Hello world"},
12 {"text": "Hello Label Studio"},
13 {"text": "What a beautiful day"},
14]
15
16# Import tasks
17resp = client.projects.import_tasks(
18 id=PROJECT_ID,
19 request=tasks,
20 return_task_ids=True,
21)
22print(resp)

Create multiple tasks with preannotations

You can also import predictions together with tasks as pre-annotated tasks. The SDK offers several ways that you can import pre-annotations into Label Studio.

One way is to import tasks in a simple JSON format, where one key in the JSON identifies the data object being labeled, and the other is the key containing the prediction. In this case, that is sentiment because that is the name we used in our labeling configuration.

For example:

1LABEL_STUDIO_URL = 'YOUR_BASE_URL'
2LABEL_STUDIO_API_KEY = 'YOUR_API_KEY'
3PROJECT_ID=1 # replace with your project ID
4
5from label_studio_sdk import LabelStudio
6
7client = LabelStudio(base_url=LABEL_STUDIO_URL, api_key=LABEL_STUDIO_API_KEY)
8
9client.projects.import_tasks(
10 id=PROJECT_ID,
11 request=[
12 {"text": "Opossums are the cutest!", "sentiment": "Positive"},
13 {"text": "Goodbye Label Studio", "sentiment": "Negative"},
14 ],
15 preannotated_from_fields=["sentiment"]
16)

See also Add model predictions.

Read more about importing pre-annotations in the Label Studio documentation.