google-assistant-sdk/googlesamples/assistant/grpc/pushtotalk.py
def assist(self):
        """Send a voice request to the Assistant and playback the response.

            Returns: True if conversation should continue.
            """
        continue_conversation = False
        device_actions_futures = []

        self.conversation_stream.start_recording()
        logging.info('Recording audio request.')

        def iter_log_assist_requests():
            for c in self.gen_assist_requests():
                assistant_helpers.log_assist_request_without_audio(c)
                yield c
            logging.debug('Reached end of AssistRequest iteration.')

        last_request = None
        # This generator yields AssistResponse proto messages
        # received from the gRPC Google Assistant API.
        for resp in self.assistant.Assist(iter_log_assist_requests(),
                                          self.deadline):
            assistant_helpers.log_assist_response_without_audio(resp)
            if resp.event_type == END_OF_UTTERANCE:
                logging.info('End of audio request detected.')
                logging.info('Stopping recording.')
                self.conversation_stream.stop_recording()

            if resp.speech_results:
                last_request = ' '.join(
                    r.transcript for r in resp.speech_results)
                logging.info('Transcript of user request: "%s".', last_request)

            if len(resp.audio_out.audio_data) > 0:
                if not self.conversation_stream.playing:
                    self.conversation_stream.stop_recording()
                    self.conversation_stream.start_playback()
                    logging.info('Playing assistant response.')
                self.conversation_stream.write(resp.audio_out.audio_data)

            if resp.dialog_state_out.conversation_state:
                conversation_state = resp.dialog_state_out.conversation_state
                logging.debug('Updating conversation state.')
                self.conversation_state = conversation_state

            if resp.dialog_state_out.volume_percentage != 0:
                volume_percentage = resp.dialog_state_out.volume_percentage
                logging.info('Setting volume to %s%%', volume_percentage)
                self.conversation_stream.volume_percentage = volume_percentage

            if resp.dialog_state_out.microphone_mode == DIALOG_FOLLOW_ON:
                continue_conversation = True
                logging.info('Expecting follow-on query from user.')

            elif resp.dialog_state_out.microphone_mode == CLOSE_MICROPHONE:
                continue_conversation = False

            if resp.device_action.device_request_json:
                device_request = json.loads(
                    resp.device_action.device_request_json)
                logging.info('Device request: %s', device_request)
                fs = self.device_handler(device_request)

                if fs:
                    device_actions_futures.extend(fs)

            if self.display and resp.screen_out.data:

                if len(device_actions_futures):
                    # Custom device actions triggered. Ignore the response from Assistant
                    continue

                # Showing data from the assistant (for big screens)
                system_browser = browser_helpers.system_browser
                html = resp.screen_out.data.decode('utf-8')
                system_browser.display(html.encode('utf-8'))

                # # Showing formatted data for mobile devices
                # if last_request:
                #     # Showing asynchronously
                #     threading.Thread(
                #         target=self.display_preview,
                #         args=[last_request]).start()
                #     # Showing synchronously (can be slow)
                #     # self.display_preview(last_request)
                #     last_request = None

            if resp.dialog_state_out.supplemental_display_text:
                text_response = resp.dialog_state_out.supplemental_display_text
                logging.info('Supplemental text: %s', text_response)

        if len(device_actions_futures):
            logging.info('Waiting for device executions to complete.')
            concurrent.futures.wait(device_actions_futures)

        logging.info('Finished playing assistant response.')
        self.conversation_stream.stop_playback()
        return continue_conversation
Send a voice request to the Assistant and playback the response.

            Returns: True if conversation should continue.
            
Send a voice request to the Assistant and playback the response.
Returns: True if conversation should continue.
Send a voice request to the Assistant and playback the response . Returns : True if conversation should continue .
def assist(self):
        continue_conversation = False
        device_actions_futures = []
        self.conversation_stream.start_recording()
        logging.info('Recording audio request.')
        def iter_log_assist_requests():
            for c in self.gen_assist_requests():
                assistant_helpers.log_assist_request_without_audio(c)
                yield c
            logging.debug('Reached end of AssistRequest iteration.')
        last_request = None
        for resp in self.assistant.Assist(iter_log_assist_requests(),
                                          self.deadline):
            assistant_helpers.log_assist_response_without_audio(resp)
            if resp.event_type == END_OF_UTTERANCE:
                logging.info('End of audio request detected.')
                logging.info('Stopping recording.')
                self.conversation_stream.stop_recording()
            if resp.speech_results:
                last_request = ' '.join(
                    r.transcript for r in resp.speech_results)
                logging.info('Transcript of user request: "%s".', last_request)
            if len(resp.audio_out.audio_data) > 0:
                if not self.conversation_stream.playing:
                    self.conversation_stream.stop_recording()
                    self.conversation_stream.start_playback()
                    logging.info('Playing assistant response.')
                self.conversation_stream.write(resp.audio_out.audio_data)
            if resp.dialog_state_out.conversation_state:
                conversation_state = resp.dialog_state_out.conversation_state
                logging.debug('Updating conversation state.')
                self.conversation_state = conversation_state
            if resp.dialog_state_out.volume_percentage != 0:
                volume_percentage = resp.dialog_state_out.volume_percentage
                logging.info('Setting volume to %s%%', volume_percentage)
                self.conversation_stream.volume_percentage = volume_percentage
            if resp.dialog_state_out.microphone_mode == DIALOG_FOLLOW_ON:
                continue_conversation = True
                logging.info('Expecting follow-on query from user.')
            elif resp.dialog_state_out.microphone_mode == CLOSE_MICROPHONE:
                continue_conversation = False
            if resp.device_action.device_request_json:
                device_request = json.loads(
                    resp.device_action.device_request_json)
                logging.info('Device request: %s', device_request)
                fs = self.device_handler(device_request)
                if fs:
                    device_actions_futures.extend(fs)
            if self.display and resp.screen_out.data:
                if len(device_actions_futures):
                    continue
                system_browser = browser_helpers.system_browser
                html = resp.screen_out.data.decode('utf-8')
                system_browser.display(html.encode('utf-8'))
            if resp.dialog_state_out.supplemental_display_text:
                text_response = resp.dialog_state_out.supplemental_display_text
                logging.info('Supplemental text: %s', text_response)
        if len(device_actions_futures):
            logging.info('Waiting for device executions to complete.')
            concurrent.futures.wait(device_actions_futures)
        logging.info('Finished playing assistant response.')
        self.conversation_stream.stop_playback()
        return continue_conversation
def assist ( self ) : continue_conversation = False device_actions_futures = [ ] self . conversation_stream . start_recording ( ) logging . info ( 'Recording audio request.' ) def iter_log_assist_requests ( ) : for c in self . gen_assist_requests ( ) : assistant_helpers . log_assist_request_without_audio ( c ) yield c logging . debug ( 'Reached end of AssistRequest iteration.' ) last_request = None for resp in self . assistant . Assist ( iter_log_assist_requests ( ) , self . deadline ) : assistant_helpers . log_assist_response_without_audio ( resp ) if resp . event_type == END_OF_UTTERANCE : logging . info ( 'End of audio request detected.' ) logging . info ( 'Stopping recording.' ) self . conversation_stream . stop_recording ( ) if resp . speech_results : last_request = ' ' . join ( r . transcript for r in resp . speech_results ) logging . info ( 'Transcript of user request: "%s".' , last_request ) if len ( resp . audio_out . audio_data ) > 0 : if not self . conversation_stream . playing : self . conversation_stream . stop_recording ( ) self . conversation_stream . start_playback ( ) logging . info ( 'Playing assistant response.' ) self . conversation_stream . write ( resp . audio_out . audio_data ) if resp . dialog_state_out . conversation_state : conversation_state = resp . dialog_state_out . conversation_state logging . debug ( 'Updating conversation state.' ) self . conversation_state = conversation_state if resp . dialog_state_out . volume_percentage != 0 : volume_percentage = resp . dialog_state_out . volume_percentage logging . info ( 'Setting volume to %s%%' , volume_percentage ) self . conversation_stream . volume_percentage = volume_percentage if resp . dialog_state_out . microphone_mode == DIALOG_FOLLOW_ON : continue_conversation = True logging . info ( 'Expecting follow-on query from user.' ) elif resp . dialog_state_out . microphone_mode == CLOSE_MICROPHONE : continue_conversation = False if resp . device_action . device_request_json : device_request = json . loads ( resp . device_action . device_request_json ) logging . info ( 'Device request: %s' , device_request ) fs = self . device_handler ( device_request ) if fs : device_actions_futures . extend ( fs ) if self . display and resp . screen_out . data : if len ( device_actions_futures ) : continue system_browser = browser_helpers . system_browser html = resp . screen_out . data . decode ( 'utf-8' ) system_browser . display ( html . encode ( 'utf-8' ) ) if resp . dialog_state_out . supplemental_display_text : text_response = resp . dialog_state_out . supplemental_display_text logging . info ( 'Supplemental text: %s' , text_response ) if len ( device_actions_futures ) : logging . info ( 'Waiting for device executions to complete.' ) concurrent . futures . wait ( device_actions_futures ) logging . info ( 'Finished playing assistant response.' ) self . conversation_stream . stop_playback ( ) return continue_conversation
Send a voice request to the Assistant and playback the response.
Send a voice request to the Assistant and playback the response .
"""Send a voice request to the Assistant and playback the response.

            Returns: True if conversation should continue.
            """ # This generator yields AssistResponse proto messages # received from the gRPC Google Assistant API. # Custom device actions triggered. Ignore the response from Assistant # Showing data from the assistant (for big screens) # # Showing formatted data for mobile devices # if last_request: #     # Showing asynchronously #     threading.Thread( #         target=self.display_preview, #         args=[last_request]).start() #     # Showing synchronously (can be slow) #     # self.display_preview(last_request) #     last_request = None
self