Skip to content

Commit

Permalink
[HOLD] Reintroduce TTS WS
Browse files Browse the repository at this point in the history
  • Loading branch information
dvonthenen committed Aug 8, 2024
1 parent 78ec6d9 commit 7a01771
Show file tree
Hide file tree
Showing 24 changed files with 2,462 additions and 143 deletions.
34 changes: 17 additions & 17 deletions deepgram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@
from .client import (
SpeakOptions,
SpeakRESTOptions,
# SpeakWebSocketOptions,
SpeakWebSocketOptions,
# FileSource,
SpeakRestSource,
SpeakSource,
)
from .client import SpeakWebSocketEvents
from .client import SpeakWebSocketEvents, SpeakWebSocketMessage

## speak REST
from .client import (
Expand All @@ -115,21 +115,21 @@
SpeakRESTResponse,
)

# ## speak WebSocket
# from .client import (
# SpeakWebSocketClient,
# AsyncSpeakWebSocketClient,
# )
# from .client import (
# SpeakWebSocketResponse,
# # OpenResponse,
# # MetadataResponse,
# FlushedResponse,
# # CloseResponse,
# # UnhandledResponse,
# WarningResponse,
# # ErrorResponse,
# )
## speak WebSocket
from .client import (
SpeakWebSocketClient,
AsyncSpeakWebSocketClient,
)
from .client import (
SpeakWebSocketResponse,
# OpenResponse,
# MetadataResponse,
FlushedResponse,
# CloseResponse,
# UnhandledResponse,
WarningResponse,
# ErrorResponse,
)

# manage
from .client import ManageClient, AsyncManageClient
Expand Down
34 changes: 17 additions & 17 deletions deepgram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@
from .clients import (
SpeakOptions,
SpeakRESTOptions,
# SpeakWebSocketOptions,
SpeakWebSocketOptions,
# FileSource,
SpeakRestSource,
SpeakSource,
)
from .clients import SpeakWebSocketEvents
from .clients import SpeakWebSocketEvents, SpeakWebSocketMessage

## speak REST
from .clients import (
Expand All @@ -119,21 +119,21 @@
SpeakRESTResponse,
)

# ## speak WebSocket
# from .clients import (
# SpeakWebSocketClient,
# AsyncSpeakWebSocketClient,
# )
# from .clients import (
# SpeakWebSocketResponse,
# # OpenResponse,
# # MetadataResponse,
# FlushedResponse,
# # CloseResponse,
# # UnhandledResponse,
# WarningResponse,
# # ErrorResponse,
# )
## speak WebSocket
from .clients import (
SpeakWebSocketClient,
AsyncSpeakWebSocketClient,
)
from .clients import (
SpeakWebSocketResponse,
# OpenResponse,
# MetadataResponse,
FlushedResponse,
# CloseResponse,
# UnhandledResponse,
WarningResponse,
# ErrorResponse,
)

# manage client classes/input
from .clients import ManageClient, AsyncManageClient
Expand Down
34 changes: 17 additions & 17 deletions deepgram/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@
from .speak import (
SpeakOptions,
SpeakRESTOptions,
# SpeakWebSocketOptions,
SpeakWebSocketOptions,
# FileSource,
SpeakRestSource,
SpeakSource,
)

from .speak import SpeakWebSocketEvents
from .speak import SpeakWebSocketEvents, SpeakWebSocketMessage

## text-to-speech REST
from .speak import (
Expand All @@ -125,21 +125,21 @@
SpeakRESTResponse,
)

# ## text-to-speech WebSocket
# from .speak import (
# SpeakWebSocketClient,
# AsyncSpeakWebSocketClient,
# )
# from .speak import (
# SpeakWebSocketResponse,
# # OpenResponse,
# # MetadataResponse,
# FlushedResponse,
# # CloseResponse,
# # UnhandledResponse,
# WarningResponse,
# # ErrorResponse,
# )
## text-to-speech WebSocket
from .speak import (
SpeakWebSocketClient,
AsyncSpeakWebSocketClient,
)
from .speak import (
SpeakWebSocketResponse,
# OpenResponse,
# MetadataResponse,
FlushedResponse,
# CloseResponse,
# UnhandledResponse,
WarningResponse,
# ErrorResponse,
)

# manage
from .manage import ManageClient, AsyncManageClient
Expand Down
1 change: 0 additions & 1 deletion deepgram/clients/abstract_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ async def _handle_request_raw(
client = httpx.AsyncClient(timeout=timeout, transport=transport)
if transport:
kwargs.pop("transport")
kwargs.pop("transport")
req = client.build_request(method, _url, headers=_headers, **kwargs)
return await client.send(req, stream=True)

Expand Down
6 changes: 3 additions & 3 deletions deepgram/clients/abstract_sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,9 @@ def _handle_request_raw(

try:
transport = kwargs.get("transport")
with httpx.Client(timeout=timeout, transport=transport) as client:
if transport:
kwargs.pop("transport")
client = httpx.Client(timeout=timeout, transport=transport)
if transport:
kwargs.pop("transport")
req = client.build_request(method, _url, headers=_headers, **kwargs)
return client.send(req, stream=True)

Expand Down
38 changes: 26 additions & 12 deletions deepgram/clients/listen/v1/websocket/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ class AsyncListenWebSocketClient: # pylint: disable=too-many-instance-attribute
_socket: WebSocketClientProtocol
_event_handlers: Dict[LiveTranscriptionEvents, list]

_last_datagram: Optional[datetime] = None

_listen_thread: Union[asyncio.Task, None]
_keep_alive_thread: Union[asyncio.Task, None]
_flush_thread: Union[asyncio.Task, None]
_last_datagram: Optional[datetime] = None

_kwargs: Optional[Dict] = None
_addons: Optional[Dict] = None
Expand All @@ -79,11 +78,10 @@ def __init__(self, config: DeepgramClientOptions):
self._keep_alive_thread = None
self._flush_thread = None

# exit
# events
self._exit_event = asyncio.Event()

# auto flush
self._flush_event = asyncio.Event()
# init handlers
self._event_handlers = {
event: [] for event in LiveTranscriptionEvents.__members__.values()
}
Expand Down Expand Up @@ -174,7 +172,7 @@ async def start(
self._logger.notice("keepalive is disabled")

# flush thread
if self._config.is_auto_flush_enabled():
if self._config.is_auto_flush_reply_enabled():
self._logger.notice("autoflush is enabled")
self._flush_thread = asyncio.create_task(self._flush())
else:
Expand Down Expand Up @@ -219,7 +217,7 @@ async def start(
raise
return False

def is_connected(self) -> bool:
async def is_connected(self) -> bool:
"""
Returns the connection status of the WebSocket.
"""
Expand Down Expand Up @@ -311,7 +309,7 @@ async def _listening(self) -> None:
self._logger.verbose("LiveResultResponse: %s", msg_result)

# auto flush
if self._config.is_inspecting_messages():
if self._config.is_inspecting_listen():
inspect_res = await self._inspect(msg_result)
if not inspect_res:
self._logger.error("inspect_res failed")
Expand Down Expand Up @@ -400,6 +398,8 @@ async def _listening(self) -> None:
self._logger.debug("AsyncListenWebSocketClient._listening LEAVE")
return

# we need to explicitly call self._signal_exit() here because we are hanging on a recv()
# note: this is different than the speak websocket client
self._logger.error(
"ConnectionClosed in AsyncListenWebSocketClient._listening with code %s: %s",
e.code,
Expand Down Expand Up @@ -508,11 +508,13 @@ async def _keep_alive(self) -> None:
return

except websockets.exceptions.ConnectionClosed as e:
if e.code == 1000:
if e.code in [1000, 1001]:
self._logger.notice(f"_keep_alive({e.code}) exiting gracefully")
self._logger.debug("AsyncListenWebSocketClient._keep_alive LEAVE")
return

# we need to explicitly call self._signal_exit() here because we are hanging on a recv()
# note: this is different than the speak websocket client
self._logger.error(
"ConnectionClosed in AsyncListenWebSocketClient._keep_alive with code %s: %s",
e.code,
Expand Down Expand Up @@ -635,11 +637,13 @@ async def _flush(self) -> None:
return

except websockets.exceptions.ConnectionClosed as e:
if e.code == 1000:
if e.code in [1000, 1001]:
self._logger.notice(f"_flush({e.code}) exiting gracefully")
self._logger.debug("AsyncListenWebSocketClient._flush LEAVE")
return

# we need to explicitly call self._signal_exit() here because we are hanging on a recv()
# note: this is different than the speak websocket client
self._logger.error(
"ConnectionClosed in AsyncListenWebSocketClient._flush with code %s: %s",
e.code,
Expand Down Expand Up @@ -731,6 +735,11 @@ async def send(self, data: Union[str, bytes]) -> bool:
self._logger.debug("AsyncListenWebSocketClient.send LEAVE")
return False

if not await self.is_connected():
self._logger.notice("is_connected is False")
self._logger.debug("AsyncListenWebSocketClient.send LEAVE")
return False

if self._socket is not None:
try:
await self._socket.send(data)
Expand All @@ -741,7 +750,7 @@ async def send(self, data: Union[str, bytes]) -> bool:
raise
return True
except websockets.exceptions.ConnectionClosed as e:
if e.code == 1000:
if e.code in [1000, 1001]:
self._logger.notice(f"send({e.code}) exiting gracefully")
self._logger.debug("AsyncListenWebSocketClient.send LEAVE")
if self._config.options.get("termination_exception_send") == "true":
Expand Down Expand Up @@ -897,7 +906,7 @@ async def _signal_exit(self) -> None:
except websockets.exceptions.ConnectionClosedOK as e:
self._logger.notice("_signal_exit - ConnectionClosedOK: %s", e.code)
except websockets.exceptions.ConnectionClosed as e:
self._logger.notice("_signal_exit - ConnectionClosed: %s", e.code)
self._logger.error("_signal_exit - ConnectionClosed: %s", e.code)
except websockets.exceptions.WebSocketException as e:
self._logger.error("_signal_exit - WebSocketException: %s", str(e))
except Exception as e: # pylint: disable=broad-except
Expand Down Expand Up @@ -931,6 +940,11 @@ async def _signal_exit(self) -> None:
self._socket = None # type: ignore

async def _inspect(self, msg_result: LiveResultResponse) -> bool:
# auto flush_inspect is generically used to track any messages you might want to snoop on
# place additional logic here to inspect messages of interest

# for auto flush functionality
# set the last datagram
sentence = msg_result.channel.alternatives[0].transcript
if len(sentence) == 0:
return True
Expand Down
Loading

0 comments on commit 7a01771

Please sign in to comment.