Toloka-Kit Python Library

repository·main·Indexed 42 days ago

https://github.com/toloka/toloka-kit

A Python library providing a high-level, object-oriented interface for the Toloka API to build and automate human-in-the-loop machine learning pipelines. It supports synchronous and asynchronous execution, streaming, and AutoQuality. The library includes specialized examples for Computer Vision, NLP, Audio Analysis, Ranking, and Spatial Crowdsourcing, with seamless integration for Jupyter notebooks and Graphite metrics.

Tokens
59.4K
Snippets
140
Records
192
Agent score
49%

What's inside Toloka-Kit

  1. Overview of Toloka-kit usage patterns

    main

    Toloka-kit provides code-based examples for automating data collection, markup, aggregation, and other data-centric applications. Using these examples allows you to:

    • Reuse projects via code: Avoid repetitive manual configuration in the Toloka interface by copying and pasting code snippets.
    • Unified environment: Run ML model training and data labeling projects within the same development environment.
    • Open-source extensibility: Leverage and contribute to community-driven code for common tasks.
  2. Core features of Toloka-Kit

    main

    Toloka-Kit is a Python library designed to simplify integration with the Toloka API for building automated human-in-the-loop ML pipelines. Key capabilities include:

    • Pythonic Entities: Toloka entities are represented as Python classes, allowing you to work with objects instead of raw JSON.
    • Execution Modes: Supports both synchronous and asynchronous (async/await) execution.
    • Streaming Support: Enables building complex pipelines that send and receive data in real time (e.g., passing data between a labeling project and a validation project).
    • AutoQuality: A feature that automatically identifies the best quality control rules for your specific project.
    • Jupyter Integration: Designed to work seamlessly within Jupyter notebooks.
  3. Explore Toloka-kit usage examples by domain

    main

    Toloka-kit provides a wide range of practical examples categorized by domain to help you implement various crowdsourcing tasks. You can find specialized notebooks for:

    • Computer Vision (CV): Image collection, classification, object detection (bounding boxes), HTR (handwriting) gathering, video collection, and text recognition (OCR).
    • Natural Language Processing (NLP): Text classification (e.g., clickbait detection), Question Answering (SQuAD), sentiment analysis, and intent classification.
    • Audio Analysis: Audio collection (TTS/ASR), audio classification (gender detection), and audio transcription.
    • Ranking: Side-by-side image comparison and preference aggregation.
    • Spatial Crowdsourcing: Collecting location-based data (e.g., monitoring physical objects).
    • Surveys: Simple survey implementation for data collection.
    • Pipelines: Integrating Toloka with ML models using Prefect or building streaming pipelines.
    • Relevance: Search and ad relevance rating.
    • Benchmarks: Pre-configured setups for standard datasets like CINIC-10 (CV) and IMDB (NLP).
    • Metrics: Visualizing data via Jupyter dashboards or sending metrics to Graphite.
  4. Use Streaming to build an automated annotation pipeline

    main

    Streaming allows you to react to events in real-time. For example, you can automatically create verification tasks as soon as a detection task is submitted. This is done using AssignmentsObserver and a Pipeline.

    1. Define a handler (e.g., DetectionSubmittedHandler) that implements __call__ to process AssignmentEvents.
    2. Register an AssignmentsObserver for each pool.
    3. Run the Pipeline with a Storage backend (like JSONLocalStorage) to ensure progress is saved.
    from toloka.streaming import AssignmentsObserver, Pipeline
    from toloka.streaming.storage import JSONLocalStorage
    
    # 1. Define handler
    class DetectionSubmittedHandler:
        def __init__(self, client, verification_pool_id):
            self.client = client
            self.verification_pool_id = verification_pool_id
    
        def __call__(self, events: List[AssignmentEvent]) -> None:
            # Logic to create verification tasks from detection results
            ...
    
    # 2. Setup Pipeline
    detection_observer = AssignmentsObserver(toloka_client, detection_pool.id)
    detection_observer.on_submitted(DetectionSubmittedHandler(toloka_client, verification_pool.id))
    
    storage = JSONLocalStorage('./storage/')
    pipeline = Pipeline(storage=storage)
    pipeline.register(detection_observer)
    
    # 3. Run
    await pipeline.run()
    from toloka.streaming import AssignmentsObserver, Pipeline
    from toloka.streaming.storage import JSONLocalStorage
    
    detection_observer = AssignmentsObserver(toloka_client, detection_pool.id)
    detection_observer.on_submitted(DetectionSubmittedHandler(toloka_client, verification_pool.id))
    
    storage = JSONLocalStorage('./storage/')
    pipeline = Pipeline(storage=storage)
    pipeline.register(detection_observer)
    
    await pipeline.run()
  5. Create a Project with a Side-by-Side Interface

    main

    A project defines the task structure and the visual interface. You can use the template_builder (aliased as tb) to construct complex layouts like SideBySideLayoutV1.

    Steps to create a project:

    1. Define the main_interface using layout components (e.g., ImageViewV1) and controls (e.g., ButtonRadioGroupFieldV1).
    2. Add plugins like HotkeysPluginV1 to improve performer efficiency.
    3. Define the input_specification and output_specification using field types like UrlSpec() and StringSpec().
    4. Wrap the configuration in a TemplateBuilderViewSpec.
    5. Assign the task_spec to the project and call toloka_client.create_project(project).
    import toloka.client.project.template_builder as tb
    import toloka.client as toloka
    
    # 1. Define Interface
    main_interface = tb.SideBySideLayoutV1(
        items=[
            tb.ImageViewV1(url=tb.InputData(path='image_left'), full_height=True),
            tb.ImageViewV1(url=tb.InputData(path='image_right'), full_height=True),
        ],
        controls=tb.ButtonRadioGroupFieldV1(
            data=tb.OutputData(path='result'),
            label='Which icon do you like more?',
            options=[
                tb.GroupFieldOption(label='Left', value='LEFT'),
                tb.GroupFieldOption(label='Right', value='RIGHT'),
                tb.GroupFieldOption(label='Loading error', value='ERROR'),
            ]
        )
    )
    
    # 2. Add Plugins
    hot_keys_plugin = tb.HotkeysPluginV1(
        key_0=tb.SetActionV1(data=tb.OutputData(path='result'), payload='ERROR'),
        key_1=tb.SetActionV1(data=tb.OutputData(path='result'), payload='LEFT'),
        key_2=tb.SetActionV1(data=tb.OutputData(path='result'), payload='RIGHT'),
    )
    
    # 3. Build View Spec
    project_interface = toloka.project.view_spec.TemplateBuilderViewSpec(
        config=tb.TemplateBuilder(
            view=main_interface,
            plugins=[hot_keys_plugin],
        )
    )
    
    # 4. Set Data Specs and Task Spec
    input_specification = {
        'image_left': toloka.project.field_spec.UrlSpec(),
        'image_right': toloka.project.field_spec.UrlSpec(),
    }
    output_specification = {'result': toloka.project.field_spec.StringSpec()}
    
    project = toloka.Project(
        assignments_issuing_type='AUTOMATED',
        public_name='Which icon do you like more?',
        public_description='Look at the icons and decide which one you like more.',
    )
    
    project.task_spec = toloka.project.task_spec.TaskSpec(
        input_spec=input_specification,
        output_spec=output_specification,
        view_spec=project_interface,
    )
    
    # 5. Create on Toloka
    project = toloka_client.create_project(project)
  6. Install Toloka-Kit

    main

    Install the core package using pip. Note that starting from v1.0.0, only the core version is installed by default. If you need additional features like pandas support, AutoQuality, or S3 integration, you must install them separately or use the [all] extra.

    To install the core package:

    pip install toloka-kit

    To install all optional dependencies:

    pip install toloka-kit[all]

    To install specific optional dependencies:

    pip install toloka-kit[pandas,autoquality,s3,zookeeper,jupyter-metrics]
    pip install toloka-kit
  7. Implement Audio Analysis tasks

    main

    The library supports audio-centric workflows:

    • Audio Collection: Gather voice recordings of specific texts for ASR (Automatic Speech Recognition) or TTS (Text-to-Speech) training.
    • Audio Classification: Classify recordings based on attributes like speaker gender.
    • Audio Transcription: Obtain text transcriptions from audio files by having performers type what they hear.
  8. Prerequisites for using Toloka-Kit

    main

    Before using the library, ensure you meet the following requirements:

    • Python version: v3.8 or higher.
    • Toloka Account: You must be registered as a requester and have a topped-up account.
    • Authentication: You must have an OAuth token set up to access the Toloka API.
  9. Implement Computer Vision tasks

    main

    The repository contains several specialized examples for Computer Vision workflows:

    • Image Collection: Collect datasets by asking performers to take photos and specify species.
    • Image Classification: Perform binary or multi-class classification on image datasets.
    • Object Detection: Annotate images using rectangular bounding boxes.
    • Text Recognition (OCR): Extract text/digits from images (e.g., water meters).
    • Video Collection: Collect video recordings of specific gestures or actions.

    Most examples are available as Google Colab notebooks for immediate experimentation.

  10. Learn the basics of Toloka-kit

    main
    For developers new to the project, the 0.getting_started/0.learn_the_basics example is the recommended starting point. It covers the fundamental concepts of working with Toloka and toloka-kit using a simple classification project (cats vs. dogs) to explain the workflow.
  11. Set up Quality Control in a Pool

    main

    You can add automated actions to restrict performers based on specific conditions. Common collectors include:

    • Income: Check performer's income.
    • SkippedInRowAssignments: Check if they skip too many tasks in a row.
    • MajorityVote: Check if their answers deviate from the majority.
    • GoldenSet: Check accuracy against known correct answers (Control tasks).

    Actions are typically toloka.actions.RestrictionV2 which can restrict a performer from a scope='PROJECT' for a specific duration.

    # Example: Restrict if Golden Set accuracy is too low
    pool.quality_control.add_action(
        collector=toloka.collectors.GoldenSet(),
        conditions=[
            toloka.conditions.GoldenSetCorrectAnswersRate < 60.0,
            toloka.conditions.GoldenSetAnswersCount >= 3
        ],
        action=toloka.actions.RestrictionV2(
            scope='PROJECT',
            duration=10,
            duration_unit='DAYS',
            private_comment='Golden set'
        )
    )
  12. Prepare and Upload Tasks (including Golden Set)

    main

    Tasks are the individual units of work.

    1. Regular Tasks: Use toloka.Task(pool_id=pool.id, input_values={...}).
    2. Golden Set (Control Tasks): These are tasks where the answer is known. Use known_solutions to provide the ground truth and set infinite_overlap=True so they can be used repeatedly to check quality.
    3. Upload: Use toloka_client.create_tasks(list_of_tasks, allow_defaults=True) to upload them to the pool.
    # Create Control Tasks (Golden Set)
    golden_tasks = [
        toloka.Task(
            pool_id=pool.id,
            input_values={'image': row['url']},
            known_solutions=[
                toloka.task.BaseTask.KnownSolution(output_values={'result': row['label']})
            ],
            infinite_overlap=True,
        )
        for i, row in golden_dataset.iterrows()
    ]
    
    # Create Regular Tasks
    tasks = [
        toloka.Task(pool_id=pool.id, input_values={'image': url})
        for url in task_dataset['url']
    ]
    
    # Upload
    created_tasks = toloka_client.create_tasks(golden_tasks + tasks, allow_defaults=True)