faster-whisper

repository·master·Indexed 11 days ago

https://github.com/systran/faster-whisper

A high-performance reimplementation of OpenAI's Whisper model using the CTranslate2 inference engine. It offers faster transcription and lower memory usage than the original implementation, supporting CPU and GPU execution with quantization options. Key features include the WhisperModel interface, BatchedInferencePipeline for increased speed, Distil-Whisper compatibility, word-level timestamps, and Silero VAD filtering.

Tokens
2.6K
Snippets
12
Records
13
Agent score
48%

What's inside faster-whisper

  1. Community integrations using faster-whisper

    master

    Several open-source projects utilize faster-whisper for various speech-to-text tasks:

    • Servers & APIs:
      • speaches: OpenAI-compatible server, supports streaming and live transcription.
      • Whisper-FastAPI: Simple API backend compatible with OpenAI, HomeAssistant, and Konele.
    • Advanced Transcription & Diarization:
      • WhisperX: Provides speaker diarization and accurate word-level timestamps via wav2vec2 alignment.
      • whisper-diarize: Speaker diarization tool using faster-whisper and NVIDIA NeMo.
      • asr-sd-pipeline: Scalable, modular multi-speaker STT solution using AzureML pipelines.
    • CLI & Standalone Tools:
      • whisper-ctranslate2: Command line client compatible with the original OpenAI Whisper client.
      • whisper-standalone-win: Standalone CLI executables for Windows, Linux, and macOS.
    • Specialized Use Cases:
      • Open-Lyrics: Transcribes audio and generates .lrc files using OpenAI-GPT.
      • wscribe: Transcript generation tool with word-level export and an editor.
      • Whisper-Streaming & WhisperLive: Implementations for real-time/nearly-live transcription.
      • Open-dubbing: AI dubbing system for translating and synchronizing audio dialogue.
  2. Install the master branch or specific commit

    master

    If you need the latest development version or a specific historical state, you can install directly from GitHub using the following commands.

    # Install the master branch
    pip install --force-reinstall "faster-whisper @ https://github.com/SYSTRAN/faster-whisper/archive/refs/heads/master.tar.gz"
    
    # Install a specific commit
    pip install --force-reinstall "faster-whisper @ https://github.com/SYSTRAN/faster-whisper/archive/a4f1cc8f11433e454c3934442b5e1a4ed5e865c3.tar.gz"
  3. Convert Whisper models to CTranslate2 format

    master

    While faster-whisper automatically downloads CTranslate2-compatible models from the Hugging Face Hub when using standard names like WhisperModel("large-v3"), you can manually convert any Whisper model compatible with the Transformers library (including fine-tuned models) using the ct2-transformers-converter tool.

    To convert a model, use the following command structure. This example converts the original openai/whisper-large-v3 and saves it in float16 quantization:

    pip install transformers[torch]>=4.23
    
    ct2-transformers-converter --model openai/whisper-large-v3 --output_dir whisper-large-v3-ct2 --copy_files tokenizer.json preprocessor_config.json --quantization float16

    Arguments:

    • --model: The model name on the Hugging Face Hub or a local path to a model directory.
    • --output_dir: The directory where the converted model will be saved.
    • --copy_files: If you include tokenizer.json, it is saved with the model. If omitted, the tokenizer configuration is automatically downloaded when the model is loaded later.
    • --quantization: Specifies the precision (e.g., float16).
  4. Configure GPU requirements for faster-whisper

    master

    To use GPU execution, you must have NVIDIA libraries installed. The latest ctranslate2 versions require:

    • cuBLAS for CUDA 12
    • cuDNN 9 for CUDA 12

    Version Compatibility Workarounds:

    • For CUDA 11 and cuDNN 8: Downgrade to ctranslate2==3.24.0.
    • For CUDA 12 and cuDNN 8: Downgrade to ctranslate2==4.4.0.

    Linux Installation via pip: You can install these libraries via pip and must set LD_LIBRARY_PATH before running your Python script.

    pip install nvidia-cublas-cu12 nvidia-cudnn-cu12==9.*
    
    export LD_LIBRARY_PATH=`python3 -c 'import os; import nvidia.cublas.lib; import nvidia.cudnn.lib; print(os.path.dirname(nvidia.cublas.lib.__file__) + ":" + os.path.dirname(nvidia.cudnn.lib.__file__))'`
  5. Optimize CPU performance using OMP_NUM_THREADS

    master

    When running faster-whisper on a CPU, transcription speed is affected by the number of threads used. Many frameworks respect the OMP_NUM_THREADS environment variable. You can set this variable when running your script to control the thread count and ensure consistent performance during benchmarks.

    OMP_NUM_THREADS=4 python3 my_script.py
  6. Transcribe with Distil-Whisper models

    master

    Distil-Whisper checkpoints (like distil-large-v3) are fully compatible with faster-whisper. They are designed to work efficiently with the faster-whisper transcription algorithm.

    from faster_whisper import WhisperModel
    
    model_size = "distil-large-v3"
    
    model = WhisperModel(model_size, device="cuda", compute_type="float16")
    segments, info = model.transcribe("audio.mp3", beam_size=5, language="en", condition_on_previous_text=False)
    
    for segment in segments:
        print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
  7. Use VAD (Voice Activity Detection) filter

    master

    To filter out non-speech parts of the audio, enable the Silero VAD filter by passing vad_filter=True to transcribe.

    By default, it removes silence longer than 2 seconds. You can customize this behavior using the vad_parameters argument, which accepts a dictionary of parameters (e.g., min_silence_duration_ms).

    Note: The VAD filter is enabled by default when using BatchedInferencePipeline.

    # Basic VAD usage
    segments, _ = model.transcribe("audio.mp3", vad_filter=True)
    
    # Customized VAD usage
    segments, _ = model.transcribe(
        "audio.mp3",
        vad_filter=True,
        vad_parameters=dict(min_silence_duration_ms=500),
    )
  8. Enable word-level timestamps

    master

    To get precise timing for individual words within a segment, pass word_timestamps=True to the transcribe method. Each segment will then contain a words attribute.

    segments, _ = model.transcribe("audio.mp3", word_timestamps=True)
    
    for segment in segments:
        for word in segment.words:
            print("[%.2fs -> %.2fs] %s" % (word.start, word.end, word.word))
  9. Basic usage of WhisperModel

    master

    The WhisperModel class is the primary interface for transcription. You can specify the device (e.g., 'cuda' or 'cpu') and the compute_type (e.g., 'float16', 'int8_float16', or 'int8') to optimize performance and memory usage.

    Important: The transcribe method returns a generator for segments. Transcription only begins when you iterate over the segments (e.g., in a for loop or by converting to a list).

    from faster_whisper import WhisperModel
    
    model_size = "large-v3"
    
    # Run on GPU with FP16
    model = WhisperModel(model_size, device="cuda", compute_type="float16")
    
    segments, info = model.transcribe("audio.mp3", beam_size=5)
    
    print("Detected language '%s' with probability %f" % (info.language, info.language_probability))
    
    for segment in segments:
        print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
  10. Load a converted CTranslate2 model

    master

    Once a model has been converted to the CTranslate2 format, you can load it into faster-whisper using the WhisperModel class. You can load it either from a local directory or from a model hosted on the Hugging Face Hub.

    Load from a local directory:

    model = faster_whisper.WhisperModel("whisper-large-v3-ct2")

    Load from Hugging Face Hub:

    model = faster_whisper.WhisperModel("username/whisper-large-v3-ct2")
  11. Perform batched transcription with BatchedInferencePipeline

    master

    For significantly faster transcription, use the BatchedInferencePipeline. It acts as a drop-in replacement for WhisperModel.transcribe but allows you to specify a batch_size to process multiple segments simultaneously.

    from faster_whisper import WhisperModel, BatchedInferencePipeline
    
    model = WhisperModel("turbo", device="cuda", compute_type="float16")
    batched_model = BatchedInferencePipeline(model=model)
    segments, info = batched_model.transcribe("audio.mp3", batch_size=16)
    
    for segment in segments:
        print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))