Function Signature
Parameters
Whisper
required
The Whisper model instance returned by
load_model().Must be a multilingual model (not .en variant) that has language tokens.torch.Tensor
required
A tensor containing the Mel spectrogram(s).Shape:
(80, 3000)for single audio segment(n_audio, 80, 3000)for batched segments(n_audio_ctx, n_audio_state)for pre-encoded audio features
Tokenizer
default:"None"
Optional tokenizer instance. If not provided, automatically creates one based on the model.The tokenizer must have language tokens enabled (multilingual models only).
Returns
Tensor
Tensor of shape
(n_audio,) containing the IDs of the most probable language tokens.If input is a single segment (2D tensor), returns a scalar tensor (0D).These are token IDs that appear after the start-of-transcript token.List[dict]
List of dictionaries (length Probabilities sum to 1.0 across all ~100 supported languages.
n_audio) containing the probability distribution over all languages.If input is a single segment, returns a single dict instead of a list.Each dictionary maps language codes to probabilities:Example
Notes
How It Works
- Encoder: Processes the Mel spectrogram to extract audio features
- Single token forward pass: Uses only the start-of-transcript (SOT) token
- Language token logits: Extracts logits for all language tokens
- Suppression: Sets all non-language tokens to
-inf - Argmax: Selects the most probable language token
- Softmax: Computes probability distribution across languages
Supported Languages
Whisper multilingual models support ~100 languages. Some common ones:Model Requirements
This function only works with multilingual models: ✅ Compatible:tiny,base,small,medium,large,large-v2,large-v3,turbo
tiny.en,base.en,small.en,medium.en
When to Use
Usedetect_language() when:
- You need to know the language before transcription
- Processing multilingual audio collections
- Implementing language routing logic
- Filtering audio by language
- Verifying language before translation
Pre-encoded Features
You can pass pre-encoded audio features to skip the encoder:Confidence Thresholds
Interpret confidence scores:Batch Processing
Process multiple audio segments efficiently:Language Code Mapping
Integration with Transcription
Performance
- Very fast: Only one forward pass through encoder and single decoder token
- Encoder caching: Reuse encoded features for both detection and decoding
- Batch-friendly: Process multiple audios in parallel
- No KV cache: Simpler than full decoding
- Single segment: ~10-20ms
- Batch of 10 segments: ~30-50ms