Skip to content

Config

Configuration module of the Wordcab Transcribe.

Settings

Configuration settings for the Wordcab Transcribe API.

Source code in src/wordcab_transcribe/config.py
@dataclass
class Settings:
    """Configuration settings for the Wordcab Transcribe API."""

    # General configuration
    project_name: str
    version: str
    description: str
    api_prefix: str
    debug: bool
    # Models configuration
    # Whisper
    whisper_model: str
    compute_type: str
    extra_languages: Union[List[str], None]
    extra_languages_model_paths: Union[Dict[str, str], None]
    # Diarization
    window_lengths: List[float]
    shift_lengths: List[float]
    multiscale_weights: List[float]
    # ASR type configuration
    asr_type: Literal["async", "live", "only_transcription", "only_diarization"]
    # Endpoint configuration
    cortex_endpoint: bool
    # API authentication configuration
    username: str
    password: str
    openssl_key: str
    openssl_algorithm: str
    access_token_expire_minutes: int
    # Cortex configuration
    cortex_api_key: str
    # Svix configuration
    svix_api_key: str
    svix_app_id: str
    # Remote servers configuration
    transcribe_server_urls: Union[List[str], None]
    diarize_server_urls: Union[List[str], None]

    @field_validator("project_name")
    def project_name_must_not_be_none(cls, value: str):  # noqa: B902, N805
        """Check that the project_name is not None."""
        if value is None:
            raise ValueError(
                "`project_name` must not be None, please verify the `.env` file."
            )

        return value

    @field_validator("version")
    def version_must_not_be_none(cls, value: str):  # noqa: B902, N805
        """Check that the version is not None."""
        if value is None:
            raise ValueError(
                "`version` must not be None, please verify the `.env` file."
            )

        return value

    @field_validator("description")
    def description_must_not_be_none(cls, value: str):  # noqa: B902, N805
        """Check that the description is not None."""
        if value is None:
            raise ValueError(
                "`description` must not be None, please verify the `.env` file."
            )

        return value

    @field_validator("api_prefix")
    def api_prefix_must_not_be_none(cls, value: str):  # noqa: B902, N805
        """Check that the api_prefix is not None."""
        if value is None:
            raise ValueError(
                "`api_prefix` must not be None, please verify the `.env` file."
            )

        return value

    @field_validator("compute_type")
    def compute_type_must_be_valid(cls, value: str):  # noqa: B902, N805
        """Check that the model precision is valid."""
        compute_type_values = [
            "int8",
            "int8_float16",
            "int8_bfloat16",
            "int16",
            "float16",
            "bfloat16",
            "float32",
        ]
        if value not in compute_type_values:
            raise ValueError(
                f"{value} is not a valid compute type. Choose one of"
                f" {compute_type_values}."
            )

        return value

    @field_validator("openssl_algorithm")
    def openssl_algorithm_must_be_valid(cls, value: str):  # noqa: B902, N805
        """Check that the OpenSSL algorithm is valid."""
        if value not in {"HS256", "HS384", "HS512"}:
            raise ValueError(
                "openssl_algorithm must be a valid algorithm, please verify the `.env`"
                " file."
            )

        return value

    @field_validator("access_token_expire_minutes")
    def access_token_expire_minutes_must_be_valid(cls, value: int):  # noqa: B902, N805
        """Check that the access token expiration is valid. Only if debug is False."""
        if value <= 0:
            raise ValueError(
                "access_token_expire_minutes must be positive, please verify the `.env`"
                " file."
            )

        return value

    def __post_init__(self):
        """Post initialization checks."""
        if self.debug is False:
            if self.username == "admin" or self.username is None:  # noqa: S105
                logger.warning(
                    f"Username is set to `{self.username}`, which is not secure for"
                    " production."
                )
            if self.password == "admin" or self.password is None:  # noqa: S105
                logger.warning(
                    f"Password is set to `{self.password}`, which is not secure for"
                    " production."
                )
            if (
                self.openssl_key == "0123456789abcdefghijklmnopqrstuvwyz"  # noqa: S105
                or self.openssl_key is None
            ):
                logger.warning(
                    f"OpenSSL key is set to `{self.openssl_key}`, which is the default"
                    " encryption key. It's absolutely not secure for production."
                    " Please change it in the `.env` file. You can generate a new key"
                    " with `openssl rand -hex 32`."
                )

        if (
            len(self.window_lengths)
            != len(self.shift_lengths)
            != len(self.multiscale_weights)
        ):
            raise ValueError(
                "Length of window_lengths, shift_lengths and multiscale_weights must"
                f" be the same.\nFound: {len(self.window_lengths)},"
                f" {len(self.shift_lengths)}, {len(self.multiscale_weights)}"
            )

__post_init__()

Post initialization checks.

Source code in src/wordcab_transcribe/config.py
def __post_init__(self):
    """Post initialization checks."""
    if self.debug is False:
        if self.username == "admin" or self.username is None:  # noqa: S105
            logger.warning(
                f"Username is set to `{self.username}`, which is not secure for"
                " production."
            )
        if self.password == "admin" or self.password is None:  # noqa: S105
            logger.warning(
                f"Password is set to `{self.password}`, which is not secure for"
                " production."
            )
        if (
            self.openssl_key == "0123456789abcdefghijklmnopqrstuvwyz"  # noqa: S105
            or self.openssl_key is None
        ):
            logger.warning(
                f"OpenSSL key is set to `{self.openssl_key}`, which is the default"
                " encryption key. It's absolutely not secure for production."
                " Please change it in the `.env` file. You can generate a new key"
                " with `openssl rand -hex 32`."
            )

    if (
        len(self.window_lengths)
        != len(self.shift_lengths)
        != len(self.multiscale_weights)
    ):
        raise ValueError(
            "Length of window_lengths, shift_lengths and multiscale_weights must"
            f" be the same.\nFound: {len(self.window_lengths)},"
            f" {len(self.shift_lengths)}, {len(self.multiscale_weights)}"
        )

access_token_expire_minutes_must_be_valid(value)

Check that the access token expiration is valid. Only if debug is False.

Source code in src/wordcab_transcribe/config.py
@field_validator("access_token_expire_minutes")
def access_token_expire_minutes_must_be_valid(cls, value: int):  # noqa: B902, N805
    """Check that the access token expiration is valid. Only if debug is False."""
    if value <= 0:
        raise ValueError(
            "access_token_expire_minutes must be positive, please verify the `.env`"
            " file."
        )

    return value

api_prefix_must_not_be_none(value)

Check that the api_prefix is not None.

Source code in src/wordcab_transcribe/config.py
@field_validator("api_prefix")
def api_prefix_must_not_be_none(cls, value: str):  # noqa: B902, N805
    """Check that the api_prefix is not None."""
    if value is None:
        raise ValueError(
            "`api_prefix` must not be None, please verify the `.env` file."
        )

    return value

compute_type_must_be_valid(value)

Check that the model precision is valid.

Source code in src/wordcab_transcribe/config.py
@field_validator("compute_type")
def compute_type_must_be_valid(cls, value: str):  # noqa: B902, N805
    """Check that the model precision is valid."""
    compute_type_values = [
        "int8",
        "int8_float16",
        "int8_bfloat16",
        "int16",
        "float16",
        "bfloat16",
        "float32",
    ]
    if value not in compute_type_values:
        raise ValueError(
            f"{value} is not a valid compute type. Choose one of"
            f" {compute_type_values}."
        )

    return value

description_must_not_be_none(value)

Check that the description is not None.

Source code in src/wordcab_transcribe/config.py
@field_validator("description")
def description_must_not_be_none(cls, value: str):  # noqa: B902, N805
    """Check that the description is not None."""
    if value is None:
        raise ValueError(
            "`description` must not be None, please verify the `.env` file."
        )

    return value

openssl_algorithm_must_be_valid(value)

Check that the OpenSSL algorithm is valid.

Source code in src/wordcab_transcribe/config.py
@field_validator("openssl_algorithm")
def openssl_algorithm_must_be_valid(cls, value: str):  # noqa: B902, N805
    """Check that the OpenSSL algorithm is valid."""
    if value not in {"HS256", "HS384", "HS512"}:
        raise ValueError(
            "openssl_algorithm must be a valid algorithm, please verify the `.env`"
            " file."
        )

    return value

project_name_must_not_be_none(value)

Check that the project_name is not None.

Source code in src/wordcab_transcribe/config.py
@field_validator("project_name")
def project_name_must_not_be_none(cls, value: str):  # noqa: B902, N805
    """Check that the project_name is not None."""
    if value is None:
        raise ValueError(
            "`project_name` must not be None, please verify the `.env` file."
        )

    return value

version_must_not_be_none(value)

Check that the version is not None.

Source code in src/wordcab_transcribe/config.py
@field_validator("version")
def version_must_not_be_none(cls, value: str):  # noqa: B902, N805
    """Check that the version is not None."""
    if value is None:
        raise ValueError(
            "`version` must not be None, please verify the `.env` file."
        )

    return value

Last update: 2023-10-12
Created: 2023-10-12