Add docstrings to DeepgramSTTService
This commit is contained in:
@@ -4,6 +4,8 @@
|
|||||||
# SPDX-License-Identifier: BSD 2-Clause License
|
# SPDX-License-Identifier: BSD 2-Clause License
|
||||||
#
|
#
|
||||||
|
|
||||||
|
"""Deepgram speech-to-text service implementation."""
|
||||||
|
|
||||||
from typing import AsyncGenerator, Dict, Optional
|
from typing import AsyncGenerator, Dict, Optional
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
@@ -41,6 +43,22 @@ except ModuleNotFoundError as e:
|
|||||||
|
|
||||||
|
|
||||||
class DeepgramSTTService(STTService):
|
class DeepgramSTTService(STTService):
|
||||||
|
"""Deepgram speech-to-text service.
|
||||||
|
|
||||||
|
Provides real-time speech recognition using Deepgram's WebSocket API.
|
||||||
|
Supports configurable models, languages, VAD events, and various audio
|
||||||
|
processing options.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
api_key: Deepgram API key for authentication.
|
||||||
|
url: Deprecated. Use base_url instead.
|
||||||
|
base_url: Custom Deepgram API base URL.
|
||||||
|
sample_rate: Audio sample rate. If None, uses default or live_options value.
|
||||||
|
live_options: Deepgram LiveOptions for detailed configuration.
|
||||||
|
addons: Additional Deepgram features to enable.
|
||||||
|
**kwargs: Additional arguments passed to the parent STTService.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
*,
|
*,
|
||||||
@@ -108,12 +126,27 @@ class DeepgramSTTService(STTService):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def vad_enabled(self):
|
def vad_enabled(self):
|
||||||
|
"""Check if Deepgram VAD events are enabled.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True if VAD events are enabled in the current settings.
|
||||||
|
"""
|
||||||
return self._settings["vad_events"]
|
return self._settings["vad_events"]
|
||||||
|
|
||||||
def can_generate_metrics(self) -> bool:
|
def can_generate_metrics(self) -> bool:
|
||||||
|
"""Check if this service can generate processing metrics.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True, as Deepgram service supports metrics generation.
|
||||||
|
"""
|
||||||
return True
|
return True
|
||||||
|
|
||||||
async def set_model(self, model: str):
|
async def set_model(self, model: str):
|
||||||
|
"""Set the Deepgram model and reconnect.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
model: The Deepgram model name to use.
|
||||||
|
"""
|
||||||
await super().set_model(model)
|
await super().set_model(model)
|
||||||
logger.info(f"Switching STT model to: [{model}]")
|
logger.info(f"Switching STT model to: [{model}]")
|
||||||
self._settings["model"] = model
|
self._settings["model"] = model
|
||||||
@@ -121,25 +154,53 @@ class DeepgramSTTService(STTService):
|
|||||||
await self._connect()
|
await self._connect()
|
||||||
|
|
||||||
async def set_language(self, language: Language):
|
async def set_language(self, language: Language):
|
||||||
|
"""Set the recognition language and reconnect.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
language: The language to use for speech recognition.
|
||||||
|
"""
|
||||||
logger.info(f"Switching STT language to: [{language}]")
|
logger.info(f"Switching STT language to: [{language}]")
|
||||||
self._settings["language"] = language
|
self._settings["language"] = language
|
||||||
await self._disconnect()
|
await self._disconnect()
|
||||||
await self._connect()
|
await self._connect()
|
||||||
|
|
||||||
async def start(self, frame: StartFrame):
|
async def start(self, frame: StartFrame):
|
||||||
|
"""Start the Deepgram STT service.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
frame: The start frame containing initialization parameters.
|
||||||
|
"""
|
||||||
await super().start(frame)
|
await super().start(frame)
|
||||||
self._settings["sample_rate"] = self.sample_rate
|
self._settings["sample_rate"] = self.sample_rate
|
||||||
await self._connect()
|
await self._connect()
|
||||||
|
|
||||||
async def stop(self, frame: EndFrame):
|
async def stop(self, frame: EndFrame):
|
||||||
|
"""Stop the Deepgram STT service.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
frame: The end frame.
|
||||||
|
"""
|
||||||
await super().stop(frame)
|
await super().stop(frame)
|
||||||
await self._disconnect()
|
await self._disconnect()
|
||||||
|
|
||||||
async def cancel(self, frame: CancelFrame):
|
async def cancel(self, frame: CancelFrame):
|
||||||
|
"""Cancel the Deepgram STT service.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
frame: The cancel frame.
|
||||||
|
"""
|
||||||
await super().cancel(frame)
|
await super().cancel(frame)
|
||||||
await self._disconnect()
|
await self._disconnect()
|
||||||
|
|
||||||
async def run_stt(self, audio: bytes) -> AsyncGenerator[Frame, None]:
|
async def run_stt(self, audio: bytes) -> AsyncGenerator[Frame, None]:
|
||||||
|
"""Send audio data to Deepgram for transcription.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
audio: Raw audio bytes to transcribe.
|
||||||
|
|
||||||
|
Yields:
|
||||||
|
Frame: None (transcription results come via WebSocket callbacks).
|
||||||
|
"""
|
||||||
await self._connection.send(audio)
|
await self._connection.send(audio)
|
||||||
yield None
|
yield None
|
||||||
|
|
||||||
@@ -172,6 +233,7 @@ class DeepgramSTTService(STTService):
|
|||||||
await self._connection.finish()
|
await self._connection.finish()
|
||||||
|
|
||||||
async def start_metrics(self):
|
async def start_metrics(self):
|
||||||
|
"""Start TTFB and processing metrics collection."""
|
||||||
await self.start_ttfb_metrics()
|
await self.start_ttfb_metrics()
|
||||||
await self.start_processing_metrics()
|
await self.start_processing_metrics()
|
||||||
|
|
||||||
@@ -235,6 +297,12 @@ class DeepgramSTTService(STTService):
|
|||||||
)
|
)
|
||||||
|
|
||||||
async def process_frame(self, frame: Frame, direction: FrameDirection):
|
async def process_frame(self, frame: Frame, direction: FrameDirection):
|
||||||
|
"""Process frames with Deepgram-specific handling.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
frame: The frame to process.
|
||||||
|
direction: The direction of frame processing.
|
||||||
|
"""
|
||||||
await super().process_frame(frame, direction)
|
await super().process_frame(frame, direction)
|
||||||
|
|
||||||
if isinstance(frame, UserStartedSpeakingFrame) and not self.vad_enabled:
|
if isinstance(frame, UserStartedSpeakingFrame) and not self.vad_enabled:
|
||||||
|
|||||||
Reference in New Issue
Block a user