emb_diversity.embeddings package

Submodules

emb_diversity.embeddings.embed_audio.encode(paths, model_name='speechbrain/spkrec-ecapa-voxceleb', cache_dir=PosixPath('.cache/embeddings'))[source]

Encode audio files into speaker embeddings, with disk caching.

Each file is loaded, mixed down to mono, and resampled to 16 kHz (the rate speechbrain’s VoxCeleb-trained models expect) before encoding.

Parameters:
  • paths (Sequence[str]) – Paths to audio files (any format supported by soundfile, e.g. .wav, .flac, .ogg — not .mp3).

  • model_name (str) – SpeechBrain model id. Defaults to “speechbrain/spkrec-ecapa-voxceleb”.

  • cache_dir (Path) – Root directory for the disk cache.

Return type:

List[List[float]]

Returns:

List of embedding vectors as lists of floats.

Note

Cache entries are keyed by file path (like embeddings.embed_text.encode() is keyed by text content), not by audio content — if a file’s content changes but its path does not, a stale cached embedding is returned.

emb_diversity.embeddings.embed_text.encode(texts, model_name='all-MiniLM-L6-v2', cache_dir=PosixPath('.cache/embeddings'), *, chunking=False, chunks=10, pooling='mean')[source]

Encode texts into embeddings using the appropriate backend, with disk caching.

Uses HuggingFace Transformers for known HF-only models, SentenceTransformers otherwise.

Long texts are handled in one of two ways:

  • Truncation (default): each text is truncated to the model’s maximum sequence length, discarding any tokens past the limit.

  • Chunking (chunking=True): each text is split into windows of the model’s max sequence length (up to chunks windows), every window is embedded, and the per-window vectors are combined into one vector with the pooling strategy. The chunking mode and the actual number of windows used are folded into the cache key, so chunked and truncated embeddings of the same text never collide.

Parameters:
  • texts (Sequence[str]) – Input texts to encode.

  • model_name (str) – Pretrained model name. Defaults to “all-MiniLM-L6-v2”.

  • cache_dir (Path) – Root directory for the disk cache.

  • chunking (bool) – If True, chunk long texts instead of truncating them.

  • chunks (int) – Maximum number of windows per text when chunking. Texts shorter than this use fewer windows; longer texts are truncated at this cap.

  • pooling (str) – How to combine per-window vectors when chunking. One of “mean” (default) or “max”.

Return type:

List[List[float]]

Returns:

List of embedding vectors as lists of floats.

Raises:

ValueError – If pooling is not a known strategy.

Module contents