|
| 1 | +# Copyright (C) 2017 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Sample that implements a text client for the Google Assistant Service.""" |
| 16 | + |
| 17 | +import os |
| 18 | +import logging |
| 19 | +import json |
| 20 | + |
| 21 | +import click |
| 22 | +import google.auth.transport.grpc |
| 23 | +import google.auth.transport.requests |
| 24 | +import google.oauth2.credentials |
| 25 | + |
| 26 | +from google.assistant.embedded.v1alpha2 import ( |
| 27 | + embedded_assistant_pb2, |
| 28 | + embedded_assistant_pb2_grpc |
| 29 | +) |
| 30 | + |
| 31 | +try: |
| 32 | + from . import ( |
| 33 | + assistant_helpers, |
| 34 | + ) |
| 35 | +except SystemError: |
| 36 | + import assistant_helpers |
| 37 | + |
| 38 | + |
| 39 | +ASSISTANT_API_ENDPOINT = 'embeddedassistant.googleapis.com' |
| 40 | +DEFAULT_GRPC_DEADLINE = 60 * 3 + 5 |
| 41 | + |
| 42 | + |
| 43 | +class SampleTextAssistant(object): |
| 44 | + """Sample Assistant that supports text based conversations. |
| 45 | +
|
| 46 | + Args: |
| 47 | + language_code: language for the conversation. |
| 48 | + device_model_id: identifier of the device model. |
| 49 | + device_id: identifier of the registered device instance. |
| 50 | + channel: authorized gRPC channel for connection to the |
| 51 | + Google Assistant API. |
| 52 | + deadline_sec: gRPC deadline in seconds for Google Assistant API call. |
| 53 | + """ |
| 54 | + |
| 55 | + def __init__(self, language_code, device_model_id, device_id, |
| 56 | + channel, deadline_sec): |
| 57 | + self.language_code = language_code |
| 58 | + self.device_model_id = device_model_id |
| 59 | + self.device_id = device_id |
| 60 | + self.conversation_state = None |
| 61 | + self.assistant = embedded_assistant_pb2_grpc.EmbeddedAssistantStub( |
| 62 | + channel |
| 63 | + ) |
| 64 | + self.deadline = deadline_sec |
| 65 | + |
| 66 | + def __enter__(self): |
| 67 | + return self |
| 68 | + |
| 69 | + def __exit__(self, etype, e, traceback): |
| 70 | + if e: |
| 71 | + return False |
| 72 | + |
| 73 | + def assist(self, text_query): |
| 74 | + """Send a text request to the Assistant and playback the response. |
| 75 | + """ |
| 76 | + def iter_assist_requests(): |
| 77 | + dialog_state_in = embedded_assistant_pb2.DialogStateIn( |
| 78 | + language_code=self.language_code, |
| 79 | + conversation_state=b'' |
| 80 | + ) |
| 81 | + if self.conversation_state: |
| 82 | + dialog_state_in.conversation_state = self.conversation_state |
| 83 | + config = embedded_assistant_pb2.AssistConfig( |
| 84 | + audio_out_config=embedded_assistant_pb2.AudioOutConfig( |
| 85 | + encoding='LINEAR16', |
| 86 | + sample_rate_hertz=16000, |
| 87 | + volume_percentage=0, |
| 88 | + ), |
| 89 | + dialog_state_in=dialog_state_in, |
| 90 | + device_config=embedded_assistant_pb2.DeviceConfig( |
| 91 | + device_id=self.device_id, |
| 92 | + device_model_id=self.device_model_id, |
| 93 | + ), |
| 94 | + text_query=text_query, |
| 95 | + ) |
| 96 | + req = embedded_assistant_pb2.AssistRequest(config=config) |
| 97 | + assistant_helpers.log_assist_request_without_audio(req) |
| 98 | + yield req |
| 99 | + |
| 100 | + display_text = None |
| 101 | + for resp in self.assistant.Assist(iter_assist_requests(), |
| 102 | + self.deadline): |
| 103 | + assistant_helpers.log_assist_response_without_audio(resp) |
| 104 | + if resp.dialog_state_out.conversation_state: |
| 105 | + conversation_state = resp.dialog_state_out.conversation_state |
| 106 | + self.conversation_state = conversation_state |
| 107 | + if resp.dialog_state_out.supplemental_display_text: |
| 108 | + display_text = resp.dialog_state_out.supplemental_display_text |
| 109 | + return display_text |
| 110 | + |
| 111 | + |
| 112 | +@click.command() |
| 113 | +@click.option('--api-endpoint', default=ASSISTANT_API_ENDPOINT, |
| 114 | + metavar='<api endpoint>', show_default=True, |
| 115 | + help='Address of Google Assistant API service.') |
| 116 | +@click.option('--credentials', |
| 117 | + metavar='<credentials>', show_default=True, |
| 118 | + default=os.path.join(click.get_app_dir('google-oauthlib-tool'), |
| 119 | + 'credentials.json'), |
| 120 | + help='Path to read OAuth2 credentials.') |
| 121 | +@click.option('--device-model-id', |
| 122 | + metavar='<device model id>', |
| 123 | + help=(('Unique device model identifier, ' |
| 124 | + 'if not specifed, it is read from --device-config'))) |
| 125 | +@click.option('--device-id', |
| 126 | + metavar='<device id>', |
| 127 | + help=(('Unique registered device instance identifier, ' |
| 128 | + 'if not specified, it is read from --device-config, ' |
| 129 | + 'if no device_config found: a new device is registered ' |
| 130 | + 'using a unique id and a new device config is saved'))) |
| 131 | +@click.option('--lang', show_default=True, |
| 132 | + metavar='<language code>', |
| 133 | + default='en-US', |
| 134 | + help='Language code of the Assistant') |
| 135 | +@click.option('--verbose', '-v', is_flag=True, default=False, |
| 136 | + help='Verbose logging.') |
| 137 | +@click.option('--grpc-deadline', default=DEFAULT_GRPC_DEADLINE, |
| 138 | + metavar='<grpc deadline>', show_default=True, |
| 139 | + help='gRPC deadline in seconds') |
| 140 | +def main(api_endpoint, credentials, |
| 141 | + device_model_id, device_id, lang, verbose, |
| 142 | + grpc_deadline, *args, **kwargs): |
| 143 | + # Setup logging. |
| 144 | + logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO) |
| 145 | + |
| 146 | + # Load OAuth 2.0 credentials. |
| 147 | + try: |
| 148 | + with open(credentials, 'r') as f: |
| 149 | + credentials = google.oauth2.credentials.Credentials(token=None, |
| 150 | + **json.load(f)) |
| 151 | + http_request = google.auth.transport.requests.Request() |
| 152 | + credentials.refresh(http_request) |
| 153 | + except Exception as e: |
| 154 | + logging.error('Error loading credentials: %s', e) |
| 155 | + logging.error('Run google-oauthlib-tool to initialize ' |
| 156 | + 'new OAuth 2.0 credentials.') |
| 157 | + return |
| 158 | + |
| 159 | + # Create an authorized gRPC channel. |
| 160 | + grpc_channel = google.auth.transport.grpc.secure_authorized_channel( |
| 161 | + credentials, http_request, api_endpoint) |
| 162 | + logging.info('Connecting to %s', api_endpoint) |
| 163 | + |
| 164 | + with SampleTextAssistant(lang, device_model_id, device_id, |
| 165 | + grpc_channel, grpc_deadline) as assistant: |
| 166 | + while True: |
| 167 | + text_query = click.prompt('') |
| 168 | + click.echo('<you> %s' % text_query) |
| 169 | + display_text = assistant.assist(text_query=text_query) |
| 170 | + click.echo('<@assistant> %s' % display_text) |
| 171 | + |
| 172 | + |
| 173 | +if __name__ == '__main__': |
| 174 | + main() |
0 commit comments