Upload tasks

Create and upload control and general tasks to the pool.

Steps to follow

1. Import Toloka-Kit

Connect the Toloka-Kit library to your script.

import toloka.client as toloka

2. Instantiate TolokaClient

Replace the sample API key with your own one.

toloka_client = toloka.
TolokaClient
('PlaceYourRealApiKey_Here', 'PRODUCTION');

3. Create control task

In this recipe we create a control task first. The main difference between a control and general task in Toloka is that a control task has a known answer to it. For this answer, the known_solutions parameter of the Task class object is used.

Find out the ID of the pool where you want to upload the created tasks. For control tasks, set infinite_overlap to True.

control_task = toloka.task.
Task
(
input_values={'image': 'https://example.com/image_example.png'},
known_solutions=[
toloka.task.
BaseTask
.
KnownSolution
(output_values={'result': 'cat'})
],
infinite_overlap=True,
pool_id='1238218'
)
Note

You need to know what values are allowed for the output data (output_values). You set the output data when you create a project.

In the platform, we create the control task using the create_task() method.

toloka_client.
create_task
(control_task)

4. Specify image URLs and form tasks

Now, let's enumerate the URLs of the images in an array. We are going to use these images for our general tasks.

image_urls = [
'https://example.com/image_1.png',
'https://example.com/image_2.png',
'https://example.com/image_3.png'
]

Populate tasks with images, use the same pool_id as at step 3.

tasks = [toloka.task.
Task
(input_values={'image': url}, pool_id='1238218')
for url in image_urls]

5. Upload tasks to Toloka

This actually uploads tasks to Toloka.

result = toloka_client.
create_tasks
(tasks,

allow_defaults=True, skip_invalid_items=False)

The task uploading progress will look like this.

100%|████████████████████████████████████████████| 100/100 [00:01<00:00, 52.35it/s]

6. Print number of created tasks

The create_tasks() request will return the TaskBatchCreateResult class object. You can use its attributes to print the information you need.

print(len(result.items))

You should get an output with the number of the created tasks which looks like this.

3

Complete code: Upload tasks

import toloka.client as toloka
toloka_client = toloka.TolokaClient('PlaceYourRealApiKey_Here', 'PRODUCTION')
control_task = toloka.task.Task(
input_values={'image': 'https://example.com/image_example.png'},
known_solutions=[
toloka.task.BaseTask.KnownSolution(output_values={'result': 'cat'})
],
infinite_overlap=True,
pool_id='1238218'
)
toloka_client.create_task(control_task)
image_urls = [
'https://example.com/image_1.png',
'https://example.com/image_2.png',
'https://example.com/image_3.png'
]
tasks = [toloka.task.Task(input_values={'image': url}, pool_id='1238218')
for url in image_urls]
result = toloka_client.create_tasks(tasks,
allow_defaults=True, skip_invalid_items=False)
print(len(result.items))
List of classes and methods used in this recipe

See also

Last updated: February 7, 2023

Toloka-Kit
OverviewGetting API keyQuick start
Recipes
Reference
toloka.client
toloka.async_client
toloka.autoquality [autoquality]
toloka.metrics
toloka.streaming