Example #1
0
    def voice_request_handler(self, update, context):
        """This method handles all voice messages, and asks result_presentation to send the response to the user."""
        try:
            ogg_file = tempfile.NamedTemporaryFile(delete=True)
            update.message.voice.get_file().download(ogg_file.name)
            text = self.params['asr'].speech_to_text(ogg_file.name)
            ogg_file.close()
            update.message.reply_text('Macaw heard: ' + text)

            user_info = {
                'first_name': update.message.chat.first_name,
                'last_name': update.message.chat.last_name,
                'is_bot': update._effective_user.is_bot
            }
            msg_info = {
                'msg_id': update.message.message_id,
                'msg_type': 'voice',
                'msg_source': 'user'
            }
            msg = Message(user_interface='telegram',
                          user_id=update.message.chat.id,
                          user_info=user_info,
                          msg_info=msg_info,
                          text=text,
                          timestamp=util.current_time_in_milliseconds())
            output = self.params['live_request_handler'](msg)
            self.result_presentation(output, {'update': update})
        except Exception:
            traceback.print_exc()
Example #2
0
    def get_output(self, conv, candidate_outputs):
        """
        The response Message generation method.

        Args:
            conv_list(list): List of util.msg.Message, each corresponding to a conversational message from / to the
            user. This list is in reverse order, meaning that the first elements is the last interaction made by user.
            candidate_outputs(dict): A dict of str (i.e., action) to list of Documents (i.e., the action's result) as
            the response. This dict is produced by action dispatcher, which means this is the aggregation of all the
            executed actions.

        Returns:
            A response Message to be sent to the user.
        """
        user_id = conv[0].user_id
        user_info = conv[0].user_info
        msg_info = dict()
        msg_info['msg_id'] = conv[0].msg_info['msg_id']
        msg_info['msg_source'] = 'system'
        text = ''
        user_interface = conv[0].user_interface

        selected_action = self.output_selection(conv, candidate_outputs)
        if selected_action is None:
            msg_info['msg_type'] = 'text'
            msg_info['msg_creator'] = 'no answer error'
            text = 'No response has been found! Please try again!'
        elif selected_action == 'qa':
            msg_info['msg_type'] = conv[0].msg_info['msg_type']
            msg_info['msg_creator'] = 'qa'
            text = candidate_outputs['qa'][0].text
        elif selected_action == 'retrieval':
            msg_info['msg_type'] = 'options'
            msg_info['msg_creator'] = 'retrieval'
            text = 'Retrieved document list (click to see the document content):'
            msg_info['options'] = [
                (output.title, '#get_doc ' + output.id, output.score)
                for output in candidate_outputs['retrieval']
            ]
        elif selected_action == '#get_doc':
            msg_info['msg_type'] = 'text'
            msg_info['msg_creator'] = '#get_doc'
            text = candidate_outputs['#get_doc'][0].text
        else:
            raise Exception('The candidate output key is not familiar!')
        timestamp = util.current_time_in_milliseconds()
        if timestamp <= conv[0].timestamp:
            raise Exception('There is a problem in the output timestamp!')
        return Message(user_interface, user_id, user_info, msg_info, text,
                       timestamp)
Example #3
0
    def get_conv_history(self, user_id, max_time, max_count):
        if max_time is None:
            res = self.col.find({'user_id': user_id}).sort([('timestamp', -1)])
        else:
            res = self.col.find({
                'user_id': user_id,
                'timestamp': {
                    '$gt': util.current_time_in_milliseconds() - max_time
                }
            }).sort([('timestamp', -1)])

        if max_count is not None:
            res = res.limit(max_count)
        return self.dict_list_to_msg_list(res)
Example #4
0
File: cis.py Project: zuacubd/macaw
    def live_request_handler(self, msg):
        try:
            # load conversation from the database and add the current message to the database
            conv = [msg] + self.msg_db.get_conv_history(
                user_id=msg.user_id, max_time=10 * 60 * 1000, max_count=10)
            self.msg_db.insert_one(msg)

            # output_msg = func_timeout(self.timeout, self.request_handler_func, args=[conv])
            output_msg = self.request_handler_func(conv)
            self.msg_db.insert_one(output_msg)
            return output_msg

        except FunctionTimedOut:
            msg_info = dict()
            msg_info['msg_id'] = msg.msg_info['msg_id']
            msg_info['msg_source'] = 'system'
            msg_info['msg_type'] = 'error'
            text = 'Time out, no result!'
            timestamp = util.current_time_in_milliseconds()
            error_msg = Message(msg.user_interface, msg.user_id, msg.user_info,
                                msg_info, text, timestamp)
            self.msg_db.insert_one(error_msg)
            return error_msg
Example #5
0
 def request_handler(self, update, context):
     """This method handles all text messages, and asks result_presentation to send the response to the user."""
     try:
         self.logger.info(update.message)
         user_info = {
             'first_name': update.message.chat.first_name,
             'last_name': update.message.chat.last_name,
             'is_bot': update._effective_user.is_bot
         }
         msg_info = {
             'msg_id': update.message.message_id,
             'msg_type': 'text',
             'msg_source': 'user'
         }
         msg = Message(user_interface='telegram',
                       user_id=update.message.chat.id,
                       user_info=user_info,
                       msg_info=msg_info,
                       text=update.message.text,
                       timestamp=util.current_time_in_milliseconds())
         output = self.params['live_request_handler'](msg)
         self.result_presentation(output, {'update': update})
     except Exception:
         traceback.print_exc()
Example #6
0
 def run(self):
     while True:
         try:
             request = input('ENTER YOUR COMMAND: ').strip()
             if len(request) == 0:
                 continue
             user_info = {'first_name': 'STDIO', 'is_bot': 'False'}
             msg_info = {
                 'msg_id': self.msg_id,
                 'msg_type':
                 'command' if request.startswith('#') else 'text',
                 'msg_source': 'user'
             }
             self.msg_id += 1
             msg = Message(user_interface='stdio',
                           user_id=-1,
                           user_info=user_info,
                           msg_info=msg_info,
                           text=request,
                           timestamp=util.current_time_in_milliseconds())
             output = self.params['live_request_handler'](msg)
             self.result_presentation(output, {})
         except Exception as ex:
             traceback.print_exc()