Google Assistant Library プログラミングを楽しむ
Google LLC は Google Assistant SDK for devices として 「Google Assistant Library」「Google Assistant Service」 の二種類のセットを提供しています。前者は高水準で稼働環境は狭め、後者は低水準で広範な稼働環境に対応しており、この周到な構成に Google の本気度が窺えます。現時点では一日あたりのリクエスト数に制限はあるものの個人でも無償で利用できることが大きな魅力です。
Compatibility and feature support
The following table summarizes the platform compatibility requirements and the
supported features for the Google Assistant Library and the Google
Assistant Service:
Library Service
Supported architectures linux-armv7l and linux-x86_64 All gRPC platforms
Supported languages Python All gRPC languages
Hands-free activation
(Ok Google)Yes No
Audio capture and playback Built in Reference code is provided
Conversation state management Built in Reference code is provided
Timers and alarms Yes No
Playback of podcasts and news Yes No
Broadcast voice messages Yes No
Visual output (HTML5) of Assistant responses No Yes
Google Home デバイスやスマートフォンアプリを通じて Google アシスタントの存在感はどんどん大きくなっています。その一方で現時点では Google Assistant SDK プログラミングのための実践的な情報を国内外を通じてあまり見かけないことが残念に思われました。ネットの奥深くに息づいている Google アシスタント陣営の一連の機能を本 SDK ごしに柔軟に呼び出すことができればこの強力で魅力的なサービスをさらに活用できるのではないでしょうか。
- 音声認識
- 文意の解釈
- 応答文の生成
- 音声合成
- 言語翻訳
そんなわけで手元ではここしばらく Google Assistant SDK に向き合っています。今回はまず Google Assistant Library ベースでこれまでに行った調査と実験の内容を紹介します。
Google Assistant Library と稼働環境
前掲の表にも記載のあるように、今のところ Google Assistant Library の稼働環境は Python を利用可能な linux-armv7l または linux-x86_64 なプラットフォームに絞られます。周辺機器としてマイクとスピーカが必須。セットアップは下記ページからの説明にそって行えば問題ないでしょう。導入手順の詳細は随所で紹介されているためここでは省略します。
(※ スペックの上では Raspberry Pi 2 Model B/B+ も適合するかもしれません)
手元では作業用に 64bit lubuntu 環境の PC と Raspberry Pi 3 Model B+ を使っています。
hotword.py プログラムのこと
Google Assistant Library をセットアップ後の googlesamples/assistant/library/ フォルダには「hotword.py」プログラムが配置されます。実質 100行ほどの短い内容ですが、`sample` と言いながらこのプログラムを利用すればウェイクワードの検知を含め Google アシスタントとのいつものやりとりをそのまま実現することができます。抽象度が高くフロントエンドをこのようにシンプルに実装できることが Google Assistant Library の大きな特長です。
セットアップずみの SDK ディレクトリ下での hotword.py の実行方法は以下の要領です。引数で指定したプロジェクト ID とモデル ID はホスト側に記憶され、変更の必要がなければ次回以降は省略できます。
$ pwd
/home/t/wk/GoogleAssistant
$ ls
env
$ source env/bin/activate
(env) $ googlesamples-assistant-hotword --project-id [設定ずみのプロジェクトID] --device-model-id [設定すみのモデルID]
hotword.py を PC / Raspberry Pi 3 で実行した様子の動画です。ここでは個人的な好みからウェイクワードへの反応音「ポコッ」の再生をコードに加えています。
![]() |
上の動画のラズパイには手持ちの以下のマイク (USB 接続) とスピーカー (3.5mm オーディオジャック接続) を写真の要領でつないでいます。
|
一連の試作においてはこの hotword.py を下敷きにすることにしました。コンパクトで見通しのよい内容でありながらアシスタントとの応酬に必要な要素の一式が収められているためカスタマイズを試みるための土台としてはまさに好適でしょう。この記事の最後の項目に Google Assistant Library プログラミングを行う上での基本的な作法をまとめています。あわせて参照して下さい。
試作
今回手がけた試作をデモ動画と素のままのソースコードを添えて以下に掲載します。
1. 指定テキストに基づく音声合成と読み上げ
テーマ: Google アシスタントの音声合成機能を単体で利用する。いわゆる Text to Speech。
内容
デモ: 動画 48秒
考えたことなど
send_text_query(query)
Sends |query| to the Assistant as if it were spoken by the user.
This will behave the same as a user speaking the hotword and making a query OR speaking the answer to a follow-on query.
Parameters: query (str) - The text query to send to the Assistant.
ソースコード
- hotword_tts.py - github.com/mkttanabe
2. 利用者の発話内容をテキストへ変換
テーマ: Google アシスタントの音声認識機能を単体で利用する。いわゆる Speech to Text。
内容
デモ: 動画 41秒
考えたことなど
ON_CONVERSATION_TURN_STARTED
ON_END_OF_UTTERANCE
ON_RECOGNIZING_SPEECH_FINISHED:
{'text': '今何時'}
ON_RESPONDING_STARTED:
{'is_error_response': False}
ON_RESPONDING_FINISHED
ON_CONVERSATION_TURN_FINISHED:
{'with_follow_on_turn': False}
61 | if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and
62 | event.args and not event.args['with_follow_on_turn']):
63 | print()
ソースコード
- hotword_input.py - github.com/mkttanabe
3. 利用者の発話内容を他言語へ連続翻訳
テーマ: Google アシスタントの 音声認識 / 文意解釈 / 言語翻訳 / 応答文生成 / 音声合成 の各機能を利用する
内容
デモ: 動画 52秒
考えたことなど
ソースコード
- hotword_translate.py - github.com/mkttanabe
4. 利用者の発話内容を復唱
テーマ: Google アシスタントの音声認識 / 音声合成機能を利用する
内容
デモ: 動画 42秒
考えたことなど
ソースコード
- hotword_echo.py - github.com/mkttanabe
しりとり
テーマ: Google アシスタントの音声認識 / 音声合成機能を利用する
内容
デモ: 動画 58秒
考えたことなど
ソースコード
- 別途
メモ: Google Assistant Library プログラミングの基本
hotword.py を通じて
前述のように Google Assistant Library のインターフェイスは抽象度が高くシンプルに扱うことができる。実ロジック 100 行程度の hotoword.py が Google Home デバイスとほぼ同等に Google Assistant と連携可能であることは興味深い。hotoword.py の内容にあらためて目を向けると、デバイス ID 等の管理情報の取り回し以外の実質的な処理は以下のごく短い内容のみであることが見てとれる。
- hotword.py#L122-L145 より
122 | with Assistant(credentials, device_model_id) as assistant: 123 | events = assistant.start() : 144 | for event in events: 145 | process_event(event)
(リファレンスより)- class google.assistant.library.Assistant(credentials, device_model_id) - developers.google.com
Client for the Google Assistant Library.
Provides basic control functionality and lifecycle handling for the Google Assistant. It is best practice to use the Assistant as a ContextManager:
with Assistant(credentials, device_model_id) as assistant:
This allows the underlying native implementation to properly handle memory management.
Once start() is called, the Assistant generates a stream of Events relaying the various states the Assistant is currently in, for example:
ON_CONVERSATION_TURN_STARTED ON_END_OF_UTTERANCE ON_RECOGNIZING_SPEECH_FINISHED: {'text': 'what time is it'} ON_RESPONDING_STARTED: {'is_error_response': False} ON_RESPONDING_FINISHED ON_CONVERSATION_TURN_FINISHED: {'with_follow_on_turn': False}
See EventType for details on all events and their arguments.
- class google.assistant.library.Assistant(credentials, device_model_id) - developers.google.com
- hotword.py#L47-L66 より
47 | def process_event(event): : 56 | if event.type == EventType.ON_CONVERSATION_TURN_STARTED: 57 | print() 58 | 59 | print(event) 60 | 61 | if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and 62 | event.args and not event.args['with_follow_on_turn']): 63 | print() 64 | if event.type == EventType.ON_DEVICE_ACTION: 65 | for command, params in event.actions: 66 | print('Do command', command, 'with params', str(params))
(リファレンスより)- ON_CONVERSATION_TURN_STARTED - developers.google.com
Indicates a new turn has started.
The Assistant is currently listening, waiting for a user query. This could be the result of hearing the hotword or start_conversation() being called on the Assistant.- start_conversation() - developers.google.com
Manually starts a new conversation with the Assistant.
Starts both recording the user’s speech and sending it to Google, similar to what happens when the Assistant hears the hotword.
This method is a no-op if the Assistant is not started or has been muted.- send_text_query(query)
Sends query to the Assistant as if it were spoken by the user.
- send_text_query(query)
- stop_conversation() - developers.google.com
Stops any active conversation with the Assistant.
The Assistant could be listening to the user’s query OR responding. If there is no active conversation, this is a no-op.
- start_conversation() - developers.google.com
- ON_CONVERSATION_TURN_FINISHED - developers.google.com
The Assistant finished the current turn.
This includes both processing a user’s query and speaking the full response, if any.
- ON_CONVERSATION_TURN_STARTED - developers.google.com
ポイント
- Google Assistant との一連の応酬を開始するには google.assistant.library.Assistant クラスのインスタンスを生成し start() をコールする
- 応酬中の状況はイベントベースで捕捉可能
- 対話開始: ON_CONVERSATION_TURN_STARTED
- 対話終了: ON_CONVERSATION_TURN_FINISHED
- 後は EventType ごとに必要な処理を記述
- start_conversation() によりウェイクワードなしで Assistant との対話を開始
- send_text_query() により発話に代え任意のテキストを Assistant へ送出可能
- stop_conversation() により Assistant との対話を任意に終了
(tanabe)
この記事へのコメント
私の環境で
send_text_query('こんにちは')
を試すと、UnicodeEncodeError:になってしまいます。
ライブラリ内のquery.encode('ASCII')が原因のようですが、回避方法がわかりません。
File "/usr/local/lib/python3.5/dist-packages-linux-armv6l/google/assistant/library/assistant.py", line 215, in send_text_query
self._lib.assistant_send_text_query(self._inst, query.encode('ASCII'))
UnicodeEncodeError: 'ascii' codec can't encode characters
回避方法をご存知でしたら、ご教授願います。
ちなみに、
send_text_query('sing a song')のように英語のテキストだとうまく動作しています。
私の環境:
- AIY Voice Kit(pi zero w と voice bonnet)
- 最新のAIYプロジェクトイメージ
どうぞよろしくお願いいたします。
「"UnicodeEncodeError: 'ascii' codec can't encode characters" sitecustomize.py」 といったキーワードで情報を確認されるのが良いのではないかと思います。
良い展開をお祈りします。
send_text_query(query) のテストは、PC環境、Pi3環境、どちらで行われたのでしょうか?
send_text_query()を日本語で使うのに、ライブラリー内に何か変更を加える必要はありましたか?