Google Assistant SDKのサンプルの中のtextinput.pyを使ってやってみます。
こんな感じ
やってみました……の図
GoogleからのAssistantの応答の再生はOpenJTalkを使ってみるのでインストールしておきましょう。
Assistant SDKのインストールとかはここを参照
目次
使用するコードはサンプルのtextinput.pyですが、これを以下のようにカスタマイズします。
① 42 – 83行
GUIと通信する部分を追加
② 222 – 236行
GUIからテキストを受け取ってAssistantに渡し、応答をテキストで受け取って音声再生用にGUIに戻す
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
import os import logging import json import click import google.auth.transport.grpc import google.auth.transport.requests import google.oauth2.credentials from socket import socket, AF_INET, SOCK_STREAM import threading HOST = 'localhost' PORT1 = 60002 PORT2 = 60003 MAX_BUFFER = 2048 NUM_THREAD = 4 hotword_flag = False request = "" from google.assistant.embedded.v1alpha2 import ( embedded_assistant_pb2, embedded_assistant_pb2_grpc ) try: from . import ( assistant_helpers, browser_helpers, ) except (SystemError, ImportError): import assistant_helpers import browser_helpers ASSISTANT_API_ENDPOINT = 'embeddedassistant.googleapis.com' DEFAULT_GRPC_DEADLINE = 60 * 3 + 5 PLAYING = embedded_assistant_pb2.ScreenOutConfig.PLAYING def responce_send(mess): while True: try: nani_sock = socket(AF_INET, SOCK_STREAM) nani_sock.connect((HOST, PORT2)) nani_sock.send(mess.encode('utf-8')) print ('responce_send:OK ') nani_sock.close() break except: #voice("populer","junbihaiikana") print ('responce_retry: ' + mess) break def request_receive(): global hotword_flag global request sock = socket(AF_INET, SOCK_STREAM) sock.bind ((HOST, PORT1)) sock.listen (NUM_THREAD) print ('receiver ready, NUM_THREAD = ' + str(NUM_THREAD)) while True: try: conn,addr = sock.accept() mess = conn.recv(MAX_BUFFER).decode('utf-8') conn.close() print('RECEIVED MESSAGE:' + mess) hotword_flag = True request = mess except: print('Receive Error') sock.close() #⑧ def request_receive_start(): #別スレッドで待ち受け th=threading.Thread(target=request_receive) th.start() class SampleTextAssistant(object): """Sample Assistant that supports text based conversations. Args: language_code: language for the conversation. device_model_id: identifier of the device model. device_id: identifier of the registered device instance. display: enable visual display of assistant response. channel: authorized gRPC channel for connection to the Google Assistant API. deadline_sec: gRPC deadline in seconds for Google Assistant API call. """ def __init__(self, language_code, device_model_id, device_id, display, channel, deadline_sec): self.language_code = language_code self.device_model_id = device_model_id self.device_id = device_id self.conversation_state = None # Force reset of first conversation. self.is_new_conversation = True self.display = display self.assistant = embedded_assistant_pb2_grpc.EmbeddedAssistantStub( channel ) self.deadline = deadline_sec def __enter__(self): return self def __exit__(self, etype, e, traceback): if e: return False def assist(self, text_query): """Send a text request to the Assistant and playback the response. """ def iter_assist_requests(): config = embedded_assistant_pb2.AssistConfig( audio_out_config=embedded_assistant_pb2.AudioOutConfig( encoding='LINEAR16', sample_rate_hertz=16000, volume_percentage=0, ), dialog_state_in=embedded_assistant_pb2.DialogStateIn( language_code=self.language_code, conversation_state=self.conversation_state, is_new_conversation=self.is_new_conversation, ), device_config=embedded_assistant_pb2.DeviceConfig( device_id=self.device_id, device_model_id=self.device_model_id, ), text_query=text_query, ) # Continue current conversation with later requests. self.is_new_conversation = False if self.display: config.screen_out_config.screen_mode = PLAYING req = embedded_assistant_pb2.AssistRequest(config=config) assistant_helpers.log_assist_request_without_audio(req) yield req text_response = None html_response = None for resp in self.assistant.Assist(iter_assist_requests(), self.deadline): assistant_helpers.log_assist_response_without_audio(resp) if resp.screen_out.data: html_response = resp.screen_out.data if resp.dialog_state_out.conversation_state: conversation_state = resp.dialog_state_out.conversation_state self.conversation_state = conversation_state if resp.dialog_state_out.supplemental_display_text: text_response = resp.dialog_state_out.supplemental_display_text return text_response, html_response @click.command() @click.option('--api-endpoint', default=ASSISTANT_API_ENDPOINT, metavar='<api endpoint>', show_default=True, help='Address of Google Assistant API service.') @click.option('--credentials', metavar='<credentials>', show_default=True, default=os.path.join(click.get_app_dir('google-oauthlib-tool'), 'credentials.json'), help='Path to read OAuth2 credentials.') @click.option('--device-model-id', metavar='<device model id>', required=True, help=(('Unique device model identifier, ' 'if not specifed, it is read from --device-config'))) @click.option('--device-id', metavar='<device id>', required=True, help=(('Unique registered device instance identifier, ' 'if not specified, it is read from --device-config, ' 'if no device_config found: a new device is registered ' 'using a unique id and a new device config is saved'))) @click.option('--lang', show_default=True, metavar='<language code>', default='en-US', help='Language code of the Assistant') @click.option('--display', is_flag=True, default=False, help='Enable visual display of Assistant responses in HTML.') @click.option('--verbose', '-v', is_flag=True, default=False, help='Verbose logging.') @click.option('--grpc-deadline', default=DEFAULT_GRPC_DEADLINE, metavar='<grpc deadline>', show_default=True, help='gRPC deadline in seconds') def main(api_endpoint, credentials, device_model_id, device_id, lang, display, verbose, grpc_deadline, *args, **kwargs): global hotword_flag global request # Setup logging. logging.basicConfig(level=logging.DEBUG if verbose else logging.INFO) # Load OAuth 2.0 credentials. try: with open(credentials, 'r') as f: credentials = google.oauth2.credentials.Credentials(token=None, **json.load(f)) http_request = google.auth.transport.requests.Request() credentials.refresh(http_request) except Exception as e: logging.error('Error loading credentials: %s', e) logging.error('Run google-oauthlib-tool to initialize ' 'new OAuth 2.0 credentials.') return # Create an authorized gRPC channel. grpc_channel = google.auth.transport.grpc.secure_authorized_channel( credentials, http_request, api_endpoint) logging.info('Connecting to %s', api_endpoint) request_receive_start() with SampleTextAssistant(lang, device_model_id, device_id, display, grpc_channel, grpc_deadline) as assistant: while True: if(hotword_flag == True): print('<you> %s' % request) response_text, response_html = assistant.assist(text_query=request) if display and response_html: system_browser = browser_helpers.system_browser system_browser.display(response_html) if response_text: print('<@assistant> %s' % response_text) responce_send(response_text) hotword_flag = False if __name__ == '__main__': main() |
テキスト入力ボタンなどを配置したGUI
【Gui.py】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from tkinter import * import tkinter import socket import os import subprocess import threading import raspi_openjtalk as jt HOST = 'localhost' PORT1 = 60002 PORT2 = 60003 MAX_BUFFER = 2048 NUM_THREAD = 4 my_pid = os.getpid() def quit(): subprocess.call("./stop_me.sh") def message_send(): temp = text.get() send_message(temp) def send_message(mes): while True: try: # 通信の確立 s_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s_sock.connect((HOST, PORT1)) # メッセージ送信 s_sock.send(mes.encode('utf-8')) # 通信の終了 s_sock.close() break except: print ('error: ' + mess) break def request_receive(): r_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) r_sock.bind ((HOST, PORT2)) r_sock.listen (NUM_THREAD) print ('receiver ready, NUM_THREAD = ' + str(NUM_THREAD)) while True: try: conn,addr = r_sock.accept() mess = conn.recv(MAX_BUFFER).decode('utf-8') conn.close() #print('RECEIVED MESSAGE:' + mess) mess = mess.replace("\n","。") jt.jtalk(mess) except: print('Receive Error') r_sock.close() # def request_receive_start(): #別スレッドで待ち受け th=threading.Thread(target=request_receive) th.start() root = tkinter.Tk() root.title(u"Google Assistant") root.geometry("400x100") root.resizable(0,0) root.protocol('WM_DELETE_WINDOW', quit) root.attributes("-topmost", True) bln = tkinter.BooleanVar() # Frame frame1 = tkinter.Frame(root) frame1.rowconfigure(1, weight=1) frame1.columnconfigure(0, weight=1) frame1.grid(sticky=(N,W,S,E)) #Button button1 = tkinter.Button(frame1, text='テキスト入力', command=message_send) button1.grid(row=0, column=0, columnspan=2, sticky=(N,W)) text = tkinter.Entry(frame1, width=55) text.grid(row=1, column=0, columnspan=2, sticky=(NW,SE)) request_receive_start() root.mainloop() |
Assistantのコンソール表示用シェル
【grpc_assistant.sh】
オプションにデバイスIDとデバイスモデルIDを指定する必要があります。
デバイスIDはSDKのインストールの過程では出てきません。
pushtotalkを実行すると表示されます、これを控えておきましょう。
例えばこんな感じ
#!/bin/bash
source /home/pi/env/bin/activate
lxterminal -e googlesamples-assistant-text2talk –device-id zzzzzz –device-model-id yyyyyyy
ファイルの配置
Assistantの環境をSDKで作った場合、ラズパイのホームに仮想環境(env)が作成されています。
そこに配置します。
/home/pi/env/bin/googlesamples-assistant-text2talk
/home/pi/env/lib/python3.5/site-packages/googlesamples/assistant/grpc/text2talk.py
以下の4つのファイルはどこに置いてもいいです。text2talkというフォルダーを作ってまとめて配置しときましょう。
raspi_openjtalk.pyはOpenJTlk用のサービスプログラム、stop_me.shはプロセス終了用のサービスプログラムです。
grpc_assistant.shは上記に従ってIDを書き換えてください。
/home/pi/text2talk/grpc_assistant.sh
/home/pi/text2talk/Gui.py
/home/pi/text2talk/raspi_openjtalk.py
/home/pi/text2talk/stop_me.sh
実行
左がGui.pyの起動画面、先に起動しておきます。右がgrpc_assistant.shの起動画面。
リクエストを入力して、ボタンをクリック
通信のIPアドレスを変えれば、外部PCなどからの入力でAssistantを使えます。
ラズパイでGoogle Assistantの応答をテキストに変換してみる
Leave a Reply