Create and upload control and general tasks to the pool.
Connect the Toloka-Kit library to your script.
import toloka.client as toloka
Replace the sample API key with your own one.
toloka_client = toloka.TolokaClient('PlaceYourRealApiKey_Here', 'PRODUCTION');
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')
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)
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]
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]
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
import toloka.client as tolokatoloka_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))
Last updated: February 7, 2023