首页 > 解决方案 > 使用带 boto3 的 ExternalQuestion 在 Amazon Mechnical Turk 上创建命中

问题描述

这个问题以前有人问过,但答案只包含了一点点答案。

问题:

如何ExternalQuestion使用 boto3 api 创建命中?

常见问题

也许 externalQuestions 尚未定义,因为它不是 boto3 的一部分:

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

或者,如果您已设法创建ExternalQuestion数据模型,则create_hit可能仍会返回以下错误,说明ExternalQuestion数据类型错误。

botocore.exceptions.ParamValidationError: Parameter validation failed: 
Invalid type for parameter Question, value: <mturk.utils.ExternalQuestion instance at 0x70b2560>, type: <type 'instance'>, valid types: <type 'basestring'>

标签: amazon-web-servicesboto3

解决方案


第一步:创建正确的ExternalQuestion数据模型。

这个答案显示了如何做这第一部分。

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)

然后,我们创建一个ExternalQuestion如下的实例:

 external_question = ExternalQuestion(
     external_url='www.google.com',
     frame_height=2200
 )

步骤 2. 创建命中

现在我们有了命中,我们可以使用create_hitor with提交它create_hit_with_with_typeExternalQuestion但棘手的部分是必须与实例一起提供的 Question 参数,它需要是一个基本字符串。文件对此很模糊。我只是因为这个答案才想出来的。

 client.create_hit_with_hit_type(
     Question = external_question.get_as_xml()
     # all the other required arguments
 )

推荐阅读