Skip to content

Commit

Permalink
Merge pull request #246 from dvonthenen/expose-missing-options
Browse files Browse the repository at this point in the history
Expose Missing Options on Prerecorded/Live
  • Loading branch information
davidvonthenen authored Jan 10, 2024
2 parents 1333f2f + 5220025 commit 6eb043a
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 10 deletions.
7 changes: 6 additions & 1 deletion deepgram/clients/live/v1/async_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Deepgram SDK contributors. All Rights Reserved.
# Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved.
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
# SPDX-License-Identifier: MIT
import asyncio
Expand Down Expand Up @@ -50,6 +50,11 @@ async def start(self, options: LiveOptions = None, addons: dict = None, **kwargs
self.logger.info("addons: %s", addons)
self.logger.info("options: %s", kwargs)

if options is not None and not options.check():
self.logger.error("options.check failed")
self.logger.debug("AsyncLiveClient.start LEAVE")
raise DeepgramError("Fatal transcription options error")

self.options = options
if addons is not None:
self.__dict__.update(addons)
Expand Down
7 changes: 6 additions & 1 deletion deepgram/clients/live/v1/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Deepgram SDK contributors. All Rights Reserved.
# Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved.
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
# SPDX-License-Identifier: MIT
import json
Expand Down Expand Up @@ -57,6 +57,11 @@ def start(self, options: LiveOptions = None, addons: dict = None, **kwargs):
self.logger.info("addon: %s", addons)
self.logger.info("options: %s", kwargs)

if options is not None and not options.check():
self.logger.error("options.check failed")
self.logger.debug("LiveClient.start LEAVE")
raise DeepgramError("Fatal transcription options error")

self.options = options
if addons is not None:
self.__dict__.update(addons)
Expand Down
28 changes: 27 additions & 1 deletion deepgram/clients/live/v1/options.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Copyright 2023 Deepgram SDK contributors. All Rights Reserved.
# Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved.
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
# SPDX-License-Identifier: MIT

from dataclasses import dataclass
from dataclasses_json import dataclass_json
from typing import List, Optional
import logging, verboselogs


@dataclass_json
Expand All @@ -17,11 +18,16 @@ class LiveOptions:
https://developers.deepgram.com/reference/streaming
"""

alternatives: Optional[int] = None
callback: Optional[str] = None
callback_method: Optional[str] = None
channels: Optional[int] = None
diarize: Optional[bool] = None
diarize_version: Optional[str] = None
encoding: Optional[str] = None
endpointing: Optional[str] = None
extra: Optional[str] = None
filler_words: Optional[bool] = None
interim_results: Optional[bool] = None
keywords: Optional[str] = None
language: Optional[str] = None
Expand All @@ -43,3 +49,23 @@ class LiveOptions:
def __getitem__(self, key):
_dict = self.to_dict()
return _dict[key]

def check(self):
verboselogs.install()
logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())
prev = logger.level
logger.setLevel(logging.ERROR)

if self.numerals:
logger.error(
"WARNING: Numerals is deprecated. Will be removed in a future version. Please use smart_format instead."
)
if self.tier:
logger.error(
"WARNING: Tier is deprecated. Will be removed in a future version."
)

logger.setLevel(prev)

return True
20 changes: 19 additions & 1 deletion deepgram/clients/prerecorded/errors.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
# Copyright 2023 Deepgram SDK contributors. All Rights Reserved.
# Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved.
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
# SPDX-License-Identifier: MIT


class DeepgramError(Exception):
"""
Exception raised for unknown errors related to the Deepgram API.
Attributes:
message (str): The error message describing the exception.
"""

def __init__(self, message: str):
super().__init__(message)
self.name = "DeepgramError"
self.message = message

def __str__(self):
return f"{self.name}: {self.message}"


class DeepgramTypeError(Exception):
"""
Exception raised for unknown errors related to unknown Types for Transcription.
Attributes:
message (str): The error message describing the exception.
"""

def __init__(self, message: str):
super().__init__(message)
self.name = "DeepgramTypeError"
Expand Down
24 changes: 22 additions & 2 deletions deepgram/clients/prerecorded/v1/async_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Deepgram SDK contributors. All Rights Reserved.
# Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved.
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
# SPDX-License-Identifier: MIT

Expand All @@ -8,7 +8,7 @@
import logging, verboselogs

from ...abstract_async_client import AbstractAsyncRestClient
from ..errors import DeepgramTypeError
from ..errors import DeepgramError, DeepgramTypeError
from ..helpers import is_buffer_source, is_readstream_source, is_url_source
from ..source import UrlSource, FileSource

Expand Down Expand Up @@ -68,6 +68,11 @@ async def transcribe_url(
self.logger.debug("PreRecordedClient.transcribe_url LEAVE")
raise DeepgramTypeError("Unknown transcription source type")

if options is not None and not options.check():
self.logger.error("options.check failed")
self.logger.debug("PreRecordedClient.transcribe_url LEAVE")
raise DeepgramError("Fatal transcription options error")

self.logger.info("url: %s", url)
self.logger.info("source: %s", source)
if isinstance(options, PrerecordedOptions):
Expand Down Expand Up @@ -123,6 +128,11 @@ async def transcribe_url_callback(
self.logger.debug("PreRecordedClient.transcribe_url_callback LEAVE")
raise DeepgramTypeError("Unknown transcription source type")

if options is not None and not options.check():
self.logger.error("options.check failed")
self.logger.debug("PreRecordedClient.transcribe_url_callback LEAVE")
raise DeepgramError("Fatal transcription options error")

self.logger.info("url: %s", url)
self.logger.info("source: %s", source)
if isinstance(options, PrerecordedOptions):
Expand Down Expand Up @@ -181,6 +191,11 @@ async def transcribe_file(
self.logger.debug("PreRecordedClient.transcribe_file LEAVE")
raise DeepgramTypeError("Unknown transcription source type")

if options is not None and not options.check():
self.logger.error("options.check failed")
self.logger.debug("PreRecordedClient.transcribe_file LEAVE")
raise DeepgramError("Fatal transcription options error")

self.logger.info("url: %s", url)
if isinstance(options, PrerecordedOptions):
self.logger.info("PrerecordedOptions switching class -> json")
Expand Down Expand Up @@ -237,6 +252,11 @@ async def transcribe_file_callback(
self.logger.debug("PreRecordedClient.transcribe_file_callback LEAVE")
raise DeepgramTypeError("Unknown transcription source type")

if options is not None and not options.check():
self.logger.error("options.check failed")
self.logger.debug("PreRecordedClient.transcribe_file_callback LEAVE")
raise DeepgramError("Fatal transcription options error")

self.logger.info("url: %s", url)
if isinstance(options, PrerecordedOptions):
self.logger.info("PrerecordedOptions switching class -> json")
Expand Down
24 changes: 22 additions & 2 deletions deepgram/clients/prerecorded/v1/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Deepgram SDK contributors. All Rights Reserved.
# Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved.
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
# SPDX-License-Identifier: MIT

Expand All @@ -7,7 +7,7 @@
import json

from ...abstract_sync_client import AbstractSyncRestClient
from ..errors import DeepgramTypeError
from ..errors import DeepgramError, DeepgramTypeError
from ..helpers import is_buffer_source, is_readstream_source, is_url_source
from ..source import UrlSource, FileSource

Expand Down Expand Up @@ -67,6 +67,11 @@ def transcribe_url(
self.logger.debug("PreRecordedClient.transcribe_url LEAVE")
raise DeepgramTypeError("Unknown transcription source type")

if options is not None and not options.check():
self.logger.error("options.check failed")
self.logger.debug("PreRecordedClient.transcribe_url LEAVE")
raise DeepgramError("Fatal transcription options error")

self.logger.info("url: %s", url)
self.logger.info("source: %s", source)
if isinstance(options, PrerecordedOptions):
Expand Down Expand Up @@ -122,6 +127,11 @@ def transcribe_url_callback(
self.logger.debug("PreRecordedClient.transcribe_url_callback LEAVE")
raise DeepgramTypeError("Unknown transcription source type")

if options is not None and not options.check():
self.logger.error("options.check failed")
self.logger.debug("PreRecordedClient.transcribe_url_callback LEAVE")
raise DeepgramError("Fatal transcription options error")

self.logger.info("url: %s", url)
self.logger.info("source: %s", source)
if isinstance(options, PrerecordedOptions):
Expand Down Expand Up @@ -180,6 +190,11 @@ def transcribe_file(
self.logger.debug("PreRecordedClient.transcribe_file LEAVE")
raise DeepgramTypeError("Unknown transcription source type")

if options is not None and not options.check():
self.logger.error("options.check failed")
self.logger.debug("PreRecordedClient.transcribe_file LEAVE")
raise DeepgramError("Fatal transcription options error")

self.logger.info("url: %s", url)
if isinstance(options, PrerecordedOptions):
self.logger.info("PrerecordedOptions switching class -> json")
Expand Down Expand Up @@ -236,6 +251,11 @@ def transcribe_file_callback(
self.logger.debug("PreRecordedClient.transcribe_file_callback LEAVE")
raise DeepgramTypeError("Unknown transcription source type")

if options is not None and not options.check():
self.logger.error("options.check failed")
self.logger.debug("PreRecordedClient.transcribe_file_callback LEAVE")
raise DeepgramError("Fatal transcription options error")

self.logger.info("url: %s", url)
if isinstance(options, PrerecordedOptions):
self.logger.info("PrerecordedOptions switching class -> json")
Expand Down
30 changes: 28 additions & 2 deletions deepgram/clients/prerecorded/v1/options.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Copyright 2023 Deepgram SDK contributors. All Rights Reserved.
# Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved.
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
# SPDX-License-Identifier: MIT

from dataclasses import dataclass
from dataclasses_json import dataclass_json
from typing import Union, List, TypedDict, Optional
import logging, verboselogs


@dataclass_json
Expand All @@ -19,13 +20,18 @@ class PrerecordedOptions:

alternatives: Optional[int] = None
callback: Optional[str] = None
callback_method: Optional[bool] = None
callback_method: Optional[str] = None
detect_entities: Optional[bool] = None
detect_language: Optional[bool] = None
detect_topics: Optional[bool] = None
diarize: Optional[bool] = None
diarize_version: Optional[str] = None
dictation: Optional[bool] = None
extra: Optional[str] = None
filler_words: Optional[bool] = None
keywords: Optional[Union[list, str]] = None
language: Optional[str] = None
measurements: Optional[bool] = None
model: Optional[str] = None
multichannel: Optional[bool] = None
numerals: Optional[bool] = None
Expand All @@ -46,3 +52,23 @@ class PrerecordedOptions:
def __getitem__(self, key):
_dict = self.to_dict()
return _dict[key]

def check(self):
verboselogs.install()
logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())
prev = logger.level
logger.setLevel(logging.ERROR)

if self.numerals:
logger.error(
"WARNING: Numerals is deprecated. Will be removed in a future version. Please use smart_format instead."
)
if self.tier:
logger.error(
"WARNING: Tier is deprecated. Will be removed in a future version."
)

logger.setLevel(prev)

return True

0 comments on commit 6eb043a

Please sign in to comment.