Andreas
Andreas

Reputation: 300

How to submit Mechanical Turk ExternalQuestions with boto3

I am trying to programmatically create a question on mechanical turk using boto3, but I seem to be doing something wrong, since the ExternalQuestion data structure that is required for create_hit seems to be missing.

I try to create the HIT like so:

import boto3

#...

client = boto3.client(
    'mturk',
    endpoint_url=endpoint_url,
    region_name=region_name,
    aws_access_key_id=aws_access_key_id,
    aws_secret_access_key=aws_secret_access_key,
)

question = ExternalQuestion(external_url=question_target, frame_height=800)

response = client.create_hit(
        MaxAssignments=10,
        Title='Test',
        Description='This is a test of ExternalQuestion',
        Question=question,
        AssignmentDurationInSeconds=60,
        LifetimeInSeconds=24 * 60 * 60,
        Reward=0.01)

Which fails:

Traceback (most recent call last):
  File "createTask.py", line 21, in <module>
    question = ExternalQuestion(external_url=question_target, frame_height=800)
NameError: name 'ExternalQuestion' is not defined

Any advice on how to proceed is highly appreciated.

Upvotes: 2

Views: 1472

Answers (3)

gbs
gbs

Reputation: 1325

If you're looking for the same class interface as in classic boto, here's a standalone code snippet to imitate it:

class ExternalQuestion:
    """
    An object for constructing an External Question.
    """
    schema_url = "http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2006-07-14/ExternalQuestion.xsd"
    template = '<ExternalQuestion xmlns="%(schema_url)s"><ExternalURL>%%(external_url)s</ExternalURL><FrameHeight>%%(frame_height)s</FrameHeight></ExternalQuestion>' % vars()

    def __init__(self, external_url, frame_height):
        self.external_url = external_url
        self.frame_height = frame_height

    def get_as_params(self, label='ExternalQuestion'):
        return {label: self.get_as_xml()}

    def get_as_xml(self):
        return self.template % vars(self)

Upvotes: 1

Gianni G
Gianni G

Reputation: 21

If you still have the older version of boto installed, the easiest thing is to use the get_as_xml() method of ExternalQuestion:

import boto3
from boto.mturk.question import ExternalQuestion

mturk = boto3.client(
    'mturk',
    endpoint_url='https://mturk-requester-sandbox.us-east-1.amazonaws.com',
    region_name='us-east-1',
    aws_access_key_id='your_access_key',
    aws_secret_access_key='your_secret_key',
)

question = ExternalQuestion("https://example.com/mypage.html", frame_height=600)
new_hit = mturk.create_hit(
    Title='Answer a simple question',
    Description='Help research a topic',
    Keywords='question, answer, research',
    Reward='0.15',
    MaxAssignments=1,
    LifetimeInSeconds=172800,
    AssignmentDurationInSeconds=600,
    AutoApprovalDelayInSeconds=14400,
    Question=question.get_as_xml(),   # <--- this does the trick
)
print "HITID = " + new_hit['HIT']['HITId']

If you take a look at the output of question.get_as_xml(), you will see it's pretty straightforward and you can generate it yourself:

<ExternalQuestion xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2006-07-14/ExternalQuestion.xsd">
   <ExternalURL>https://example.com/mypage.html</ExternalURL>
   <FrameHeight>600</FrameHeight>
</ExternalQuestion>

You will need to make sure to escape characters in your question URL so that it's a valid XML file.

Upvotes: 2

Tyler Cowan
Tyler Cowan

Reputation: 850

This is a direct snip from my production code. I open an XML file which you can get a template for from the requester site and then just ammend it to contain your own javascript and html. I will attach a sample below.

Python

import boto3
region_name = 'us-east-1'
aws_access_key_id = '*********************'
aws_secret_access_key = '*********************'
endpoint_url = 'https://mturk-requester-sandbox.us-east-1.amazonaws.com'

# Uncomment this line to use in production
#endpoint_url = 'https://mturk-requester.us-east-1.amazonaws.com'
client = boto3.client(
    'mturk',
    endpoint_url=endpoint_url,
    region_name=region_name,
    aws_access_key_id=aws_access_key_id,
    aws_secret_access_key=aws_secret_access_key,
)
questionSampleFile = open("K:/" + str(somefile) + ".xml", "r")
questionSample = questionSampleFile.read()

localRequirements = [{
    'QualificationTypeId': '00000000000000000071',
    'Comparator': 'NotIn',
    'LocaleValues': [{
     'Country': 'WF'
   }],
   'RequiredToPreview': True
    }]
xReward = '0.25'
# Create the HIT 
response = client.create_hit(
    MaxAssignments = 1,
    #AutoApprovalDelayInSeconds = 259200,
    #3 days for lifetime
    LifetimeInSeconds = 172800,
    #1 hour to finish the assignment
    AssignmentDurationInSeconds = 5400,
    Reward = xReward,
    Title = 'Enter Missing Data',
    Keywords = 'data entry, typing, inspection',
    Description = 'Edit and Add Data from PDF',
    Question = questionSample,
    QualificationRequirements = localRequirements
)

XML

<HTMLQuestion xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2011-11-11/HTMLQuestion.xsd">
  <HTMLContent><![CDATA[

]]>
  </HTMLContent>
  <FrameHeight>900</FrameHeight>
</HTMLQuestion>

Upvotes: 1

Related Questions