From 0407f40190581bb7edfc68fd8e344caba34a6b09 Mon Sep 17 00:00:00 2001 From: Will Feng Date: Wed, 7 Jun 2017 17:36:09 -0400 Subject: [PATCH 1/6] Created MTurkAgent --- parlai/mturk/core/agents.py | 202 ++++++++++++++ parlai/mturk/core/data_model.py | 25 +- parlai/mturk/core/handler_template.py | 60 +++-- parlai/mturk/core/manage_hit.py | 247 ------------------ parlai/mturk/core/mturk_index.html | 20 +- parlai/mturk/core/setup_aws.py | 10 +- .../mturk/tasks/multi_agent_dialog/sample.py | 22 ++ 7 files changed, 294 insertions(+), 292 deletions(-) create mode 100644 parlai/mturk/core/agents.py delete mode 100644 parlai/mturk/core/manage_hit.py create mode 100644 parlai/mturk/tasks/multi_agent_dialog/sample.py diff --git a/parlai/mturk/core/agents.py b/parlai/mturk/core/agents.py new file mode 100644 index 00000000000..0fd58687eac --- /dev/null +++ b/parlai/mturk/core/agents.py @@ -0,0 +1,202 @@ +# Copyright (c) 2017-present, Facebook, Inc. +# All rights reserved. +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. An additional grant +# of patent rights can be found in the PATENTS file in the same directory. + +from parlai.core.agents import Agent +from parlai.core.worlds import display_messages + +import os +import time +from datetime import datetime +import random +import string +import webbrowser +import json +import requests +from parlai.core.agents import create_agent_from_shared +from .setup_aws import setup_aws, check_mturk_balance, create_hit_type, create_hit_with_hit_type, setup_aws_credentials + + +def _get_new_messages(json_api_endpoint_url, task_group_id, conversation_id, after_message_id, excluded_agent_id=None, included_agent_id=None): + params = { + 'method_name': 'get_new_messages', + 'task_group_id': task_group_id, + 'last_message_id': after_message_id, + 'conversation_id': conversation_id, + } + if excluded_agent_id: + params['excluded_agent_id'] = excluded_agent_id + if included_agent_id: + params['included_agent_id'] = included_agent_id + + request = requests.get(json_api_endpoint_url, params=params) + return json.loads(request.json()) + +def _send_new_message(json_api_endpoint_url, task_group_id, conversation_id, agent_id, message_text=None, reward=None, episode_done=False): + post_data_dict = { + 'method_name': 'send_new_message', + 'task_group_id': task_group_id, + 'conversation_id': conversation_id, + 'cur_agent_id': agent_id, + 'episode_done': episode_done, + } + if message_text: + post_data_dict['text'] = message_text + if reward: + post_data_dict['reward'] = reward + + request = requests.post(json_api_endpoint_url, data=json.dumps(post_data_dict)) + return json.loads(request.json()) + +def _get_review_status_count(json_api_endpoint_url, task_group_id, conversation_id, review_status, requester_key): + params = { + 'method_name': 'get_review_status_count', + 'task_group_id': task_group_id, + 'conversation_id': conversation_id, + 'review_status': review_status, + 'requester_key': requester_key + } + request = requests.get(json_api_endpoint_url, params=params) + return request.json() + +class MTurkAgent(Agent): + + skip_init = False + html_api_endpoint_url = None + json_api_endpoint_url = None + requester_key_gt = None + + def __init__(self, opt, shared=None): + super().__init__(opt) + + self.id = opt['agent_id'] + self.task_name = opt['task_name'] + self.is_sandbox = opt['is_sandbox'] + self.conversation_id = opt['conversation_id'] + self.mturk_agent_ids = opt['mturk_agent_ids'] + + self.hit_reward = opt.get('reward', 0.5) + self.hit_title = opt.get('hit_title', 'hit title') + self.hit_description = opt.get('hit_description', 'hit description') + self.hit_keywords = opt.get('hit_keywords', 'hitkeywords') + self.task_description = "'''" + opt.get('task_description', 'hit task description') + "'''" + + self.last_message_id = 0 + + if not self.__class__.skip_init: + print("\nYou are going to allow workers from Amazon Mechanical Turk to chat with your dialog model running on your local machine.\nDuring this process, Internet connection is required, and you should turn off your computer's auto-sleep feature.\n") + key_input = input("Please press Enter to continue... ") + print("") + + setup_aws_credentials() + + if not check_mturk_balance(num_hits=1, hit_reward=self.hit_reward, is_sandbox=self.is_sandbox): + return + + if not self.__class__.skip_init: + print('Setting up MTurk backend...') + html_api_endpoint_url, json_api_endpoint_url, requester_key_gt = setup_aws(self.task_description, 1, self.is_sandbox) + self.__class__.html_api_endpoint_url = html_api_endpoint_url + self.__class__.json_api_endpoint_url = json_api_endpoint_url + self.__class__.requester_key_gt = requester_key_gt + + self.__class__.skip_init = True + self.html_api_endpoint_url = self.__class__.html_api_endpoint_url + self.json_api_endpoint_url = self.__class__.json_api_endpoint_url + self.requester_key_gt = self.__class__.requester_key_gt + + self.task_group_id = str(self.task_name) + '_' + str(self.conversation_id) + + print('Creating HITs...') + hit_type_id = create_hit_type( + hit_title=self.hit_title, + hit_description=self.hit_description + ' (ID: ' + self.task_group_id + ', Role: ' + self.id + ')', + hit_keywords=self.hit_keywords, + hit_reward=self.hit_reward, + is_sandbox=self.is_sandbox + ) + mturk_chat_url = self.html_api_endpoint_url + "?method_name=chat_index&task_group_id="+str(self.task_group_id)+"&conversation_id="+str(self.conversation_id)+"&cur_agent_id="+str(self.id) + mturk_page_url = create_hit_with_hit_type( + page_url=mturk_chat_url, + hit_type_id=hit_type_id, + is_sandbox=self.is_sandbox + ) + + print("MTurk setup done.\n") + print("Link to your HIT: " + mturk_page_url + "\n") + print("Waiting for Turkers to complete the tasks... (Please don't close your laptop or put your computer into sleep or standby mode.)\n") + + def observe(self, msg): + if msg['id'] not in self.mturk_agent_ids: # If the message sender is an mturk agent, then there is no need to upload this message to db since it's already been done on the message sender side. + agent_last_message_in_db = None + conversation_dict = _get_new_messages( + json_api_endpoint_url=self.json_api_endpoint_url, + task_group_id=self.task_group_id, + conversation_id=self.conversation_id, + after_message_id=self.last_message_id, + included_agent_id=msg['id'])['conversation_dict'] + if self.conversation_id in conversation_dict: + agent_last_message_in_db = conversation_dict[self.conversation_id][0] + agent_last_message_in_db.pop('message_id', None) + if 'episode_done' not in msg: + msg['episode_done'] = False + if agent_last_message_in_db == msg: + return + + _send_new_message( + json_api_endpoint_url=self.json_api_endpoint_url, + task_group_id=self.task_group_id, + conversation_id=self.conversation_id, + agent_id=msg['id'], + message_text=msg.get('text', None), + reward=msg.get('reward', None), + episode_done=msg.get('episode_done', False), + ) + + def act(self): + while True: + ret = _get_new_messages( + json_api_endpoint_url=self.json_api_endpoint_url, + task_group_id=self.task_group_id, + conversation_id=self.conversation_id, + after_message_id=self.last_message_id, + included_agent_id=self.id + ) + conversation_dict = ret['conversation_dict'] + + if str(self.conversation_id) in conversation_dict: + new_last_message_id = ret['last_message_id'] + if new_last_message_id: + self.last_message_id = new_last_message_id + + new_messages = conversation_dict[str(self.conversation_id)] + + print(display_messages([new_messages[0]])) + return new_messages[0] + + time.sleep(1) + + def epoch_done(self): + return False + + def shutdown(self): + if _get_review_status_count(json_api_endpoint_url=self.json_api_endpoint_url, task_group_id=self.task_group_id, conversation_id=self.conversation_id, review_status='approved', requester_key=self.requester_key_gt) + \ + _get_review_status_count(json_api_endpoint_url=self.json_api_endpoint_url, task_group_id=self.task_group_id, conversation_id=self.conversation_id, review_status='rejected', requester_key=self.requester_key_gt) > 0: + return + else: + while _get_review_status_count(json_api_endpoint_url=self.json_api_endpoint_url, task_group_id=self.task_group_id, conversation_id=self.conversation_id, review_status='pending', requester_key=self.requester_key_gt) < len(self.mturk_agent_ids): + time.sleep(2) + + mturk_agent_ids_string = str(self.mturk_agent_ids).replace("'", '''"''') + mturk_approval_url = self.html_api_endpoint_url + "?method_name=approval_index&task_group_id="+str(self.task_group_id)+"&conversation_id="+str(self.conversation_id)+"&mturk_agent_ids="+mturk_agent_ids_string+"&requester_key="+self.requester_key_gt + print("\nAll HITs are done! Please go to the following link to approve/reject them (or they will be auto-approved in 4 weeks if no action is taken):\n") + print(mturk_approval_url) + print("") + + # Loop for checking approval status + while _get_review_status_count(json_api_endpoint_url=self.json_api_endpoint_url, task_group_id=self.task_group_id, conversation_id=self.conversation_id, review_status='pending', requester_key=self.requester_key_gt) > 0: + time.sleep(2) + + print("All reviews are done!") \ No newline at end of file diff --git a/parlai/mturk/core/data_model.py b/parlai/mturk/core/data_model.py index 9f204440926..f93e3579bee 100644 --- a/parlai/mturk/core/data_model.py +++ b/parlai/mturk/core/data_model.py @@ -100,7 +100,7 @@ def send_new_message(db_session, task_group_id, conversation_id, agent_id, messa return new_message_object -def get_new_messages(db_session, task_group_id, conversation_id=None, after_message_id=None, excluded_agent_id=None, populate_meta_info=False): +def get_new_messages(db_session, task_group_id, conversation_id=None, after_message_id=None, excluded_agent_id=None, included_agent_id=None, populate_meta_info=False): """ Return: conversation_dict = { @@ -123,6 +123,10 @@ def get_new_messages(db_session, task_group_id, conversation_id=None, after_mess if not after_message_id: after_message_id = -1 + included_agent_ids = [] + if included_agent_id: + included_agent_ids = [included_agent_id] + excluded_agent_ids = [] if excluded_agent_id: excluded_agent_ids = [excluded_agent_id] @@ -130,6 +134,8 @@ def get_new_messages(db_session, task_group_id, conversation_id=None, after_mess last_message_id = None query = db_session.query(Message).filter(Message.task_group_id==task_group_id).filter(Message.id > after_message_id) + if len(included_agent_ids) > 0: + query = query.filter(Message.agent_id.in_(included_agent_ids)) if len(excluded_agent_ids) > 0: query = query.filter(~Message.agent_id.in_(excluded_agent_ids)) if conversation_id: @@ -164,7 +170,12 @@ def get_new_messages(db_session, task_group_id, conversation_id=None, after_mess def set_hit_info(db_session, task_group_id, conversation_id, assignment_id, hit_id, worker_id, is_sandbox, approval_status='pending'): - existing_object = db_session.query(MTurkHITInfo).filter(MTurkHITInfo.task_group_id==task_group_id).filter(MTurkHITInfo.conversation_id==conversation_id).first() + existing_object = db_session.query(MTurkHITInfo) \ + .filter(MTurkHITInfo.task_group_id==task_group_id) \ + .filter(MTurkHITInfo.conversation_id==conversation_id) \ + .filter(MTurkHITInfo.assignment_id==assignment_id) \ + .filter(MTurkHITInfo.hit_id==hit_id) \ + .first() if not existing_object: new_hit_info_object = MTurkHITInfo( task_group_id=task_group_id, @@ -187,12 +198,12 @@ def set_hit_info(db_session, task_group_id, conversation_id, assignment_id, hit_ db_session.commit() -def get_hit_info(db_session, task_group_id, conversation_id): - existing_object = db_session.query(MTurkHITInfo).filter(MTurkHITInfo.task_group_id==task_group_id).filter(MTurkHITInfo.conversation_id==conversation_id).first() - return existing_object +def get_all_matching_hit_infos(db_session, task_group_id, conversation_id): + matching_hit_infos = list(db_session.query(MTurkHITInfo).filter(MTurkHITInfo.task_group_id==task_group_id).filter(MTurkHITInfo.conversation_id==conversation_id).all()) + return matching_hit_infos -def get_pending_review_count(db_session, task_group_id): - return db_session.query(MTurkHITInfo).filter(MTurkHITInfo.task_group_id==task_group_id).filter(MTurkHITInfo.approval_status=='pending').count() +def get_review_status_count(db_session, task_group_id, conversation_id, review_status): + return db_session.query(MTurkHITInfo).filter(MTurkHITInfo.task_group_id==task_group_id).filter(MTurkHITInfo.conversation_id==conversation_id).filter(MTurkHITInfo.approval_status==review_status).count() def get_all_review_status(db_session, task_group_id): return db_session.query(MTurkHITInfo).filter(MTurkHITInfo.task_group_id==task_group_id).order_by(MTurkHITInfo.conversation_id).all() \ No newline at end of file diff --git a/parlai/mturk/core/handler_template.py b/parlai/mturk/core/handler_template.py index ce5a0d3b0d0..b4155a438ac 100755 --- a/parlai/mturk/core/handler_template.py +++ b/parlai/mturk/core/handler_template.py @@ -112,6 +112,7 @@ def get_new_messages(event, context): if 'conversation_id' in event['query']: conversation_id = int(event['query']['conversation_id']) excluded_agent_id = event['query'].get('excluded_agent_id', None) + included_agent_id = event['query'].get('included_agent_id', None) conversation_dict, new_last_message_id = data_model.get_new_messages( db_session=db_session, @@ -119,6 +120,7 @@ def get_new_messages(event, context): conversation_id=conversation_id, after_message_id=last_message_id, excluded_agent_id=excluded_agent_id, + included_agent_id=included_agent_id, populate_meta_info=True ) @@ -179,12 +181,12 @@ def approval_index(event, context): task_group_id = event['query']['task_group_id'] conversation_id = event['query']['conversation_id'] - cur_agent_id = event['query']['cur_agent_id'] + mturk_agent_ids = event['query']['mturk_agent_ids'] template_context = {} template_context['task_group_id'] = task_group_id template_context['conversation_id'] = conversation_id - template_context['cur_agent_id'] = cur_agent_id + template_context['mturk_agent_ids'] = mturk_agent_ids template_context['task_description'] = task_description template_context['is_cover_page'] = False template_context['is_approval_page'] = True @@ -212,39 +214,41 @@ def review_hit(event, context): conversation_id = int(params['conversation_id']) action = params['action'] # 'approve' or 'reject' - hit_info = data_model.get_hit_info( + hit_infos = data_model.get_all_matching_hit_infos( db_session=db_session, task_group_id=task_group_id, conversation_id=conversation_id ) - if hit_info: - assignment_id = hit_info.assignment_id - client = boto3.client( - service_name = 'mturk', - region_name = 'us-east-1', - endpoint_url = 'https://mturk-requester-sandbox.us-east-1.amazonaws.com' - ) - # Region is always us-east-1 - if not hit_info.is_sandbox: - client = boto3.client(service_name = 'mturk', region_name='us-east-1') - - if action == 'approve': - client.approve_assignment(AssignmentId=assignment_id) - hit_info.approval_status = 'approved' - elif action == 'reject': - client.reject_assignment(AssignmentId=assignment_id, RequesterFeedback='') - hit_info.approval_status = 'rejected' - db_session.add(hit_info) - db_session.commit() + if len(hit_infos) > 0: + for hit_info in hit_infos: + assignment_id = hit_info.assignment_id + client = boto3.client( + service_name = 'mturk', + region_name = 'us-east-1', + endpoint_url = 'https://mturk-requester-sandbox.us-east-1.amazonaws.com' + ) + # Region is always us-east-1 + if not hit_info.is_sandbox: + client = boto3.client(service_name = 'mturk', region_name='us-east-1') + + if action == 'approve': + client.approve_assignment(AssignmentId=assignment_id) + hit_info.approval_status = 'approved' + elif action == 'reject': + client.reject_assignment(AssignmentId=assignment_id, RequesterFeedback='') + hit_info.approval_status = 'rejected' + db_session.add(hit_info) + db_session.commit() + except KeyError: raise Exception('400') -def get_pending_review_count(event, context): +def get_review_status_count(event, context): if event['method'] == 'GET': """ Handler for getting the number of pending reviews. - Expects , as query parameters. + Expects , , as query parameters. """ try: requester_key = event['query']['requester_key'] @@ -252,9 +256,13 @@ def get_pending_review_count(event, context): raise Exception('403') task_group_id = event['query']['task_group_id'] - return data_model.get_pending_review_count( + conversation_id = event['query']['conversation_id'] + review_status = event['query']['review_status'] + return data_model.get_review_status_count( db_session=db_session, - task_group_id=task_group_id + task_group_id=task_group_id, + conversation_id=conversation_id, + review_status=review_status ) except KeyError: raise Exception('400') diff --git a/parlai/mturk/core/manage_hit.py b/parlai/mturk/core/manage_hit.py deleted file mode 100644 index 244a22cd7b9..00000000000 --- a/parlai/mturk/core/manage_hit.py +++ /dev/null @@ -1,247 +0,0 @@ -# Copyright (c) 2017-present, Facebook, Inc. -# All rights reserved. -# This source code is licensed under the BSD-style license found in the -# LICENSE file in the root directory of this source tree. An additional grant -# of patent rights can be found in the PATENTS file in the same directory. - -import os -import time -from datetime import datetime -import random -import string -import webbrowser -import json -import requests -from parlai.core.agents import create_agent_from_shared -from .setup_aws import setup_aws, check_mturk_balance, create_hit_type, create_hit_with_hit_type, setup_aws_credentials - - -def _get_random_alphanumeric_string(N): - return ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(N)) - - -def _setup_relay(task_config, num_hits, is_sandbox): - """Sets up relay server - """ - # set up relay server - html_api_endpoint_url, json_api_endpoint_url, requester_key_gt = setup_aws(task_config, num_hits, is_sandbox) - - return html_api_endpoint_url, json_api_endpoint_url, requester_key_gt - -def _send_new_message(json_api_endpoint_url, task_group_id, conversation_id, agent_id, message_text=None, reward=None, episode_done=False): - post_data_dict = { - 'method_name': 'send_new_message', - 'task_group_id': task_group_id, - 'conversation_id': conversation_id, - 'cur_agent_id': agent_id, - 'episode_done': episode_done, - } - if message_text: - post_data_dict['text'] = message_text - if reward: - post_data_dict['reward'] = reward - - request = requests.post(json_api_endpoint_url, data=json.dumps(post_data_dict)) - return json.loads(request.json()) - -def _get_new_messages(json_api_endpoint_url, task_group_id, after_message_id, excluded_agent_id=None): - params = { - 'method_name': 'get_new_messages', - 'task_group_id': task_group_id, - 'last_message_id': after_message_id, - } - if excluded_agent_id: - params['excluded_agent_id'] = excluded_agent_id - - request = requests.get(json_api_endpoint_url, params=params) - return json.loads(request.json()) - -def _get_pending_review_count(json_api_endpoint_url, task_group_id, requester_key): - params = { - 'method_name': 'get_pending_review_count', - 'task_group_id': task_group_id, - 'requester_key': requester_key - } - request = requests.get(json_api_endpoint_url, params=params) - return request.json() - -def _get_all_review_status(json_api_endpoint_url, task_group_id, requester_key): - params = { - 'method_name': 'get_all_review_status', - 'task_group_id': task_group_id, - 'requester_key': requester_key - } - request = requests.get(json_api_endpoint_url, params=params) - return request.json() - -def create_hits(opt, task_config, task_module_name, bot, chat_page_only=False): - num_hits = opt['num_hits'] - hit_reward = opt['reward'] - is_sandbox = opt['is_sandbox'] - verbose = opt['verbose'] - - print("\nYou are going to allow workers from Amazon Mechanical Turk to chat with your dialog model running on your local machine.\nDuring this process, Internet connection is required, and you should turn off your computer's auto-sleep feature.\n") - key_input = input("Please press Enter to continue... ") - print("") - - setup_aws_credentials() - if not check_mturk_balance(num_hits=num_hits, hit_reward=hit_reward, is_sandbox=is_sandbox): - return - - task_group_id = str(int(time.time())) + '_' + _get_random_alphanumeric_string(10) # Random string to further avoid collision - - print('Setting up MTurk backend...') - html_api_endpoint_url, json_api_endpoint_url, requester_key_gt = _setup_relay(task_config, num_hits, is_sandbox) - - approval_index_url_template = html_api_endpoint_url + "?method_name=approval_index&task_group_id={{task_group_id}}&conversation_id=1&cur_agent_id={{cur_agent_id}}&requester_key="+requester_key_gt - - worker_agent_id = task_config['worker_agent_id'] - bot_agent_id = bot.getID() - cids = range(1, num_hits+1) - cid_map = {cid: i for i, cid in enumerate(cids)} - c_done_map = {cid: False for cid in cids} - logs = {cid: [] for cid in cids} - - shared = bot.share() - bots = [] - last_message_id = -1 - - # If the bot needs to send the first message of the conversation, it will send it here - for cid in cids: - new_bot = create_agent_from_shared(shared) - new_bot.conversation_id = cid - bots.append(new_bot) - response = new_bot.act() - if response: - if response.get('episode_done', False): - c_done_map[cid] = True - if verbose: - print('Conversation '+str(cid)+' - Bot says: ' + str(response)) - logs[cid].append(response) - new_message = _send_new_message( - json_api_endpoint_url=json_api_endpoint_url, - task_group_id=task_group_id, - conversation_id=cid, - agent_id=bot_agent_id, - message_text=response.get('text', None), - reward=response.get('reward', None), - episode_done=response.get('episode_done', False), - ) - if new_message['message_id'] > last_message_id: - last_message_id = new_message['message_id'] - - hits_created = False - conversations_remaining = set(cids) - - # Main loop for polling and handling new messages - while len(conversations_remaining) > 0: - ret = _get_new_messages( - json_api_endpoint_url=json_api_endpoint_url, - task_group_id=task_group_id, - after_message_id=last_message_id, - excluded_agent_id=bot_agent_id, - ) - conversation_dict = ret['conversation_dict'] - new_last_message_id = ret['last_message_id'] - - if new_last_message_id: - last_message_id = new_last_message_id - - time.sleep(1) - - for conversation_id, new_messages in conversation_dict.items(): - conversation_id = int(conversation_id) - if conversation_id in conversations_remaining and len(new_messages) > 0: - agent = bots[cid_map[conversation_id]] - for new_message in new_messages: - if verbose: - print('Conversation '+str(conversation_id)+' - Bot received: ' + str(new_message)) - logs[conversation_id].append(new_message) - agent.observe(new_message) - if new_message.get('episode_done', False) or c_done_map[conversation_id]: - # We're done here - conversations_remaining.remove(conversation_id) - print('Conversation '+str(conversation_id)+' is DONE!\n') - else: - # Agent still needs to reply - response = agent.act() - if response: - if response.get('episode_done', False): - c_done_map[conversation_id] = True - if verbose: - print('Conversation '+str(conversation_id)+' - Bot says: ' + str(response)) - logs[conversation_id].append(response) - _send_new_message( - json_api_endpoint_url=json_api_endpoint_url, - task_group_id=task_group_id, - conversation_id=conversation_id, - agent_id=bot_agent_id, - message_text=response.get('text', None), - reward=response.get('reward', None), - episode_done=response.get('episode_done', False), - ) - - # We don't create new HITs until this point, so that the HIT page will always have the conversation fully populated. - if not hits_created: - print('Creating HITs...') - hit_type_id = create_hit_type( - hit_title=task_config['hit_title'], - hit_description=task_config['hit_description'] + ' (ID: ' + task_group_id + ')', - hit_keywords=task_config['hit_keywords'], - hit_reward=hit_reward, - is_sandbox=is_sandbox - ) - mturk_chat_url = None - mturk_page_url = None - for cid in cids: - mturk_chat_url = html_api_endpoint_url + "?method_name=chat_index&task_group_id="+str(task_group_id)+"&conversation_id="+str(cid)+"&cur_agent_id="+str(worker_agent_id) - if not chat_page_only: - mturk_page_url = create_hit_with_hit_type( - page_url=mturk_chat_url, - hit_type_id=hit_type_id, - is_sandbox=is_sandbox - ) - - print("MTurk setup done.\n") - if chat_page_only: - webbrowser.open(mturk_chat_url) - else: - print("Link to your HIT: " + mturk_page_url + "\n") - print("Waiting for Turkers to complete the tasks... (Please don't close your laptop or put your computer into sleep or standby mode.)\n") - hits_created = True - - while _get_pending_review_count(json_api_endpoint_url=json_api_endpoint_url, task_group_id=task_group_id, requester_key=requester_key_gt) != num_hits: - time.sleep(2) - - mturk_approval_url = html_api_endpoint_url + "?method_name=approval_index&task_group_id="+str(task_group_id)+"&conversation_id=1&cur_agent_id="+worker_agent_id+"&requester_key="+requester_key_gt - print("\nAll HITs are done! Please go to the following link to approve/reject them (or they will be auto-approved in 4 weeks if no action is taken):\n") - print(mturk_approval_url) - print("") - - approval_status_dict = {cid: '' for cid in cids} - # Loop for checking approval status - while _get_pending_review_count(json_api_endpoint_url=json_api_endpoint_url, task_group_id=task_group_id, requester_key=requester_key_gt) > 0: - time.sleep(2) - - print("Approvals are done!") - - for hit_info in _get_all_review_status(json_api_endpoint_url=json_api_endpoint_url, task_group_id=task_group_id, requester_key=requester_key_gt): - conversation_id = hit_info['conversation_id'] - approval_status_dict[conversation_id] = hit_info['approval_status'] - - logs_approved = {cid:log for (cid,log) in logs.items() if approval_status_dict[cid] == 'approved'} - logs_rejected = {cid:log for (cid,log) in logs.items() if approval_status_dict[cid] == 'rejected'} - - # Saving logs to file - # Log format: {conversation_id: [list of messages in the conversation]} - mturk_log_path = opt['mturk_log_path'] - task_group_path = os.path.join(mturk_log_path, - task_module_name + '_' + - datetime.now().strftime('%Y-%m-%d_%H:%M:%S')) - os.makedirs(task_group_path) - with open(os.path.join(task_group_path, 'approved.json'), 'w') as fout: - fout.write(json.dumps(logs_approved)) - with open(os.path.join(task_group_path, 'rejected.json'), 'w') as fout: - fout.write(json.dumps(logs_rejected)) - - print("All conversations are saved to "+opt['mturk_log_path']+" in JSON format.\n") diff --git a/parlai/mturk/core/mturk_index.html b/parlai/mturk/core/mturk_index.html index 3ab11c89838..a6494c9ceae 100755 --- a/parlai/mturk/core/mturk_index.html +++ b/parlai/mturk/core/mturk_index.html @@ -180,6 +180,7 @@

Live Chat

show_new_messages_on_UI(new_messages); if (done_after_responding) { update_for_response_type('done'); + task_done = true; } else { check_done(new_messages); } @@ -242,10 +243,15 @@

Live Chat

}); var mturk_submit_url = `{{mturk_submit_url}}`; - var verbose_log = false; + var verbose_log = true; var task_group_id = `{{task_group_id}}`; var conversation_id = parseInt(`{{conversation_id}}`); var cur_agent_id = `{{cur_agent_id}}`; + var mturk_agent_ids = `{{mturk_agent_ids}}`; + if (mturk_agent_ids) { + mturk_agent_ids = JSON.parse(mturk_agent_ids); + console.log(mturk_agent_ids); + } var is_cover_page = (`{{is_cover_page}}` === 'True') ? true : false; var is_approval_page = (`{{is_approval_page}}` === 'True') ? true : false; var num_hits = parseInt(`{{num_hits}}`); @@ -254,7 +260,7 @@

Live Chat

var messages_processed = {}; var messages_shown = {}; var done_after_responding = false; - var self_display_name = 'You'; + var task_done = false; function show_new_messages_on_UI(new_messages) { for (var i = 0; i < new_messages.length; i++) { @@ -269,7 +275,7 @@

Live Chat

} if (!(message_id in messages_shown)) { - if (agent_id != cur_agent_id) { + if ((!is_approval_page && agent_id != cur_agent_id) || (is_approval_page && $.inArray(agent_id, mturk_agent_ids) == -1)) { $('div#message_thread').append(`