Skip to main content
https://img.shields.io/travis/etscrivner/turkleton.svg https://coveralls.io/repos/etscrivner/turkleton/badge.svg?branch=master https://img.shields.io/pypi/v/turkleton.svg Documentation Status

Dead simple Python interfaces for Amazon Mechanical Turk.

Installation

Simply use pip to download the package from PyPI

$ pip install turkleton

Features

The existing Python APIs for Mechanical Turk are thin wrappers at best - we can do better.

Turkleton aims to leverage the expressive powers of Python to improve the whole situation. While still under active development, the main features are:

  • Simple interface for defining tasks from pre-built layouts.

  • Simple interface for defining schema of assignment results.

  • Easily upload tasks in batches.

  • Easily download and validate assignments.

Examples

In turkleton there are several objects to be aware of: Tasks, HITs, and Assignments. A Task is a template from which HITs are created. A HIT corresponds to HIT in the Amazon Mechanical Turk API and represents an uploaded Task. Assignments are contained within HITs. An individual Assignment represents the set of answers submitted by a single worker. A HIT can have many Assignments.

Setting Up Your Connection

Turkleton uses a per-process global connection. It should be initialized before you attempt to upload or download anything. You can initialize it like so:

from turkleton import connection
connection.setup(AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY)

That’s it!

Creating A Task And Uploading It

To define a HIT you create a Task representing the template of the assignment you want a worker to complete. For example:

import datetime

from turkleton.assignment import task

class MyTask(task.BaseTask):
    __layout_id__ = 'MY LAYOUT ID'
    __reward__ = 0.25
    __title__ = 'Guess How Old From Picture'
    __description__ = 'Look at a picture and guess how old the person is.'
    __keywords__ = ['image', 'categorization']
    __time_per_assignment__ = datetime.timedelta(minutes=5)

Here we’ve created a Task from an existing layout. Now that we’ve defined our task we can easily upload HITs by filling out the layout parameters:

task = MyTask(image_url='http://test.com/img.png', first_guess='29')
hit = task.upload(batch_id='1234')

This will create a new assignment from the task template and upload it to Mechanical Turk. The optional batch_id parameter allows you to set the annotation for the task to an arbitrary string that you can use to retrieve tasks later in batches.

You can upload many tasks in a loop easily as follows:

for image_url in all_image_urls:
    MyTask.create_and_upload(
        image_url=image_url, first_guess='29', batch_id='1234'
    )

If you’d like to leave off the batch id you can also use the context manager:

with task.batched_upload(batch_id='1234')
    for image_url in all_image_urls:
       MyTask.create_and_upload(image_url=image_url, first_guess='29')

Downloading The Results

To download results for a HIT you first need to define an assignment. The assignment defines what values are expected and their types. These are used to automatically parse answers to the various questions:

from turkleton.assignment import assignment
from turkleton.assignment import answers

class MyAssignment(assignment.BaseAssignment):
    categories = answers.MultiChoiceAnswer(question_name='Categories')
    notes = answers.TextAnswer(question_name='AdditionalNotes', default='')
    does_not_match_any = answers.BooleanAnswer(
        question_name='DoesNotMatchAnyCategories', default=False
    )

You can then download all of the HITs in a given batch as follows:

from turkleton.assignment import hit
reviewable_hits = hit.get_reviewable_by_batch_id('1234')

Each HIT may then have multiple assignments associated with it. You can download the assignments, review them, and then dispose of the HIT as follows:

for each in MyAssignment.get_by_hit_id(hit.hit_id):
    print('{} - {} - {}'.format(each.categories, each.notes, each.does_not_match_any))
    if is_valid_assignment(each):
        each.accept('Good job!')
    else:
        each.reject('Assignment does not follow instructions.')
hit.dispose()

History

1.2.1 (2015-06-15)

  • Bugfix, error when retrieving hits by batch id

1.2.0 (2015-06-11)

  • More answer types

  • Bugfix where answers retained single value

1.1.0 (2015-06-06)

  • Improvements to connection management

  • More convenient syntax for uploading batches

1.0.0 (2015-06-05)

  • Major version revisions and updates

0.1.0 (2015-01-11)

  • First release on PyPI.

Metadata

Release files for turkleton 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for turkleton 1.2.1
File Size Uploaded Provenance Actions
turkleton-1.2.1.tar.gz 23.9 kB Without provenance Details for turkleton-1.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for turkleton 1.2.1
File Interpreter ABI Platform Actions
turkleton-1.2.1-py2.py3-none-any.whl Python 2, Python 3 none any Details for turkleton-1.2.1-py2.py3-none-any.whl

Total release size: 37.7 kB

Release files / turkleton-1.2.1.tar.gz

Download URL turkleton-1.2.1.tar.gz
Size 23.9 kB
Tags Source
SHA-256 checksum
How to use checksums
29a634ad943e8e404f90cb669685b02c697b9652f5c99200d4da3b05969fde05
BLAKE2b-256 checksum
How to use checksums
f04e49052224e9798853446651229f53d667f872b3740ef616c30a9787d4f31c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / turkleton-1.2.1-py2.py3-none-any.whl

Download URL turkleton-1.2.1-py2.py3-none-any.whl
Size 13.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
0218ab16a1c3677d2e33de90db85eedcd9ddb5a37ef0e8c7e74b387f6f1a0827
BLAKE2b-256 checksum
How to use checksums
1a50fda8bafac681e139c8d29ecc15a8db53a64cad336e77a52a52c434d56b8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for turkleton 1.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page