Skip to content

Commit 7bf02c6

Browse files
[HOLD] Reintroduce TTS WS
1 parent 78ec6d9 commit 7bf02c6

File tree

24 files changed

+2390
-139
lines changed

24 files changed

+2390
-139
lines changed

deepgram/__init__.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
from .client import (
9797
SpeakOptions,
9898
SpeakRESTOptions,
99-
# SpeakWebSocketOptions,
99+
SpeakWebSocketOptions,
100100
# FileSource,
101101
SpeakRestSource,
102102
SpeakSource,
@@ -115,21 +115,21 @@
115115
SpeakRESTResponse,
116116
)
117117

118-
# ## speak WebSocket
119-
# from .client import (
120-
# SpeakWebSocketClient,
121-
# AsyncSpeakWebSocketClient,
122-
# )
123-
# from .client import (
124-
# SpeakWebSocketResponse,
125-
# # OpenResponse,
126-
# # MetadataResponse,
127-
# FlushedResponse,
128-
# # CloseResponse,
129-
# # UnhandledResponse,
130-
# WarningResponse,
131-
# # ErrorResponse,
132-
# )
118+
## speak WebSocket
119+
from .client import (
120+
SpeakWebSocketClient,
121+
AsyncSpeakWebSocketClient,
122+
)
123+
from .client import (
124+
SpeakWebSocketResponse,
125+
# OpenResponse,
126+
# MetadataResponse,
127+
FlushedResponse,
128+
# CloseResponse,
129+
# UnhandledResponse,
130+
WarningResponse,
131+
# ErrorResponse,
132+
)
133133

134134
# manage
135135
from .client import ManageClient, AsyncManageClient

deepgram/client.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
from .clients import (
101101
SpeakOptions,
102102
SpeakRESTOptions,
103-
# SpeakWebSocketOptions,
103+
SpeakWebSocketOptions,
104104
# FileSource,
105105
SpeakRestSource,
106106
SpeakSource,
@@ -119,21 +119,21 @@
119119
SpeakRESTResponse,
120120
)
121121

122-
# ## speak WebSocket
123-
# from .clients import (
124-
# SpeakWebSocketClient,
125-
# AsyncSpeakWebSocketClient,
126-
# )
127-
# from .clients import (
128-
# SpeakWebSocketResponse,
129-
# # OpenResponse,
130-
# # MetadataResponse,
131-
# FlushedResponse,
132-
# # CloseResponse,
133-
# # UnhandledResponse,
134-
# WarningResponse,
135-
# # ErrorResponse,
136-
# )
122+
## speak WebSocket
123+
from .clients import (
124+
SpeakWebSocketClient,
125+
AsyncSpeakWebSocketClient,
126+
)
127+
from .clients import (
128+
SpeakWebSocketResponse,
129+
# OpenResponse,
130+
# MetadataResponse,
131+
FlushedResponse,
132+
# CloseResponse,
133+
# UnhandledResponse,
134+
WarningResponse,
135+
# ErrorResponse,
136+
)
137137

138138
# manage client classes/input
139139
from .clients import ManageClient, AsyncManageClient

deepgram/clients/__init__.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
from .speak import (
106106
SpeakOptions,
107107
SpeakRESTOptions,
108-
# SpeakWebSocketOptions,
108+
SpeakWebSocketOptions,
109109
# FileSource,
110110
SpeakRestSource,
111111
SpeakSource,
@@ -125,21 +125,21 @@
125125
SpeakRESTResponse,
126126
)
127127

128-
# ## text-to-speech WebSocket
129-
# from .speak import (
130-
# SpeakWebSocketClient,
131-
# AsyncSpeakWebSocketClient,
132-
# )
133-
# from .speak import (
134-
# SpeakWebSocketResponse,
135-
# # OpenResponse,
136-
# # MetadataResponse,
137-
# FlushedResponse,
138-
# # CloseResponse,
139-
# # UnhandledResponse,
140-
# WarningResponse,
141-
# # ErrorResponse,
142-
# )
128+
## text-to-speech WebSocket
129+
from .speak import (
130+
SpeakWebSocketClient,
131+
AsyncSpeakWebSocketClient,
132+
)
133+
from .speak import (
134+
SpeakWebSocketResponse,
135+
# OpenResponse,
136+
# MetadataResponse,
137+
FlushedResponse,
138+
# CloseResponse,
139+
# UnhandledResponse,
140+
WarningResponse,
141+
# ErrorResponse,
142+
)
143143

144144
# manage
145145
from .manage import ManageClient, AsyncManageClient

deepgram/clients/abstract_async_client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ async def _handle_request_raw(
348348
client = httpx.AsyncClient(timeout=timeout, transport=transport)
349349
if transport:
350350
kwargs.pop("transport")
351-
kwargs.pop("transport")
352351
req = client.build_request(method, _url, headers=_headers, **kwargs)
353352
return await client.send(req, stream=True)
354353

deepgram/clients/abstract_sync_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,9 @@ def _handle_request_raw(
337337

338338
try:
339339
transport = kwargs.get("transport")
340-
with httpx.Client(timeout=timeout, transport=transport) as client:
341-
if transport:
342-
kwargs.pop("transport")
340+
client = httpx.Client(timeout=timeout, transport=transport)
341+
if transport:
342+
kwargs.pop("transport")
343343
req = client.build_request(method, _url, headers=_headers, **kwargs)
344344
return client.send(req, stream=True)
345345

deepgram/clients/listen/v1/websocket/async_client.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ class AsyncListenWebSocketClient: # pylint: disable=too-many-instance-attribute
5353
_socket: WebSocketClientProtocol
5454
_event_handlers: Dict[LiveTranscriptionEvents, list]
5555

56-
_last_datagram: Optional[datetime] = None
57-
5856
_listen_thread: Union[asyncio.Task, None]
5957
_keep_alive_thread: Union[asyncio.Task, None]
6058
_flush_thread: Union[asyncio.Task, None]
59+
_last_datagram: Optional[datetime] = None
6160

6261
_kwargs: Optional[Dict] = None
6362
_addons: Optional[Dict] = None
@@ -79,11 +78,10 @@ def __init__(self, config: DeepgramClientOptions):
7978
self._keep_alive_thread = None
8079
self._flush_thread = None
8180

82-
# exit
81+
# events
8382
self._exit_event = asyncio.Event()
8483

85-
# auto flush
86-
self._flush_event = asyncio.Event()
84+
# init handlers
8785
self._event_handlers = {
8886
event: [] for event in LiveTranscriptionEvents.__members__.values()
8987
}
@@ -174,7 +172,7 @@ async def start(
174172
self._logger.notice("keepalive is disabled")
175173

176174
# flush thread
177-
if self._config.is_auto_flush_enabled():
175+
if self._config.is_auto_flush_reply_enabled():
178176
self._logger.notice("autoflush is enabled")
179177
self._flush_thread = asyncio.create_task(self._flush())
180178
else:
@@ -219,7 +217,7 @@ async def start(
219217
raise
220218
return False
221219

222-
def is_connected(self) -> bool:
220+
async def is_connected(self) -> bool:
223221
"""
224222
Returns the connection status of the WebSocket.
225223
"""
@@ -311,7 +309,7 @@ async def _listening(self) -> None:
311309
self._logger.verbose("LiveResultResponse: %s", msg_result)
312310

313311
# auto flush
314-
if self._config.is_inspecting_messages():
312+
if self._config.is_inspecting_listen():
315313
inspect_res = await self._inspect(msg_result)
316314
if not inspect_res:
317315
self._logger.error("inspect_res failed")
@@ -400,6 +398,8 @@ async def _listening(self) -> None:
400398
self._logger.debug("AsyncListenWebSocketClient._listening LEAVE")
401399
return
402400

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

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

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

637639
except websockets.exceptions.ConnectionClosed as e:
638-
if e.code == 1000:
640+
if e.code in [1000, 1001]:
639641
self._logger.notice(f"_flush({e.code}) exiting gracefully")
640642
self._logger.debug("AsyncListenWebSocketClient._flush LEAVE")
641643
return
642644

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

738+
if not await self.is_connected():
739+
self._logger.notice("is_connected is False")
740+
self._logger.debug("AsyncListenWebSocketClient.send LEAVE")
741+
return False
742+
734743
if self._socket is not None:
735744
try:
736745
await self._socket.send(data)
@@ -741,7 +750,7 @@ async def send(self, data: Union[str, bytes]) -> bool:
741750
raise
742751
return True
743752
except websockets.exceptions.ConnectionClosed as e:
744-
if e.code == 1000:
753+
if e.code in [1000, 1001]:
745754
self._logger.notice(f"send({e.code}) exiting gracefully")
746755
self._logger.debug("AsyncListenWebSocketClient.send LEAVE")
747756
if self._config.options.get("termination_exception_send") == "true":
@@ -897,7 +906,7 @@ async def _signal_exit(self) -> None:
897906
except websockets.exceptions.ConnectionClosedOK as e:
898907
self._logger.notice("_signal_exit - ConnectionClosedOK: %s", e.code)
899908
except websockets.exceptions.ConnectionClosed as e:
900-
self._logger.notice("_signal_exit - ConnectionClosed: %s", e.code)
909+
self._logger.error("_signal_exit - ConnectionClosed: %s", e.code)
901910
except websockets.exceptions.WebSocketException as e:
902911
self._logger.error("_signal_exit - WebSocketException: %s", str(e))
903912
except Exception as e: # pylint: disable=broad-except
@@ -931,6 +940,11 @@ async def _signal_exit(self) -> None:
931940
self._socket = None # type: ignore
932941

933942
async def _inspect(self, msg_result: LiveResultResponse) -> bool:
943+
# auto flush_inspect is generically used to track any messages you might want to snoop on
944+
# place additional logic here to inspect messages of interest
945+
946+
# for auto flush functionality
947+
# set the last datagram
934948
sentence = msg_result.channel.alternatives[0].transcript
935949
if len(sentence) == 0:
936950
return True

0 commit comments

Comments
 (0)