From dff4f4ee675807479b1ca16271bef73582fd45e5 Mon Sep 17 00:00:00 2001 From: David vonThenen <12752197+dvonthenen@users.noreply.github.com> Date: Mon, 10 Jun 2024 17:16:24 -0700 Subject: [PATCH] update --- deepgram/audio/microphone/microphone.py | 11 ++++++++--- deepgram/options.py | 6 ++++++ .../advanced/prerecorded/direct_invocation/main.py | 5 +++-- examples/advanced/streaming/mute-microphone/main.py | 2 -- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/deepgram/audio/microphone/microphone.py b/deepgram/audio/microphone/microphone.py index 232f87c5..572fb20e 100644 --- a/deepgram/audio/microphone/microphone.py +++ b/deepgram/audio/microphone/microphone.py @@ -5,7 +5,7 @@ import inspect import asyncio import threading -from typing import Optional +from typing import Optional, Callable import logging from deepgram.utils import verboselogs @@ -39,7 +39,7 @@ class Microphone: # pylint: disable=too-many-instance-attributes def __init__( self, - push_callback: Optional[object], + push_callback: Optional[Callable] = None, verbose: int = LOGGING, rate: int = RATE, chunk: int = CHUNK, @@ -84,7 +84,7 @@ def is_active(self) -> bool: self._logger.debug("Microphone.is_active LEAVE") return val - def set_callback(self, push_callback) -> None: + def set_callback(self, push_callback: Callable) -> None: """ Set the callback function to be called when data is received. """ @@ -102,6 +102,11 @@ def start(self) -> bool: self._logger.info("chunk: %d", self._chunk) self._logger.info("input_device_id: %d", self._input_device_index) + if self._push_callback_org is None: + self._logger.error("start() failed. No callback set.") + self._logger.debug("Microphone.start LEAVE") + return False + if inspect.iscoroutinefunction(self._push_callback_org): self._logger.verbose("async/await callback - wrapping") # Run our own asyncio loop. diff --git a/deepgram/options.py b/deepgram/options.py index 851c6323..03a8b569 100644 --- a/deepgram/options.py +++ b/deepgram/options.py @@ -38,6 +38,9 @@ def __init__( self.logger = verboselogs.VerboseLogger(__name__) self.logger.addHandler(logging.StreamHandler()) + if api_key is None: + api_key = "" + self.verbose = verbose self.api_key = api_key self._update_headers(headers=headers) @@ -99,6 +102,9 @@ def __init__( self.logger.addHandler(logging.StreamHandler()) self.logger.setLevel(verboselogs.WARNING) # temporary set for setup + if api_key is None: + api_key = "" + if api_key == "": api_key = os.getenv("DEEPGRAM_API_KEY", "") if api_key == "": diff --git a/examples/advanced/prerecorded/direct_invocation/main.py b/examples/advanced/prerecorded/direct_invocation/main.py index c79d375d..d6b88b4a 100644 --- a/examples/advanced/prerecorded/direct_invocation/main.py +++ b/examples/advanced/prerecorded/direct_invocation/main.py @@ -13,7 +13,7 @@ load_dotenv() AUDIO_URL = { - "url": "https://static.deepgram.com/examples/Bueller-Life-moves-pretty-fast.wav" + "url": "https://static.deepgram.com/examples-tmp/Bueller-Life-moves-pretty-fast.wav" } @@ -38,7 +38,8 @@ def main(): except Exception as e: print(f"Exception: {e}") - traceback.print_exc() + # enable the following line to print the stack trace + # traceback.print_exc() if __name__ == "__main__": diff --git a/examples/advanced/streaming/mute-microphone/main.py b/examples/advanced/streaming/mute-microphone/main.py index f6f385d0..86e9a2b6 100644 --- a/examples/advanced/streaming/mute-microphone/main.py +++ b/examples/advanced/streaming/mute-microphone/main.py @@ -40,9 +40,7 @@ def on_message(self, result, **kwargs): sentence = result.channel.alternatives[0].transcript if len(sentence) == 0: return - microphone.mute() print(f"speaker: {sentence}") - microphone.unmute() def on_metadata(self, metadata, **kwargs): print(f"\n\n{metadata}\n\n")