Skip to content

Instantly share code, notes, and snippets.

@ganeshkamathp
Created May 1, 2017 15:57
Show Gist options
  • Save ganeshkamathp/c6f58e084c112110b64a9e1d84d244d7 to your computer and use it in GitHub Desktop.
Save ganeshkamathp/c6f58e084c112110b64a9e1d84d244d7 to your computer and use it in GitHub Desktop.
macos google assistant
"""Sample that implements gRPC client for Google Assistant API."""
from google.assistant.embedded.v1alpha1 import embedded_assistant_pb2
from google.rpc import code_pb2
import logging
import os.path
from googlesamples.assistant import (
assistant_helpers,
audio_helpers,
auth_helpers,
common_settings
)
def main():
logging.basicConfig(level=logging.INFO)
api_endpoint = 'embeddedassistant.googleapis.com'
# Load credentials.
try:
creds = auth_helpers.load_credentials(
"credentials.json", scopes=[common_settings.ASSISTANT_OAUTH_SCOPE]
)
except Exception as e:
logging.error('Error loading credentials: %s', e)
logging.error('Run auth_helpers to initialize new OAuth2 credentials.')
return
grpc_channel = auth_helpers.create_grpc_channel(api_endpoint, creds)
logging.info('Connecting to %s', api_endpoint)
assistant = embedded_assistant_pb2.EmbeddedAssistantStub(grpc_channel)
audio_source = audio_helpers.SoundDeviceStream(
sample_rate=16000,
sample_width=2,
block_size=6400,
flush_size=25600
)
audio_sink = audio_helpers.SoundDeviceStream(
sample_rate=16000,
sample_width=2,
block_size=6400,
flush_size=25600
)
conversation_stream = audio_helpers.ConversationStream(
source=audio_source,
sink=audio_sink,
iter_size=3200
)
conversation_state_bytes = None
wait_for_user_trigger = False
keep_running = True
while keep_running:
if wait_for_user_trigger:
input('Press Enter to send a new request...')
conversation_stream.start_recording()
logging.info('Recording audio request.')
def gen_converse_requests():
converse_state = None
if conversation_state_bytes:
logging.debug('Sending converse_state: %s',
conversation_state_bytes)
converse_state = embedded_assistant_pb2.ConverseState(
conversation_state=conversation_state_bytes,
)
config = embedded_assistant_pb2.ConverseConfig(
audio_in_config=embedded_assistant_pb2.AudioInConfig(
encoding='LINEAR16',
sample_rate_hertz=16000
),
audio_out_config=embedded_assistant_pb2.AudioOutConfig(
encoding='LINEAR16',
sample_rate_hertz=16000,
volume_percentage=50
),
converse_state=converse_state
)
yield embedded_assistant_pb2.ConverseRequest(config=config)
for data in conversation_stream:
yield embedded_assistant_pb2.ConverseRequest(audio_in=data)
def iter_converse_requests():
for c in gen_converse_requests():
assistant_helpers.log_converse_request_without_audio(c)
yield c
conversation_stream.start_playback()
for resp in assistant.Converse(iter_converse_requests(), 185):
assistant_helpers.log_converse_response_without_audio(resp)
if resp.error.code != code_pb2.OK:
logging.error('server error: %s', resp.error.message)
break
if resp.event_type == embedded_assistant_pb2.ConverseResponse.END_OF_UTTERANCE:
logging.info('End of audio request detected')
conversation_stream.stop_recording()
if resp.result.spoken_request_text:
logging.info('Transcript of user request: "%s".',
resp.result.spoken_request_text)
logging.info('Playing assistant response.')
if len(resp.audio_out.audio_data) > 0:
conversation_stream.write(resp.audio_out.audio_data)
if resp.result.spoken_response_text:
logging.info(
'Transcript of TTS response '
'(only populated from IFTTT): "%s".',
resp.result.spoken_response_text)
if resp.result.conversation_state:
conversation_state_bytes = resp.result.conversation_state
if resp.result.volume_percentage != 0:
logging.info('Volume should be set to %s%%', resp.result.volume_percentage)
if resp.result.microphone_mode == embedded_assistant_pb2.ConverseResult.DIALOG_FOLLOW_ON:
wait_for_user_trigger = False
logging.info('Expecting follow-on query from user.')
elif resp.result.microphone_mode == embedded_assistant_pb2.ConverseResult.CLOSE_MICROPHONE:
wait_for_user_trigger = True
keep_running = False
logging.info('Finished playing assistant response.')
conversation_stream.stop_playback()
conversation_stream.close()
if __name__ == '__main__':
main()
#!/bin/bash
source env/bin/activate
python3.5 google_assistant.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment