Repository: livekit/agents
File: livekit-plugins/livekit-plugins-elevenlabs/livekit/plugins/elevenlabs/stt.py (Python)

Code before:
)
from livekit.agents.stt import SpeechEventType, STTCapabilities
from livekit.agents.types import NOT_GIVEN, NotGivenOr
from livekit.agents.utils import AudioBuffer, http_context, is_given
from livekit.agents.voice.io import TimedString

from .log import logger
from .models import STTRealtimeSampleRates

API_BASE_URL_V1 = "https://api.elevenlabs.io/v1"
AUTHORIZATION_HEADER = "xi-api-key"


class VADOptions(TypedDict, total=False):
    vad_silence_threshold_secs: float | None
    """Silence threshold in seconds for VAD. Default to 1.5"""
    vad_threshold: float | None
    """Threshold for voice activity detection. Default to 0.4"""
    min_speech_duration_ms: int | None
    """Minimum speech duration in milliseconds. Default to 250"""
    min_silence_duration_ms: int | None
    """Minimum silence duration in milliseconds. Default to 2500"""


# https://elevenlabs.io/docs/overview/models#models-overview
type ElevenLabsSTTModels = Literal["scribe_v1", "scribe_v2", "scribe_v2_realtime"]


@dataclass
class STTOptions:
    model_id: ElevenLabsSTTModels
    api_key: str
    base_url: str
    language_code: str | None
    tag_audio_events: bool
    include_timestamps: bool
    sample_rate: STTRealtimeSampleRates
    server_vad: NotGivenOr[VADOptions | None]


class STT(stt.STT):
    def __init__(
        self,
        *,
        api_key: NotGivenOr[str] = NOT_GIVEN,
        base_url: NotGivenOr[str] = NOT_GIVEN,
        language_code: NotGivenOr[str] = NOT_GIVEN,
        tag_audio_events: bool = True,
        use_realtime: bool = False,
        sample_rate: STTRealtimeSampleRates = 16000,
        server_vad: NotGivenOr[VADOptions] = NOT_GIVEN,

Review comment (suggestion):
This (`type` statement) is a 3.12 syntax IIRC. I'd love to use it, but I don't think we or our users are ready just yet.

Code after:
)
from livekit.agents.stt import SpeechEventType, STTCapabilities
from livekit.agents.types import NOT_GIVEN, NotGivenOr
from livekit.agents.utils import AudioBuffer, http_context, is_given
from livekit.agents.voice.io import TimedString

from .log import logger
from .models import STTRealtimeSampleRates

API_BASE_URL_V1 = "https://api.elevenlabs.io/v1"
AUTHORIZATION_HEADER = "xi-api-key"


class VADOptions(TypedDict, total=False):
    vad_silence_threshold_secs: float | None
    """Silence threshold in seconds for VAD. Default to 1.5"""
    vad_threshold: float | None
    """Threshold for voice activity detection. Default to 0.4"""
    min_speech_duration_ms: int | None
    """Minimum speech duration in milliseconds. Default to 250"""
    min_silence_duration_ms: int | None
    """Minimum silence duration in milliseconds. Default to 2500"""


# https://elevenlabs.io/docs/overview/models#models-overview
ElevenLabsSTTModels = Literal["scribe_v1", "scribe_v2", "scribe_v2_realtime"]


@dataclass
class STTOptions:
    model_id: ElevenLabsSTTModels | str
    api_key: str
    base_url: str
    language_code: str | None
    tag_audio_events: bool
    include_timestamps: bool
    sample_rate: STTRealtimeSampleRates
    server_vad: NotGivenOr[VADOptions | None]


class STT(stt.STT):
    def __init__(
        self,
        *,
        api_key: NotGivenOr[str] = NOT_GIVEN,
        base_url: NotGivenOr[str] = NOT_GIVEN,
        language_code: NotGivenOr[str] = NOT_GIVEN,
        tag_audio_events: bool = True,
        use_realtime: NotGivenOr[bool] = NOT_GIVEN,  # Deprecated
        sample_rate: STTRealtimeSampleRates = 16000,
        server_vad: NotGivenOr[VADOptions] = NOT_GIVEN,