def set_mute(self): """ Set the Brain.ai Core mute status (mute or not) Curl test: curl -i -H "Content-Type: application/json" --user admin:secret -X POST \ -d '{"mute": "True"}' http://127.0.0.1:5000/mute """ if not request.get_json() or 'mute' not in request.get_json(): data = {"Error": "Wrong parameters, 'mute' not set"} return jsonify(error=data), 400 # get mute if present mute = self.get_value_flag_from_request(http_request=request, flag_to_find="mute", is_boolean=True) SettingEditor.set_mute_status(mute=mute) data = {"mute": mute} return jsonify(data), 200
def run_synapse_by_audio(self): """ Give an order to Brain.ai with an audio file Test with curl curl -i --user admin:secret -X POST http://localhost:5000/synapses/start/audio -F "file=@/path/to/input.wav" With mute flag curl -i --user admin:secret -X POST http://localhost:5000/synapses/start/audio -F "file=@path/to/file.wav" -F mute="true" :return: """ # check if the post request has the file part if 'file' not in request.files: data = {"error": "No file provided"} return jsonify(error=data), 400 uploaded_file = request.files['file'] # if user does not select file, browser also # submit a empty part without filename if uploaded_file.filename == '': data = {"error": "No file provided"} return jsonify(error=data), 400 # Store the mute value, then apply depending of the request parameters old_mute_value = self.settings.options.mute if request.form.get("mute"): SettingEditor.set_mute_status( mute=Utils.str_to_bool(request.form.get("mute"))) # save the file filename = secure_filename(uploaded_file.filename) base_path = os.path.join(self.app.config['UPLOAD_FOLDER']) uploaded_file.save(os.path.join(base_path, filename)) # now start analyse the audio with STT engine audio_path = base_path + os.sep + filename logger.debug("[FlaskAPI] run_synapse_by_audio: with file path %s" % audio_path) if not self.allowed_file(audio_path): audio_path = self._convert_to_wav(audio_file_path=audio_path) ol = OrderListener(callback=self.audio_analyser_callback, audio_file_path=audio_path) ol.start() ol.join() # wait the Order Analyser processing. We need to wait in this thread to keep the context while not self.order_analyser_return: time.sleep(0.1) self.order_analyser_return = False if self.api_response is not None and self.api_response: data = jsonify(self.api_response) self.api_response = None logger.debug("[FlaskAPI] run_synapse_by_audio: data %s" % data) if request.form.get("mute"): SettingEditor.set_mute_status(mute=old_mute_value) return data, 201 else: data = {"error": "The given order doesn't match any synapses"} if request.form.get("mute"): SettingEditor.set_mute_status(mute=old_mute_value) return jsonify(error=data), 400
def run_synapse_by_name(self, synapse_name): """ Run a synapse by its name test with curl: curl -i --user admin:secret -X POST http://127.0.0.1:5000/synapses/start/id/say-hello-fr run a synapse without making brain speaking curl -i -H "Content-Type: application/json" --user admin:secret -X POST \ -d '{"mute":"true"}' http://127.0.0.1:5000/synapses/start/id/say-hello-fr Run a synapse by its name and pass order's parameters curl -i -H "Content-Type: application/json" --user admin:secret -X POST \ -d '{"mute":"true", "parameters": {"parameter1": "value1" }}' \ http://127.0.0.1:5000/synapses/start/id/say-hello-fr :param synapse_name: name(id) of the synapse to execute :return: """ # get a synapse object from the name logger.debug("[FlaskAPI] run_synapse_by_name: synapse name -> %s" % synapse_name) synapse_target = BrainLoader().brain.get_synapse_by_name( synapse_name=synapse_name) # Store the mute value, then apply depending of the request parameters old_mute_value = self.settings.options.mute mute = self.get_value_flag_from_request(http_request=request, flag_to_find="mute", is_boolean=True) if mute is not None: SettingEditor.set_mute_status(mute=mute) # get parameters parameters = self.get_parameters_from_request(request) if synapse_target is None: data = {"synapse name not found": "%s" % synapse_name} if mute is not None: SettingEditor.set_mute_status(mute=old_mute_value) return jsonify(error=data), 404 else: # generate a MatchedSynapse from the synapse matched_synapse = MatchedSynapse(matched_synapse=synapse_target, overriding_parameter=parameters) # get the current LIFO buffer from the singleton lifo_buffer = LifoManager.get_singleton_lifo() lifo_buffer.add_synapse_list_to_lifo([matched_synapse]) response = lifo_buffer.execute(is_api_call=True) data = jsonify(response) if mute is not None: SettingEditor.set_mute_status(mute=old_mute_value) return data, 201
def run_synapse_by_order(self): """ Give an order to Brain.ai via API like it was from a spoken one Test with curl curl -i --user admin:secret -H "Content-Type: application/json" -X POST \ -d '{"order":"my order"}' http://localhost:5000/synapses/start/order In case of quotes in the order or accents, use a file cat post.json: {"order":"j'aime"} curl -i --user admin:secret -H "Content-Type: application/json" -X POST \ --data @post.json http://localhost:5000/order/ Can be used with mute flag curl -i --user admin:secret -H "Content-Type: application/json" -X POST \ -d '{"order":"my order", "mute":"true"}' http://localhost:5000/synapses/start/order :return: """ if not request.get_json() or 'order' not in request.get_json(): data = {"Error": "Wrong parameters, 'order' not set"} return jsonify(error=data), 400 order = request.get_json('order') # Store the mute value, then apply depending of the request parameters old_mute_value = self.settings.options.mute mute = self.get_value_flag_from_request(http_request=request, flag_to_find="mute", is_boolean=True) if mute is not None: SettingEditor.set_mute_status(mute=mute) if order is not None: # get the order order_to_run = order["order"] logger.debug( "[FlaskAPI] run_synapse_by_order: order to run -> %s" % order_to_run) api_response = SynapseLauncher.run_matching_synapse_from_order( order_to_run, self.brain, self.settings, is_api_call=True) data = jsonify(api_response) if mute is not None: SettingEditor.set_mute_status(mute=old_mute_value) return data, 201 else: data = {"error": "order cannot be null"} if mute is not None: SettingEditor.set_mute_status(mute=old_mute_value) return jsonify(error=data), 400
def _set_settings(self): # PLAYERS if self.default_player: SettingEditor.set_default_player(self.default_player) if self.players: for player_el in self.players: if isinstance(player_el, dict): for player_name in player_el: name = player_name parameters = player_el[name] new_player = Player(name=name, parameters=parameters) SettingEditor.set_players(new_player) # STT if self.default_stt: SettingEditor.set_default_stt(self.default_stt) if self.speech_to_text: for stt_el in self.speech_to_text: if isinstance(stt_el, dict): for stt_name in stt_el: name = stt_name parameters = stt_el[name] new_stt = Stt(name=name, parameters=parameters) SettingEditor.set_stts(new_stt) # TRIGGER if self.default_trigger: SettingEditor.set_default_trigger(self.default_trigger) if self.triggers: for trigger_el in self.triggers: if isinstance(trigger_el, dict): for trigger_name in trigger_el: name = trigger_name parameters = trigger_el[name] new_trigger = Trigger(name=name, parameters=parameters) SettingEditor.set_trigger(new_trigger) # TTS if self.default_tts: SettingEditor.set_default_tts(self.default_tts) if self.text_to_speech: for tts_el in self.text_to_speech: if isinstance(tts_el, dict): for tts_name in tts_el: name = tts_name parameters = tts_el[name] new_tts = Tts(name=name, parameters=parameters) SettingEditor.set_ttss(new_tts) # Options if self.deaf is not None: signal_order = SignalLauncher.get_order_instance() if signal_order is not None: SettingEditor.set_deaf_status(signal_order.trigger_instance, self.deaf) if self.mute is not None: SettingEditor.set_mute_status(self.mute) if self.energy_threshold is not None: SettingEditor.set_energy_threshold(self.energy_threshold) if self.adjust_for_ambient_noise_second is not None: SettingEditor.set_adjust_for_ambient_noise_second( self.adjust_for_ambient_noise_second) # Hooks if self.hooks: SettingEditor.set_hooks(self.hooks) # Variables if self.var_files: variables = dict() for files in self.var_files: var = Utils.get_real_file_path(files) # var is None has been checked previously in _is_parameters_ok() method variables.update(YAMLLoader.get_config(var)) SettingEditor.set_variables(variables) if self.variable is not None: SettingEditor.set_variables(self.variable)
def test_set_mute_status(self): with mock.patch("brain.core.ConfigurationManager.SettingLoader" ) as mock_setting_loader: mock_setting_loader.return_value(self.sl) SettingEditor.set_mute_status(mute=True) self.assertTrue(self.sl.settings.options.mute)