Esempio n. 1
0
    def bing_callback(self, recognizer, audio):
        """
        called from the background thread
        """
        try:
            captured_audio = recognizer.recognize_bing(audio,
                                                       key=self.key,
                                                       language=self.language,
                                                       show_all=self.show_all)
            Utils.print_info("Speech to Text Engine: Bing thinks you said %s" %
                             captured_audio)
            self._analyse_audio(captured_audio)

        except sr.UnknownValueError:
            Utils.print_warning(
                "Speech to Text Engine: Bing could not understand audio")
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except sr.RequestError as e:
            Utils.print_danger(
                "Speech to Text Engine: could not request results from Bing; {0}"
                .format(e))
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except AssertionError:
            Utils.print_warning(
                "Speech to Text Engine: no audio caught from microphone")
            self._analyse_audio(audio_to_text=None)
Esempio n. 2
0
    def wit_callback(self, recognizer, audio):
        try:
            captured_audio = recognizer.recognize_wit(audio,
                                                      key=self.key,
                                                      show_all=self.show_all)
            Utils.print_info(
                "Speech to Text Engine: Wit.ai thinks you said %s" %
                captured_audio)
            self._analyse_audio(captured_audio)

        except sr.UnknownValueError:
            Utils.print_warning(
                "Speech to Text Engine: Wit.ai could not understand audio")
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except sr.RequestError as e:
            Utils.print_danger(
                "Speech to Text Engine: could not request results from Wit.ai; {0}"
                .format(e))
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except AssertionError:
            Utils.print_warning(
                "Speech to Text Engine: no audio caught from microphone")
            self._analyse_audio(audio_to_text=None)
Esempio n. 3
0
    def apiai_callback(self, recognizer, audio):
        """
        called from the background thread
        :param recognizer:
        :param audio:
        :return:
        """
        try:
            captured_audio = recognizer.recognize_api(audio,
                                                      client_access_token=self.key,
                                                      language=self.language,
                                                      session_id=self.session_id,
                                                      show_all=self.show_all)
            Utils.print_info("Speech to Text Engine: Api.ai thinks you said %s" % captured_audio)
            self._analyse_audio(captured_audio)

        except sr.UnknownValueError as e:
            Utils.print_warning("Speech to Text Engine: Api.ai could not understand audio; {0}".format(e))
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except sr.RequestError as e:
            Utils.print_danger("Speech to Text Engine: could not request results from Api.ai; {0}".format(e))
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except AssertionError:
            Utils.print_warning("Speech to Text Engine: no audio caught from microphone")
            self._analyse_audio(audio_to_text=None)
Esempio n. 4
0
    def sphinx_callback(self, recognizer, audio):
        """
        called from the background thread
        """
        try:
            captured_audio = recognizer.recognize_sphinx(
                audio,
                language=self.language,
                keyword_entries=self.keyword_entries,
                grammar=self.grammar_file)
            Utils.print_info(
                "Speech to Text Engine: CMU Sphinx thinks you said %s" %
                captured_audio)
            self._analyse_audio(captured_audio)

        except sr.UnknownValueError:
            Utils.print_warning(
                "Speech to Text Engine: CMU Sphinx could not understand audio")
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except sr.RequestError as e:
            Utils.print_danger(
                "Speech to Text Engine: could not request results from CMU Sphinx; {0}"
                .format(e))
            # callback anyway, we need to listen again for a new order
            self._analyse_audio(audio_to_text=None)
        except AssertionError:
            Utils.print_warning(
                "Speech to Text Engine: no audio caught from microphone")
            self._analyse_audio(audio_to_text=None)
Esempio n. 5
0
def signal_handler(signal, frame):
    """
    Handles keyboard signal input Ctrl+C

    :param signal: signal handler
    :param frame: execution frame

    """
    Utils.print_warning("\nBrain.ai service stopped\n")
    sys.exit(0)
Esempio n. 6
0
def main():
    """Entry point of Brain.ai program."""

    Utils.print_info("")

    Utils.print_info("Brain.ai (Version 0.1.4)")
    Utils.print_info("Copyright (C) 2018 by Alexander Paul P. Quinit")
    Utils.print_info("This program comes with ABSOLUTELY NO WARRANTY")
    Utils.print_info(
        "This is free software, and you are welcome to redistribute it under certain conditions"
    )

    Utils.print_info("")

    # parse argument. the script name is removed
    try:
        parser = parse_args(sys.argv[1:])
    except SystemExit:
        sys.exit(1)

    # check if we want debug
    configure_logging(debug=parser.debug)

    logger.debug("brain args: %s" % parser)

    # by default, no brain file is set.
    # Use the default one: brain.yml in the root path
    brain_file = None

    # check if user set a brain.yml file
    if parser.brain_file:
        brain_file = parser.brain_file

    # check the user provide a valid action
    if parser.action not in ACTION_LIST:
        Utils.print_warning("%s is not a recognised action\n" % parser.action)
        sys.exit(1)

    # install modules
    if parser.action == "install":
        if not parser.git_url:
            Utils.print_danger("please specify the git url")
            sys.exit(1)
        else:
            parameters = {"git_url": parser.git_url}
            res_manager = ResourcesManager(**parameters)
            res_manager.install()
        return

    # uninstall modules
    if parser.action == "uninstall":
        if not parser.neuron_name \
                and not parser.stt_name \
                and not parser.tts_name \
                and not parser.trigger_name \
                and not parser.signal_name:
            Utils.print_danger("please specify a module name with "
                               "--neuron-name "
                               "or --stt-name "
                               "or --tts-name "
                               "or --trigger-name "
                               "or --signal-name")
            sys.exit(1)
        else:
            res_manager = ResourcesManager()
            res_manager.uninstall(neuron_name=parser.neuron_name,
                                  stt_name=parser.stt_name,
                                  tts_name=parser.tts_name,
                                  trigger_name=parser.trigger_name,
                                  signal_name=parser.signal_name)
        return

    # load the brain once
    brain_loader = BrainLoader(file_path=brain_file)
    brain = brain_loader.brain

    # load settings
    # get global configuration once
    settings_loader = SettingLoader()
    settings = settings_loader.settings

    if parser.action == "start":

        # user set a synapse to start
        if parser.run_synapse is not None:
            SynapseLauncher.start_synapse_by_list_name([parser.run_synapse],
                                                       brain=brain)
        if parser.run_order is not None:
            SynapseLauncher.run_matching_synapse_from_order(parser.run_order,
                                                            brain=brain,
                                                            settings=settings,
                                                            is_api_call=False)

        if (parser.run_synapse is None) and (parser.run_order is None):
            # if --deaf
            if parser.deaf:
                settings.options.deaf = True

            # start rest api
            start_rest_api(settings, brain)
            start_brain(settings, brain)