def try_to_get_credentials(client_secrets): """Try to get credentials, or print an error and quit on failure.""" if os.path.exists(ASSISTANT_CREDENTIALS): return auth_helpers.load_credentials(ASSISTANT_CREDENTIALS, scopes=[ASSISTANT_OAUTH_SCOPE]) if not os.path.exists(VR_CACHE_DIR): os.mkdir(VR_CACHE_DIR) if not os.path.exists(client_secrets) and os.path.exists( OLD_CLIENT_SECRETS): client_secrets = OLD_CLIENT_SECRETS if not os.path.exists(client_secrets): print('You need client secrets to use the Assistant API.') print('Follow these instructions:') print( ' https://developers.google.com/api-client-library/python/auth/installed-app' '#creatingcred') print('and put the file at', client_secrets) sys.exit(1) if not os.getenv('DISPLAY') and not sys.stdout.isatty(): print(""" To use the Assistant API, manually start the application from the dev terminal. See the "Turn on the Assistant API" section of the Voice Recognizer User's Guide for more info.""") sys.exit(1) credentials = auth_helpers.credentials_flow_interactive( client_secrets, scopes=[ASSISTANT_OAUTH_SCOPE]) auth_helpers.save_credentials(ASSISTANT_CREDENTIALS, credentials) logging.info('OAuth credentials initialized: %s', ASSISTANT_CREDENTIALS) return credentials
def setup_assistant(): """ This sets up the OAuth credentials for the Google Assistant. """ ue.log("Initializing Google Assistant.") # Initialize credentials credentials = os.path.join(sys.path[0], common_settings.ASSISTANT_CREDENTIALS_FILENAME) # Load credentials. try: global creds creds = auth_helpers.load_credentials( credentials, scopes=[common_settings.ASSISTANT_OAUTH_SCOPE, common_settings.PUBSUB_OAUTH_SCOPE] ) except Exception: # Maybe we didn't load the credentials yet? # This could happen on first run client_secret = os.path.join(sys.path[0], 'client_secrets.json') creds = auth_helpers.credentials_flow_interactive(client_secret, common_settings.ASSISTANT_OAUTH_SCOPE) auth_helpers.save_credentials(credentials, creds) try: creds = auth_helpers.load_credentials( credentials, scopes=[common_settings.ASSISTANT_OAUTH_SCOPE] ) except Exception as e: ue.log_error('Error loading credentials: ' + str(e)) ue.log_error('Run auth_helpers to initialize new OAuth2 credentials.') # Return invalid status code return -1 # Define endpoint # This might where you can inject custom API.AI behaviors? api_endpoint = ASSISTANT_API_ENDPOINT # Create an authorized gRPC channel. grpc_channel = auth_helpers.create_grpc_channel( api_endpoint, creds ) ue.log('Connecting to '+ str(api_endpoint)) global assistant assistant = embedded_assistant_pb2.EmbeddedAssistantStub(grpc_channel) global msg_queue msg_queue = [] return 0 # Initialized Google Assistant successfully
def begin_play(self): """Samples for the Google Assistant API. Examples: Run the sample with microphone input and speaker output: $ python -m googlesamples.assistant Run the sample with file input and speaker output: $ python -m googlesamples.assistant -i <input file> Run the sample with file input and output: $ python -m googlesamples.assistant -i <input file> -o <output file> """ ue.log('Initializing Google Samples API.') # Initialize defaults credentials = os.path.join( sys.path[0], common_settings.ASSISTANT_CREDENTIALS_FILENAME) # Load credentials. try: creds = auth_helpers.load_credentials( credentials, scopes=[common_settings.ASSISTANT_OAUTH_SCOPE]) except Exception: # Maybe we didn't load the credentials yet? # This could happen on first run creds = auth_helpers.credentials_flow_interactive( credentials, common_settings.ASSISTANT_OAUTH_SCOPE) auth_helpers.save_credentials(credentials, creds) try: creds = auth_helpers.load_credentials( credentials, scopes=[common_settings.ASSISTANT_OAUTH_SCOPE]) except Exception as e: ue.log_error('Error loading credentials: ' + str(e)) ue.log_error( 'Run auth_helpers to initialize new OAuth2 credentials.') return ue.log('Begin play done!') # Define endpoint # This might where you can inject custom API.AI behaviors? api_endpoint = ASSISTANT_API_ENDPOINT # Create an authorized gRPC channel. grpc_channel = auth_helpers.create_grpc_channel(api_endpoint, creds) ue.log('Connecting to ' + str(api_endpoint)) # Set up audio parameters audio_sample_rate = common_settings.DEFAULT_AUDIO_SAMPLE_RATE audio_sample_width = common_settings.DEFAULT_AUDIO_SAMPLE_WIDTH audio_iter_size = common_settings.DEFAULT_AUDIO_ITER_SIZE audio_block_size = common_settings.DEFAULT_AUDIO_DEVICE_BLOCK_SIZE audio_flush_size = common_settings.DEFAULT_AUDIO_DEVICE_FLUSH_SIZE # Configure audio source and sink. audio_device = None audio_source = audio_device = (audio_device or audio_helpers.SoundDeviceStream( sample_rate=audio_sample_rate, sample_width=audio_sample_width, block_size=audio_block_size, flush_size=audio_flush_size)) audio_sink = audio_device = (audio_device or audio_helpers.SoundDeviceStream( sample_rate=audio_sample_rate, sample_width=audio_sample_width, block_size=audio_block_size, flush_size=audio_flush_size)) # Create conversation stream with the given audio source and sink. conversation_stream = audio_helpers.ConversationStream( source=audio_source, sink=audio_sink, iter_size=audio_iter_size, sample_width=audio_sample_width, ) ue.log('Audio device: ' + str(audio_device)) self.assistant = SampleAssistant(conversation_stream, grpc_channel, common_settings.DEFAULT_GRPC_DEADLINE) self.assistant.converse()