Ejemplo n.º 1
0
def createHits(question, answers, params):
	if SANDBOX:
		mturk_url = 'mechanicalturk.sandbox.amazonaws.com'
		preview_url = 'https://workersandbox.mturk.com/mturk/preview?groupId='
	else:
		mturk_url = 'mechanicalturk.amazonaws.com'
		preview_url = 'https://mturk.com/mturk/preview?groupId='

	#Create Hit Form Structure	
	overview  = Overview()
	overview.append_field('Title', 'We want to know the crowds opinion!')
	overview.append(FormattedContent('<a href="http://programthecrowd.com/">Visit us here</a>'))
	questionContent = QuestionContent()
	questionContent.append_field('Title', question);
	answerChoices = SelectionAnswer(min=1, max=1, style='checkbox', selections=answers, type='text', other=False)
	q = Question(identifier='Help', content=questionContent, answer_spec=AnswerSpecification(answerChoices), is_required=True)
	questionForm = QuestionForm();
	questionForm.append(overview)
	questionForm.append(q)
	hitIdList = []
	global conn
	# key = params['aws_access_key']
	# secret = params['aws_secret_key']
	conn = MTurkConnection(aws_access_key_id='AKIAJBTEJI2RGTJH7OBA', aws_secret_access_key='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', host=mturk_url)
	
	#For Loop to create and post hits
	for i in range(0, NUMBER_OF_HITS):
		create_hit_rs = conn.create_hit(questions=questionForm, lifetime=LIFETIME, max_assignments=NUMBER_OF_ASSIGNMENTS, title=TITLE, keywords=KEYWORDS, reward=REWARD, duration=DURATION, approval_delay=APPROVAL_DELAY, annotation=DESCRIPTION)
		#print(preview_url + create_hit_rs[0].HITTypeId)
		#print("HIT ID: " + create_hit_rs[0].HITId)
		hitIdList.append(create_hit_rs[0].HITId);

	return hitIdList
Ejemplo n.º 2
0
    def RetrieveHIT(self, sandbox = 'false'):
        """
        RetrieveHit retrieves the HIT assigned to this object and parses its data into local attributes for later retrieval.

        If the value of sandbox is 'true' then the HIT will be retrieved from the Sandbox, otherwise it will be accessed from
        the standard AWS servers.
        """

        if sandbox is 'true':
            self.host = 'mechanicalturk.sandbox.amazonaws.com'

	conn = MTurkConnection(host = self.host, aws_access_key_id = self.AWS_KEY, aws_secret_access_key = self.AWS_SECRET)
        retrieved_hit_list = conn.get_hit(hit_id = self.hit_id)
	retrieved_hit = retrieved_hit_list[0]

	assignments_list = conn.get_assignments(hit_id = self.hit_id, page_size = retrieved_hit.MaxAssignments)

	returned_data = []

	for i in assignments_list:
		this_assignment = []
		for j in i.answers[0]:
			qa_pair = [j.QuestionIdentifier, j.SelectionIdentifier]
			this_assignment.append(qa_pair)
		returned_data.append(this_assignment)
	return returned_data
Ejemplo n.º 3
0
def getHITType():
    # Changing this will add another hit type and might mess up later fetches...
    # Only change if you know what you are doing...

    _mtc = MTurkConnection(host=_host)

    _title = "Select Person-Object Interactions in Images"
    _description = "Please click on all the objects (or other people) that the highlighted person is interacting with."
    _reward = _mtc.get_price_as_price(0.10)
    _duration = 60 * 10
    _keywords = "person, people, image, images, object, objects, actions, interactions"
    _approval_delay = 60 * 60 * 24 * 5
    _qualifications = Qualifications()
    _qualifications.add(
        PercentAssignmentsApprovedRequirement('GreaterThanOrEqualTo',
                                              98,
                                              required_to_preview=True))
    _qualifications.add(
        NumberHitsApprovedRequirement('GreaterThanOrEqualTo',
                                      100,
                                      required_to_preview=True))
    _qualifications.add(
        LocaleRequirement('EqualTo', 'US', required_to_preview=True))

    return _mtc.register_hit_type(title=_title,
                                  description=_description,
                                  reward=_reward,
                                  duration=_duration,
                                  keywords=_keywords,
                                  approval_delay=_approval_delay,
                                  qual_req=_qualifications)
Ejemplo n.º 4
0
def getReviewableHITs( verbose = True ):

    mtc = MTurkConnection( host = _host )
    hitType = getHITType()[0]
    
    page_size = 100
    # this gets the first page and allows to check how many other pages
    hits = mtc.get_reviewable_hits( page_size = page_size )

    total_pages = float( hits.TotalNumResults ) / page_size
    int_total = int( total_pages )
    if( total_pages - int_total > 0 ):
        total_pages = int_total + 1
    else:
        total_pages = int_total
            
    if verbose:
        print "Total Reviewable HITs: [%s]" % hits.TotalNumResults
        print "Total Number of Pages: [%i]" % total_pages
   
    # first page was already retrieved
    pn = 1
    if verbose:
        print " -> request page [%i]" % pn    

    while pn < total_pages:
        pn = pn + 1
        if verbose:
            print " -> request page [%i]" % pn        
        temp_hits = mtc.get_reviewable_hits( hit_type=hitType.HITTypeId, page_size=page_size, page_number=pn )
        # extend the hit list
        hits.extend(temp_hits)
        
    return hits
Ejemplo n.º 5
0
 def get(self):
     access_key = self.request.get(argument_name='accessKey', default_value=None)
     secret_key = self.request.get(argument_name='secretKey', default_value=None)
     question_id = self.request.get(argument_name='questionId', default_value=None)
     if access_key == None or secret_key == None or question_id == None:
         self.error(400)
         return
     
     entity = Question.get_by_id(ids=int(question_id))
     if entity == None:
         self.error(404)
         return
     
     connection = MTurkConnection(aws_access_key_id=access_key,
                                  aws_secret_access_key=secret_key,
                                  host='mechanicalturk.amazonaws.com')
     assignment_result_set = connection.get_assignments(hit_id=entity.hit_id)
     answers = []
     try:
         for assignment in assignment_result_set:
             for answer_result_set in assignment.answers:
                 for answer in answer_result_set:
                     for field in answer.fields:
                         if field[0] == 'text':
                             answers.append(field[1])
     except:
         self.error(500)
         return
     
     self.response.headers['Content-Type'] = 'application/json'
     self.response.out.write(simplejson.dumps({'answers': answers}, indent=4))
Ejemplo n.º 6
0
def create_hits(batch, num_assignments, duration, reward, examples):

	"""
	Creates HITs and posts on Mechanical Turk for a batch of pairs of strings to be matched

	Args:
		batch (list) : List of pairs of strings to be matched
		num_assignments (int) : Number of assignments for each HIT
		duration (float) : Time for which a HIT is to be posted
		reward (float) : Reward for each correct answer
		examples (tuple) : Input examples for which HTML is to be generated
	"""
	question_form  = create_question(batch, examples)

	mtc = MTurkConnection(aws_access_key_id = aws_parameters['access_key'],
                      aws_secret_access_key = aws_parameters['secret_key'],
                      debug = 1, 
                      host = DEV_HOST)

	mtc.create_hit(questions = question_form,
           max_assignments = num_assignments,
           title = title,
           description = description,
           keywords = keywords,
           duration = duration,
           reward = reward)
Ejemplo n.º 7
0
def main():
    # parse arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('keys_file')
    parser.add_argument('--production', default=False, action='store_true')
    args = parser.parse_args()

    # read keys
    access_key_id, secret_key = None, None

    with open(args.keys_file, 'r') as keys:
        for line in keys.xreadlines():
            items = [xx.strip() for xx in line.split('=')]
            if items[0] == 'AWSAccessKeyId':
                access_key_id = items[1]
            elif items[0] == 'AWSSecretKey':
                secret_key = items[1]

    if not access_key_id or not secret_key:
        raise RuntimeError('Invalid keys file format.')

    # set up URLs
    if args.production:
        mturk_url = 'mechanicalturk.amazonaws.com'
        preview_url = 'https://www.mturk.com/mturk/preview?groupId='
    else:
        print 'SANDBOX'
        mturk_url = 'mechanicalturk.sandbox.amazonaws.com'
        preview_url = 'https://workersandbox.mturk.com/mturk/preview?groupId='

    # connect
    connection = MTurkConnection(aws_access_key_id=access_key_id,
                                 aws_secret_access_key=secret_key,
                                 host=mturk_url)

    # make the HIT
    question = ExternalQuestion(
        external_url=
        'https://kirbpowell.github.io/crowdsourcing-assignment2/',  # URL to serve HIT
        frame_height=600)  # height of frame

    reward = Price(amount=0.25)  # reward for HIT completion

    create_hit_result = connection.create_hit(
        title='Count Recognizable People in Pictures',
        description='Count how many *recognizable* people are in some pictures',
        keywords=[
            'count', 'people', 'pictures', 'simple', 'easy', 'quick', 'label',
            'classification'
        ],
        max_assignments=10,  # number of assignments
        lifetime=datetime.timedelta(days=2),  # time HIT is available
        duration=datetime.timedelta(minutes=5),  # time to complete
        approval_delay=datetime.timedelta(days=7),  # time til HIT approved
        question=question,
        reward=reward,
        response_groups=('Minimal', 'HITDetail'))

    print('Preview: ' + preview_url + create_hit_result[0].HITTypeId)
    print('HIT Id: ' + create_hit_result[0].HITId)
Ejemplo n.º 8
0
 def __init__(self):
     self.config = self.set_config()
     self.mturk = MTurkConnection(
         aws_access_key_id=self.config['aws_access_key_id'],
         aws_secret_access_key=self.config['aws_secret_access_key'],
         host=self.config['host'])
     self.mturk_tmpl = MturkTmpl()
Ejemplo n.º 9
0
class MTurk:
    ASSIGNMENTS_PER_PAGE = 100

    def __init__(self):
        self._mturk = MTurkConnection()

    def get_hit(self, hit_id):
        try:
            hit = self._mturk.get_hit(hit_id)
        except MTurkRequestError as error:
            raise LookupError(error.message)
        else:
            return hit[0]

    def get_assignments(self, hit_id):
        hit = self.get_hit(hit_id)
        max_responses = int(hit.MaxAssignments)
        pages = int(max_responses/self.ASSIGNMENTS_PER_PAGE) + 1
        assignments = [self.get_assignments_page(hit_id, page_number=n)
                       for n in range(1, pages+1)]
        return pd.concat(assignments)

    def get_assignments_page(self, hit_id, page_number):
        assignments = self._mturk.get_assignments(
            hit_id=hit_id,
            page_size=self.ASSIGNMENTS_PER_PAGE,
            page_number=page_number,
        )
        return assignments_to_frame(assignments)
Ejemplo n.º 10
0
def payTurkersAssignments():

    _mtc = MTurkConnection( host = _host )

    rejected = 0
    approved = 0
    failed_rejected = 0
    failed_approved = 0
    
    failed_approved_list = []
    failed_rejected_list = []

    return_dict = processAssignments( save=False )

    # list with the assignments that are not rejected nor flagged
    _good_assignments = return_dict['_good_assignments']
    for ass in _good_assignments:
        try:
            _mtc.approve_assignment( ass['assignment_id'] )
            approved += 1
        except MTurkRequestError:
            failed_approved += 1
            failed_approved_list.append( ass )

    # list containing the assignments that were flagged by the turkers
    _flagged_assignments = return_dict['_flagged_assignments']            
    for ass in _flagged_assignments:
        try:
            _mtc.approve_assignment( ass['assignment_id'] )
            approved += 1
        except MTurkRequestError:
            failed_approved += 1
            failed_approved_list.append( ass )

    # list with the assignments were something inexpected on my side happened
    _error_assignments = return_dict['_error_assignments']
    for ass in _error_assignments:
        try:
            _mtc.approve_assignment( ass['assignment_id'] )
            approved += 1
        except MTurkRequestError:
            failed_approved += 1
            failed_approved_list.append( ass )
                
    # list with the assignments that were rejected
    _rejected_assignments = return_dict['_rejected_assignments']
    for ass in _rejected_assignments:
        try:
            _mtc.reject_assignment( ass['assignment_id'] )
            rejected += 1
        except MTurkRequestError:
            failed_rejected += 1
            failed_rejected_list.append( ass )
            
    print "Approved:        [%d]"%approved
    print "Rejected:        [%d]"%rejected
    print "Not Approved:    [%d]"%failed_approved
    print "Not Rejected:    [%d]"%failed_rejected 
    
    return (failed_approved_list, failed_rejected_list)
Ejemplo n.º 11
0
def fetch_results(hit):
    # check mturk for new results for this particular HIT
    conn = MTurkConnection(
        aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
        aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
        host=settings.AWS_HOST)
    # using the HIT ID, check results
    results = []
    assignments = conn.get_assignments(hit.hit_id)
    # go through the assignments
    for ass in assignments:
        # if there's already a result for this assignment, skip it
        if Result.objects.filter(assignment_id=ass.AssignmentId):
            continue
        # parse out the result
        data = {}
        for answer in ass.answers[0]:
            data[answer.QuestionIdentifier] = answer.FreeText

        # create new Result objects for each of them
        result = Result(assignment_id=ass.AssignmentId,
                        hit=hit,
                        value=json.dumps(data))
        result.save()
        results.append(result)

    return results
Ejemplo n.º 12
0
def getReviewableHITs(verbose=True):

    mtc = MTurkConnection(host=_host)
    page_size = 50
    hitType = getHITType()[0]
    hits = mtc.get_reviewable_hits(page_size=page_size)

    if verbose:
        print "Total results to fetch %s " % hits.TotalNumResults
        print "Request hits page %i" % 1

    total_pages = float(hits.TotalNumResults) / page_size
    int_total = int(total_pages)
    if (total_pages - int_total > 0):
        total_pages = int_total + 1
    else:
        total_pages = int_total

    pn = 1
    while pn < total_pages:
        pn = pn + 1
        if verbose:
            print "Request hits page %i" % pn
            temp_hits = mtc.get_reviewable_hits(hit_type=hitType.HITTypeId,
                                                page_size=page_size,
                                                page_number=pn)
            hits.extend(temp_hits)

    return hits
Ejemplo n.º 13
0
def create_hit(problem, hit_type, params={}):
    """Utility method for creating a new HIT on AMT"""
    hit = Hit(hit_id='?', hit_type=hit_type, problem=problem, params=json.dumps(params),
            title=hit_type.title%params, description=hit_type.description%params, body=hit_type.body%params)
    hit.save()

    # post a HIT on Mechanical Turk using boto
    q = ExternalQuestion(external_url=settings.URL_ROOT + hit.get_absolute_url(), frame_height=800)
    conn = MTurkConnection(aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
                          aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
                          host=settings.AWS_HOST)

    # remove commas from the keywords if they exist
    keywords=[k.replace(',', '') for k in hit_type.keywords.split()]
    create_hit_rs = conn.create_hit(question=q, lifetime=hit_type.lifetime, max_assignments=hit_type.max_assignments,
        keywords=keywords, reward=hit_type.payment, duration=hit_type.duration, approval_delay=hit_type.approval_delay,
        title=hit.title, description=hit.description, annotation=`hit_type`)
    #code.interact(local=locals())
    assert(create_hit_rs.status == True)

    # set the new HIT ID to be the hit_id for the new row.
    hit.hit_id = create_hit_rs[0].HITId
    hit.save()

    return hit
def get_progress(HITId):

    mtc = MTurkConnection(aws_access_key_id=ACCESS_ID,
                      aws_secret_access_key=SECRET_KEY,
                      host=HOST)

    hits = mtc.get_all_hits()
    hits_dict = dict()

    for hit in hits:
        hits_dict[hit.HITId] = hit

    curr_hit = hits_dict[HITId]

    assign_list = []
    assignments = mtc.get_assignments(curr_hit.HITId)
    for assignment in assignments:
        assign_list.append(assignment)

    num_assign_completed = len(assign_list)
    num_assign_total = int(curr_hit.MaxAssignments)

    progress = (float(num_assign_completed) / float(num_assign_total)) * 100

    return progress
Ejemplo n.º 15
0
def pollTurk():
	ACCESS_ID=os.environ['ACCESS_KEY_ID']
	SECRET_KEY=os.environ['SECRET_ACCESS_KEY']
	HOST = 'mechanicalturk.sandbox.amazonaws.com'

	mtc = MTurkConnection(aws_access_key_id=ACCESS_ID,
                      aws_secret_access_key=SECRET_KEY,
                      host=HOST)

	hits = get_all_reviewable_hits(mtc)
 
	for hit in hits:
	    assignments = mtc.get_assignments(hit.HITId)
	    for assignment in assignments:
	        print "Answers of the worker %s" % assignment.WorkerId
	        for question_form_answer in assignment.answers[0]:
	            for key in question_form_answer.fields:
	                print "%s" % (key)
	                #find the username based on hitID assignmentID
	                username = databaseUser.getUsernameFromHitID(hit.HITId)
	                diaryEntry=databaseUser.getDiaryEntryFromHitID(hit.HITId)
	                print "username is %s" % (username)
	                print "diaryEntry is %s" % (diaryEntry)
	                #insert assignmentID and response for the user
	                databaseUser.updateResponse(username,assignment.AssignmentId,key)
	                r = requests.post(slackUrl, data=json.dumps({'text':"----------------\n"+username+" wrote \n"+diaryEntry+"\n mTurk response \n" + key+'\n----------------'}))
	        #mtc.approve_assignment(assignment.AssignmentId)
	        print "--------------------"
	    #mtc.disable_hit(hit.HITId)
Ejemplo n.º 16
0
def getReviewableAssignments():

    mtc = MTurkConnection( host = _host )
    # note: if there are more than 100 assignments per hit the function
    # must be modified to retrieve all pages of the assignments
    page_size = 100
    
    _assignments = []

    #_num_hits = sum(1 for _ in mtc.get_all_hits())
    #print "Total Number of HITs: [%d]" %(_num_hits)
    
    _num_reviewable = 0
    _num_hits = 0
    print "Analyzed [%d] HITs" %(_num_hits+1)
    for hit in mtc.get_all_hits():
        _num_hits += 1
        if _num_hits % 500 == 0:
            print "Analyzed [%d] HITs" %_num_hits
        
        tmp_assign = [_assignment for _assignment in mtc.get_assignments( hit.HITId, page_size = page_size )]
        if len( tmp_assign ) == NUMBER_HIT_ASSIGNMENTS:
            _num_reviewable += 1 
        
        _assignments.extend( tmp_assign )
    
    print "Total Number of HITs: [%d]" %( _num_hits )
    print "Total Number of Assignments: [%d]" %( len(_assignments) )
    print "Total Number of Reviewavle HITs: [%d]" %( _num_reviewable )
    
    return _assignments    
Ejemplo n.º 17
0
def qualify(qualification, value, worker):
    """Assign a qualification to a worker."""
    # create connection to AWS
    from boto.mturk.connection import MTurkConnection
    config = PsiturkConfig()
    config.load_config()
    aws_access_key_id = config.get('AWS Access', 'aws_access_key_id')
    aws_secret_access_key = config.get('AWS Access', 'aws_secret_access_key')
    conn = MTurkConnection(aws_access_key_id, aws_secret_access_key)

    def get_workers_with_qualification(qualification):
        """Get workers with the given qualification."""
        results = []
        continue_flag = True
        page = 1
        while(continue_flag):
            new_results = conn.get_qualifications_for_qualification_type(
                qualification,
                page_size=100,
                page_number=page)

            if(len(new_results) == 0):
                continue_flag = False
            else:
                results.extend(new_results)
                page = page + 1

        return results

    results = get_workers_with_qualification(qualification)
    workers = [x.SubjectId for x in results]

    # assign the qualification
    click.echo(
        "Assigning qualification {} with value {} to worker {}".format(
            qualification,
            value,
            worker))

    if worker in workers:
        result = conn.update_qualification_score(qualification, worker, value)
    else:
        result = conn.assign_qualification(qualification, worker, value)

    if result:
        click.echo(result)

    # print out the current set of workers with the qualification
    results = get_workers_with_qualification(qualification)

    click.echo("{} workers with qualification {}:".format(
        len(results),
        qualification))

    values = [r.IntegerValue for r in results]
    unique_values = list(set([r.IntegerValue for r in results]))
    for v in unique_values:
        click.echo("{} with value {}".format(
            len([val for val in values if val == v]),
            v))
Ejemplo n.º 18
0
class MTurk(object):
    def __init__(self):
        self.mtc = MTurkConnection(aws_access_key_id=ACCESS_ID,
                                   aws_secret_access_key=SECRET_KEY,
                                   host=HOST)
    def balance(self):
        return self.mtc.get_account_balance()

    def create_question(self, id, title):
        content = QuestionContent()
        content.append_field('Title', title)
        text = FreeTextAnswer()
        return Question(identifier=id, content=content, answer_spec=AnswerSpecification(text))

    def create_hit(self, title, description=''):
        question_form = QuestionForm()
        question_form.append(self.create_question(id='1', title='Comments'))
        question_form.append(self.create_question(id='2', title='More Comments'))
        self.mtc.create_hit(questions=question_form, max_assignments=1, title=title, description=description,
            duration=60*5, reward=0.01)

    def external(self):
        q = ExternalQuestion(external_url="http://mturk-hit-wizard.herokuapp.com/view/2", frame_height=800)
        #conn = MTurkConnection(host=HOST)
        keywords=['boto', 'test', 'doctest']
        create_hit_rs = self.mtc.create_hit(question=q, lifetime=60*65,max_assignments=2,title="Boto External Question Test", keywords=keywords,reward = 0.05, duration=60*6,approval_delay=60*60, annotation='An annotation from boto external question test', response_groups=['Minimal','HITDetail','HITQuestion','HITAssignmentSummary',])
        assert(create_hit_rs.status == True)
def getHITType():
    # Changing this will add another hit type and might mess up later fetches...
    # Only change if you know what you are doing...

    _mtc = MTurkConnection(host=_host)

    _title = "Guess the Closest Part of a Person!"
    _description = "Help us find out which body part of a person is closest to the camera that took the picture."
    _keywords = "person, people, image, images, object, objects, depth, comparisons, human3.6m"

    _reward = _mtc.get_price_as_price(0.1)
    _duration = 60 * 15
    _approval_delay = 60 * 60 * 24 * 10

    _qualifications = Qualifications()
    _qualifications.add(
        PercentAssignmentsApprovedRequirement('GreaterThanOrEqualTo',
                                              98,
                                              required_to_preview=True))
    _qualifications.add(
        NumberHitsApprovedRequirement('GreaterThanOrEqualTo',
                                      100,
                                      required_to_preview=True))
    _qualifications.add(
        LocaleRequirement('EqualTo', 'US', required_to_preview=True))

    return _mtc.register_hit_type(title=_title,
                                  description=_description,
                                  reward=_reward,
                                  duration=_duration,
                                  keywords=_keywords,
                                  approval_delay=_approval_delay,
                                  qual_req=_qualifications)
Ejemplo n.º 20
0
 def __init__(self,username,password,ACCESS_ID,SECRET_KEY,initialize=False):
     self.ACCESS_ID = ACCESS_ID
     self.SECRET_KEY = SECRET_KEY
     self.engine = sqlalchemy.create_engine('mysql+mysqlconnector://%s:%s@localhost' % (username,password))
     self.engine.connect()
     self.mtc = MTurkConnection(self.ACCESS_ID,self.SECRET_KEY,host='mechanicalturk.sandbox.amazonaws.com')
     try:
         self.engine.execute('USE amusic;')
         c = self.engine.execute('SELECT * FROM conf;').fetchone()
         self.conf['minPitch'] = c[0]
         self.conf['maxPitch'] = c[1]
         self.conf['minStartTime'] = c[2]
         self.conf['maxStartTime'] = c[3]
         self.conf['minNoteDuration'] = c[4]
         self.conf['maxNoteDuration'] = c[5]
         self.conf['minNoteCount'] = c[6]
         self.conf['maxNoteCount'] = c[7]
         self.conf['currentPopulation'] = c[8]
         self.conf['bucket'] = c[9]
         self.conf['mturkLayoutID'] = c[10]
         self.conf['hitTitle'] = c[11]
         self.conf['hitDescription'] = c[12]
         self.conf['hitKeywords'] = c[13]
         self.conf['hitRewardPerAssignment'] = c[14]
     except:pass
Ejemplo n.º 21
0
def get_extract_keywords_results():

    MTURK_HOST = run_mturk('get_extract_keywords_results')
    if not MTURK_HOST:
        return

    connection = MTurkConnection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY,
                                 host=MTURK_HOST)

    for hit_object in KeywordExtractionHIT.objects.filter(processed=False):
        logger.info('Found unprocessed HIT {0}'.format(hit_object.HITId))

        answers = {}
        assignments = [a for a in connection.get_assignments(hit_object.HITId) if a.AssignmentStatus == 'Approved']
        for assignment in assignments:
            logger.info('Found approved assignment for HIT {0}'.format(hit_object.HITId))
            for question_form_answer in assignment.answers[0]:
                answers[question_form_answer.qid] = question_form_answer.fields[0]

        for i in range(min(len(KEYWORDS_HIT_KEYWORD_FIELDS), len(KEYWORDS_HIT_DEFINITION_FIELDS))):
            keyword_qid = KEYWORDS_HIT_KEYWORD_FIELDS[i][0]
            definition_qid = KEYWORDS_HIT_DEFINITION_FIELDS[i][0]
            try:
                keyword = answers[keyword_qid]
                definition = answers[definition_qid]
                if keyword:
                    Keyword.objects.create(word=keyword, definition=definition, note=hit_object.note, unreviewed=True)
            except KeyError:
                pass

        if assignments:
            logger.info('Done processing HIT {0}'.format(hit_object.HITId))
            hit_object.processed = True
            hit_object.save()
Ejemplo n.º 22
0
    def SubmitHIT(self, sandbox = 'false'):
        """"
        Constructs a HIT from the HITGenerator's attributes, registers it with Amazon, and returns the HITId as a unicode string.

        If the sandbox flag is set to true then the hit will be registered with the Sandbox, otherwise it is registered to AWS
        directly.  All of the necessary data must have been submitted during the HITGenerator's initiation.
        """

        if sandbox is 'true':
            self.host = 'mechanicalturk.sandbox.amazonaws.com'

        conn = MTurkConnection(host = self.host, aws_access_key_id = self.AWS_KEY, aws_secret_access_key = self.AWS_SECRET)

        answer_specification = AnswerSpecification(SelectionAnswer(style = self.answer_style, selections = self.answer_options))

        questions = []
        for i in self.question_list:
	    questions.append(Question(identifier=i[1], content = QuestionContent(i[0]), answer_spec = answer_specification))

        question_form = QuestionForm(questions)

        self.hit_response = conn.create_hit(question = question_form,
                                 lifetime = self.lifetime,
                                 max_assignments = self.assignment_count,
                                 title = self.title,
                                 description = self.description,
                                 keywords = self.keywords,
                                 reward = self.reward,
                                 duration = self.duration,
                                 approval_delay = self.approval_delay,
                                 annotation = self.annotation)

            # Returns the HITId as a unicode string
        self.HITId = self.hit_response.HITId
        return self.HITId
Ejemplo n.º 23
0
    def submitHITs(self, mtc=None, questionForms=[], max_assignments=5,
                   title='Rank the most important sentences',
                   description='Rank the following sentences by importance',
                   keywords='summary, survey',
                   duration=60*3,
                   reward=0.3):
        """ Creates and submits a list of HITTS with the exact same
            title, descriptions, durations and prices from a list of questions.
        """
        if mtc is None:
            mtc = MTurkConnection(aws_access_key_id=ACCESS_ID,
                                  aws_secret_access_key=SECRET_KEY,
                                  host=HOST)

        for questionForm in questionForms:
            mtc.create_hit(questions=questionForm[1],
                           max_assignments=max_assignments,
                           title=title,
                           description=description,
                           keywords=keywords,
                           duration=60*60*12,
                           approval_delay=60*60*12,
                           annotation=questionForm[0],
                           reward=0.03)
        pass
Ejemplo n.º 24
0
  def generate_hit(self, num_assignments, hit_duration, hit_reward):
    """
    Purpose: Generate and publish the HIT
    Parameters: num_assignments is the number of avaliable assignments for hit, 
                hit_duration is the duration of the hit in seconds (60*5 for 5 minutes),
                hit_reward is the reward given per hit in dollars (0.05 is 5 cents)
    """
    # CONNECT TO MTURK

    mtc = MTurkConnection(aws_access_key_id = self.access_id,
                      aws_secret_access_key = self.secret_key,
                      host = self.host)

    # BUILD OVERVIEW 
     
    overview = Overview()

    overview.append_field('Title', 'The following one or more sentences constitute an incomplete story.')
    story = ""
    for sentence in self.story_sentences:
      story += sentence + " "
    overview.append(FormattedContent(story))
  
    # BUILD QUESTION 1: Copy the first sentence of the story 
     
    qc1 = QuestionContent()
    qc1.append_field('Title','Copy verbatim the first sentence of the provided incomplete story. Please keep all capitalization and punctuation as given. Your sumbission will automatically be rejected if any character is incorrect.')
    fta1 = FreeTextAnswer()
    q1 = Question(identifier='verify_sentence', content = qc1, answer_spec = AnswerSpecification(fta1), is_required = True)

    # BUILD QUESTION 2: Vote on the best sentence to continue the story
    
    sentence_options = []
    for i, sentence in enumerate (self.vote_sentences):
      selection = (sentence, str(i))
      sentence_options.append(selection)
    qc2 = QuestionContent()
    qc2.append_field('Title','Choose the best sentence to continue the story.')
    fta2 = SelectionAnswer(min=1, max=1,style='radiobutton',
                      selections=sentence_options,
                      type='text',
                      other=False)
    q2 = Question(identifier='vote_sentence', content = qc2, answer_spec = AnswerSpecification(fta2), is_required = True)

    # BUILD THE QUESTION FORM 
     
    question_form = QuestionForm()
    question_form.append(overview)
    question_form.append(q1)
    question_form.append(q2)
     
    # CREATE THE HIT 
     
    mtc.create_hit(questions = question_form,
                   max_assignments = num_assignments,
                   title = self.title,
                   description = self.description,
                   keywords = self.keywords,
                   duration = hit_duration,
                   reward = hit_reward)
Ejemplo n.º 25
0
 def prepareBatch(self):
     if not self.amusic.conf['mturkLayoutID']:
         print "LayoutID not set"
         return
     l = [i[0] for i in self.amusic.engine.execute('SELECT title FROM song WHERE population="%s";'%(self.title)).fetchall()]
     for d,i in enumerate(l):
         print 'Song %d/%d (%s)'%(d+1,len(l),i)
         s = Song(self,i)
         s.fromDB()
         s.synth(i+'.mp3' if i.find('.')==-1 else i[:i.find('.')]+'.mp3',upload=True)
     print "Creating HITs..."
     hostName = 'mechanicalturk.amazonaws.com'
     #hostName = 'mechanicalturk.sandbox.amazonaws.com'
     mtc = MTurkConnection(self.amusic.ACCESS_ID,self.amusic.SECRET_KEY,host=hostName)#,debug=5)
     print "host:", hostName
     s = "http://%s.s3.amazonaws.com/tracks/" % (self.amusic.conf['bucket'])
     n=1
     for d,i in enumerate(l):
         for j in l[d+1:]:
             f1 = i+'.mp3' if i.find('.')==-1 else i[:i.find('.')]+'.mp3'
             f2 = j+'.mp3' if j.find('.')==-1 else j[:j.find('.')]+'.mp3'
             print "\tHIT %d/%d (%s,%s)" % (n,len(l)*(len(l)-1)/2,f1,f2)
             params = LayoutParameters()
             params.add(LayoutParameter('file1',s+f1))
             params.add(LayoutParameter('file2',s+f2))
             mtc.create_hit(hit_layout=self.amusic.conf['mturkLayoutID'],layout_params=params,reward=self.amusic.conf['hitRewardPerAssignment'],title=self.amusic.conf['hitTitle'],description=self.amusic.conf['hitDescription'],keywords=self.amusic.conf['hitKeywords'])
             n+=1
    def __init__(self):
        aws_id = os.environ['AWS_ACCESS_KEY_ID']
        aws_k = os.environ['AWS_ACCESS_KEY']

        try:
            self.conn = MTurkConnection(aws_access_key_id=aws_id,\
                          aws_secret_access_key=aws_k,\
                          host=HOST)
        except Exception as e:
            print(e)

        self.ah = AssignmentHandler(self.conn)
        self.th = TurkerHandler(self.conn)
        self.hh = HitHandler(self.conn, TEMPLATE_DIR)
        self.mh = MongoElicitationHandler()
        self.ph = PromptHandler()
        self.filter = Filter(self.mh)
        self.balance = self.conn.get_account_balance()[0].amount
        self.batch_cost = 1
        if self.balance > self.batch_cost:
            self.balance = self.batch_cost
        else:
            raise IOError
        self.logger = logging.getLogger(
            "transcription_engine.elicitation_pipeline_handler")
Ejemplo n.º 27
0
    def submitHITs(self,
                   mtc=None,
                   questionForms=[],
                   max_assignments=5,
                   title='Rank the most important sentences',
                   description='Rank the following sentences by importance',
                   keywords='summary, survey',
                   duration=60 * 3,
                   reward=0.3):
        """ Creates and submits a list of HITTS with the exact same
            title, descriptions, durations and prices from a list of questions.
        """
        if mtc is None:
            mtc = MTurkConnection(aws_access_key_id=ACCESS_ID,
                                  aws_secret_access_key=SECRET_KEY,
                                  host=HOST)

        for questionForm in questionForms:
            mtc.create_hit(questions=questionForm[1],
                           max_assignments=max_assignments,
                           title=title,
                           description=description,
                           keywords=keywords,
                           duration=60 * 60 * 12,
                           approval_delay=60 * 60 * 12,
                           annotation=questionForm[0],
                           reward=0.03)
        pass
Ejemplo n.º 28
0
def fetch_results(hit):
    # check mturk for new results for this particular HIT
    conn = MTurkConnection(
        aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
        aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
        host=settings.AWS_HOST,
    )
    # using the HIT ID, check results
    results = []
    assignments = conn.get_assignments(hit.hit_id)
    # go through the assignments
    for ass in assignments:
        # if there's already a result for this assignment, skip it
        if Result.objects.filter(assignment_id=ass.AssignmentId):
            continue
        # parse out the result
        data = {}
        for answer in ass.answers[0]:
            data[answer.QuestionIdentifier] = answer.FreeText

        # create new Result objects for each of them
        result = Result(assignment_id=ass.AssignmentId, hit=hit, value=json.dumps(data))
        result.save()
        results.append(result)

    return results
Ejemplo n.º 29
0
def qualify(qualification, value, worker):
    """Assign a qualification to a worker."""
    # create connection to AWS
    from boto.mturk.connection import MTurkConnection
    config = PsiturkConfig()
    config.load_config()
    aws_access_key_id = config.get('AWS Access', 'aws_access_key_id')
    aws_secret_access_key = config.get('AWS Access', 'aws_secret_access_key')
    conn = MTurkConnection(aws_access_key_id, aws_secret_access_key)

    def get_workers_with_qualification(qualification):
        """Get workers with the given qualification."""
        results = []
        continue_flag = True
        page = 1
        while(continue_flag):
            new_results = conn.get_qualifications_for_qualification_type(
                qualification,
                page_size=100,
                page_number=page)

            if(len(new_results) == 0):
                continue_flag = False
            else:
                results.extend(new_results)
                page = page + 1

        return results

    results = get_workers_with_qualification(qualification)
    workers = [x.SubjectId for x in results]

    # assign the qualification
    click.echo(
        "Assigning qualification {} with value {} to worker {}".format(
            qualification,
            value,
            worker))

    if worker in workers:
        result = conn.update_qualification_score(qualification, worker, value)
    else:
        result = conn.assign_qualification(qualification, worker, value)

    if result:
        click.echo(result)

    # print out the current set of workers with the qualification
    results = get_workers_with_qualification(qualification)

    click.echo("{} workers with qualification {}:".format(
        len(results),
        qualification))

    values = [r.IntegerValue for r in results]
    unique_values = list(set([r.IntegerValue for r in results]))
    for v in unique_values:
        click.echo("{} with value {}".format(
            len([val for val in values if val == v]),
            v))
Ejemplo n.º 30
0
def finalize_assignment(assignment_id, approve=True):
  connection = MTurkConnection(aws_access_key_id=MTURK_ACCESS_KEY, aws_secret_access_key=MTURK_SECRET_KEY,
   host=MTURK_HOST)

  if approve:
    connection.approve_assignment(assignment_id)
  else:
    connection.reject_assignment(assignment_id)
Ejemplo n.º 31
0
def cancel_hit(hit):
    hostURL = SANDBOX_HOST if hit.sandbox else HOST

    connection = MTurkConnection(
        aws_access_key_id=hit.aws_access_key, aws_secret_access_key=hit.aws_secret_key, host=hostURL
    )

    return connection.disable_hit(hit.mturkid)
Ejemplo n.º 32
0
def cancel_hit(hit):
    hostURL = SANDBOX_HOST if hit.sandbox else HOST

    connection = MTurkConnection(aws_access_key_id=hit.aws_access_key,
                                 aws_secret_access_key=hit.aws_secret_key,
                                 host=hostURL)

    return connection.disable_hit(hit.mturkid)
Ejemplo n.º 33
0
def con(HOST=HOST):
    try:
        mtc = MTurkConnection(aws_access_key_id=ACCESS_ID, aws_secret_access_key=SECRET_KEY, host=HOST)
        print('Connected to '+HOST)
        print('Balance: '+str(mtc.get_account_balance()))
        return mtc
    except:
        print('Failed to connect to '+HOST+', plz make sure you are connecting to the correct host with correct credentials')
Ejemplo n.º 34
0
 def handle(self, *args, **options):
     # create a connection
     mturk = MTurkConnection(
         getattr(settings, 'MTURK_AWS_KEY', settings.MEDIASYNC['AWS_KEY']),
         getattr(settings, 'MTURK_AWS_SECRET', settings.MEDIASYNC['AWS_SECRET']),
         host = 'mechanicalturk.sandbox.amazonaws.com' if options['sandbox'] else 'mechanicalturk.amazonaws.com'
     )
     
     # if --delete, delete all the old ones first.
     if options['delete_first']:
         for hit in mturk.get_all_hits():
             mturk.disable_hit(hit.HITId)
     
     if options['exclude']:
         exclude_reader = csv.DictReader(open(options['exclude'], 'r'))
         exclude = set()
         for row in exclude_reader:
             exclude.add(row['td_id'])
     
     # iterate over items and create them one by one
     cursor = connection.cursor()
     cursor.execute(
         """
         select entity_id, type from matchbox_wikipediainfo, matchbox_entity where entity_id not in (select entity_id from matchbox_sunlightinfo where bio is not null) and bio != '' and bio is not null and entity_id = matchbox_entity.id %s order by entity_id limit %s;
         """ % ("and type = '%s'" % options['type'] if options['type'] else '', '%s'), # hack to put the interpolation string back in for PG to catch it
     [options['count']])
     
     for row in cursor:
         if options['exclude']:
             if str(row[0]).replace('-', '') in exclude:
                 continue
         
         if options['practice']:
             print row[0]
             continue
         
         try:
             hit = mturk.create_hit(
                 question = FakeQuestionForm(get_hit_xml(row[0])),
                 max_assignments = 3,
                 annotation = row[0],
                 
                 title = "Wikipedia match validation",
                 description = "We have matched a set of entities in a database to descriptions pulled from Wikipedia via an automated process. Confirm that the match is correct.",
                 reward = 0.06,
                 duration = datetime.timedelta(minutes=30),
                 lifetime = datetime.timedelta(days=7),
                 keywords = ['wikipedia', 'matching'],
                 approval_delay = datetime.timedelta(days=3),
                 qualifications = Qualifications([PercentAssignmentsApprovedRequirement("GreaterThan", 90)])
             )
             print hit[0].HITId
         except Exception as e:
             sys.stderr.write("Failed to create hit %s\n" % row[0])
             sys.stderr.write(getattr(e, 'body', ''))
             sys.stderr.write('\n')
         except:
             pass
Ejemplo n.º 35
0
def post_HIT1(ACCESS_ID,SECRET_KEY,HOST,url_to_task):
    mtc = MTurkConnection(aws_access_key_id=ACCESS_ID,
                      aws_secret_access_key=SECRET_KEY,
                      host=HOST)
 
    title = 'Dev deploying simulation test Report From SERVER'
    description = ('Report on events in a simulation')
    keywords = 'website, rating, opinions'
    instructions=('<p>You will take part in a web-based experiment where you will watch a simple simulation and provide reports on events</p>'
                 '<p>Instructions:</p>'
                  '<p>1. Click the link below, which will open the webpage in a new window in your browser</p>'
                  '<p>2. Follow the instructions on the website</p>'
                  '<p>3. Once you have completed your work, you will receive a Reward Code</p>'
                  '<p>4. Return to the mechanical turk webpage and enter your code in the Reward Code text box</p>'
                  '<p>5. Your work will then be checked, after which you will receive your payment</p>'
                  '<br/>CLICK "ACCEPT HIT" BEFORE FOLLOWING LINK'
                  '<br/>YOU WILL NOT BE PAID WITHOUT ACCEPTING THE HIT')
                    
                    
    #---------------  BUILD OVERVIEW -------------------
     
    overview = Overview()
    overview.append_field('Title', description)
    overview.append(FormattedContent(instructions))
    overview.append(FormattedContent('<p>Click "Accept HIT" then click this link <a target="_blank"'
                                     ' href="'+url_to_task+'">'
                                     ' Link to task</a></p>'))
 
    #---------------  BUILD QUESTION 1 -------------------
     
    qc1 = QuestionContent()
    qc1.append_field('Title','Enter reward code here:')
     
    fta1 = FreeTextAnswer(num_lines=1)
    
    q1 = Question(identifier='reward_code',
                  content=qc1,
                  answer_spec=AnswerSpecification(fta1),
                  is_required=True)
     
     
    #--------------- BUILD THE QUESTION FORM -------------------
     
    question_form = QuestionForm()
    question_form.append(overview)
    question_form.append(q1)
    
     
    #--------------- CREATE THE HIT -------------------
     
    mtc.create_hit(questions=question_form,
                   max_assignments=1,
                   title=title,
                   description=description,
                   keywords=keywords,
                   duration = 60*5,
                   reward=0.05)
Ejemplo n.º 36
0
def test():
    q = ExternalQuestion(external_url="http://websort.net/s/F3481C", frame_height=800)
    conn = MTurkConnection(host='mechanicalturk.sandbox.amazonaws.com')
    keywords=['boto', 'test', 'doctest']
    qualifications = Qualifications()
    qualifications.add(PercentAssignmentsApprovedRequirement(comparator="GreaterThan", integer_value="95"))
    create_hit_rs = conn.create_hit(question=q, lifetime=60*65,max_assignments=2,title="Boto External Question Test", keywords=keywords,reward = 0.05, duration=60*6,approval_delay=60*60, annotation='An annotation from boto external question test', qualifications=qualifications)
    assert(create_hit_rs.status == True)
    print create_hit_rs.HITTypeId
Ejemplo n.º 37
0
 def __init__(self, host, aws_access_key_id, aws_secret_access_key):
     self.host = host
     self.connection = MTurkConnection(
         aws_access_key_id=aws_access_key_id,
         aws_secret_access_key=aws_secret_access_key,
         host=settings.MTURK_HOST)
     self.connection.APIVersion = "2014-08-15"
     if not self.host:
         raise ValueError("Please provide a host url")
def test():
    q = ExternalQuestion(external_url="http://websort.net/s/F3481C", frame_height=800)
    conn = MTurkConnection(host='mechanicalturk.sandbox.amazonaws.com')
    keywords=['boto', 'test', 'doctest']
    qualifications = Qualifications()
    qualifications.add(PercentAssignmentsApprovedRequirement(comparator="GreaterThan", integer_value="95"))
    create_hit_rs = conn.create_hit(question=q, lifetime=60*65, max_assignments=2, title="Boto External Question Test", keywords=keywords, reward = 0.05, duration=60*6, approval_delay=60*60, annotation='An annotation from boto external question test', qualifications=qualifications)
    assert(create_hit_rs.status == True)
    print create_hit_rs.HITTypeId
Ejemplo n.º 39
0
    def __init__(self):
        if settings.IS_DEV_ENV or settings.USE_AMT_SANDBOX:
            HOST = 'mechanicalturk.sandbox.amazonaws.com'
        else:
            HOST = 'mechanicalturk.amazonaws.com'

        self.connection = MTurkConnection(
                aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
                aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
                host=HOST)
Ejemplo n.º 40
0
	def getHit(self,hitID):
		mtc = MTurkConnection(aws_access_key_id=self.ACCESS_ID,
                      aws_secret_access_key=self.SECRET_KEY,
                      host=self.HOST)
		assignments = mtc.get_assignments(hitID)
		for assignment in assignments: #unique to number of assignments in HIT
			print "Answers of the worker %s" % assignment.WorkerId
			for question_form_answer in assignment.answers[0]:
				for value in question_form_answer.fields:
					print "%s" % (value)
Ejemplo n.º 41
0
def PostHits_loop():
    # To post a HIT, first connect to Turk using our access codes:
    # The ACCESS_ID and SECRET_KEY are loaded before this function is called
    # (e.g., from alvarezlab import * );
    mtc = MTurkConnection(aws_access_key_id=ACCESS_ID,
                          aws_secret_access_key=SECRET_KEY,
                          host=HOST)

    for x in range(startHSet, endHSet + 1):
        HSet = x
        urlForHIT = "https://scorsese.wjh.harvard.edu/turk/experiments/cfm/Search10/index_cmtrial_Search10.html?HSetNum=%d" % HSet

        # Now lets setup a structure for our external HIT. We need the URL we want to be
        # shown within the Turk window and also how tall we want the Turk iframe to be:
        q = ExternalQuestion(external_url=urlForHIT, frame_height=frameHeight)

        # And any qualifications we want people to have:
        qualifications = mtqu.Qualifications()

        qualifications.add(
            mtqu.PercentAssignmentsApprovedRequirement(
                'GreaterThanOrEqualTo', percentAssignmentsApprovedRequirement))
        qualifications.add(mtqu.LocaleRequirement("EqualTo",
                                                  localeRequirement))
        if (qualificationID != "NONE"):
            qualifications.add(
                mtqu.Requirement(qualificationID, "EqualTo", 1,
                                 notifyWorkerOfQualification))

        # Post:
        theHIT = mtc.create_hit(
            question=q,
            lifetime=minToSec(minutesBeforeHitExpires),
            max_assignments=numAssignmentsToPost,
            title=titleForWorkers,
            description=descriptionForWorkers,
            keywords=keywordsForWorkers,
            qualifications=qualifications,
            reward=pay,
            duration=minToSec(minutesForUsersToFinish),
            approval_delay=minToSec(minutesBeforeAutoApproved),
            annotation=projectNameForRequesters)

        # get more info about the hit
        hit = mtc.get_hit(theHIT[0].HITId)

        # Print out the HIT's ID if all went well:
        # pprint(vars(hit[0]))
        # print "Experiment info:"
        # print HOST
        print "preview hit in HSet ", HSet, ": "
        # print urlForHIT, "\n"
        # print "HITId"
        # print theHIT[0].HITId, "\n"
        print PREVIEW + "?groupId=" + hit[0].HITGroupId, "\n"
    def create_hits(self, category_id, task_id, num_hits):

        print "Connecting to Turk host at"
        print app.config['MTURK_HOST']
        sys.stdout.flush()

        mturk = MTurkConnection(app.config['AWS_ACCESS_KEY_ID'],
                                app.config['AWS_SECRET_ACCESS_KEY'],
                                host=app.config['MTURK_HOST'])

        print "Uploading %d hits to turk" % num_hits
        hits = []
        qualifications = app.config['QUALIFICATIONS']

        for hit_num in range(num_hits):

            category = app.config['EXAMPLE_CATEGORIES'][category_id]
            hit_html = category['hit_html']
            hit_html = hit_html.replace('${task_id}', task_id)
            hit_html = hit_html.replace('${requester_id}',
                                        app.config['CROWDJS_REQUESTER_ID'])
            hit_html = hit_html.replace(
                '${task_data_url}', app.config['CROWDJS_GET_TASK_DATA_URL'])
            hit_html = hit_html.replace(
                '${submit_answer_url}',
                app.config['CROWDJS_SUBMIT_ANSWER_URL'])
            hit_html = hit_html.replace('${compute_taboo_url}',
                                        app.config['SUBMIT_TABOO_URL'])
            hit_html = hit_html.replace('${return_hit_url}',
                                        app.config['CROWDJS_RETURN_HIT_URL'])
            hit_html = hit_html.replace('${assign_url}',
                                        app.config['CROWDJS_ASSIGN_URL'])
            hit_html = hit_html.replace('${taboo_threshold}',
                                        str(app.config['TABOO_THRESHOLD']))
            hit_html = hit_html.replace(
                '${mturk_external_submit}',
                str(app.config['MTURK_EXTERNAL_SUBMIT']))

            question = HTMLQuestion(hit_html, 800)
            hit = mturk.create_hit(
                title=category['task_name'],
                description=category['task_description'],
                question=question,
                reward=Price(category['price']),
                duration=datetime.timedelta(minutes=10),
                lifetime=datetime.timedelta(days=7),
                keywords=
                'information extraction, events, natural language processing',
                max_assignments=1,
                approval_delay=3600,
                qualifications=qualifications)[0]

            hits.append(hit.HITId)

        return hits
Ejemplo n.º 43
0
def submit_extract_keywords_hit(note):
    """Create a Mechanical Turk HIT that asks a worker to
    choose keywords and definitions from the given note."""

    try:
        MTURK_HOST = os.environ['MTURK_HOST']
    except:
        logger.warn('Could not find Mechanical Turk secrets, not running submit_extract_keywords_hit')
        return

    connection = MTurkConnection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY,
                                 host=MTURK_HOST)

    if note.course.school:
        title = KEYWORDS_HIT_TITLE_TEMPLATE.format(course=note.course.name, school=note.course.school.name)
    else:
        title = KEYWORDS_HIT_TITLE_TEMPLATE.format(course=note.course.name, school=note.course.department.school.name)

    overview = Overview()
    overview.append(FormattedContent(KEYWORDS_HIT_OVERVIEW_TEMPLATE.format(domain=Site.objects.get_current(),
                                                                  link=note.get_absolute_url())))

    keyword_fta = FreeTextAnswer()
    keyword_fta.num_lines = 1

    definition_fta = FreeTextAnswer()
    definition_fta.num_lines = 3

    question_form = QuestionForm()
    question_form.append(overview)

    for i in range(min(len(KEYWORDS_HIT_KEYWORD_FIELDS), len(KEYWORDS_HIT_DEFINITION_FIELDS))):
        keyword_content = QuestionContent()
        keyword_content.append_field('Title', KEYWORDS_HIT_KEYWORD_FIELDS[i][1])
        keyword_question = Question(identifier=KEYWORDS_HIT_KEYWORD_FIELDS[i][0],
                                    content=keyword_content,
                                    answer_spec=AnswerSpecification(keyword_fta),
                                    is_required=True if i <= 10 else False)
        question_form.append(keyword_question)

        definition_content = QuestionContent()
        definition_content.append_field('Title', KEYWORDS_HIT_DEFINITION_FIELDS[i][1])
        definition_question = Question(identifier=KEYWORDS_HIT_DEFINITION_FIELDS[i][0],
                                       content=definition_content,
                                       answer_spec=AnswerSpecification(definition_fta),
                                       is_required=False)
        question_form.append(definition_question)

    hit = connection.create_hit(questions=question_form, max_assignments=1,
                          title=title, description=KEYWORDS_HIT_DESCRIPTION,
                          keywords=KEYWORDS_HIT_KEYWORDS, duration=KEYWORDS_HIT_DURATION,
                          reward=KEYWORDS_HIT_REWARD, qualifications=KEYWORDS_HIT_QUALIFICATION,
                          annotation=str(note.id))[0]

    HIT.objects.create(HITId=hit.HITId, note=note, processed=False)
Ejemplo n.º 44
0
 def __init__(self):
     self.config = HaCRSUtil.get_config('../config.ini')
     HOST = self.config.get('mturk','host')
     AWS_ACCESS_KEY_ID = self.config.get('mturk', 'access_key_id')
     AWS_SECRET_ACCESS_KEY = self.config.get('mturk', 'secret_access_key')
     self.MTconnection = MTurkConnection(aws_access_key_id=AWS_ACCESS_KEY_ID,
          aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
          host=HOST)
     self.db = HaCRSDB()
     self.con, self.cur = HaCRSUtil.get_db(self.config)
     self.mt = HaCRSTurker()
Ejemplo n.º 45
0
def notify(hit):
    hostURL = SANDBOX_HOST if hit.sandbox else HOST

    connection = MTurkConnection(
        aws_access_key_id=hit.aws_access_key, aws_secret_access_key=hit.aws_secret_key, host=hostURL
    )

    for worker in hit_workers(hit):
        params = {"WorkerId": worker.mturkid, "Subject": "It's time to join", "MessageText": "Hello"}

        connection._process_request("NotifyWorkers", params)
Ejemplo n.º 46
0
def createHits(question, answers, params):
    if SANDBOX:
        mturk_url = 'mechanicalturk.sandbox.amazonaws.com'
        preview_url = 'https://workersandbox.mturk.com/mturk/preview?groupId='
    else:
        mturk_url = 'mechanicalturk.amazonaws.com'
        preview_url = 'https://mturk.com/mturk/preview?groupId='

    #Create Hit Form Structure
    overview = Overview()
    overview.append_field('Title', 'We want to know the crowds opinion!')
    overview.append(
        FormattedContent(
            '<a href="http://programthecrowd.com/">Visit us here</a>'))
    questionContent = QuestionContent()
    questionContent.append_field('Title', question)
    answerChoices = SelectionAnswer(min=1,
                                    max=1,
                                    style='checkbox',
                                    selections=answers,
                                    type='text',
                                    other=False)
    q = Question(identifier='Help',
                 content=questionContent,
                 answer_spec=AnswerSpecification(answerChoices),
                 is_required=True)
    questionForm = QuestionForm()
    questionForm.append(overview)
    questionForm.append(q)
    hitIdList = []
    global conn
    # key = params['aws_access_key']
    # secret = params['aws_secret_key']
    conn = MTurkConnection(
        aws_access_key_id='AKIAJBTEJI2RGTJH7OBA',
        aws_secret_access_key='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',
        host=mturk_url)

    #For Loop to create and post hits
    for i in range(0, NUMBER_OF_HITS):
        create_hit_rs = conn.create_hit(questions=questionForm,
                                        lifetime=LIFETIME,
                                        max_assignments=NUMBER_OF_ASSIGNMENTS,
                                        title=TITLE,
                                        keywords=KEYWORDS,
                                        reward=REWARD,
                                        duration=DURATION,
                                        approval_delay=APPROVAL_DELAY,
                                        annotation=DESCRIPTION)
        #print(preview_url + create_hit_rs[0].HITTypeId)
        #print("HIT ID: " + create_hit_rs[0].HITId)
        hitIdList.append(create_hit_rs[0].HITId)

    return hitIdList
Ejemplo n.º 47
0
 def connect_to_turk(self):
     is_sandbox = self.config.getboolean('HIT Configuration', 'using_sandbox')
     if is_sandbox:
         host = 'mechanicalturk.sandbox.amazonaws.com'
     else:
         host = 'mechanicalturk.amazonaws.com'
     
     mturkparams = dict(
         aws_access_key_id = self.config.get('AWS Access', 'aws_access_key_id'),
         aws_secret_access_key = self.config.get('AWS Access', 'aws_secret_access_key'),
         host=host)
     self.mtc = MTurkConnection(**mturkparams)
Ejemplo n.º 48
0
def getConnection(is_prod=False):
    if is_prod:
        mtc = MTurkConnection(aws_access_key_id=ACCESS_ID,
                              aws_secret_access_key=SECRET_KEY,
                              host=SANDBOX_HOST)
    else:
        mtc = MTurkConnection(aws_access_key_id=ACCESS_ID,
                              aws_secret_access_key=SECRET_KEY,
                              host=SANDBOX_HOST)

    #print mtc.get_account_balance()
    return mtc
Ejemplo n.º 49
0
    def __init__(self):
        #connect to MTurk
        self.connect = MTurkConnection(self.AWS_ACCESS_KEY_ID,
                                       self.AWS_SECRET_ACCESS_KEY,
                                       host=self.HOST_SANDBOX)

        #Qualification setting
        q = self.qualifications = Qualifications()

        # if required_to_preview == True unqualified user even can't view the hit.
        #        q.add( PercentAssignmentsApprovedRequirement( comparator="GreaterThan", integer_value="95" ) )
        q.add(AdultRequirement(comparator="EqualTo", integer_value="1"))
Ejemplo n.º 50
0
def make_poll(title, question, description, keywords, poll_price, ratings):
    """Take submitted request and answers.

    Resubmit for polling to ensure validity."""

    ACCESS_ID = mtkey.ACCESS_KEY
    SECRET_KEY = mtkey.SECRET_KEY

    HOST = 'mechanicalturk.amazonaws.com'
    # link to HITs: https://requester.mturk.com/mturk/manageHITs

    # HOST = 'mechanicalturk.sandbox.amazonaws.com'
    # link to HITs: https://requestersandbox.mturk.com/mturk/manageHITs

    mtc = MTurkConnection(aws_access_key_id=ACCESS_ID,
                          aws_secret_access_key=SECRET_KEY,
                          host=HOST)

    #---------------  BUILD OVERVIEW -------------------

    overview = Overview()
    overview.append_field('Title', title)

    #---------------  BUILD POLL  -------------------

    qc2 = QuestionContent()
    qc2.append_field('Title', question)

    fta2 = SelectionAnswer(min=1, max=4, style='checkbox',
                           selections=ratings,
                           type='text',
                           other=False)

    q2 = Question(identifier='selection',
                  content=qc2,
                  answer_spec=AnswerSpecification(fta2),
                  is_required=True)

    #--------------- BUILD THE POLL FORM -------------------

    question_form = QuestionForm()
    question_form.append(overview)
    question_form.append(q2)    

    #--------------- CREATE THE HIT -------------------

    return mtc.create_hit(questions=question_form,
                          max_assignments=8,
                          title=title,
                          description=description,
                          keywords=keywords,
                          duration=60*5,
                          reward=poll_price)
Ejemplo n.º 51
0
def create_hit(href, hit):
    if hit.type == "MULTIPLE_URLS":  # the max_workers was not set in this form
        hit.max_workers = (
            hit.size1
            + hit.size2
            + hit.size3
            + hit.size4
            + hit.size5
            + hit.size6
            + hit.size7
            + hit.size8
            + hit.size9
            + hit.size10
        )

    hostURL = SANDBOX_HOST if hit.sandbox else HOST

    qualifications = Qualifications()
    if hit.hit_approval > 0:
        qualifications.add(NumberHitsApprovedRequirement("GreaterThan", hit.hit_approval, False))
    if hit.hit_approval_rate > 0:
        qualifications.add(PercentAssignmentsApprovedRequirement("GreaterThan", hit.hit_approval_rate, False))
    if hit.accepted_hit_rate > 0:
        qualifications.add(PercentAssignmentsSubmittedRequirement("GreaterThan", hit.accepted_hit_rate, False))
    if hit.returned_hit_rate > 0:
        qualifications.add(PercentAssignmentsReturnedRequirement("GreaterThan", hit.returned_hit_rate, False))
    if hit.abandoned_hit_rate > 0:
        qualifications.add(PercentAssignmentsAbandonedRequirement("LessThan", hit.abandoned_hit_rate, False))
    if hit.rejected_hit_rate > 0:
        qualifications.add(PercentAssignmentsRejectedRequirement("LessThan", hit.rejected_hit_rate, False))
    if hit.locale_qualification is not None and hit.locale_qualification != "all":
        qualifications.add(LocaleRequirement("EqualTo", hit.locale_qualification, False))

    connection = MTurkConnection(
        aws_access_key_id=hit.aws_access_key, aws_secret_access_key=hit.aws_secret_key, host=hostURL, is_secure=True
    )

    return Response(
        connection.create_hit(
            question=ExternalQuestion(href, hit.frame_height),
            lifetime=hit.lifetime,
            max_assignments=hit.max_workers,
            title=hit.title,
            description=hit.description,
            keywords=["turktime"],  # TODO
            reward=hit.reward,
            duration=hit.duration,
            approval_delay=hit.approval_delay,
            qualifications=qualifications,
            response_groups=["Minimal", "HITDetail", "HITQuestion", "HITAssignmentSummary"],
        )
    )
Ejemplo n.º 52
0
    def __init__(self, sandbox=True):
        if sandbox:
            self.host = "mechanicalturk.sandbox.amazonaws.com"
            self.external_submit_endpoint = "https://workersandbox.mturk.com/mturk/externalSubmit"
        else:
            self.host = "mechanicalturk.amazonaws.com"
            self.external_submit_endpoint = "https://www.mturk.com/mturk/externalSubmit"

        self.base_url = "https://labelmelite.csail.mit.edu"
        self.connection = MTurkConnection(
            aws_access_key_id=amt_config.AWS_ACCESS_KEY_ID,
            aws_secret_access_key=amt_config.AWS_SECRET_ACCESS_KEY,
            host=self.host)
Ejemplo n.º 53
0
def register_hit_type(credentials, host='mechanicalturk.sandbox.amazonaws.com'):
    conn = MTurkConnection(*credentials, host=host)
    q = qual.Qualifications()
    q.add(qual.NumberHitsApprovedRequirement('GreaterThanOrEqualTo', 50))
    q.add(qual.PercentAssignmentsApprovedRequirement('GreaterThanOrEqualTo', 80))
    reward = conn.get_price_as_price(0.15)
    title = 'Photoquality'
    description = "Photoquality Assesments"
    duration = 60*60
    approval_delay = 240*60
    keywords = ['photograph quality', 'image quality', 'ranking', 'photography', 'images', 'image cognition', 'vision']
    return conn.register_hit_type(title, description, reward, duration, 
                    keywords=keywords, approval_delay=approval_delay, qual_req=q)
Ejemplo n.º 54
0
def create_hits():
	if SANDBOX:
		mturk_url = 'mechanicalturk.sandbox.amazonaws.com'
		preview_url = 'https://workersandbox.mturk.com/mturk/preview?groupId='
	else:
		mturk_url = 'mechanicalturk.amazonaws.com'
		preview_url = 'https://mturk.com/mturk/preview?groupId='

	q = ExternalQuestion(external_url=HIT_URL, frame_height=800)
	conn = MTurkConnection(aws_access_key_id=AWS_ACCESS_KEY, aws_secret_access_key=AWS_SECRET_KEY, host=mturk_url)
	for i in range(0, NUMBER_OF_HITS):
		create_hit_rs = conn.create_hit(question=q, lifetime=LIFETIME, max_assignments=NUMBER_OF_ASSIGNMENTS, title=TITLE, keywords=KEYWORDS, reward=REWARD, duration=DURATION, approval_delay=APPROVAL_DELAY, annotation=DESCRIPTION)
		print(preview_url + create_hit_rs[0].HITTypeId)
		print("HIT ID: " + create_hit_rs[0].HITId)
Ejemplo n.º 55
0
def create_hits():
	if SANDBOX:
		mturk_url = 'mechanicalturk.sandbox.amazonaws.com'
		preview_url = 'https://workersandbox.mturk.com/mturk/preview?groupId='
	else:
		mturk_url = 'mechanicalturk.amazonaws.com'
		preview_url = 'https://mturk.com/mturk/preview?groupId='

	q = ExternalQuestion(external_url="https://census.stanford.edu/client/demo/maintask.html", frame_height=800)
	conn = MTurkConnection(aws_access_key_id=AWS_ACCESS_KEY, aws_secret_access_key=AWS_SECRET_KEY, host=mturk_url)
	keywords=['census']
	for i in range(0, NUMBER_OF_HITS):
		create_hit_rs = conn.create_hit(question=q, lifetime=LIFETIME,max_assignments=NUMBER_OF_ASSIGNMENTS,title=TITLE, keywords=keywords,reward = REWARD, duration=60*6,approval_delay=60*60, annotation=EXPLANATION)
		print(preview_url + create_hit_rs[0].HITTypeId)
Ejemplo n.º 56
0
def main():
    mtc = MTurkConnection(aws_access_key_id='AKIAIQ3NNF7F7GTVRASA',
                          aws_secret_access_key='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',
                          host='mechanicalturk.sandbox.amazonaws.com')

    hits = get_all_reviewable_hits(mtc)

    for hit in hits:
        assignments = mtc.get_assignments(hit.HITId)
        for assignment in assignments:
            print assignment.AssignmentId
            for qf in assignment.answers[0]:
                print qf.fields[0]
            print "--------------------"
Ejemplo n.º 57
0
    def post(self):
        access_key = self.request.get(argument_name='accessKey',
                                      default_value=None)
        secret_key = self.request.get(argument_name='secretKey',
                                      default_value=None)
        answers_limit = self.request.get(argument_name='answersLimit',
                                         default_value=None)
        image = self.request.get(argument_name='image', default_value=None)
        text = self.request.get(argument_name='text', default_value=None)
        if access_key == None or secret_key == None or answers_limit == None or text == None:
            self.error(400)
            return

        if image == None:
            entity = Question(text=text)
        else:
            raw_image = images.Image(image_data=image)
            raw_image.resize(height=440, width=440)
            hit_image = raw_image.execute_transforms(
                output_encoding=images.PNG)
            entity = Question(image=hit_image, text=text)
        entity.put()

        connection = MTurkConnection(aws_access_key_id=access_key,
                                     aws_secret_access_key=secret_key,
                                     host='mechanicalturk.amazonaws.com')
        question = ExternalQuestion(
            'http://www.turgleapi.com/api/hit?questionId=' +
            str(entity.key().id()), 880)
        result_set = connection.create_hit(
            question=question,
            lifetime=timedelta(minutes=90),
            max_assignments=answers_limit,
            title='Answer A Simple Question',
            description='Can you answer a simple question for me?',
            keywords='question, simple',
            reward=0.01,
            duration=timedelta(minutes=9),
            approval_delay=timedelta(days=3))
        try:
            for hit in result_set:
                entity.hit_id = hit.HITId
                entity.put()
        except:
            self.error(500)
            return

        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(
            simplejson.dumps({'questionId': entity.key().id()}, indent=4))
def create_hits():
	if SANDBOX:
		mturk_url = 'mechanicalturk.sandbox.amazonaws.com'
		preview_url = 'https://workersandbox.mturk.com/mturk/preview?groupId='
	else:
		mturk_url = 'mechanicalturk.amazonaws.com'
		preview_url = 'https://mturk.com/mturk/preview?groupId='

	q = ExternalQuestion(external_url=HIT_URL, frame_height=800)
	conn = MTurkConnection(aws_access_key_id=AWS_ACCESS_KEY, aws_secret_access_key=AWS_SECRET_KEY, host=mturk_url)
	for i in range(0, NUMBER_OF_HITS):
		create_hit_rs = conn.create_hit(question=q, lifetime=LIFETIME, max_assignments=NUMBER_OF_ASSIGNMENTS, title=TITLE, keywords=KEYWORDS, reward=REWARD, duration=DURATION, approval_delay=APPROVAL_DELAY, annotation=DESCRIPTION)
		print(preview_url + create_hit_rs[0].HITTypeId)
		print("HIT ID: " + create_hit_rs[0].HITId)