def simple_display(opt, world, turn): if opt['batchsize'] > 1: raise RuntimeError('Simple view only support batchsize=1') act = world.get_acts()[0] if turn == 0: text = "- - - NEW EPISODE: " + act.get('id', "[no agent id]") + " - - -" print(colorize(text, 'highlight')) text = act.get('text', '[no text field]') print(colorize(text, 'text')) labels = act.get('labels', act.get('eval_labels', ['[no labels field]'])) labels = '|'.join(labels) print(' ' + colorize(labels, 'labels'))
def simple_display(opt, world, turn): if opt['batchsize'] > 1: raise RuntimeError('Simple view only support batchsize=1') teacher, response = world.get_acts() if turn == 0: text = "- - - NEW EPISODE: " + teacher.get('id', "[no agent id]") + "- - -" print(colorize(text, 'highlight')) text = teacher.get('text', '[no text field]') print(colorize(text, 'text')) response_text = response.get('text', 'No response') labels = teacher.get('labels', teacher.get('eval_labels', ['[no labels field]'])) labels = '|'.join(labels) print(colorize(' labels: ' + labels, 'labels')) print(colorize(' model: ' + response_text, 'text2'))
def act(self): reply = Message() reply['id'] = self.getID() try: reply_text = input(colorize("Enter Your Message:", 'text') + ' ') # f = open("my_log.txt","a+") # f.write(reply_text+"\n") # f.close() except EOFError: self.finished = True return {'episode_done': True} reply_text = reply_text.replace('\\n', '\n') reply['episode_done'] = False if self.opt.get('single_turn', False): reply.force_set('episode_done', True) reply['label_candidates'] = self.fixedCands_txt if '[DONE]' in reply_text: # let interactive know we're resetting raise StopIteration reply['text'] = reply_text if '[EXIT]' in reply_text: self.finished = True raise StopIteration return reply
def __init__(self, opt, shared=None): super().__init__(opt) self.id = 'localHuman' self.episodeDone = False self.finished = False self.fixedCands_txt = load_cands( self.opt.get('local_human_candidates_file')) print( colorize( "Enter [DONE] if you want to end the episode, [EXIT] to quit.", 'highlight', ))
def simple_display_chateval(opt, world, turn): if opt['batchsize'] > 1: raise RuntimeError('Simple view only support batchsize=1') teacher, response = world.get_acts() if turn == 0: text = "- - - NEW EPISODE: " + teacher.get('id', "[no agent id]") + "- - -" print(colorize(text, 'highlight')) text = teacher.get('text', '[no text field]') print(colorize(text, 'text')) response_text = response.get('text', 'No response') labels = teacher.get('labels', teacher.get('eval_labels', ['[no labels field]'])) labels = '|'.join(labels) print(colorize(' labels: ' + labels, 'labels')) # print(colorize(' model: ' + response_text, 'text2')) # for the chateval timestr = time.strftime("%Y%m%d-%H%M%S") # script_input_path = str(opt.get('script_input_path')) # script_file = open(script_input_path, 'r', encoding='utf-8') # file_name = script_input_path.split('/')[-1].split('.')[0] if opt.get('chateval_multi') == True: print(colorize(' model: ' + response_text, 'text2')) script_response = open( opt.get('script_out_path') + '/' + 'test' + '_' + opt.get('model_file') + '_' + timestr + '.txt', 'w') print(colorize(' model: ' + response_text, 'text2')) script_response.write("%s\n" % (response_text)) # if opt.get('chateval_multi_num') == 2: # script_response.write("%s\n" % (response_text)) # elif opt.get('chateval_multi_num') == 3: # pass # else: # print("We only consider to 2 and 3 turns") else: print("Chateval multiturn script something wrong!")
def __init__(self, opt, shared=None, query_txt=None): super().__init__(opt) self.id = 'localHuman' self.episodeDone = False self.finished = False self.fixedCands_txt = load_cands( self.opt.get('local_human_candidates_file')) # utilizing given query text self.query_txt = self.make_txt_readable(query_txt) self.line_no = 0 print( colorize( "Enter [DONE] if you want to end the episode, [EXIT] to quit.", 'highlight', ))
def act(self): reply = Message() reply['id'] = self.getID() reply_text = input(colorize("Enter Your Message:", 'field') + ' ') reply_text = reply_text.replace('\\n', '\n') if self.opt.get('single_turn', False): reply_text += '[DONE]' reply['episode_done'] = False reply['label_candidates'] = self.fixedCands_txt if '[DONE]' in reply_text: reply.force_set('episode_done', True) self.episodeDone = True reply_text = reply_text.replace('[DONE]', '') reply['text'] = reply_text if '[EXIT]' in reply_text: self.finished = True return reply
def _pretty_lines(indent_space, field, value, style): line = '{}{} {}'.format( indent_space, colorize('[' + field + ']:', 'field'), colorize(value, style) ) return line
def display_messages( msgs: List[Dict[str, Any]], prettify: bool = False, ignore_agent_reply: bool = False, add_fields: str = '', max_len: int = 1000, verbose: bool = False, ) -> Optional[str]: """ Return a string describing the set of messages provided. If prettify is true, candidates are displayed using prettytable. add_fields provides a list of fields in the msgs which should be displayed if verbose is off. """ def _token_losses_line( msg: Dict[str, Any], fields_to_show: List[str], space: str ) -> Optional[str]: """ Displays the loss associated with each token. Can be used for debugging generative models. See TorchGeneratorAgent._construct_token_losses for an example implementation. """ key = 'token_losses' token_losses = msg.get(key, None) if key not in fields_to_show or not token_losses: return None # Reduce losses to 4 significant figures formatted_tl = ' | '.join( [f"{tl[0]} {float('{:.4g}'.format(tl[1]))}" for tl in token_losses] ) return _pretty_lines(space, key, formatted_tl, 'text2') def _pretty_lines(indent_space, field, value, style): line = '{}{} {}'.format( indent_space, colorize('[' + field + ']:', 'field'), colorize(value, style) ) return line lines = [] episode_done = False extra_add_fields_ = add_fields.split(',') for index, msg in enumerate(msgs): if msg is None or (index == 1 and ignore_agent_reply): # We only display the first agent (typically the teacher) if we # are ignoring the agent reply. continue if msg.get('episode_done'): episode_done = True # Possibly indent the text (for the second speaker, if two). space = '' if len(msgs) == 2 and index == 1: space = ' ' agent_id = msg.get('id', '[no id field]') if verbose: line = _pretty_lines( indent_space=space, field='id', value=agent_id, style='id' ) lines.append(line) # Only display rewards !=0 as they are confusing in non-RL tasks. if msg.get('reward', 0) != 0: lines.append(space + '[reward: {r}]'.format(r=msg['reward'])) fields_to_show = [] if verbose: fields_to_show = [field for field in msg] else: fields_to_show = [ field for field in msg if field in list(MUST_SHOW_MESSAGE_FIELDS) + extra_add_fields_ ] fields_to_show.sort() # Display fields without special format for field in fields_to_show: if field not in SPECIAL_FORMATED_DISPLAY_MESSAGE_FIELDS: if type(msg[field]) is list: value = _ellipse(msg[field], sep='\n ') else: value = clip_text(str(msg.get(field)), max_len) line = _pretty_lines( indent_space=space, field=field, value=value, style='text2' ) lines.append(line) # Display fields WITH special format requirements # Display Image if type(msg.get('image')) in [str, torch.Tensor]: lines.append(f'[ image ]: {msg["image"]}') # Display Text if msg.get('text', ''): value = clip_text(msg['text'], max_len) style = 'bold_text' if index == 0 else 'labels' field = 'text' if verbose else agent_id line = _pretty_lines( indent_space=space, field=field, value=value, style=style ) lines.append(line) # Display Label Fields for field in {'labels', 'eval_labels', 'label_candidates', 'text_candidates'}: if msg.get(field) and field in fields_to_show: line = _pretty_lines( indent_space=space, field=field, value=_ellipse(msg[field]), style=field, ) lines.append(line) if msg.get('metrics') and verbose: lines.append( _pretty_lines( indent_space=space, field='metrics', value="\n" + nice_report(msg['metrics']), style='text', ) ) # Handling this separately since we need to clean up the raw output before displaying. token_loss_line = _token_losses_line(msg, fields_to_show, space) if token_loss_line: lines.append(token_loss_line) if episode_done: lines.append( colorize('- - - - - - - END OF EPISODE - - - - - - - - - -', 'highlight') ) return '\n'.join(lines)
def display_messages( msgs: List[Dict[str, Any]], prettify: bool = False, ignore_fields: str = '', max_len: int = 1000, verbose: bool = False, ) -> Optional[str]: """ Return a string describing the set of messages provided. If prettify is true, candidates are displayed using prettytable. ignore_fields provides a list of fields in the msgs which should not be displayed. """ def _token_losses_line(msg: Dict[str, Any], ignore_fields: List[str], space: str) -> Optional[str]: """ Displays the loss associated with each token. Can be used for debugging generative models. See TorchGeneratorAgent._construct_token_losses for an example implementation. """ key = 'token_losses' token_losses = msg.get(key, None) if key in ignore_fields or not token_losses: return None # Reduce losses to 4 significant figures formatted_tl = ' | '.join([ f"{tl[0]} {float('{:.4g}'.format(tl[1]))}" for tl in token_losses ]) return f'{space}[{key}]: {formatted_tl}' lines = [] episode_done = False ignore_fields_ = ignore_fields.split(',') for index, msg in enumerate(msgs): if msg is None or (index == 1 and 'agent_reply' in ignore_fields_): # We only display the first agent (typically the teacher) if we # are ignoring the agent reply. continue agent_id = msg.get('id', '[no id field]') if verbose: lines.append( colorize('[id]:', 'field') + ' ' + colorize(agent_id, 'id')) if msg.get('episode_done'): episode_done = True # Possibly indent the text (for the second speaker, if two). space = '' if len(msgs) == 2 and index == 1: space = ' ' # Only display rewards !=0 as they are confusing in non-RL tasks. if msg.get('reward', 0) != 0: lines.append(space + '[reward: {r}]'.format(r=msg['reward'])) for key in msg: if key not in DISPLAY_MESSAGE_DEFAULT_FIELDS and key not in ignore_fields_: field = colorize('[' + key + ']:', 'field') if type(msg[key]) is list: value = _ellipse(msg[key], sep='\n ') else: value = clip_text(str(msg.get(key)), max_len) line = field + ' ' + colorize(value, 'text2') lines.append(space + line) if type(msg.get('image')) in [str, torch.Tensor]: lines.append(f'[ image ]: {msg["image"]}') if msg.get('text', ''): text = clip_text(msg['text'], max_len) if index == 0: style = 'bold_text' else: style = 'labels' if verbose: lines.append(space + colorize('[text]:', 'field') + ' ' + colorize(text, style)) else: lines.append(space + colorize("[" + agent_id + "]:", 'field') + ' ' + colorize(text, style)) for field in { 'labels', 'eval_labels', 'label_candidates', 'text_candidates' }: if msg.get(field) and field not in ignore_fields_: string = '{}{} {}'.format( space, colorize('[' + field + ']:', 'field'), colorize(_ellipse(msg[field]), field), ) lines.append(string) # Handling this separately since we need to clean up the raw output before displaying. token_loss_line = _token_losses_line(msg, ignore_fields_, space) if token_loss_line: lines.append(token_loss_line) if episode_done: lines.append( colorize('- - - - - - - END OF EPISODE - - - - - - - - - -', 'highlight')) return '\n'.join(lines)
def get_reply(self): reply_text = input(colorize('Enter Your Message:', 'field') + ' ') reply_text = reply_text.replace('\\n', '\n') return reply_text
def __init__(self, *args, **kwargs): from parlai.utils.strings import colorize logo = "" logo += colorize(' _', 'red') + "\n" logo += colorize(' /', 'red') + colorize('"', 'brightblack') logo += colorize(")", "yellow") + "\n" logo += colorize(' //', 'red') + colorize(')', 'yellow') + '\n' logo += colorize(' ==', 'green') logo += colorize("/", 'blue') + colorize('/', 'red') + colorize( "'", 'yellow') logo += colorize("===", 'green') + " ParlAI\n" logo += colorize(" /", 'blue') kwargs['description'] = logo return super().__init__(*args, **kwargs)
def __init__(self, *args, **kwargs): # used to target help messages more correctly, see GH #3182 self._help_subparser = None from parlai.utils.strings import colorize logo = "" logo += colorize(' _', 'red') + "\n" logo += colorize(' /', 'red') + colorize('"', 'brightblack') logo += colorize(")", "yellow") + "\n" logo += colorize(' //', 'red') + colorize(')', 'yellow') + '\n' logo += colorize(' ==', 'green') logo += colorize("/", 'blue') + colorize('/', 'red') + colorize( "'", 'yellow') logo += colorize("===", 'green') + " ParlAI\n" logo += colorize(" /", 'blue') kwargs['description'] = logo return super().__init__(*args, **kwargs)
def display_image(): if os.environ.get('PARLAI_DISPLAY_LOGO') == 'OFF': return logo = colorize('ParlAI - Dialogue Research Platform', 'labels') print(logo)