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

Commit fa6d7d2

Browse files
committed
google-assistant-sdk/pushtotalk: add html output
Bug: 77657440 Bug: 78541315 Change-Id: Ia8aefa2373fc5824addd369775bbcce36c189840
1 parent 92bd8ac commit fa6d7d2

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,21 @@
4040
from . import (
4141
assistant_helpers,
4242
audio_helpers,
43+
browser_helpers,
4344
device_helpers
4445
)
4546
except (SystemError, ImportError):
4647
import assistant_helpers
4748
import audio_helpers
49+
import browser_helpers
4850
import device_helpers
4951

5052

5153
ASSISTANT_API_ENDPOINT = 'embeddedassistant.googleapis.com'
5254
END_OF_UTTERANCE = embedded_assistant_pb2.AssistResponse.END_OF_UTTERANCE
5355
DIALOG_FOLLOW_ON = embedded_assistant_pb2.DialogStateOut.DIALOG_FOLLOW_ON
5456
CLOSE_MICROPHONE = embedded_assistant_pb2.DialogStateOut.CLOSE_MICROPHONE
57+
PLAYING = embedded_assistant_pb2.ScreenOutConfig.PLAYING
5558
DEFAULT_GRPC_DEADLINE = 60 * 3 + 5
5659

5760

@@ -70,12 +73,13 @@ class SampleAssistant(object):
7073
"""
7174

7275
def __init__(self, language_code, device_model_id, device_id,
73-
conversation_stream,
76+
conversation_stream, display,
7477
channel, deadline_sec, device_handler):
7578
self.language_code = language_code
7679
self.device_model_id = device_model_id
7780
self.device_id = device_id
7881
self.conversation_stream = conversation_stream
82+
self.display = display
7983

8084
# Opaque blob provided in AssistResponse that,
8185
# when provided in a follow-up AssistRequest,
@@ -142,6 +146,7 @@ def iter_log_assist_requests():
142146
for r in resp.speech_results))
143147
if len(resp.audio_out.audio_data) > 0:
144148
if not self.conversation_stream.playing:
149+
self.conversation_stream.stop_recording()
145150
self.conversation_stream.start_playback()
146151
logging.info('Playing assistant response.')
147152
self.conversation_stream.write(resp.audio_out.audio_data)
@@ -165,6 +170,9 @@ def iter_log_assist_requests():
165170
fs = self.device_handler(device_request)
166171
if fs:
167172
device_actions_futures.extend(fs)
173+
if self.display and resp.screen_out.data:
174+
system_browser = browser_helpers.system_browser
175+
system_browser.display(resp.screen_out.data)
168176

169177
if len(device_actions_futures):
170178
logging.info('Waiting for device executions to complete.')
@@ -200,6 +208,9 @@ def gen_assist_requests(self):
200208
device_model_id=self.device_model_id,
201209
)
202210
)
211+
if self.display:
212+
config.screen_out_config.screen_mode = PLAYING
213+
203214
# The first AssistRequest must contain the AssistConfig
204215
# and no audio data.
205216
yield embedded_assistant_pb2.AssistRequest(config=config)
@@ -241,6 +252,9 @@ def gen_assist_requests(self):
241252
metavar='<language code>',
242253
default='en-US',
243254
help='Language code of the Assistant')
255+
@click.option('--display', is_flag=True, default=False,
256+
help='Enable visual display of Assistant '
257+
'rich media responses (for certain queries).')
244258
@click.option('--verbose', '-v', is_flag=True, default=False,
245259
help='Verbose logging.')
246260
@click.option('--input-audio-file', '-i',
@@ -279,7 +293,8 @@ def gen_assist_requests(self):
279293
@click.option('--once', default=False, is_flag=True,
280294
help='Force termination after a single conversation.')
281295
def main(api_endpoint, credentials, project_id,
282-
device_model_id, device_id, device_config, lang, verbose,
296+
device_model_id, device_id, device_config,
297+
lang, display, verbose,
283298
input_audio_file, output_audio_file,
284299
audio_sample_rate, audio_sample_width,
285300
audio_iter_size, audio_block_size, audio_flush_size,
@@ -424,7 +439,7 @@ def blink(speed, number):
424439
time.sleep(delay)
425440

426441
with SampleAssistant(lang, device_model_id, device_id,
427-
conversation_stream,
442+
conversation_stream, display,
428443
grpc_channel, grpc_deadline,
429444
device_handler) as assistant:
430445
# If file arguments are supplied:
87.5 KB
Binary file not shown.

google-assistant-sdk/tests/test_endtoend.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,35 @@ def test_endtoend_pushtotalk():
8383
assert os.path.getsize(audio_out_file) > 0
8484

8585

86+
def test_endtoend_pushtotalk_htmloutput(device_model, device_instance):
87+
temp_dir = tempfile.mkdtemp()
88+
audio_out_file = os.path.join(temp_dir, 'out.raw')
89+
env = os.environ.copy()
90+
env['TMPDIR'] = temp_dir
91+
out = subprocess.check_output(['python', '-m',
92+
'googlesamples.assistant.grpc.pushtotalk',
93+
'--verbose',
94+
'--device-model-id', device_model,
95+
'--device-id', device_instance,
96+
'-i', 'tests/data/grapefruit.riff',
97+
'--display',
98+
'-o', audio_out_file],
99+
stderr=subprocess.STDOUT, env=env)
100+
print(out)
101+
assert 'grapefruit' in builtins.str(out).lower()
102+
assert os.path.getsize(audio_out_file) > 0
103+
files = [os.path.join(path, f)
104+
for path, _, fs in os.walk(temp_dir) for f in fs]
105+
assert len(files) > 0
106+
screen_out = None
107+
for f in files:
108+
if os.path.basename(f) == 'google-assistant-sdk-screen-out.html':
109+
screen_out = f
110+
assert screen_out is not None
111+
with open(screen_out, 'r') as f:
112+
assert 'pamplemousse' in f.read()
113+
114+
86115
def test_registration_pushtotalk(device_model):
87116
temp_dir = tempfile.mkdtemp()
88117
audio_out_file = os.path.join(temp_dir, 'out.raw')
@@ -136,7 +165,7 @@ def test_endtoend_textinput(device_model, device_instance):
136165
assert 'pamplemousse' in out
137166

138167

139-
def test_endtoend_htmloutput(device_model, device_instance):
168+
def test_endtoend_textinput_htmloutput(device_model, device_instance):
140169
temp_dir = tempfile.mkdtemp()
141170
env = os.environ.copy()
142171
env['TMPDIR'] = temp_dir

0 commit comments

Comments
 (0)