### Install faster-whisper from PyPI Source: https://github.com/systran/faster-whisper/blob/master/README.md Install the faster-whisper library using pip. This is the standard installation method. ```bash pip install faster-whisper ``` -------------------------------- ### Install faster-whisper for Development Source: https://github.com/systran/faster-whisper/blob/master/CONTRIBUTING.md Install the library in editable mode with development dependencies. This is recommended for active development. ```bash git clone https://github.com/SYSTRAN/faster-whisper.git cd faster-whisper/ pip install -e .[dev] ``` -------------------------------- ### Install faster-whisper from Specific Commit Source: https://github.com/systran/faster-whisper/blob/master/README.md Install a specific version of faster-whisper from a GitHub commit hash. ```bash pip install --force-reinstall "faster-whisper @ https://github.com/SYSTRAN/faster-whisper/archive/a4f1cc8f11433e454c3934442b5e1a4ed5e865c3.tar.gz" ``` -------------------------------- ### Install faster-whisper from Master Branch Source: https://github.com/systran/faster-whisper/blob/master/README.md Install the latest development version of faster-whisper directly from the master branch on GitHub. ```bash pip install --force-reinstall "faster-whisper @ https://github.com/SYSTRAN/faster-whisper/archive/refs/heads/master.tar.gz" ``` -------------------------------- ### Install NVIDIA Libraries with Pip on Linux Source: https://github.com/systran/faster-whisper/blob/master/README.md Installs cuBLAS and cuDNN for CUDA 12 using pip on Linux. Ensure LD_LIBRARY_PATH is set before running Python. ```bash 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__))'` ``` -------------------------------- ### Basic Transcription with Faster-Whisper Source: https://github.com/systran/faster-whisper/blob/master/README.md Transcribe an audio file using Faster-Whisper. Specify the model size, device, and compute type. Transcription starts when the generator is iterated. ```python from faster_whisper import WhisperModel model_size = "large-v3" # Run on GPU with FP16 model = WhisperModel(model_size, device="cuda", compute_type="float16") # or run on GPU with INT8 # model = WhisperModel(model_size, device="cuda", compute_type="int8_float16") # or run on CPU with INT8 # model = WhisperModel(model_size, device="cpu", compute_type="int8") 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)) ``` -------------------------------- ### Custom VAD Parameters Source: https://github.com/systran/faster-whisper/blob/master/README.md Customize VAD behavior by providing a dictionary of parameters. This example reduces the minimum silence duration to 500 milliseconds. ```python segments, _ = model.transcribe( "audio.mp3", vad_filter=True, vad_parameters=dict(min_silence_duration_ms=500), ) ``` -------------------------------- ### Transcription with Distil-Whisper Model Source: https://github.com/systran/faster-whisper/blob/master/README.md Use a Distil-Whisper model, such as 'distil-large-v3', for transcription. This example specifies the language and disables conditioning on previous text. ```python 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)) ``` -------------------------------- ### Convert Whisper Model using CLI Source: https://github.com/systran/faster-whisper/blob/master/README.md Use the ct2-transformers-converter script to convert Whisper models from the Transformers library to CTranslate2 format. Specify the model name or path, output directory, and desired quantization. Ensure transformers[torch] is installed. ```bash 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 ``` -------------------------------- ### Word-Level Timestamps Source: https://github.com/systran/faster-whisper/blob/master/README.md Enable word-level timestamps during transcription. This provides start and end times for each recognized word. ```python 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)) ``` -------------------------------- ### Download and Manage Whisper Models Source: https://context7.com/systran/faster-whisper/llms.txt Shows how to list available models, download them to a specific directory, and load models from local paths or Hugging Face Hub. ```python from faster_whisper.utils import available_models, download_model # List available model sizes models = available_models() print("Available models:", models) ``` ```python # Download model to specific directory model_path = download_model( "large-v3", output_dir="./models/whisper-large-v3", local_files_only=False, ) print(f"Model downloaded to: {model_path}") ``` ```python # Load from local directory model = WhisperModel("./models/whisper-large-v3", device="cuda") ``` ```python # Load from Hugging Face Hub (custom model) model = WhisperModel("Systran/faster-whisper-large-v3", device="cuda") ``` ```python # Load with authentication for private models model = WhisperModel( "username/private-whisper-model", device="cuda", use_auth_token="hf_xxx", ) ``` ```python # Multi-GPU setup model = WhisperModel( "large-v3", device="cuda", device_index=[0, 1, 2, 3], # Use 4 GPUs num_workers=4, # Parallel workers ) ``` -------------------------------- ### Load and Transcribe with Distil-Whisper Models Source: https://context7.com/systran/faster-whisper/llms.txt Demonstrates loading and transcribing audio using Distil-Whisper models, optimized for English and faster inference. Recommends specific settings for optimal performance. ```python from faster_whisper import WhisperModel # Load distil-large-v3 (recommended for English) model = WhisperModel("distil-large-v3", device="cuda", compute_type="float16") # Distil models work best with these settings segments, info = model.transcribe( "audio.mp3", beam_size=5, language="en", # Distil models are English-only condition_on_previous_text=False, # Recommended for distil models ) for segment in segments: print(f"[{segment.start:.2f}s -> {segment.end:.2f}s] {segment.text}") ``` -------------------------------- ### Initialize WhisperModel and Transcribe Audio Source: https://context7.com/systran/faster-whisper/llms.txt Load a Whisper model and perform basic transcription. Transcription is lazy by default and can be forced to completion by converting the generator to a list. ```python from faster_whisper import WhisperModel # Load model - automatically downloads from Hugging Face Hub # Available sizes: tiny, base, small, medium, large-v1, large-v2, large-v3, turbo model = WhisperModel("large-v3", device="cuda", compute_type="float16") # For CPU with INT8 quantization (lower memory usage) # model = WhisperModel("large-v3", device="cpu", compute_type="int8") # Transcribe audio file segments, info = model.transcribe("audio.mp3", beam_size=5) # Print detected language print(f"Detected language '{info.language}' with probability {info.language_probability:.2f}") print(f"Audio duration: {info.duration:.2f}s") # Iterate over segments (transcription runs lazily during iteration) for segment in segments: print(f"[{segment.start:.2fs} -> {segment.end:.2fs}] {segment.text}") # To run transcription to completion immediately, convert to list segments, info = model.transcribe("audio.mp3") segments = list(segments) # Transcription runs here ``` -------------------------------- ### Run Existing Tests Source: https://github.com/systran/faster-whisper/blob/master/CONTRIBUTING.md Execute the test suite to ensure no existing functionality has been broken by your changes. Consider adding new tests for your contributions. ```bash pytest tests/ ``` -------------------------------- ### Enable Word-Level Timestamps Source: https://context7.com/systran/faster-whisper/llms.txt Initialize the model to support word-level timing extraction. ```python from faster_whisper import WhisperModel model = WhisperModel("large-v3", device="cuda", compute_type="float16") ``` -------------------------------- ### Transcribe Audio with WhisperModel Source: https://context7.com/systran/faster-whisper/llms.txt Demonstrates transcribing audio using WhisperModel, accepting file paths, numpy arrays, or file-like objects. ```python from faster_whisper import WhisperModel model = WhisperModel("base", device="cpu") # From file path segments, info = model.transcribe("audio.mp3") ``` ```python # From numpy array audio = decode_audio("audio.mp3") segments, info = model.transcribe(audio) ``` ```python # From file-like object with open("audio.mp3", "rb") as f: segments, info = model.transcribe(f) ``` -------------------------------- ### Reformat and Validate Code Source: https://github.com/systran/faster-whisper/blob/master/CONTRIBUTING.md Apply code formatting and linting using Black, isort, and flake8. These tools ensure code style consistency and catch potential issues. These checks are also performed automatically in the CI. ```bash black . isort . flake8 . ``` -------------------------------- ### Configure Faster-Whisper Logging Source: https://github.com/systran/faster-whisper/blob/master/README.md Set the logging level for the faster_whisper library to DEBUG to see detailed logs. This requires importing the `logging` module. ```python import logging logging.basicConfig() logging.getLogger("faster_whisper").setLevel(logging.DEBUG) ``` -------------------------------- ### Load Converted Model from Local Directory Source: https://github.com/systran/faster-whisper/blob/master/README.md Instantiate a WhisperModel object by providing the path to a locally converted CTranslate2 model directory. ```python model = faster_whisper.WhisperModel("whisper-large-v3-ct2") ``` -------------------------------- ### Decode audio files Source: https://context7.com/systran/faster-whisper/llms.txt Load and preprocess audio files into 16kHz mono format using PyAV. ```python from faster_whisper.audio import decode_audio import numpy as np ``` -------------------------------- ### Set CPU threads for performance benchmarking Source: https://github.com/systran/faster-whisper/blob/master/README.md Use the OMP_NUM_THREADS environment variable to control the number of threads when running scripts on CPU. ```bash OMP_NUM_THREADS=4 python3 my_script.py ``` -------------------------------- ### Load Converted Model from Hugging Face Hub Source: https://github.com/systran/faster-whisper/blob/master/README.md Load a faster-whisper model directly from the Hugging Face Hub by specifying its repository name. Ensure the model has been previously uploaded. ```python model = faster_whisper.WhisperModel("username/whisper-large-v3-ct2") ``` -------------------------------- ### Configure Advanced Transcription Options Source: https://context7.com/systran/faster-whisper/llms.txt Use the transcribe method with specific parameters to control language detection, beam search, temperature fallback, and VAD filtering. ```python from faster_whisper import WhisperModel model = WhisperModel("medium", device="cuda", compute_type="float16") # Transcribe with explicit language and advanced options segments, info = model.transcribe( "audio.mp3", language="en", # Specify language (skip auto-detection) task="transcribe", # "transcribe" or "translate" (to English) beam_size=5, # Beam size for decoding best_of=5, # Number of candidates when sampling temperature=[0.0, 0.2, 0.4, 0.6, 0.8, 1.0], # Temperature fallback sequence compression_ratio_threshold=2.4, # Retry with higher temp if too repetitive log_prob_threshold=-1.0, # Retry if avg log prob below threshold no_speech_threshold=0.6, # Skip segment if no_speech_prob above this condition_on_previous_text=True, # Use previous output as prompt initial_prompt="This is a technical podcast about AI.", # Context prompt word_timestamps=False, # Enable word-level timestamps vad_filter=False, # Enable voice activity detection log_progress=True, # Show progress bar ) # Access transcription info print(f"Language: {info.language} (prob: {info.language_probability:.2f})") print(f"Duration: {info.duration:.2f}s, After VAD: {info.duration_after_vad:.2f}s") for segment in segments: print(f"[{segment.start:.2f}s -> {segment.end:.2f}s] {segment.text}") print(f" avg_logprob: {segment.avg_logprob:.3f}, no_speech_prob: {segment.no_speech_prob:.3f}") ``` -------------------------------- ### Configure Logging for Faster Whisper Source: https://context7.com/systran/faster-whisper/llms.txt Adjust the logging level for the 'faster_whisper' library to obtain detailed information about the transcription process, including VAD filtering, language detection, and segment processing. Set the level to `logging.DEBUG` for verbose output and `logging.WARNING` to suppress it in production. ```python import logging # Configure logging to see detailed output logging.basicConfig() logging.getLogger("faster_whisper").setLevel(logging.DEBUG) from faster_whisper import WhisperModel model = WhisperModel("base", device="cpu") segments, info = model.transcribe("audio.mp3", vad_filter=True) # Debug output includes: # - Processing audio with duration 00:01:30.500 # - Detected language 'en' with probability 0.98 # - VAD filter removed 00:00:15.200 of audio # - VAD filter kept the following audio segments: [0.00s -> 15.20s], [18.50s -> 45.00s] # - Processing segment at 00:00:00.000 # Suppress logging for production logging.getLogger("faster_whisper").setLevel(logging.WARNING) ``` -------------------------------- ### Translate Audio to English with Faster Whisper Source: https://context7.com/systran/faster-whisper/llms.txt Use the `task='translate'` parameter to convert audio from any supported language to English. This allows for cross-lingual transcription in a single step. Ensure the model is loaded with the appropriate device and compute type. ```python from faster_whisper import WhisperModel model = WhisperModel("large-v3", device="cuda", compute_type="float16") # Translate non-English audio to English segments, info = model.transcribe( "french_audio.mp3", task="translate", # Translate to English ) print(f"Original language: {info.language}") for segment in segments: print(f"[{segment.start:.2f}s -> {segment.end:.2f}s] {segment.text}") # Auto-detect language and translate segments, info = model.transcribe("unknown_language.mp3", task="translate") # Transcribe in original language (default) segments, info = model.transcribe("french_audio.mp3", task="transcribe") ``` -------------------------------- ### Perform high-throughput batched transcription Source: https://context7.com/systran/faster-whisper/llms.txt Use BatchedInferencePipeline to process audio chunks in parallel for improved performance. ```python from faster_whisper import WhisperModel, BatchedInferencePipeline # Create base model model = WhisperModel("turbo", device="cuda", compute_type="float16") # Wrap with batched inference pipeline batched_model = BatchedInferencePipeline(model=model) # Transcribe with batching - same API as WhisperModel.transcribe() segments, info = batched_model.transcribe( "audio.mp3", batch_size=16, # Number of parallel chunks word_timestamps=True, # Word timestamps supported vad_filter=True, # VAD enabled by default for batched language="en", ) print(f"Language: {info.language}, Duration: {info.duration:.2f}s") for segment in segments: print(f"[{segment.start:.2f}s -> {segment.end:.2f}s] {segment.text}") ``` -------------------------------- ### Transcribe with word-level timestamps Source: https://context7.com/systran/faster-whisper/llms.txt Configure punctuation merging to associate punctuation marks with adjacent words during transcription. ```python segments, info = model.transcribe( "audio.mp3", word_timestamps=True, prepend_punctuations="\"'"¿([{-", # Merge these with next word append_punctuations="\"'.。,,!!??::")]}、", # Merge these with previous word ) for segment in segments: print(f"\nSegment [{segment.start:.2f}s -> {segment.end:.2f}s]:") for word in segment.words: print(f" [{word.start:.2f}s -> {word.end:.2f}s] '{word.word}' (prob: {word.probability:.2f})") ``` -------------------------------- ### Batched Transcription with Faster-Whisper Source: https://github.com/systran/faster-whisper/blob/master/README.md Perform batched transcription using `BatchedInferencePipeline`. This can improve throughput for multiple audio files or long audio. The pipeline uses a pre-initialized `WhisperModel`. ```python 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)) ``` -------------------------------- ### Configure Voice Activity Detection (VAD) Source: https://context7.com/systran/faster-whisper/llms.txt Filter out non-speech segments to improve transcription speed and accuracy. ```python from faster_whisper import WhisperModel from faster_whisper.vad import VadOptions model = WhisperModel("large-v3", device="cuda", compute_type="float16") # Enable VAD with default parameters segments, info = model.transcribe("audio.mp3", vad_filter=True) print(f"Original duration: {info.duration:.2f}s") print(f"Duration after VAD: {info.duration_after_vad:.2f}s") print(f"Removed {info.duration - info.duration_after_vad:.2f}s of silence") # Customize VAD parameters segments, info = model.transcribe( "audio.mp3", vad_filter=True, vad_parameters=dict( threshold=0.5, # Speech probability threshold min_speech_duration_ms=250, # Minimum speech chunk duration min_silence_duration_ms=500, # Minimum silence to split on max_speech_duration_s=30, # Maximum speech chunk duration speech_pad_ms=400, # Padding around speech chunks ), ) # Or use VadOptions class directly vad_options = VadOptions( threshold=0.5, min_silence_duration_ms=1000, speech_pad_ms=200, ) segments, info = model.transcribe("audio.mp3", vad_filter=True, vad_parameters=vad_options) ``` -------------------------------- ### Process Transcription Segments Source: https://context7.com/systran/faster-whisper/llms.txt Iterates through transcription segments, converts them to dictionaries, and demonstrates generating SRT subtitle format. ```python from faster_whisper import WhisperModel from dataclasses import asdict model = WhisperModel("base", device="cpu") segments, info = model.transcribe("audio.mp3", word_timestamps=True) # Convert segment to dictionary for segment in segments: seg_dict = asdict(segment) print(seg_dict) ``` ```python # Generate SRT subtitle format def generate_srt(segments): srt_output = [] for i, segment in enumerate(segments, 1): start = format_timestamp(segment.start, always_include_hours=True, decimal_marker=",") end = format_timestamp(segment.end, always_include_hours=True, decimal_marker=",") srt_output.append(f"{i}\n{start} --> {end}\n{segment.text.strip()}\n") return "\n".join(srt_output) ``` -------------------------------- ### Detect spoken language Source: https://context7.com/systran/faster-whisper/llms.txt Identify the language of an audio file using the model's detection capabilities. ```python from faster_whisper import WhisperModel from faster_whisper.audio import decode_audio model = WhisperModel("large-v3", device="cuda", compute_type="float16") # Detect language from audio file language, probability, all_probs = model.detect_language("audio.mp3") print(f"Detected: {language} (probability: {probability:.2f})") # Show top 5 language probabilities top_languages = sorted(all_probs, key=lambda x: x[1], reverse=True)[:5] for lang, prob in top_languages: print(f" {lang}: {prob:.4f}") # Detect with multiple segments for better accuracy language, probability, all_probs = model.detect_language( "audio.mp3", language_detection_segments=3, # Analyze first 3 segments language_detection_threshold=0.5, # Confidence threshold ) # Use VAD for cleaner detection language, probability, all_probs = model.detect_language( "audio.mp3", vad_filter=True, ) ``` -------------------------------- ### Decode Audio to Numpy Array Source: https://context7.com/systran/faster-whisper/llms.txt Decodes an audio file into a numpy array. Supports splitting stereo channels. ```python audio = decode_audio("audio.mp3", sampling_rate=16000) print(f"Audio shape: {audio.shape}, dtype: {audio.dtype}") print(f"Duration: {len(audio) / 16000:.2f} seconds") ``` ```python left_channel, right_channel = decode_audio( "stereo_audio.wav", sampling_rate=16000, split_stereo=True, ) print(f"Left channel: {left_channel.shape}") print(f"Right channel: {right_channel.shape}") ``` -------------------------------- ### Format Timestamps with faster-whisper Source: https://context7.com/systran/faster-whisper/llms.txt Utilizes the `format_timestamp` utility function to convert seconds into human-readable time formats with customizable options. ```python from faster_whisper.utils import format_timestamp print(format_timestamp(125.5)) print(format_timestamp(3725.123)) print(format_timestamp(45.0, always_include_hours=True)) print(format_timestamp(30.5, decimal_marker=",")) ``` -------------------------------- ### Force Transcription Completion Source: https://github.com/systran/faster-whisper/blob/master/README.md Ensure transcription completes by converting the segments generator to a list. This is necessary if you need all segments before proceeding. ```python segments, _ = model.transcribe("audio.mp3") segments = list(segments) # The transcription will actually run here. ``` -------------------------------- ### VAD Filter for Silence Removal Source: https://github.com/systran/faster-whisper/blob/master/README.md Use the Voice Activity Detection (VAD) filter to remove segments of silence from the transcription. The default minimum silence duration is 2 seconds. ```python segments, _ = model.transcribe("audio.mp3", vad_filter=True) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.