Skip to content
This repository was archived by the owner on Oct 19, 2023. It is now read-only.

Commit bded2ca

Browse files
committed
google-assistant-sdk/pushtotalk: update sample to v1alpha2
- Converse -> Assist - ConverseRequest -> AssistRequest - ConverseResponse -> AssistResponse - ConverseState -> DialogStateIn - ConverseResult -> DialogStateOut - InterimSpokenRequestTextList -> SpeechRecognitionResult Bug: 69066657 Change-Id: I0c84c743529c122cc0b7445cfb66aef8eb7cbe4e
1 parent 9b0cb2d commit bded2ca

File tree

2 files changed

+68
-72
lines changed

2 files changed

+68
-72
lines changed

google-assistant-sdk/googlesamples/assistant/grpc/assistant_helpers.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,39 @@
1616

1717
import logging
1818

19-
from google.assistant.embedded.v1alpha1 import embedded_assistant_pb2
19+
from google.assistant.embedded.v1alpha2 import embedded_assistant_pb2
2020

2121

22-
END_OF_UTTERANCE = embedded_assistant_pb2.ConverseResponse.END_OF_UTTERANCE
23-
24-
25-
def log_converse_request_without_audio(converse_request):
26-
"""Log ConverseRequest fields without audio data."""
22+
def log_assist_request_without_audio(assist_request):
23+
"""Log AssistRequest fields without audio data."""
2724
if logging.getLogger().isEnabledFor(logging.DEBUG):
28-
resp_copy = embedded_assistant_pb2.ConverseRequest()
29-
resp_copy.CopyFrom(converse_request)
25+
resp_copy = embedded_assistant_pb2.AssistRequest()
26+
resp_copy.CopyFrom(assist_request)
3027
if len(resp_copy.audio_in) > 0:
3128
size = len(resp_copy.audio_in)
3229
resp_copy.ClearField('audio_in')
33-
logging.debug('ConverseRequest: audio_in (%d bytes)',
30+
logging.debug('AssistRequest: audio_in (%d bytes)',
3431
size)
3532
return
36-
logging.debug('ConverseRequest: %s', resp_copy)
33+
logging.debug('AssistRequest: %s', resp_copy)
3734

3835

39-
def log_converse_response_without_audio(converse_response):
40-
"""Log ConverseResponse fields without audio data."""
36+
def log_assist_response_without_audio(assist_response):
37+
"""Log AssistResponse fields without audio data."""
4138
if logging.getLogger().isEnabledFor(logging.DEBUG):
42-
resp_copy = embedded_assistant_pb2.ConverseResponse()
43-
resp_copy.CopyFrom(converse_response)
39+
resp_copy = embedded_assistant_pb2.AssistResponse()
40+
resp_copy.CopyFrom(assist_response)
4441
has_audio_data = (resp_copy.HasField('audio_out') and
4542
len(resp_copy.audio_out.audio_data) > 0)
4643
if has_audio_data:
4744
size = len(resp_copy.audio_out.audio_data)
4845
resp_copy.audio_out.ClearField('audio_data')
4946
if resp_copy.audio_out.ListFields():
50-
logging.debug('ConverseResponse: %s audio_data (%d bytes)',
47+
logging.debug('AssistResponse: %s audio_data (%d bytes)',
5148
resp_copy,
5249
size)
5350
else:
54-
logging.debug('ConverseResponse: audio_data (%d bytes)',
51+
logging.debug('AssistResponse: audio_data (%d bytes)',
5552
size)
5653
return
57-
logging.debug('ConverseResponse: %s', resp_copy)
54+
logging.debug('AssistResponse: %s', resp_copy)

google-assistant-sdk/googlesamples/assistant/grpc/pushtotalk.py

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@
2727
import google.auth.transport.requests
2828
import google.oauth2.credentials
2929

30-
from google.assistant.embedded.v1alpha1 import (
30+
from google.assistant.embedded.v1alpha2 import (
3131
embedded_assistant_pb2,
3232
embedded_assistant_pb2_grpc
3333
)
34-
from google.rpc import code_pb2
3534
from tenacity import retry, stop_after_attempt, retry_if_exception
3635

3736
try:
@@ -47,9 +46,9 @@
4746

4847

4948
ASSISTANT_API_ENDPOINT = 'embeddedassistant.googleapis.com'
50-
END_OF_UTTERANCE = embedded_assistant_pb2.ConverseResponse.END_OF_UTTERANCE
51-
DIALOG_FOLLOW_ON = embedded_assistant_pb2.ConverseResult.DIALOG_FOLLOW_ON
52-
CLOSE_MICROPHONE = embedded_assistant_pb2.ConverseResult.CLOSE_MICROPHONE
49+
END_OF_UTTERANCE = embedded_assistant_pb2.AssistResponse.END_OF_UTTERANCE
50+
DIALOG_FOLLOW_ON = embedded_assistant_pb2.DialogStateOut.DIALOG_FOLLOW_ON
51+
CLOSE_MICROPHONE = embedded_assistant_pb2.DialogStateOut.CLOSE_MICROPHONE
5352
DEFAULT_GRPC_DEADLINE = 60 * 3 + 5
5453

5554

@@ -67,16 +66,18 @@ class SampleAssistant(object):
6766
device_handler: callback for device actions.
6867
"""
6968

70-
def __init__(self, device_model_id, device_id, conversation_stream,
69+
def __init__(self, language_code, device_model_id, device_id,
70+
conversation_stream,
7171
channel, deadline_sec, device_handler):
72+
self.language_code = language_code
7273
self.device_model_id = device_model_id
7374
self.device_id = device_id
7475
self.conversation_stream = conversation_stream
7576

76-
# Opaque blob provided in ConverseResponse that,
77-
# when provided in a follow-up ConverseRequest,
77+
# Opaque blob provided in AssistResponse that,
78+
# when provided in a follow-up AssistRequest,
7879
# gives the Assistant a context marker within the current state
79-
# of the multi-Converse()-RPC "conversation".
80+
# of the multi-Assist()-RPC "conversation".
8081
# This value, along with MicrophoneMode, supports a more natural
8182
# "conversation" with the Assistant.
8283
self.conversation_state = None
@@ -106,7 +107,7 @@ def is_grpc_error_unavailable(e):
106107

107108
@retry(reraise=True, stop=stop_after_attempt(3),
108109
retry=retry_if_exception(is_grpc_error_unavailable))
109-
def converse(self):
110+
def assist(self):
110111
"""Send a voice request to the Assistant and playback the response.
111112
112113
Returns: True if conversation should continue.
@@ -117,46 +118,39 @@ def converse(self):
117118
self.conversation_stream.start_recording()
118119
logging.info('Recording audio request.')
119120

120-
def iter_converse_requests():
121-
for c in self.gen_converse_requests():
122-
assistant_helpers.log_converse_request_without_audio(c)
121+
def iter_assist_requests():
122+
for c in self.gen_assist_requests():
123+
assistant_helpers.log_assist_request_without_audio(c)
123124
yield c
124125
self.conversation_stream.start_playback()
125126

126-
# This generator yields ConverseResponse proto messages
127+
# This generator yields AssistResponse proto messages
127128
# received from the gRPC Google Assistant API.
128-
for resp in self.assistant.Converse(iter_converse_requests(),
129-
self.deadline):
130-
assistant_helpers.log_converse_response_without_audio(resp)
131-
if resp.error.code != code_pb2.OK:
132-
logging.error('server error: %s', resp.error.message)
133-
break
129+
for resp in self.assistant.Assist(iter_assist_requests(),
130+
self.deadline):
131+
assistant_helpers.log_assist_response_without_audio(resp)
134132
if resp.event_type == END_OF_UTTERANCE:
135133
logging.info('End of audio request detected')
136134
self.conversation_stream.stop_recording()
137-
if resp.result.spoken_request_text:
135+
if resp.speech_results:
138136
logging.info('Transcript of user request: "%s".',
139-
resp.result.spoken_request_text)
137+
' '.join(r.transcript
138+
for r in resp.speech_results))
140139
logging.info('Playing assistant response.')
141140
if len(resp.audio_out.audio_data) > 0:
142141
self.conversation_stream.write(resp.audio_out.audio_data)
143-
if resp.result.spoken_response_text:
144-
logging.info(
145-
'Transcript of TTS response '
146-
'(only populated from IFTTT): "%s".',
147-
resp.result.spoken_response_text)
148-
if resp.result.conversation_state:
149-
self.conversation_state = resp.result.conversation_state
150-
if resp.result.volume_percentage != 0:
151-
logging.info('Setting volume to %s%%',
152-
resp.result.volume_percentage)
153-
self.conversation_stream.volume_percentage = (
154-
resp.result.volume_percentage
155-
)
156-
if resp.result.microphone_mode == DIALOG_FOLLOW_ON:
142+
if resp.dialog_state_out.conversation_state:
143+
conversation_state = resp.dialog_state_out.conversation_state
144+
logging.debug('Updating conversation state.')
145+
self.conversation_state = conversation_state
146+
if resp.dialog_state_out.volume_percentage != 0:
147+
volume_percentage = resp.dialog_state_out.volume_percentage
148+
logging.info('Setting volume to %s%%', volume_percentage)
149+
self.conversation_stream.volume_percentage = volume_percentage
150+
if resp.dialog_state_out.microphone_mode == DIALOG_FOLLOW_ON:
157151
continue_conversation = True
158152
logging.info('Expecting follow-on query from user.')
159-
elif resp.result.microphone_mode == CLOSE_MICROPHONE:
153+
elif resp.dialog_state_out.microphone_mode == CLOSE_MICROPHONE:
160154
continue_conversation = False
161155
if resp.device_action.device_request_json:
162156
device_request = json.loads(
@@ -174,17 +168,17 @@ def iter_converse_requests():
174168
self.conversation_stream.stop_playback()
175169
return continue_conversation
176170

177-
def gen_converse_requests(self):
178-
"""Yields: ConverseRequest messages to send to the API."""
171+
def gen_assist_requests(self):
172+
"""Yields: AssistRequest messages to send to the API."""
179173

180-
converse_state = None
181-
if self.conversation_state:
182-
logging.debug('Sending converse_state: %s',
183-
self.conversation_state)
184-
converse_state = embedded_assistant_pb2.ConverseState(
185-
conversation_state=self.conversation_state,
174+
dialog_state_in = embedded_assistant_pb2.DialogStateIn(
175+
language_code=self.language_code,
176+
conversation_state=b''
186177
)
187-
config = embedded_assistant_pb2.ConverseConfig(
178+
if self.conversation_state:
179+
logging.debug('Sending conversation state.')
180+
dialog_state_in.conversation_state = self.conversation_state
181+
config = embedded_assistant_pb2.AssistConfig(
188182
audio_in_config=embedded_assistant_pb2.AudioInConfig(
189183
encoding='LINEAR16',
190184
sample_rate_hertz=self.conversation_stream.sample_rate,
@@ -194,18 +188,18 @@ def gen_converse_requests(self):
194188
sample_rate_hertz=self.conversation_stream.sample_rate,
195189
volume_percentage=self.conversation_stream.volume_percentage,
196190
),
197-
converse_state=converse_state,
191+
dialog_state_in=dialog_state_in,
198192
device_config=embedded_assistant_pb2.DeviceConfig(
199-
device_model_id=self.device_model_id,
200193
device_id=self.device_id,
194+
device_model_id=self.device_model_id,
201195
)
202196
)
203-
# The first ConverseRequest must contain the ConverseConfig
197+
# The first AssistRequest must contain the AssistConfig
204198
# and no audio data.
205-
yield embedded_assistant_pb2.ConverseRequest(config=config)
199+
yield embedded_assistant_pb2.AssistRequest(config=config)
206200
for data in self.conversation_stream:
207201
# Subsequent requests need audio data, but not config.
208-
yield embedded_assistant_pb2.ConverseRequest(audio_in=data)
202+
yield embedded_assistant_pb2.AssistRequest(audio_in=data)
209203

210204

211205
@click.command()
@@ -237,6 +231,10 @@ def gen_converse_requests(self):
237231
click.get_app_dir('googlesamples-assistant'),
238232
'device_config.json'),
239233
help='Path to save and restore the device configuration')
234+
@click.option('--lang', show_default=True,
235+
metavar='<language code>',
236+
default='en-US',
237+
help='Language code of the Assistant')
240238
@click.option('--verbose', '-v', is_flag=True, default=False,
241239
help='Verbose logging.')
242240
@click.option('--input-audio-file', '-i',
@@ -275,7 +273,7 @@ def gen_converse_requests(self):
275273
@click.option('--once', default=False, is_flag=True,
276274
help='Force termination after a single conversation.')
277275
def main(api_endpoint, credentials, project,
278-
device_model_id, device_id, device_config, verbose,
276+
device_model_id, device_id, device_config, lang, verbose,
279277
input_audio_file, output_audio_file,
280278
audio_sample_rate, audio_sample_width,
281279
audio_iter_size, audio_block_size, audio_flush_size,
@@ -398,13 +396,14 @@ def onoff(on):
398396
with open(device_config, 'w') as f:
399397
json.dump(payload, f)
400398

401-
with SampleAssistant(device_model_id, device_id, conversation_stream,
399+
with SampleAssistant(lang, device_model_id, device_id,
400+
conversation_stream,
402401
grpc_channel, grpc_deadline,
403402
device_handler) as assistant:
404403
# If file arguments are supplied:
405404
# exit after the first turn of the conversation.
406405
if input_audio_file or output_audio_file:
407-
assistant.converse()
406+
assistant.assist()
408407
return
409408

410409
# If no file arguments supplied:
@@ -415,7 +414,7 @@ def onoff(on):
415414
while True:
416415
if wait_for_user_trigger:
417416
click.pause(info='Press Enter to send a new request...')
418-
continue_conversation = assistant.converse()
417+
continue_conversation = assistant.assist()
419418
# wait for user trigger if there is no follow-up turn in
420419
# the conversation.
421420
wait_for_user_trigger = not continue_conversation

0 commit comments

Comments
 (0)