Skip to content

Commit 91d19df

Browse files
Merge pull request #396 from dvonthenen/implement-flush-feature
Implements `Finalize`
2 parents a901570 + 8b87848 commit 91d19df

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

deepgram/clients/live/enums.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class LiveTranscriptionEvents(str, Enum):
1616
Metadata: str = "Metadata"
1717
UtteranceEnd: str = "UtteranceEnd"
1818
SpeechStarted: str = "SpeechStarted"
19+
Finalize: str = "Finalize"
1920
Error: str = "Error"
2021
Unhandled: str = "Unhandled"
2122
Warning: str = "Warning"

deepgram/clients/live/v1/async_client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,31 @@ async def send(self, data: Union[str, bytes]) -> bool:
461461
self.logger.spam("AsyncLiveClient.send LEAVE")
462462
return False
463463

464+
async def finalize(self) -> bool:
465+
"""
466+
Finalizes the Transcript connection by flushing it
467+
"""
468+
self.logger.spam("AsyncLiveClient.finalize ENTER")
469+
470+
if self._exit_event.is_set():
471+
self.logger.notice("finalize exiting gracefully")
472+
self.logger.debug("AsyncLiveClient.finalize LEAVE")
473+
return False
474+
475+
if self._socket is not None:
476+
self.logger.notice("sending Finalize...")
477+
ret = await self.send(json.dumps({"type": "Finalize"}))
478+
479+
if not ret:
480+
self.logger.error("finalize failed")
481+
self.logger.spam("AsyncLiveClient.finalize LEAVE")
482+
return False
483+
484+
self.logger.notice("finalize succeeded")
485+
self.logger.spam("AsyncLiveClient.finalize LEAVE")
486+
487+
return True
488+
464489
# closes the WebSocket connection gracefully
465490
async def finish(self) -> bool:
466491
"""

deepgram/clients/live/v1/client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,31 @@ def send(self, data: Union[str, bytes]) -> bool:
462462
self.logger.spam("LiveClient.send LEAVE")
463463
return False
464464

465+
def finalize(self) -> bool:
466+
"""
467+
Finalizes the Transcript connection by flushing it
468+
"""
469+
self.logger.spam("LiveClient.finalize ENTER")
470+
471+
if self._exit_event.is_set():
472+
self.logger.notice("finalize exiting gracefully")
473+
self.logger.debug("LiveClient.finalize LEAVE")
474+
return False
475+
476+
if self._socket is not None:
477+
self.logger.notice("sending Finalize...")
478+
ret = self.send(json.dumps({"type": "Finalize"}))
479+
480+
if not ret:
481+
self.logger.error("finalize failed")
482+
self.logger.spam("LiveClient.finalize LEAVE")
483+
return False
484+
485+
self.logger.notice("finalize succeeded")
486+
self.logger.spam("LiveClient.finalize LEAVE")
487+
488+
return True
489+
465490
# closes the WebSocket connection gracefully
466491
def finish(self) -> bool:
467492
"""

deepgram/clients/live/v1/response.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ class LiveResultResponse:
153153
duration: float = 0
154154
start: float = 0
155155
is_final: bool = False
156+
from_finalize: Optional[bool] = field(
157+
default=None, metadata=config(exclude=lambda f: f is None)
158+
)
156159
speech_final: bool = False
157160
channel: Channel = None
158161
metadata: Metadata = None

0 commit comments

Comments
 (0)