deepgram: add VAD event handlers

This commit is contained in:
Aleix Conchillo Flaqué
2024-12-04 17:24:27 -08:00
parent 0ae8ca0813
commit 3d76b30a7c
4 changed files with 117 additions and 2 deletions

View File

@@ -35,7 +35,6 @@ try:
LiveResultResponse,
LiveTranscriptionEvents,
SpeakOptions,
logging,
)
except ModuleNotFoundError as e:
logger.error(f"Exception: {e}")
@@ -151,7 +150,10 @@ class DeepgramSTTService(STTService):
self._connection: AsyncListenWebSocketClient = self._client.listen.asyncwebsocket.v("1")
self._connection.on(LiveTranscriptionEvents.Transcript, self._on_message)
if self.vad_enabled:
self._register_event_handler("on_speech_started")
self._register_event_handler("on_utterance_end")
self._connection.on(LiveTranscriptionEvents.SpeechStarted, self._on_speech_started)
self._connection.on(LiveTranscriptionEvents.UtteranceEnd, self._on_utterance_end)
@property
def vad_enabled(self):
@@ -203,6 +205,10 @@ class DeepgramSTTService(STTService):
async def _on_speech_started(self, *args, **kwargs):
await self.start_ttfb_metrics()
await self.start_processing_metrics()
await self._call_event_handler("on_speech_started", *args, **kwargs)
async def _on_utterance_end(self, *args, **kwargs):
await self._call_event_handler("on_utterance_end", *args, **kwargs)
async def _on_message(self, *args, **kwargs):
result: LiveResultResponse = kwargs["result"]