如何通过Mturk API从沙箱上的HIT获得结果

时间:2018-05-18 17:39:59

标签: python boto3 mechanicalturk

我创建了一个XML文件来向MTurk发布问题,HIT在工人沙箱中可见。我的几个朋友甚至提交了对HIT的回复,但是我无法查看这个HIT的结果。

这是我用来发布HIT的代码:

import boto3
MTURK_SANDBOX = 'https://mturk-requester-sandbox.us-east-1.amazonaws.com'
MTURK_PROD = 'https://mturk-requester.us-east-1.amazonaws.com'
mturk = boto3.client('mturk',
   aws_access_key_id = "blah",
   aws_secret_access_key = "blah",
   region_name='us-east-1',
   endpoint_url = MTURK_SANDBOX
)
# Read in the questions.xml file saved in the same directory
question = open(file='E:\\QuestionsXML\\5235319d5aa41337122b28bc.xml',mode='r', encoding='utf-8').read()
new_hit = mturk.create_hit(
Title = 'Map the following question to the paragraph/content where the answer to the question may lie.',
Description = 'Please review the following content to figure out where the answer to the posted question may reside',
Keywords = 'images, quick, labeling',
Reward = '0.00',
MaxAssignments = 1,
LifetimeInSeconds = 1728,
AssignmentDurationInSeconds = 600,
AutoApprovalDelayInSeconds = 14400,
Question = question,
AssignmentReviewPolicy={
'PolicyName':'ScoreMyKnownAnswers/2011-09-01',
'Parameters':[
{'Key':'AnswerKey', 'MapEntries':[{'Key': 'question_1', 'Values':['yes']}]},
{'Key': 'ApproveIfKnownAnswerScoreIsAtLeast', 'Values':['1']},
{'Key': 'RejectIfKnownAnswerScoreIsLessThan', 'Values':['1']},{'Key': 'RejectReason', 'Values':['Sorry, we could not approve your submission as you did not correctly identify the image containing the Flamingo.']},
{'Key': 'ExtendIfKnownAnswerScoreIsLessThan','Values':['1']}
]
})
print("A new HIT has been created. You can preview it here:")
print(new_hit['HIT']['HITGroupId'])
print("https://workersandbox.mturk.com/mturk/preview?groupId=" + str(new_hit['HIT']['HITGroupId']))

此代码提供了以下内容:

A new HIT has been created. You can preview it here:
3X1QANT5IR1WSSCGJ0Y38AZG1ZW2RB
https://workersandbox.mturk.com/mturk/preview?groupId=3X1QANT5IR1WSSCGJ0Y38AZG1ZW2RB

我使用以下代码检索结果:

mturk = boto3.client('mturk',
   aws_access_key_id = "blah",
   aws_secret_access_key = "blah",
   region_name='us-east-1',
   endpoint_url = MTURK_SANDBOX
)
# You will need the following library
# to help parse the XML answers supplied from MTurk
# Install it in your local environment with
# pip install xmltodict
import xmltodict
# Use the hit_id previously created
hit_id = '3X1QANT5IR1WSSCGJ0Y38AZG1ZW2RB'
# We are only publishing this task to one Worker
# So we will get back an array with one item if it has been completed
worker_results = mturk.list_assignments_for_hit(HITId=hit_id, AssignmentStatuses=['Submitted'])

但它说找不到HIT:

---------------------------------------------------------------------------
RequestError                              Traceback (most recent call last)
<ipython-input-22-e82852bb5415> in <module>()
     14 # We are only publishing this task to one Worker
     15 # So we will get back an array with one item if it has been completed
---> 16 worker_results = mturk.list_assignments_for_hit(HITId=hit_id, AssignmentStatuses=['Submitted'])

~\Anaconda3\lib\site-packages\botocore\client.py in _api_call(self, *args, **kwargs)
    312                     "%s() only accepts keyword arguments." % py_operation_name)
    313             # The "self" in this scope is referring to the BaseClient.
--> 314             return self._make_api_call(operation_name, kwargs)
    315 
    316         _api_call.__name__ = str(py_operation_name)

~\Anaconda3\lib\site-packages\botocore\client.py in _make_api_call(self, operation_name, api_params)
    610             error_code = parsed_response.get("Error", {}).get("Code")
    611             error_class = self.exceptions.from_code(error_code)
--> 612             raise error_class(parsed_response, operation_name)
    613         else:
    614             return parsed_response

RequestError: An error occurred (RequestError) when calling the ListAssignmentsForHIT operation: Hit 3X1QANT5IR1WSSCGJ0Y38AZG1ZW2RB does not exist. (1526664623533 s)

请让我知道如何解决这个问题。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

解决方案很简单:

mturk = boto3.client('mturk',
   aws_access_key_id = "blah",
   aws_secret_access_key = "blah",
   region_name='us-east-1',
   endpoint_url = MTURK_SANDBOX
)
# You will need the following library
# to help parse the XML answers supplied from MTurk
# Install it in your local environment with
# pip install xmltodict
import xmltodict
# Use the hit_id previously created
hit_id = new_hit['HIT']['HITId']
# We are only publishing this task to one Worker
# So we will get back an array with one item if it has been completed
worker_results = mturk.list_assignments_for_hit(HITId=hit_id, AssignmentStatuses=['Submitted'])