Exemplo n.º 1
0
    def __init__(self,
                 mqtt_hostname='mosquitto',
                 mqtt_port=1883,
                 training_mqtt_hostname=None,
                 training_mqtt_port=None):
        """ Initialisation.

        :param config: a YAML configuration.
        :param assistant: the client assistant class, holding the
                          intent handler and intents registry.
        """
        self.thread_handler = ThreadHandler()
        self.client = mqtt.Client()
        self.training_client = mqtt.Client()
        self.client.on_connect = self.on_connect
        self.client.on_disconnect = self.on_disconnect
        self.client.on_message = self.on_message
        self.training_client.on_message = self.on_training_message
        self.mqtt_hostname = mqtt_hostname
        self.mqtt_port = mqtt_port
        self.training_mqtt_hostname = training_mqtt_hostname if training_mqtt_hostname is not None else os.environ.get(
            'training_mqtt_hostname', None),
        self.training_mqtt_port = training_mqtt_port if training_mqtt_port is not None else os.environ.get(
            'training_mqtt_port', None),
        self.thread_targets = [
            self.startMqtt
        ]  # don't activate training by default, rely on subclasses to add startTrainingMqtt to thread targets as required
        self.subscribe_to = '#'
        self.training_mqtt_hostname = training_mqtt_hostname
        self.training_mqtt_port = training_mqtt_port
Exemplo n.º 2
0
    def __init__(
        self,
        disable_nlu=os.environ.get('rasa_disable_nlu', 'no'),
        disable_core=os.environ.get('rasa_disable_core', 'no'),
        mqtt_hostname=os.environ.get('mqtt_hostname', 'mosquitto'),
        mqtt_port=os.environ.get('mqtt_port', 1883),
        nlu_model_path=os.environ.get('rasa_nlu_model_path', 'models/nlu'),
        snips_assistant_path=os.environ.get('rasa_snips_assistant_path',
                                            'models/snips'),
        snips_user_id=os.environ.get('rasa_snips_user_id', 'user_Kr5A7b4OD'),
        core_model_path=os.environ.get('rasa_core_model_path', 'models/core'),
        config_file=os.environ.get('rasa_config_file',
                                   'rasa_config/config.json'),
        domain_file=os.environ.get('rasa_domain_file',
                                   'rasa_config/domain.yml'),
        nlu_training_file=os.environ.get('rasa_nlu_training_file',
                                         'rasa_config/nlu.md'),
        core_training_file=os.environ.get('rasa_core_training_file',
                                          'rasa_config/stories.md'),
        lang=os.environ.get('rasa_lang', 'en-GB')):
        """ Initialisation.
        :param config: a YAML configuration.
        :param assistant: the client assistant class, holding the
                          intent handler and intents registry.
        """

        self.thread_handler = ThreadHandler()
        self.client = mqtt.Client()
        self.client.on_connect = self.on_connect
        self.client.on_disconnect = self.on_disconnect
        self.client.on_message = self.on_message
        self.mqtt_hostname = mqtt_hostname
        self.mqtt_port = mqtt_port
        self.lang = lang
        # RASA config
        self.disable_nlu = disable_nlu
        self.disable_core = disable_core
        self.interpreter = None
        self.nlu_model_path = nlu_model_path
        self.core_model_path = core_model_path
        # to generate stub assistant
        self.snips_assistant_path = snips_assistant_path
        self.snips_user_id = snips_user_id
        self.config_file = config_file
        # RASA training config
        self.domain_file = domain_file
        self.nlu_training_file = nlu_training_file
        self.core_training_file = core_training_file

        self.isNluTraining = False
        self.isCoreTraining = False

        # save modified times on source files
        self.nlu_modified = self.getNluModified()
        self.core_modified = self.getCoreModified()
        self.core_domain_modified = self.getCoreDomainModified()
        self.nlu_model_modified = self.getNluModelModified()
        self.core_model_modified = self.getCoreModelModified()

        self.loadModels(True)
    def __init__(self):
        self.thread_handler = ThreadHandler()

        config = SnipsConfigParser.read_configuration_file('config.ini')
        if config.get('configuration') is not None:
            customConfig = config['configuration']
            self.respeaker = SnipsRespeaker(customConfig)
        else:
            self.respeaker = SnipsRespeaker()

        self.thread_handler.run(target=self.start_blocking)
        self.thread_handler.start_run_loop()
class Skill:

    def __init__(self):
        self.thread_handler = ThreadHandler()

        config = SnipsConfigParser.read_configuration_file('config.ini')
        if config.get('configuration') is not None:
            customConfig = config['configuration']
            self.respeaker = SnipsRespeaker(customConfig)
        else:
            self.respeaker = SnipsRespeaker()

        self.thread_handler.run(target=self.start_blocking)
        self.thread_handler.start_run_loop()

    def start_blocking(self, run_event):
        while run_event.is_set():
            with Hermes(MQTT_ADDR) as h:
                h.subscribe_session_started(self.action_session_started) \
                    .subscribe_session_ended(self.action_session_ended) \
                    .start()

    def action_session_started(self, hermes, session):
        print('Session STARTED')
        self.thread_handler.run(target=self.dialogue_session_started)

    def action_session_ended(self, hermes, session):
        print('Session ENDED')
        self.thread_handler.run(target=self.dialogue_session_ended)

    def dialogue_session_started(self, run_event):
        self.respeaker.hotword_detected()

    def dialogue_session_ended(self, run_event):
        self.respeaker.stop_working()
Exemplo n.º 5
0
    def __init__(self,
                 mqtt_hostname='mosquitto',
                 mqtt_port=1883,
                 ):
        """ Initialisation.

        :param config: a YAML configuration.
        :param assistant: the client assistant class, holding the
                          intent handler and intents registry.
        """
        self.thread_handler = ThreadHandler()
        self.client = mqtt.Client()
        self.client.on_connect = self.on_connect
        self.client.on_disconnect = self.on_disconnect
        self.client.on_message = self.on_message
        self.mqtt_hostname = mqtt_hostname
        self.mqtt_port = mqtt_port
Exemplo n.º 6
0
    def __init__(self, mqtt_hostname, mqtt_port, logger=None):
        """ Initialisation.

        :param config: a YAML configuration.
        :param assistant: the client assistant class, holding the
                          intent handler and intents registry.
        """
        self.logger = logger
        self.thread_handler = ThreadHandler()
        self.state_handler = StateHandler(self.thread_handler, logger)

        self.client = mqtt.Client()
        self.client.on_connect = self.on_connect
        self.client.on_disconnect = self.on_disconnect
        self.client.on_message = self.on_message
        self.mqtt_hostname = mqtt_hostname
        self.mqtt_port = mqtt_port

        self.first_hotword_detected = False
Exemplo n.º 7
0
    def __init__(
        self,
        mqtt_hostname=os.environ.get('rasa_nlu_mqtt_hostname', 'mosquitto'),
        mqtt_port=os.environ.get('rasa_nlu_mqtt_port', 1883),
        training_mqtt_hostname=None,
        training_mqtt_port=None,
        nlu_model_path=os.environ.get('rasa_nlu_model_path', 'models/nlu'),
        nlu_models=os.environ.get('rasa_nlu_models', 'default'),
        nlu_training_path=os.environ.get('rasa_nlu_training_path',
                                         'training_data/nlu/'),
        config_file=os.environ.get('rasa_nlu_default_config_file',
                                   'training_data/nlu/config.json'),
        config_file_slots=os.environ.get(
            'rasa_nlu_default_config_file_slots',
            'training_data/nlu/config-slots.json'),
        lang=os.environ.get('rasa_nlu_lang', 'en-GB'),
        snips_user_id=os.environ.get('rasa_nlu_snips_user_id',
                                     'user_Kr5A7b4OD'),
    ):
        """ Initialisation.
        :param config: a YAML configuration.
        :param assistant: the client assistant class, holding the
                          intent handler and intents registry.
        """
        SnipsMqttServer.__init__(self, mqtt_hostname, mqtt_port,
                                 training_mqtt_hostname, training_mqtt_port)
        # RASA config
        self.trainingIds = {}
        self.training_request_models = {}
        self.interpreter = {}
        self.interpreter_slots = {}
        self.nlu_model_path = nlu_model_path
        self.nlu_training_path = nlu_training_path
        self.default_config_file = config_file
        self.default_config_file_slots = config_file_slots
        # for matching snips intents
        self.snips_user_id = snips_user_id

        self.models = nlu_models.split(",")

        self.thread_handler = ThreadHandler()
        self.thread_targets.append(self.startTrainingMqtt)
        self.training_client.on_message = self.on_training_message

        self.thread_targets.append(self.watchModels)

        self.lang = lang
        self.subscribe_to = 'hermes/nlu/query,hermes/nlu/partialQuery'
        self.nlu_modified = {}
        self.nlu_model_modified = {}

        self.loadModels(True)
        self.nlu_modified = self.getNluModifiedAll()
        self.nlu_model_modified = self.getNluModelModifiedAll()
Exemplo n.º 8
0
    def __init__(self, mqtt_hostname, mqtt_port, nlu_model_path, config_path):
        """ Initialisation.

        :param config: a YAML configuration.
        :param assistant: the client assistant class, holding the
                          intent handler and intents registry.
        """
        self.thread_handler = ThreadHandler()

        self.client = mqtt.Client()
        self.client.on_connect = self.on_connect
        self.client.on_disconnect = self.on_disconnect
        self.client.on_message = self.on_message
        self.mqtt_hostname = mqtt_hostname
        self.mqtt_port = mqtt_port
        self.nlu_model_path = nlu_model_path
        self.config_path = config_path
        # create an NLU interpreter based on trained NLU model
        self.interpreter = Interpreter.load(self.nlu_model_path,
                                            RasaNLUConfig(self.config_path))
Exemplo n.º 9
0
    def __init__(self,
                 mqtt_hostname=os.environ.get('rasa_core_mqtt_hostname',
                                              'mosquitto'),
                 mqtt_port=os.environ.get('rasa_core_mqtt_port', 1883),
                 training_mqtt_hostname=None,
                 training_mqtt_port=None,
                 core_model_path=os.environ.get('rasa_core_model_path',
                                                'models/dialogue'),
                 domain_file=os.environ.get('rasa_core_domain_file', None),
                 core_training_file=os.environ.get('rasa_core_training_file',
                                                   None),
                 lang=os.environ.get('rasa_core_lang', 'en-GB')):
        """ Initialisation.
        :param config: a YAML configuration.
        :param assistant: the client assistant class, holding the
                          intent handler and intents registry.
        """
        SnipsMqttServer.__init__(self, mqtt_hostname, mqtt_port,
                                 training_mqtt_hostname, training_mqtt_port)
        self.thread_handler = ThreadHandler()
        self.thread_targets.append(self.startTrainingMqtt)
        self.thread_targets.append(self.watchModels)

        #self.client = mqtt.Client()
        #self.client.on_connect = self.on_connect
        #self.client.on_disconnect = self.on_disconnect
        #self.client.on_message = self.on_message
        #self.mqtt_hostname = mqtt_hostname
        #self.mqtt_port = mqtt_port
        #self.training_client.on_message = self.on_message
        self.lang = lang
        self.subscribe_to = 'hermes/intent/+'
        # RASA config
        self.core_model_path = core_model_path
        # RASA training config
        self.domain_file = domain_file
        self.core_training_file = core_training_file
        self.core_model_path = core_model_path
        #self.agentLoaded = SnipsMqttAgent(self.domain_file,policies=[MemoizationPolicy(), KerasPolicy()],core_server = self)
        self.agentLoaded = None
        self.trainingId = None
        self.siteId = None
        self.sessionId = None
        self.core_modified = self.getCoreModified()
        self.core_domain_modified = self.getCoreDomainModified()
        self.core_model_modified = self.getCoreModelModified()
        self.loadModels()
Exemplo n.º 10
0
import pytest_capturelog
import logging


def logAssert(test, msg):
    if not test:
        logging.error(msg)
        assert test, msg


def test_log(log_info):
    logging.info("testing foo")
    logAssert(log_info == 'foo', "foo is not foo")


t = ThreadHandler()
username = '******'
config_file = 'data_test/iris_config.ini'
port = '5000'
target = 'class'
file = 'data_test/iris.csv'
config_test_file = 'data_test/iris_config_test.ini'
df = pd.read_csv('data_test/iris.csv')
fs = FeatureSelection(df)

categories = [
    'numerical', 'numerical', 'numerical', 'numerical', 'categorical'
]
unique_values = [-1, -1, -1, -1, 3]
default_list = {
    'sepal_length': 5.8,
Exemplo n.º 11
0
class Server():
    """ Snips core server. """
    DIALOGUE_EVENT_STARTED, DIALOGUE_EVENT_ENDED, DIALOGUE_EVENT_QUEUED = range(
        3)

    def __init__(self, mqtt_hostname, mqtt_port, logger=None):
        """ Initialisation.

        :param config: a YAML configuration.
        :param assistant: the client assistant class, holding the
                          intent handler and intents registry.
        """
        self.logger = logger
        self.thread_handler = ThreadHandler()
        self.state_handler = StateHandler(self.thread_handler, logger)

        self.client = mqtt.Client()
        self.client.on_connect = self.on_connect
        self.client.on_disconnect = self.on_disconnect
        self.client.on_message = self.on_message
        self.mqtt_hostname = mqtt_hostname
        self.mqtt_port = mqtt_port

        self.first_hotword_detected = False

    def start(self):
        """ Start the MQTT client. """
        self.thread_handler.run(target=self.start_blocking)
        self.thread_handler.start_run_loop(self.logger)

    def start_blocking(self, run_event):
        """ Start the MQTT client, as a blocking method.

        :param run_event: a run event object provided by the thread handler.
        """

        self.log_info("Connecting to {} on port {}".format(
            self.mqtt_hostname, str(self.mqtt_port)))

        retry = 0
        while True and run_event.is_set():
            try:
                self.log_info("Trying to connect to {}".format(
                    self.mqtt_hostname))
                self.client.connect(self.mqtt_hostname, self.mqtt_port, 60)
                break
            except (socket_error, Exception) as e:
                self.log_info("MQTT error {}".format(e))
                time.sleep(5 + int(retry / 5))
                retry = retry + 1

        topics = [(MQTT_TOPIC_INTENT + '#', 0), (MQTT_TOPIC_HOTWORD + '#', 0),
                  (MQTT_TOPIC_ASR + '#', 0), (MQTT_TOPIC_TTS + '#', 0),
                  (MQTT_TOPIC_NLU + '#', 0)]
        self.log_info("Subscribing to topics {}".format(topics))
        self.client.subscribe(topics)

        while run_event.is_set():
            try:
                self.client.loop()
            except AttributeError as e:
                self.log_info("Error in mqtt run loop {}".format(e))
                time.sleep(1)

    # pylint: disable=unused-argument,no-self-use
    def on_connect(self, client, userdata, flags, result_code):
        """ Callback when the MQTT client is connected.

        :param client: the client being connected.
        :param userdata: unused.
        :param flags: unused.
        :param result_code: result code.
        """
        self.log_info("Connected with result code {}".format(result_code))
        self.state_handler.set_state(State.welcome)

    # pylint: disable=unused-argument
    def on_disconnect(self, client, userdata, result_code):
        """ Callback when the MQTT client is disconnected. In this case,
            the server waits five seconds before trying to reconnected.

        :param client: the client being disconnected.
        :param userdata: unused.
        :param result_code: result code.
        """
        self.log_info("Disconnected with result code " + str(result_code))
        self.state_handler.set_state(State.goodbye)
        time.sleep(5)
        self.thread_handler.run(target=self.start_blocking)

    # pylint: disable=unused-argument
    def on_message(self, client, userdata, msg):
        """ Callback when the MQTT client received a new message.

        :param client: the MQTT client.
        :param userdata: unused.
        :param msg: the MQTT message.
        """
        if msg is None:
            return

        self.log_info("New message on topic {}".format(msg.topic))
        self.log_debug("Payload {}".format(msg.payload))

        if msg.payload is None or len(msg.payload) == 0:
            pass
        """
        if msg.payload:
            payload = json.loads(msg.payload.decode('utf-8'))
            site_id = payload.get('siteId')
            session_id = payload.get('sessionId')
        """

        if msg.topic is not None and msg.topic == MQTT_TOPIC_NLU + "intentParsed":
            self.state_handler.set_state(State.nlu_intent_parsed)
        elif msg.topic is not None and msg.topic == MQTT_TOPIC_HOTWORD + "toggleOn":
            self.state_handler.set_state(State.hotword_toggle_on)
        elif MQTT_TOPIC_HOTWORD_DETECTED_RE.match(msg.topic):
            if not self.first_hotword_detected:
                self.client.publish("hermes/feedback/sound/toggleOff",
                                    payload=None,
                                    qos=0,
                                    retain=False)
                self.first_hotword_detected = True
            self.state_handler.set_state(State.hotword_detected)
        elif msg.topic is not None and msg.topic == MQTT_TOPIC_NLU + "intentNotRecognized":
            self.state_handler.set_state(State.error)
        elif msg.topic is not None and msg.topic.startswith(MQTT_TOPIC_TTS):
            self.state_handler.set_state(State.say)

        self.log_debug("Switching state handler to {}".format(
            self.state_handler.state))

    def log_info(self, message):
        if self.logger is not None:
            self.logger.info(message)

    def log_debug(self, message):
        if self.logger is not None:
            self.logger.debug(message)

    def log_error(self, message):
        if self.logger is not None:
            self.logger.error(message)
Exemplo n.º 12
0
class SnipsTTSServer():
    
    def __init__(self,
                 mqtt_hostname='mosquitto',
                 mqtt_port=1883,
                 ):
        """ Initialisation.

        :param config: a YAML configuration.
        :param assistant: the client assistant class, holding the
                          intent handler and intents registry.
        """
        self.thread_handler = ThreadHandler()
        self.client = mqtt.Client()
        self.client.on_connect = self.on_connect
        self.client.on_disconnect = self.on_disconnect
        self.client.on_message = self.on_message
        self.mqtt_hostname = mqtt_hostname
        self.mqtt_port = mqtt_port
        
    # MQTT LISTENING SERVER
    def start(self):
        self.thread_handler.run(target=self.start_blocking)
        self.thread_handler.start_run_loop()
        # send audioFrames
        
        

    def start_blocking(self, run_event):
        self.log("Connecting TTS to {} on port {}".format(self.mqtt_hostname, str(self.mqtt_port)))
        retry = 0
        while True and run_event.is_set():
            try:
                self.log("Trying to connect TTS to {} {}".format(self.mqtt_hostname,self.mqtt_port))
                self.client.connect(self.mqtt_hostname, self.mqtt_port, 60)
                break
            except (socket_error, Exception) as e:
                self.log("MQTT error {}".format(e))
                time.sleep(5 + int(retry / 5))
                retry = retry + 1
        # SUBSCRIBE 
        self.client.subscribe('hermes/tts/say', 0)
        while run_event.is_set():
            try:
                self.client.loop()
            except AttributeError as e:
                self.log("Error in mqtt run loop {}".format(e))
                time.sleep(1)

    def on_connect(self, client, userdata, flags, result_code):
        self.log("Connected TTS with result code {}".format(result_code))

    def on_disconnect(self, client, userdata, result_code):
        self.log("Disconnected TTS with result code " + str(result_code))
        time.sleep(5)
        self.thread_handler.run(target=self.start_blocking)

    def on_message(self, client, userdata, msg):
        print("MESSAGEtts: {}".format(msg.topic))
            
        if msg.topic is not None and msg.topic=="hermes/tts/say":
            print("MESSAGE OK: {}".format(msg.topic))
            payload = json.loads(msg.payload)
            # .decode('utf-8')
            sessionId = payload.get('sessionId')
            siteId = payload.get('siteId','default')
            lang = payload.get('lang','en-GB')
            theId = sessionId
            fileName = '/tmp/speaking.wav'
            
            os.system('/usr/bin/pico2wave -w=' + fileName + ' "{}" '.format(payload.get('text')))
            #pubCommand = "mosquitto_pub -h " +self.mqtt_hostname+" -t 'hermes/audioServer/default/playBytes/0049a91e-8449-4398-9752-07c858234' -f '" + fileName + "'"
            #print(pubCommand)
            #os.system(pubCommand)
            
            fp = open(fileName)
            f = fp.read()
            topic = 'hermes/audioServer/{}/playBytes'.format(siteId)
            if theId is not None:
                topic = topic + '/{}'.format(theId[::-1])
            self.client.publish(topic, payload=bytes(f),qos=0)
            #print("PUBLISHED on " + topic)
            os.remove(fileName)
            
            
                    
    def log(self, message):
       print (message)
Exemplo n.º 13
0
class SnipsMqttServer():
    def __init__(self,
                 mqtt_hostname='mosquitto',
                 mqtt_port=1883,
                 training_mqtt_hostname=None,
                 training_mqtt_port=None):
        """ Initialisation.

        :param config: a YAML configuration.
        :param assistant: the client assistant class, holding the
                          intent handler and intents registry.
        """
        self.thread_handler = ThreadHandler()
        self.client = mqtt.Client()
        self.training_client = mqtt.Client()
        self.client.on_connect = self.on_connect
        self.client.on_disconnect = self.on_disconnect
        self.client.on_message = self.on_message
        self.training_client.on_message = self.on_training_message
        self.mqtt_hostname = mqtt_hostname
        self.mqtt_port = mqtt_port
        self.training_mqtt_hostname = training_mqtt_hostname if training_mqtt_hostname is not None else os.environ.get(
            'training_mqtt_hostname', None),
        self.training_mqtt_port = training_mqtt_port if training_mqtt_port is not None else os.environ.get(
            'training_mqtt_port', None),
        self.thread_targets = [
            self.startMqtt
        ]  # don't activate training by default, rely on subclasses to add startTrainingMqtt to thread targets as required
        self.subscribe_to = '#'
        self.training_mqtt_hostname = training_mqtt_hostname
        self.training_mqtt_port = training_mqtt_port

    def getTrainingMqttHostname(self):
        return self.training_mqtt_hostname if self.training_mqtt_hostname is not None else self.mqtt_hostname

    def getTrainingMqttPort(self):
        return self.training_mqtt_port if self.training_mqtt_port is not None else self.mqtt_port

    # MQTT LISTENING SERVER
    def start(self):
        self.log("START")
        for threadTarget in self.thread_targets:
            print("RUN {}".format(threadTarget))
            self.thread_handler.run(target=threadTarget)
        self.thread_handler.start_run_loop()

    def startMqtt(self, run_event):
        self.log("Connecting to {} on port {}".format(self.mqtt_hostname,
                                                      str(self.mqtt_port)))
        retry = 0
        while True and run_event.is_set():
            try:
                self.log("Trying to connect to {} {}".format(
                    self.mqtt_hostname, self.mqtt_port))
                self.client.connect(self.mqtt_hostname, self.mqtt_port, 60)
                # SUBSCRIBE
                print(self.subscribe_to)
                print(self.subscribe_to.split(","))
                for sub in self.subscribe_to.split(","):
                    if len(sub) > 0:
                        print('sub {}'.format(sub))
                        self.client.subscribe(sub)
                    else:
                        print('no subscriptions')

                break
            except (socket_error, Exception) as e:
                self.log("MQTT error {}".format(e))
                time.sleep(5 + int(retry / 5))
                retry = retry + 1
        while run_event.is_set():
            #try:
            self.client.loop()
        #except AttributeError as e:
        #    self.log("Error in mqtt run loop {}".format(e))
        #    time.sleep(1)

    def startTrainingMqtt(self, run_event):
        self.log("Connecting to {} on port {}".format(
            self.getTrainingMqttHostname(), str(self.training_mqtt_port)))
        retry = 0
        while True and run_event.is_set():
            try:
                self.log("Trying to connect to {} {}".format(
                    self.getTrainingMqttHostname(),
                    self.getTrainingMqttPort()))
                self.training_client.connect(self.getTrainingMqttHostname(),
                                             self.getTrainingMqttPort(), 60)
                break
            except (socket_error, Exception) as e:
                self.log("MQTT error {}".format(e))
                time.sleep(5 + int(retry / 5))
                retry = retry + 1
        while run_event.is_set():
            #try:
            self.training_client.loop()
        #except AttributeError as e:
        #    self.log("Error in mqtt run loop {}".format(e))
        #    time.sleep(1)

    def on_training_message(self, client, userdata, msg):
        self.log("TRAINING MESSAGE {}".format(msg.topic))

    def on_connect(self, client, userdata, flags, result_code):
        self.log("Connected with result code {}".format(result_code))

    def on_disconnect(self, client, userdata, result_code):
        self.log("Disconnected with result code " + str(result_code))
        time.sleep(5)
        for threadTarget in self.thread_targets:
            self.thread_handler.run(target=threadTarget)

    def on_message(self, client, userdata, msg):
        self.log("MESSAGE {}".format(msg.topic))

    def log(self, message):
        print(message)
Exemplo n.º 14
0
class RasaNLUServer():
    """ Snips core server. """
    def __init__(self, mqtt_hostname, mqtt_port, nlu_model_path, config_path):
        """ Initialisation.

        :param config: a YAML configuration.
        :param assistant: the client assistant class, holding the
                          intent handler and intents registry.
        """
        self.thread_handler = ThreadHandler()
        print('model')
        print(nlu_model_path)
        self.client = mqtt.Client()
        self.client.on_connect = self.on_connect
        self.client.on_disconnect = self.on_disconnect
        self.client.on_message = self.on_message
        self.mqtt_hostname = mqtt_hostname
        self.mqtt_port = mqtt_port
        self.nlu_model_path = nlu_model_path
        self.config_path = config_path
        # create an NLU interpreter based on trained NLU model
        self.interpreter = Interpreter.load(self.nlu_model_path,
                                            RasaNLUConfig(self.config_path))
        print('loaded model')

    def start(self):
        """ Start the MQTT client. """
        self.thread_handler.run(target=self.start_blocking)
        self.thread_handler.start_run_loop()

    def start_blocking(self, run_event):
        """ Start the MQTT client, as a blocking method.

        :param run_event: a run event object provided by the thread handler.
        """
        self.log("Connecting to {} on port {}".format(self.mqtt_hostname,
                                                      str(self.mqtt_port)))

        retry = 0
        while True and run_event.is_set():
            try:
                self.log("Trying to connect to {}".format(self.mqtt_hostname))
                self.client.connect(self.mqtt_hostname, self.mqtt_port, 60)
                break
            except (socket_error, Exception) as e:
                self.log("MQTT error {}".format(e))
                time.sleep(5 + int(retry / 5))
                retry = retry + 1
        self.client.subscribe('#', 0)
        while run_event.is_set():
            try:
                self.client.loop()
            except AttributeError as e:
                self.log("Error in mqtt run loop {}".format(e))
                time.sleep(1)

    # pylint: disable=unused-argument,no-self-use
    def on_connect(self, client, userdata, flags, result_code):
        """ Callback when the MQTT client is connected.

        :param client: the client being connected.
        :param userdata: unused.
        :param flags: unused.
        :param result_code: result code.
        """
        self.log("Connected with result code {}".format(result_code))

    # pylint: disable=unused-argument
    def on_disconnect(self, client, userdata, result_code):
        """ Callback when the MQTT client is disconnected. In this case,
            the server waits five seconds before trying to reconnected.

        :param client: the client being disconnected.
        :param userdata: unused.
        :param result_code: result code.
        """
        self.log("Disconnected with result code " + str(result_code))
        time.sleep(5)
        self.thread_handler.run(target=self.start_blocking)

    # pylint: disable=unused-argument
    def on_message(self, client, userdata, msg):
        """ Callback when the MQTT client received a new message.

        :param client: the MQTT client.
        :param userdata: unused.
        :param msg: the MQTT message.
        """
        #self.log(client)
        #self.log(userdata)
        #self.log(msg.payload)
        if msg.payload is None or len(msg.payload) == 0:
            pass
        if msg.topic is not None and msg.topic.startswith(
                "hermes/nlu") and msg.topic.endswith('/query') and msg.payload:
            self.log("New message on topic {}".format(msg.topic))
            payload = json.loads(msg.payload.decode('utf-8'))
            print(payload)
            if 'input' in payload:
                sessionId = payload['sessionId']
                id = payload['id']
                text = payload['input']
                print(text)
                if (text == "restart server"):
                    print('restart server')
                    self.interpreter = Interpreter.load(
                        self.nlu_model_path, RasaNLUConfig(self.config_path))
                else:
                    lookup = self.interpreter.parse(text)

                    slots = []

                    for entity in lookup['entities']:
                        slot = {
                            "entity": entity['value'],
                            "range": {
                                "end": entity['end'],
                                "start": entity['start']
                            },
                            "rawValue": entity['value'],
                            "slotName": "entity",
                            "value": {
                                "kind": "Custom",
                                "value": entity['value']
                            }
                        }
                        slots.append(slot)
                    print(slots)
                    intentName = "user_Kr5A7b4OD__{}".format(
                        lookup['intent']['name'])
                    self.client.publish(
                        'hermes/nlu/intentParsed',
                        payload=json.dumps({
                            "id": id,
                            "sessionId": sessionId,
                            "input": text,
                            "intent": {
                                "intentName": intentName,
                                "probability": 1.0
                            },
                            "slots": slots
                        }),
                        #
                        qos=0,
                        retain=False)

    def log(self, message):
        print(message)
Exemplo n.º 15
0
class SnipsMqttServer():
    def __init__(
        self,
        mqtt_hostname='mosquitto',
        mqtt_port=1883,
    ):
        """ Initialisation.

        :param config: a YAML configuration.
        :param assistant: the client assistant class, holding the
                          intent handler and intents registry.
        """
        self.thread_handler = ThreadHandler()
        self.client = mqtt.Client()
        self.client.on_connect = self.on_connect
        self.client.on_disconnect = self.on_disconnect
        self.client.on_message = self.on_message
        self.mqtt_hostname = mqtt_hostname
        self.mqtt_port = mqtt_port
        self.thread_targets = [self.startMqtt]
        self.subscribe_to = '#'

    # MQTT LISTENING SERVER
    def start(self):
        self.log("START")
        for threadTarget in self.thread_targets:
            print("RUN {}".format(threadTarget))
            self.thread_handler.run(target=threadTarget)
        self.thread_handler.start_run_loop()

    def startMqtt(self, run_event):
        self.log("Connecting to {} on port {}".format(self.mqtt_hostname,
                                                      str(self.mqtt_port)))
        retry = 0
        while True and run_event.is_set():
            try:
                self.log("Trying to connect to {} {}".format(
                    self.mqtt_hostname, self.mqtt_port))
                self.client.connect(self.mqtt_hostname, self.mqtt_port, 60)
                break
            except (socket_error, Exception) as e:
                self.log("MQTT error {}".format(e))
                time.sleep(5 + int(retry / 5))
                retry = retry + 1
        # SUBSCRIBE
        for sub in self.subscribe_to.split(","):
            self.client.subscribe(sub)

        while run_event.is_set():
            #try:
            self.client.loop()
        #except AttributeError as e:
        #    self.log("Error in mqtt run loop {}".format(e))
        #    time.sleep(1)

    def on_connect(self, client, userdata, flags, result_code):
        self.log("Connected with result code {}".format(result_code))

    def on_disconnect(self, client, userdata, result_code):
        self.log("Disconnected with result code " + str(result_code))
        time.sleep(5)
        for threadTarget in self.thread_targets:
            self.thread_handler.run(target=threadTarget)

    def on_message(self, client, userdata, msg):
        self.log("MESSAGE {}".format(msg.topic))

    def log(self, message):
        print(message)
Exemplo n.º 16
0
class SnipsRasaServer():
    def __init__(
        self,
        disable_nlu=os.environ.get('rasa_disable_nlu', 'no'),
        disable_core=os.environ.get('rasa_disable_core', 'no'),
        mqtt_hostname=os.environ.get('mqtt_hostname', 'mosquitto'),
        mqtt_port=os.environ.get('mqtt_port', 1883),
        nlu_model_path=os.environ.get('rasa_nlu_model_path',
                                      'rasa_config/models/default/current'),
        snips_assistant_path=os.environ.get('rasa_snips_assistant_path',
                                            'models/snips'),
        snips_user_id=os.environ.get('rasa_snips_user_id', 'user_Kr5A7b4OD'),
        core_model_path=os.environ.get('rasa_core_model_path',
                                       'rasa_config/models/dialogue'),
        config_file=os.environ.get('rasa_config_file',
                                   'rasa_config/config.json'),
        domain_file=os.environ.get('rasa_domain_file',
                                   'rasa_config/domain.yml'),
        nlu_training_file=os.environ.get('rasa_nlu_training_file',
                                         'rasa_config/nlu.md'),
        core_training_file=os.environ.get('rasa_core_training_file',
                                          'rasa_config/stories.md'),
        lang=os.environ.get('rasa_lang', 'en-GB')):
        """ Initialisation.
        :param config: a YAML configuration.
        :param assistant: the client assistant class, holding the
                          intent handler and intents registry.
        """

        self.thread_handler = ThreadHandler()
        self.client = mqtt.Client()
        self.client.on_connect = self.on_connect
        self.client.on_disconnect = self.on_disconnect
        self.client.on_message = self.on_message
        self.mqtt_hostname = mqtt_hostname
        self.mqtt_port = mqtt_port
        self.lang = lang
        # RASA config
        self.disable_nlu = disable_nlu
        self.disable_core = disable_core
        self.interpreter = None
        self.nlu_model_path = nlu_model_path
        self.core_model_path = core_model_path
        # to generate stub assistant
        self.snips_assistant_path = snips_assistant_path
        self.snips_user_id = snips_user_id
        self.config_file = config_file
        # RASA training config
        self.domain_file = domain_file
        self.nlu_training_file = nlu_training_file
        self.core_training_file = core_training_file

        self.isNluTraining = False
        self.isCoreTraining = False

        # save modified times on source files
        self.nlu_modified = self.getNluModified()
        self.core_modified = self.getCoreModified()
        self.core_domain_modified = self.getCoreDomainModified()
        self.nlu_model_modified = self.getNluModelModified()
        self.core_model_modified = self.getCoreModelModified()
        self.agent = agent = Agent(
            self.domain_file, policies=[MemoizationPolicy(),
                                        KerasPolicy()])
        self.agentLoaded = None
        self.loadModels(True)

    def getNluModified(self):
        if os.path.isfile(self.nlu_training_file):
            return os.path.getmtime(self.nlu_training_file)

    def getCoreModified(self):
        if os.path.isfile(self.core_training_file):
            return os.path.getmtime(self.core_training_file)

    def getNluModelModified(self):
        if os.path.isfile("{}/metadata.json".format(self.nlu_model_path)):
            return os.path.getmtime("{}/metadata.json".format(
                self.nlu_model_path))

    def getCoreModelModified(self):
        if os.path.isfile("{}/domain.json".format(self.core_model_path)):
            return os.path.getmtime("{}/domain.json".format(
                self.core_model_path))

    def getCoreDomainModified(self):
        if os.path.isfile(self.domain_file):
            return os.path.getmtime(self.domain_file)

    def isNluModified(self):
        return self.getNluModified(
        ) != self.nlu_modified or self.getNluModified(
        ) > self.getNluModelModified()

    def isCoreModified(self):
        return self.getCoreModified(
        ) != self.core_modified or self.getCoreModified(
        ) > self.getCoreModelModified() or self.getCoreDomainModified(
        ) != self.core_domain_modified or self.getCoreDomainModified(
        ) > self.getCoreModelModified()

    def isNluModelModified(self):
        return self.getNluModelModified() != self.nlu_model_modified

    def isCoreModelModified(self):
        return self.getCoreModelModified() != self.core_model_modified

    def isNluModelMissing(self):
        return not os.path.isfile("{}/metadata.json".format(
            self.nlu_model_path))

    def isCoreModelMissing(self):
        return not os.path.isfile("{}/domain.json".format(
            self.core_model_path))

    # these function read extended rasa stories format and output something suitable for training
    def generateNLU(self):
        if os.path.getmtime(self.nlu_training_file) != self.nlu_modified:
            # do generation
            pass

    def generateCore(self):
        pass

    def generateDomain(self):
        pass

    def watchModels(self, run_event):
        while True and run_event.is_set():
            self.loadModels()
            time.sleep(10)

    def trainModels(self, force=False):
        self.train_nlu(force)
        self.train_core(force)

    # RASA model generation
    def loadModels(self, force=False):
        self.trainModels()
        self.interpreter = Interpreter.load("{}/".format(self.nlu_model_path),
                                            RasaNLUConfig(self.config_file))

        # if file exists import os.path os.path.exists(file_path)
        # create an NLU interpreter and dialog agent based on trained models
        if self.disable_nlu != "yes":
            if force or self.isNluModelModified():
                self.nlu_model_modified = self.getNluModelModified()
                self.nlu_modified = self.getNluModified()
                print('loaded nlu model')

        #self.interpreter = RasaNLUInterpreter("models/nlu/default/current")
        if self.disable_core != "yes":
            if force or self.isCoreModelModified():
                self.agentLoaded = self.agent.load(
                    self.core_model_path, interpreter=self.nlu_model_path)
                self.core_model_modified = self.getCoreModelModified()
                self.core_modified = self.getCoreModified()
                self.core_domain_modified = self.getCoreDomainModified()
                print('loaded core model')

    # RASA training
    def train_nlu(self, force=False):
        if self.disable_nlu != "yes" and not self.isNluTraining:
            #print("TRY NLU TRAIN {} {} {}".format(force,self.isNluModified() , self.isNluModelMissing()))
            if (force or self.isNluModified() or self.isNluModelMissing()):
                self.isNluTraining = True
                print("NLU TRAIN {} {} {}".format(force, self.isNluModified(),
                                                  self.isNluModelMissing()))

                from rasa_nlu.converters import load_data
                from rasa_nlu.config import RasaNLUConfig
                from rasa_nlu.model import Trainer

                training_data = load_data(self.nlu_training_file)
                trainer = Trainer(RasaNLUConfig(self.config_file))
                trainer.train(training_data)
                #model_directory = trainer.persist('models/nlu/', fixed_model_name="current")
                pathParts = self.nlu_model_path.split('/')
                modelName = pathParts[-1]
                shortPath = "/".join(pathParts[:-2])
                print("model {} path {}".format(modelName, shortPath))
                model_directory = trainer.persist(shortPath,
                                                  fixed_model_name=modelName)
                #self.core_model_modified=self.getCoreModelModified()
                self.isNluTraining = False
                self.nlu_modified = self.getNluModified()
                return model_directory

    def train_core(self, force=False):
        if self.disable_core != "yes" and not self.isCoreTraining:
            #print("TRY CORE TRAIN {} {} {} ".format(force,self.isCoreModified()   , self.isCoreModelMissing()))
            if force or self.isCoreModified() or self.isCoreModelMissing():
                self.isCoreTraining = True
                print("CORE TRAIN {} {} {} ".format(force,
                                                    self.isCoreModified(),
                                                    self.isCoreModelMissing()))

                self.agent.train(self.core_training_file,
                                 max_history=3,
                                 epochs=100,
                                 batch_size=50,
                                 augmentation_factor=50,
                                 validation_split=0.2)
                self.agent.persist(self.core_model_path)
                self.isCoreTraining = False
                self.core_modified = self.getCoreModified()
                self.core_domain_modified = self.getCoreDomainModified()
                return agent

    # MQTT LISTENING SERVER
    def start(self):
        self.thread_handler.run(target=self.start_blocking)
        self.thread_handler.run(target=self.watchModels)
        self.thread_handler.start_run_loop()

    def start_blocking(self, run_event):
        self.log("Connecting to {} on port {}".format(self.mqtt_hostname,
                                                      str(self.mqtt_port)))
        retry = 0
        while True and run_event.is_set():
            try:
                self.log("Trying to connect to {} {}".format(
                    self.mqtt_hostname, self.mqtt_port))
                self.client.connect(self.mqtt_hostname, self.mqtt_port, 60)
                break
            except (socket_error, Exception) as e:
                self.log("MQTT error {}".format(e))
                time.sleep(5 + int(retry / 5))
                retry = retry + 1
        # SUBSCRIBE
        self.client.subscribe('#', 0)
        while run_event.is_set():
            try:
                self.client.loop()
            except AttributeError as e:
                self.log("Error in mqtt run loop {}".format(e))
                time.sleep(1)

    def on_connect(self, client, userdata, flags, result_code):
        self.log("Connected with result code {}".format(result_code))

    def on_disconnect(self, client, userdata, result_code):
        self.log("Disconnected with result code " + str(result_code))
        time.sleep(5)
        self.thread_handler.run(target=self.start_blocking)

    def on_message(self, client, userdata, msg):
        if msg.topic is not None and msg.topic.startswith(
                "hermes/audioServer"):
            pass
        else:
            print("MESSAGE: {}".format(msg.topic))
            if msg.topic is not None and "{}".format(
                    msg.topic
            ).startswith("hermes/nlu") and "{}".format(msg.topic).endswith(
                    '/query') and msg.payload and self.disable_nlu != "yes":
                self.handleNluQuery(msg)
            elif msg.topic is not None and "{}".format(msg.topic).startswith(
                    "hermes/intent"
            ) and msg.payload and self.disable_core != "yes":
                self.handleCoreQuery(msg)

    def handleCoreQuery(self, msg):
        self.log("Core query {}".format(msg.topic))
        payload = json.loads(msg.payload.decode('utf-8'))
        print(payload)
        print('#######################')
        #print(payload.get('slots','fail'))
        if 'input' in payload:
            theId = payload.get('id')
            sessionId = payload.get('sessionId')
            siteId = payload.get('siteId', 'default')
            entities = []
            ## strip snips user id from entity name
            #intentNameParts = payload['intent']['intentName'].split('__')
            #print(intentNameParts)
            #if len(intentNameParts) > 1:
            #intentName = intentNameParts[1]
            ##intentName = '__'.join(intentNameParts)
            #if 'slots' in payload and payload['slots'] is not None:
            #for entity in payload['slots']:
            #entities.append({ "start": entity['range']['start'],"end": entity['range']['end'],"value": entity['rawValue'],"entity": entity['slotName']})
            ##output = {
            #"text": payload['input'],
            #"intent": {
            #"name": intentName,
            #"confidence": 1.0
            #},
            #"entities": entities
            #}
            #self.log("CORE HANDLER {}".format(json.dumps(output)))
            #message = json.dumps(output)
            print('IN')
            print(payload['input'])
            response = self.agentLoaded.handle_message(
                payload['input'], output_channel=CollectingOutputChannel())
            print("OUT")
            print(response)
            if response is not None and len(response) > 0:
                self.client.publish('hermes/tts/say',
                                    payload=json.dumps({
                                        "lang": self.lang,
                                        "sessionId": sessionId,
                                        "text": response[0],
                                        "siteId": siteId,
                                        "id": theId
                                    }),
                                    qos=0,
                                    retain=False)
                self.client.publish(
                    'hermes/dialogue/endSession',
                    json.dumps({
                        "sessionId": sessionId,
                        "siteId": siteId
                    }))

    def handleNluQuery(self, msg):
        self.log("NLU query {}".format(msg.topic))
        payload = json.loads(msg.payload.decode('utf-8'))
        print(payload)
        if 'input' in payload:
            sessionId = payload['sessionId']
            id = payload['id']
            text = payload['input']
            print(text)
            lookup = self.interpreter.parse(text)

            slots = []

            for entity in lookup['entities']:
                slot = {
                    "entity": entity['value'],
                    "range": {
                        "end": entity['end'],
                        "start": entity['start']
                    },
                    "rawValue": entity['value'],
                    "slotName": "entity",
                    "value": {
                        "kind": "Custom",
                        "value": entity['value']
                    }
                }
                slots.append(slot)
            print(slots)
            intentName = "{}__{}".format(self.snips_user_id,
                                         lookup['intent']['name'])
            self.client.publish('hermes/nlu/intentParsed',
                                payload=json.dumps({
                                    "id": id,
                                    "sessionId": sessionId,
                                    "input": text,
                                    "intent": {
                                        "intentName": intentName,
                                        "probability": 1.0
                                    },
                                    "slots": slots
                                }),
                                qos=0,
                                retain=False)

    def log(self, message):
        print(message)
class SnipsAudioServer():
    def __init__(self,
                 mqtt_hostname='mosquitto',
                 mqtt_port=1883,
                 site='default'):
        """ Initialisation.

        :param config: a YAML configuration.
        :param assistant: the client assistant class, holding the
                          intent handler and intents registry.
        """
        self.thread_handler = ThreadHandler()
        self.client = mqtt.Client()
        self.client.on_connect = self.on_connect
        self.client.on_disconnect = self.on_disconnect
        self.client.on_message = self.on_message
        self.mqtt_hostname = mqtt_hostname
        self.mqtt_port = mqtt_port
        self.site = site

    # MQTT LISTENING SERVER
    def start(self):
        self.thread_handler.run(target=self.start_blocking)
        self.thread_handler.run(target=self.sendAudioFrames)
        self.thread_handler.start_run_loop()

    def start_blocking(self, run_event):
        self.log("Connecting TTS to {} on port {}".format(
            self.mqtt_hostname, str(self.mqtt_port)))
        retry = 0
        while True and run_event.is_set():
            try:
                self.log("Trying to connect TTS to {} {}".format(
                    self.mqtt_hostname, self.mqtt_port))
                self.client.connect(self.mqtt_hostname, self.mqtt_port, 60)
                break
            except (socket_error, Exception) as e:
                self.log("MQTT error {}".format(e))
                time.sleep(5 + int(retry / 5))
                retry = retry + 1
        # SUBSCRIBE
        self.client.subscribe('hermes/audioServer/+/playBytes/+', 0)
        while run_event.is_set():
            try:
                self.client.loop()
            except AttributeError as e:
                self.log("Error in mqtt run loop {}".format(e))
                time.sleep(1)

    def on_connect(self, client, userdata, flags, result_code):
        self.log("Connected TTS with result code {}".format(result_code))

    def on_disconnect(self, client, userdata, result_code):
        self.log("Disconnected TTS with result code " + str(result_code))
        time.sleep(5)
        self.thread_handler.run(target=self.start_blocking)

    def on_message(self, client, userdata, msg):
        parts = msg.topic.split('/')
        if msg.topic.startswith(
                "hermes/audioServer/") and parts[3] == 'playBytes':
            siteId = parts[2]
            wf = wave.open(io.BytesIO(bytes(msg.payload)), 'rb')
            p = pyaudio.PyAudio()
            CHUNK = 256
            stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                            channels=wf.getnchannels(),
                            rate=wf.getframerate(),
                            output=True)

            data = wf.readframes(CHUNK)

            while data != None:
                stream.write(data)
                data = wf.readframes(CHUNK)

            stream.stop_stream()
            stream.close()

            p.terminate()

    def sendAudioFrames(self, run_event):

        audio = pyaudio.PyAudio()
        stream = audio.open(format=pyaudio.paInt16,
                            channels=1,
                            rate=16000,
                            input=True,
                            frames_per_buffer=256)
        while True and run_event.is_set():
            frames = stream.read(256)
            # generate wav file in memory
            output = io.BytesIO()
            waveFile = wave.open(output, "wb")
            waveFile.setnchannels(1)
            waveFile.setsampwidth(2)
            waveFile.setframerate(16000)
            waveFile.writeframes(frames)
            #waveFile.close()
            topic = 'hermes/audioServer/{}/audioFrame'.format(self.site)
            self.client.publish(topic, payload=output.getvalue(), qos=0)
            #output.close()  # discard buffer memory

    def log(self, message):
        print(message)