예제 #1
0
    def run(self):
        if self.articles:
            article = random.sample(self.articles, 1)[0]
            print(colors.code(fg='green') + 'Article: ' + article + '\n')
        else:
            article = raw_input(
                colors.code(fg='green') + 'Article: ').decode('utf-8')
        self.bot.set_article(article, session_key='12345')

        while True:
            user_utt = raw_input(
                colors.code(fg='yellow') + 'Me: ').decode('utf-8')
            print(colors.reset_code())
            bot_utt, all_utt_dicts = self.bot.respond_to(
                user_utt, session_key='12345')
            print(format_utt_dicts(all_utt_dicts,
                                   utt_line_wrap=60).encode('utf-8'))
            print(colors.colorize(
                'Bot: ' + bot_utt, fg='green').encode('utf-8'))
            if self.score_answers:
                n = self._get_utt_number(
                    'Enter the number of the best answer: ',
                    len(all_utt_dicts))
                if n is not None:
                    self.bot.log_best_utt_num(n, session_key=12345)
                else:
                    print('Invalid number, not scoring.')
예제 #2
0
 def run(self):
     session_prefix = str(uuid.uuid4())
     art_cnt = 1
     session_key = session_prefix + str(art_cnt)
     for line in codecs.open(self.utt_file, encoding='utf8'):
         line = line.strip()
         if not line:
             continue
         while session_key in self.computations:
             for _sk, res in self.get_responses():
                 assert _sk == session_key
                 if config.debug:
                     pprint.pprint(
                         AsyncBot.get_debug_information(session_key))
                 self.print_response(res[1], res[0])
             time.sleep(1)
         if line.lower().startswith('article: '):
             art_cnt += 1
             self.remove_session(session_key)
             session_key = session_prefix + str(art_cnt)
             article = line[len('article: '):]
             print(colors.code(fg='green') + 'Article: ' + article + '\n')
             if article:
                 comp_started = self.async_set_article(session_key, article)
                 assert comp_started
         else:
             utt = line
             print(colors.colorize('User: '******'yellow'))
             comp_started = self.async_handle_user_utt(session_key, utt)
             assert comp_started
     if self.gather_stats:
         self.print_stats()
예제 #3
0
 def check_user_nags(self):
     t = time.time()
     for session_key, (nag_time, nag_cnt) in self.user_nag_timer.items():
         if t > nag_time:
             nag = user_nags[min(len(user_nags) - 1, nag_cnt)]
             print(
                 colors.colorize("Nagging the user in %s" % (session_key, ),
                                 fg="blue"))
             yield (session_key, (nag, []))
             self.set_user_nag_timer(session_key, bump=1)
예제 #4
0
 def respond_to(
         self, state, last_user_utt_dict, last_bot_utt, user_utt_dict):
     t0 = dt.datetime.now()
     state, ret, score = self._respond_to(
         state, last_user_utt_dict, last_bot_utt, user_utt_dict)
     seconds = (dt.datetime.now() - t0).total_seconds()
     print(colors.colorize('[%s took %.2f s]' % (self.name, seconds),
                           fg='purple'))
     ret = U(ret)
     return state, ret, score
예제 #5
0
 def log_dialogue_pair(self, session_key, user_utt, bot_utts):
     if not self.keep_logs or user_utt is None or not bot_utts:
         return
     try:
         d = {
             'dialogue_pair': {
                 'user': user_utt,
                 'bot': bot_utts[0],
                 'all_talkers': bot_utts[1],
             }
         }
         self.write_to_logfile(d, session_key)
     except:
         print(colors.colorize(utils.get_ex_info(), fg="red"))
예제 #6
0
    def print_response(self, all_utt_dicts, bot_utt):
        if self.gather_stats:
            for ud in all_utt_dicts:
                if ud['talker_name'].endswith('sel_fup'):
                    self.stats['_followup_count'] += 1
                    self.stats[ud['talker_name']] += 1
                elif not ud['talker_name'].endswith('fup'):
                    self.stats['_response_count'] += 1
                    self.stats[ud['talker_name']] += 1
                    break  # don't record other bots

        print(
            format_utt_dicts(all_utt_dicts, utt_line_wrap=60).encode('utf-8'))
        print(colors.colorize('Bot: ' + bot_utt, fg='green').encode('utf-8'))
예제 #7
0
    def async_set_article(self, session_key, article):
        self.set_user_nag_timer(session_key)

        if session_key in self.computations:
            return False
        self.computations[session_key] = AsyncBot.async_set_article(
            session_key, article, self.talker_names)
        self.computations[session_key]._t_start = time.time()
        self.computations[session_key]._user_utt = None

        if self.keep_logs:
            try:
                d = {'article': {'text': article}, 'session_key': session_key}
                self.write_to_logfile(d, session_key)
            except:
                print(colors.colorize(utils.get_ex_info(), fg="red"))
        return True
예제 #8
0
    def get_responses(self):
        new_comps = {}
        ready = []
        to_start = []
        for session_key, comp in self.computations.items():
            if not comp.ready():
                if (time.time() - comp._t_start >
                        config.global_response_timeout):
                    celery.revoke_all(comp)
                    print(
                        colors.colorize("Removing stale computation for %s" %
                                        (session_key),
                                        fg='red'))
                    # try to salvage a result!
                    utt = AsyncBot.get_utterances_so_far(session_key)
                    self.log_dialogue_pair(session_key, comp._user_utt, utt)
                    if utt:
                        ready.append((session_key, utt))
                        self.set_user_nag_timer(session_key)
                else:
                    new_comps[session_key] = comp
            else:
                try:
                    res = comp.get()
                except:
                    try:
                        res = AsyncBot.get_utterances_so_far(session_key)
                    except:
                        res = None
                    print(colors.colorize(utils.get_ex_info(), fg='red'))
                self.log_dialogue_pair(session_key, comp._user_utt, res)

                if 0:
                    print(
                        colors.colorize("Got async results: %s" % (res, ),
                                        fg='blue'))
                else:
                    print(
                        colors.colorize("Got async results for %s" %
                                        (session_key, ),
                                        fg='blue'))

                if session_key in self.unprocessed_utts:
                    to_start.append(session_key)
                else:
                    # Start nagging timer
                    self.set_user_nag_timer(session_key)

                if res and res[0] is not None:
                    # Only return reponses, not results of set article
                    # and similar crap
                    ready.append((session_key, res))
                for timings_key in [session_key, 'all_sessions']:
                    timings = get_timings(timings_key)  # TODO XXX
                    if timings and self.print_timings:
                        print(
                            colors.colorize(
                                'Total task time in session %s %f:' % (
                                    timings_key,
                                    sum((t[1] for t in timings)),
                                ),
                                fg='blue'))
                        print(
                            colors.colorize(pprint.pformat(timings,
                                                           depth=2,
                                                           width=60),
                                            fg='blue'))
                for errors_key in [session_key, 'all_sessions']:
                    errors = get_async_errors(errors_key)
                    if errors:
                        print(
                            colors.colorize('Errors during query %s:\n%s' % (
                                timings_key,
                                u'\n'.join(errors),
                            ),
                                            fg='red'))

                print(
                    colors.colorize('Bot took %f s' %
                                    (time.time() - comp._t_start),
                                    fg='blue'))
        self.computations = new_comps
        for session_key in to_start:
            ret = self.async_handle_user_utt(session_key, '')
            assert ret
        ready.extend(self.check_user_nags())
        return ready