X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=google_assistant.py;h=0af4fa9271df3bf2e6de6f3571cfa6c2eb6d1f23;hb=a4bf4d05230474ad14243d67ac7f8c938f670e58;hp=71301e4779c2736a4a908df84b7ec7e67ba02b3f;hpb=1574e8a3a8982fab9278ad534f9427d464e4bffb;p=python_utils.git diff --git a/google_assistant.py b/google_assistant.py index 71301e4..0af4fa9 100644 --- a/google_assistant.py +++ b/google_assistant.py @@ -1,7 +1,9 @@ #!/usr/bin/env python3 import logging -from typing import NamedTuple +import sys +from typing import NamedTuple, Optional +import warnings import requests import speech_recognition as sr # type: ignore @@ -19,14 +21,14 @@ parser.add_argument( type=str, default="http://kiosk.house:3000", metavar="URL", - help="How to contact the Google Assistant bridge" + help="How to contact the Google Assistant bridge", ) parser.add_argument( "--google_assistant_username", type=str, metavar="GOOGLE_ACCOUNT", default="scott.gasch", - help="The user account for talking to Google Assistant" + help="The user account for talking to Google Assistant", ) @@ -34,7 +36,7 @@ class GoogleResponse(NamedTuple): success: bool response: str audio_url: str - audio_transcription: str + audio_transcription: Optional[str] # None if not available. def __repr__(self): return f""" @@ -45,10 +47,16 @@ audio_url: {self.audio_url}""" def tell_google(cmd: str, *, recognize_speech=True) -> GoogleResponse: + """Alias for ask_google.""" return ask_google(cmd, recognize_speech=recognize_speech) def ask_google(cmd: str, *, recognize_speech=True) -> GoogleResponse: + """Send a command string to Google via the google_assistant_bridge as the + user google_assistant_username and return the response. If recognize_speech + is True, perform speech recognition on the audio response from Google so as + to translate it into text (best effort, YMMV). + """ logging.debug(f"Asking google: '{cmd}'") payload = { "command": cmd, @@ -59,7 +67,7 @@ def ask_google(cmd: str, *, recognize_speech=True) -> GoogleResponse: success = False response = "" audio = "" - audio_transcription = "" + audio_transcription: Optional[str] = "" if r.status_code == 200: j = r.json() success = bool(j["success"]) @@ -79,17 +87,30 @@ def ask_google(cmd: str, *, recognize_speech=True) -> GoogleResponse: sample_rate=24000, sample_width=2, ) - audio_transcription = recognizer.recognize_google( - speech, - ) - logger.debug(f"Transcription: '{audio_transcription}'") + try: + audio_transcription = recognizer.recognize_google( + speech, + ) + logger.debug(f"Transcription: '{audio_transcription}'") + except sr.UnknownValueError as e: + logger.exception(e) + msg = 'Unable to parse Google assistant\'s response.' + logger.warning(msg) + warnings.warn(msg, stacklevel=3) + audio_transcription = None + return GoogleResponse( + success=success, + response=response, + audio_url=audio, + audio_transcription=audio_transcription, + ) else: - logger.error( - f'HTTP request to {url} with {payload} failed; code {r.status_code}' + message = f'HTTP request to {url} with {payload} failed; code {r.status_code}' + logger.error(message) + return GoogleResponse( + success=False, + response=message, + audio_url=audio, + audio_transcription=audio_transcription, ) - return GoogleResponse( - success=success, - response=response, - audio_url=audio, - audio_transcription=audio_transcription, - ) + sys.exit(-1)