def _generator(self): """Generator function that continuously yields audio chunks from the buffer. Used to stream data to the Google Speech API Asynchronously. :return A streaming request with the audio data. First request carries config data per Dialogflow docs. :rtype: Iterator[:class:`StreamingDetectIntentRequest`] """ # First message contains session, query_input, and params query_input = QueryInput(audio_config=self._audio_config) contexts = converters.contexts_msg_to_struct(self.last_contexts) params = QueryParameters(contexts=contexts) req = StreamingDetectIntentRequest( session=self._session, query_input=query_input, query_params=params, single_utterance=True, output_audio_config=self._output_audio_config) yield req if self.USE_AUDIO_SERVER: with AudioServerStream() as stream: audio_generator = stream.generator() for content in audio_generator: yield StreamingDetectIntentRequest(input_audio=content) else: with MicrophoneStream() as stream: audio_generator = stream.generator() for content in audio_generator: yield StreamingDetectIntentRequest(input_audio=content)
def detect_intent_text(self, msg): """Use the Dialogflow API to detect a user's intent. Goto the Dialogflow console to define intents and params. :param msg: DialogflowRequest msg :return query_result: Dialogflow's query_result with action parameters :rtype: DialogflowResult """ # Create the Query Input text_input = TextInput(text=msg.query_text, language_code=self._language_code) query_input = QueryInput(text=text_input) # Create QueryParameters user_contexts = utils.converters.contexts_msg_to_struct(msg.contexts) self.last_contexts = utils.converters.contexts_msg_to_struct( self.last_contexts) contexts = self.last_contexts + user_contexts params = QueryParameters(contexts=contexts) try: self.contaChiamateApi += 1 #incremento il contatore chiamate Api ''' response = self._session_cli.detect_intent( session=self._session, query_input=query_input, query_params=params, output_audio_config=self._output_audio_config) ''' response = self._session_cli.detect_intent(session=self._session, query_input=query_input, query_params=params) except google.api_core.exceptions.Cancelled as c: rospy.logwarn("DF_CLIENT: Caught a Google API Client cancelled " "exception. Check request format!:\n{}".format(c)) except google.api_core.exceptions.Unknown as u: rospy.logwarn("DF_CLIENT: Unknown Exception Caught:\n{}".format(u)) except google.api_core.exceptions.ServiceUnavailable: rospy.logwarn( "DF_CLIENT: Deadline exceeded exception caught. The response " "took too long or you aren't connected to the internet!") except google.api_core.exceptions.DeadlineExceeded as u: rospy.logwarn( "DF_CLIENT: DeadlineExceeded Exception Caught:\n{}".format(u)) else: if response is None: rospy.logwarn("DF_CLIENT: No response received!") return None # Salvo il contesto self.last_contexts = utils.converters.contexts_struct_to_msg( response.query_result.output_contexts) # Ritorno la struttura dell'Intent riconosciuto return response.query_result
def create_query_parameters(contexts=None): """Creates a QueryParameter with contexts. Last contexts used if contexts is empty. No contexts if none found. :param contexts: The ROS DialogflowContext message :type contexts: list(DialogflowContext) :return: A Dialogflow query parameters object. :rtype: QueryParameters """ # Create a context list is contexts are passed if contexts: rospy.logdebug("DF_CLIENT: Using the following contexts:\n{}".format( print_context_parameters(contexts))) contexts = contexts_msg_to_struct(contexts) return QueryParameters(contexts=contexts)
def detect_intent_text(self, msg): """Use the Dialogflow API to detect a user's intent. Goto the Dialogflow console to define intents and params. :param msg: DialogflowRequest msg :return query_result: Dialogflow's query_result with action parameters :rtype: DialogflowResult """ # Create the Query Input text_input = TextInput(text=msg.query_text, language_code=self._language_code) query_input = QueryInput(text=text_input) # Create QueryParameters user_contexts = converters.contexts_msg_to_struct(msg.contexts) self.last_contexts = converters.contexts_msg_to_struct( self.last_contexts) contexts = self.last_contexts + user_contexts params = QueryParameters(contexts=contexts) try: response = self._session_cli.detect_intent( session=self._session, query_input=query_input, query_params=params, output_audio_config=self._output_audio_config) except google.api_core.exceptions.ServiceUnavailable: rospy.logwarn( "DF_CLIENT: Deadline exceeded exception caught. The response " "took too long or you aren't connected to the internet!") else: # Store context for future use self.last_contexts = converters.contexts_struct_to_msg( response.query_result.output_contexts) df_msg = converters.result_struct_to_msg(response.query_result) self._results_pub.publish(df_msg) rospy.loginfo(output.print_result(response.query_result)) # Play audio if self.PLAY_AUDIO: self._play_stream(response.output_audio) return df_msg