def main(api_endpoint, credentials, project_id,
         device_model_id, device_id, device_config,
         lang, display, verbose,
         input_audio_file, output_audio_file,
         audio_sample_rate, audio_sample_width,
         audio_iter_size, audio_block_size, audio_flush_size,
         grpc_deadline, once, *args, **kwargs):
    """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>
    """
    # Setup logging.
    logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO)

    # Load OAuth 2.0 credentials.
    try:
        with open(credentials, 'r') as f:
            credentials = google.oauth2.credentials.Credentials(token=None,
                                                                **json.load(f))
            http_request = google.auth.transport.requests.Request()
            credentials.refresh(http_request)
    except Exception as e:
        logging.error('Error loading credentials: %s', e)
        logging.error('Run google-oauthlib-tool to initialize '
                      'new OAuth 2.0 credentials.')
        sys.exit(-1)

    # Create an authorized gRPC channel.
    grpc_channel = google.auth.transport.grpc.secure_authorized_channel(
        credentials, http_request, api_endpoint)
    logging.info('Connecting to %s', api_endpoint)

    # Configure audio source and sink.
    audio_device = None
    if input_audio_file:
        audio_source = audio_helpers.WaveSource(
            open(input_audio_file, 'rb'),
            sample_rate=audio_sample_rate,
            sample_width=audio_sample_width
        )
    else:
        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
            )
        )
    if output_audio_file:
        audio_sink = audio_helpers.WaveSink(
            open(output_audio_file, 'wb'),
            sample_rate=audio_sample_rate,
            sample_width=audio_sample_width
        )
    else:
        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,
    )

    if not device_id or not device_model_id:
        try:
            with open(device_config) as f:
                device = json.load(f)
                device_id = device['id']
                device_model_id = device['model_id']
                logging.info("Using device model %s and device id %s",
                             device_model_id,
                             device_id)
        except Exception as e:
            logging.warning('Device config not found: %s' % e)
            logging.info('Registering device')
            if not device_model_id:
                logging.error('Option --device-model-id required '
                              'when registering a device instance.')
                sys.exit(-1)
            if not project_id:
                logging.error('Option --project-id required '
                              'when registering a device instance.')
                sys.exit(-1)
            device_base_url = (
                'https://%s/v1alpha2/projects/%s/devices' % (api_endpoint,
                                                             project_id)
            )
            device_id = str(uuid.uuid1())
            payload = {
                'id': device_id,
                'model_id': device_model_id,
                'client_type': 'SDK_SERVICE'
            }
            session = google.auth.transport.requests.AuthorizedSession(
                credentials
            )
            r = session.post(device_base_url, data=json.dumps(payload))
            if r.status_code != 200:
                logging.error('Failed to register device: %s', r.text)
                sys.exit(-1)
            logging.info('Device registered: %s', device_id)
            pathlib.Path(os.path.dirname(device_config)).mkdir(exist_ok=True)
            with open(device_config, 'w') as f:
                json.dump(payload, f)

    device_handler = device_helpers.DeviceRequestHandler(device_id)

    @device_handler.command('action.devices.commands.OnOff')
    def onoff(on):
        if on:
            logging.info('Turning device on')
        else:
            logging.info('Turning device off')

    @device_handler.command('com.example.commands.BlinkLight')
    def blink(speed, number):
        logging.info('Blinking device %s times.' % number)
        delay = 1
        if speed == "SLOWLY":
            delay = 2
        elif speed == "QUICKLY":
            delay = 0.5
        for i in range(int(number)):
            logging.info('Device is blinking.')
            time.sleep(delay)

    with SampleAssistant(lang, device_model_id, device_id,
                         conversation_stream, display,
                         grpc_channel, grpc_deadline,
                         device_handler) as assistant:
        # If file arguments are supplied:
        # exit after the first turn of the conversation.
        if input_audio_file or output_audio_file:
            assistant.assist()
            return

        # If no file arguments supplied:
        # keep recording voice requests using the microphone
        # and playing back assistant response using the speaker.
        # When the once flag is set, don't wait for a trigger. Otherwise, wait.
        wait_for_user_trigger = not once
        while True:
            if wait_for_user_trigger:
                click.pause(info='Press Enter to send a new request...')
            continue_conversation = assistant.assist()
            # wait for user trigger if there is no follow-up turn in
            # the conversation.
            wait_for_user_trigger = not continue_conversation

            # If we only want one conversation, break.
            if once and (not continue_conversation):
                break
def main ( api_endpoint , credentials , project_id , device_model_id , device_id , device_config , lang , display , verbose , input_audio_file , output_audio_file , audio_sample_rate , audio_sample_width , audio_iter_size , audio_block_size , audio_flush_size , grpc_deadline , once , * args , ** kwargs ) : logging . basicConfig ( level = logging . DEBUG if verbose else logging . INFO ) try : with open ( credentials , 'r' ) as f : credentials = google . oauth2 . credentials . Credentials ( token = None , ** json . load ( f ) ) http_request = google . auth . transport . requests . Request ( ) credentials . refresh ( http_request ) except Exception as e : logging . error ( 'Error loading credentials: %s' , e ) logging . error ( 'Run google-oauthlib-tool to initialize ' 'new OAuth 2.0 credentials.' ) sys . exit ( - 1 ) grpc_channel = google . auth . transport . grpc . secure_authorized_channel ( credentials , http_request , api_endpoint ) logging . info ( 'Connecting to %s' , api_endpoint ) audio_device = None if input_audio_file : audio_source = audio_helpers . WaveSource ( open ( input_audio_file , 'rb' ) , sample_rate = audio_sample_rate , sample_width = audio_sample_width ) else : 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 ) ) if output_audio_file : audio_sink = audio_helpers . WaveSink ( open ( output_audio_file , 'wb' ) , sample_rate = audio_sample_rate , sample_width = audio_sample_width ) else : 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 ) ) conversation_stream = audio_helpers . ConversationStream ( source = audio_source , sink = audio_sink , iter_size = audio_iter_size , sample_width = audio_sample_width , ) if not device_id or not device_model_id : try : with open ( device_config ) as f : device = json . load ( f ) device_id = device [ 'id' ] device_model_id = device [ 'model_id' ] logging . info ( "Using device model %s and device id %s" , device_model_id , device_id ) except Exception as e : logging . warning ( 'Device config not found: %s' % e ) logging . info ( 'Registering device' ) if not device_model_id : logging . error ( 'Option --device-model-id required ' 'when registering a device instance.' ) sys . exit ( - 1 ) if not project_id : logging . error ( 'Option --project-id required ' 'when registering a device instance.' ) sys . exit ( - 1 ) device_base_url = ( 'https://%s/v1alpha2/projects/%s/devices' % ( api_endpoint , project_id ) ) device_id = str ( uuid . uuid1 ( ) ) payload = { 'id' : device_id , 'model_id' : device_model_id , 'client_type' : 'SDK_SERVICE' } session = google . auth . transport . requests . AuthorizedSession ( credentials ) r = session . post ( device_base_url , data = json . dumps ( payload ) ) if r . status_code != 200 : logging . error ( 'Failed to register device: %s' , r . text ) sys . exit ( - 1 ) logging . info ( 'Device registered: %s' , device_id ) pathlib . Path ( os . path . dirname ( device_config ) ) . mkdir ( exist_ok = True ) with open ( device_config , 'w' ) as f : json . dump ( payload , f ) device_handler = device_helpers . DeviceRequestHandler ( device_id ) @ device_handler . command ( 'action.devices.commands.OnOff' ) def onoff ( on ) : if on : logging . info ( 'Turning device on' ) else : logging . info ( 'Turning device off' ) @ device_handler . command ( 'com.example.commands.BlinkLight' ) def blink ( speed , number ) : logging . info ( 'Blinking device %s times.' % number ) delay = 1 if speed == "SLOWLY" : delay = 2 elif speed == "QUICKLY" : delay = 0.5 for i in range ( int ( number ) ) : logging . info ( 'Device is blinking.' ) time . sleep ( delay ) with SampleAssistant ( lang , device_model_id , device_id , conversation_stream , display , grpc_channel , grpc_deadline , device_handler ) as assistant : if input_audio_file or output_audio_file : assistant . assist ( ) return wait_for_user_trigger = not once while True : if wait_for_user_trigger : click . pause ( info = 'Press Enter to send a new request...' ) continue_conversation = assistant . assist ( ) wait_for_user_trigger = not continue_conversation if once and ( not continue_conversation ) : break
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>