### Setup Development Environment Source: https://github.com/pyannote/pyannote-audio/blob/main/README.md Install the package in editable mode with development and testing dependencies, and initialize pre-commit hooks. ```bash pip install -e .[dev,testing] pre-commit install ``` -------------------------------- ### Install pyannote.audio and Setup AMI Corpus (Colab) Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/add_your_own_task.ipynb Installs pyannote.audio, ipython, and downloads a mini version of the AMI corpus for use in Google Colab. It also clones the necessary setup repository and navigates to the correct directory. ```python !pip install -qq pyannote.audio==3.1.1 !pip install -qq ipython==7.34.0 !git clone https://github.com/pyannote/AMI-diarization-setup.git %cd ./AMI-diarization-setup/pyannote/ !bash ./download_ami_mini.sh %cd /content ``` -------------------------------- ### Setup Environment for pyannote.audio Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/applying_a_pipeline.ipynb Install the necessary library dependencies and download sample assets required for running the tutorials. This includes setting up the environment for both Google Colab and local development. ```python !pip install -qq pyannote.audio==3.1.1 !pip install -qq ipython==7.34.0 !wget -q "https://github.com/pyannote/pyannote-audio/raw/develop/tutorials/assets/sample.wav" !wget -q "https://github.com/pyannote/pyannote-audio/raw/develop/tutorials/assets/sample.rttm" !wget -q -P ./assets/ "https://github.com/pyannote/pyannote-audio/blob/develop/tutorials/assets/download-model.png" !wget -q -P ./assets/ "https://github.com/pyannote/pyannote-audio/blob/develop/tutorials/assets/download-pipeline.png" ``` ```python ROOT_DIR = "/pyannote-audio" AUDIO_FILE = f"{ROOT_DIR}/tutorials/assets/sample.wav" REFERENCE = f"{ROOT_DIR}/tutorials/assets/sample.rttm" ``` -------------------------------- ### Install Dependencies and Download Assets Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/applying_a_model.ipynb Installs the required pyannote.audio version and downloads sample audio and annotation files for Colab environments. ```bash !pip install -qq pyannote.audio==3.1.1 !pip install -qq ipython==7.34.0 !wget -q "https://github.com/pyannote/pyannote-audio/raw/develop/tutorials/assets/sample.wav" !wget -q "https://github.com/pyannote/pyannote-audio/raw/develop/tutorials/assets/sample.rttm" !wget -q -P ./assets/ "https://github.com/pyannote/pyannote-audio/blob/develop/tutorials/assets/download-model.png" ``` -------------------------------- ### Install pyannote.audio and IPython Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/intro.ipynb Installs the pyannote.audio library and IPython for interactive use. Version 3.1.1 of pyannote.audio and 7.34.0 of IPython are specified. ```python !pip install -qq pyannote.audio==3.1.1 !pip install -qq ipython==7.34.0 ``` -------------------------------- ### Install pyannote.audio library Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/MRE_template.ipynb Installs the specific version of the pyannote.audio library required for the reproduction environment using pip. ```bash !pip install -qqq pyannote.audio==3.1.1 ``` -------------------------------- ### Data Preparation and Setup Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/add_your_own_task.ipynb Methods for preparing training/validation metadata and configuring task specifications. ```APIDOC ## METHOD post_prepare_data ### Description Processes training metadata and extracts unique class labels from annotations. ### Parameters - **prepared_data** (Dict) - Required - Dictionary to be populated with 'train_metadata' and 'classes'. ## METHOD setup ### Description Configures task specifications including problem type (MULTI_LABEL_CLASSIFICATION) and resolution. ### Parameters - **stage** (str) - Optional - The current training stage. ``` -------------------------------- ### Install pyannote.audio Dependencies Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/adapting_pretrained_pipeline.ipynb Installs the required pyannote.audio library and rich formatting tools via pip. This is the initial step to set up the environment for diarization tasks. ```python !pip install -qq pyannote.audio==2.1.1 !pip install -qq rich ``` -------------------------------- ### Install pyannote.audio and dependencies for Google Colab Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/community/eval_separation_pipeline.ipynb Installs the necessary Python libraries including speechbrain, whisperx, and pyannote.audio with separation support to prepare the environment for evaluation. ```python !pip install -qq speechbrain==0.5.16 !pip install -qq ipython==7.34.0 !pip install -qq ipywidgets openai-whisper whisperx==3.1.5 meeteval !pip install -qq pyannote.audio[separation]==3.3.0 ``` -------------------------------- ### CLI Training with Custom Model Configuration (Pyannote Audio) Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/add_your_own_model.ipynb This example shows how to train a custom model using the `pyannote-audio-train` CLI. It requires defining the model in a Python package, setting the PYTHONPATH, creating a Hydra configuration file for the model, and then running the training command with specified parameters. ```bash # 1. Define your model in a proper Python package: # /your/favorite/directory/ # your_package_name/ # __init__.py # needs to be here but can be empty # custom_model.py # contains the definition of your model # 2. Add the package to your PYTHONPATH: # $ export PYTHONPATH=/your/favorite/directory # 3. Check that you can import it from Python: # >>> from your_package_name.custom_model import MyCustomModel # 4. Tell Hydra about this new model: # /your/favorite/directory/ # custom_config/ # model/ # MyCustomModel.yaml # # where the content of MyCustomModel.yaml is: # # @package _group_ # _target_: your_package_name.custom_model.MyCustomModel # param1: 32 # param2: 16 # 5. Enjoy training: # $ pyannote-audio-train --config-dir=/your/favorite/directory/custom_config \ # protocol=Debug.SpeakerDiarization.Debug \ # task=VoiceActivityDetection \ # model=MyCustomModel \ # model.param2=12 ``` -------------------------------- ### Configuring Training with Data Augmentation Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/training_a_model.ipynb This example shows how to integrate data augmentation into the training process using the 'torch-audiomentations' library. It defines a Voice Activity Detection task with background noise augmentation. ```python from pyannote.audio.tasks import VoiceActivityDetection from torch_audiomentations import AddBackgroundNoise # Assuming 'ami' is a pre-defined dataset or configuration background_noise_dir = "/path/to/background/noise/directory" augmentation = AddBackgroundNoise(background_noise_dir) vad_task = VoiceActivityDetection(ami, augmentation=augmentation) ``` -------------------------------- ### Integrate with Premium pyannoteAI Speaker Diarization Service Source: https://context7.com/pyannote/pyannote-audio/llms.txt This example illustrates how to use the premium speaker diarization pipeline offered by pyannoteAI. It shows loading a high-precision model and processing an audio file, with the computation happening on pyannoteAI servers. The output format is consistent with the open-source pipeline. ```python from pyannote.audio import Pipeline # Load premium precision-2 pipeline # Requires API key from https://dashboard.pyannote.ai pipeline = Pipeline.from_pretrained( "pyannote/speaker-diarization-precision-2", token="YOUR_PYANNOTEAI_API_KEY" ) # Process audio (runs on pyannoteAI servers) output = pipeline("audio.wav") # Results have same format as open-source pipeline for turn, speaker in output.speaker_diarization: print(f"start={turn.start:.1f}s stop={turn.end:.1f}s {speaker}") # Output: # start=0.2s stop=1.6s SPEAKER_00 # start=1.8s stop=4.0s SPEAKER_01 # start=4.2s stop=5.6s SPEAKER_00 # Premium features available via pyannoteAI SDK # See https://docs.pyannote.ai for voiceprinting, confidence scores, etc. ``` -------------------------------- ### Load Model for Diarization - Python (Error Example) Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/MRE_template.ipynb Demonstrates an attempt to load a pre-trained model using `Model.from_pretrained`. This specific example highlights a common error where a pipeline identifier is incorrectly used with the `Model` class, leading to an `EntryNotFoundError`. ```python from pyannote.audio import Model model = Model.from_pretrained( "pyannote/speaker-diarization-3.1", token=hf_token) ``` -------------------------------- ### Access and Iterate Through Training Files Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/overlapped_speech_detection.ipynb This code shows how to access the training set of a loaded pyannote.database protocol. It demonstrates how to get the first training file from the protocol's training set. ```python first_training_file = next(protocol.train()) ``` -------------------------------- ### Load and Apply Community-1 Open-Source Speaker Diarization Pipeline Source: https://github.com/pyannote/pyannote-audio/blob/main/README.md This Python code demonstrates how to load the 'pyannote/speaker-diarization-community-1' pipeline using a Hugging Face access token. It then applies the pipeline to an audio file locally and prints the diarization results, including start and end times for each speaker segment. The code also shows how to send the pipeline to a GPU for faster processing. ```python import torch from pyannote.audio import Pipeline from pyannote.audio.pipelines.utils.hook import ProgressHook # Community-1 open-source speaker diarization pipeline pipeline = Pipeline.from_pretrained( "pyannote/speaker-diarization-community-1", token="HUGGINGFACE_ACCESS_TOKEN") # send pipeline to GPU (when available) pipeline.to(torch.device("cuda")) # apply pretrained pipeline (with optional progress hook) with ProgressHook() as hook: output = pipeline("audio.wav", hook=hook) # runs locally # print the result for turn, speaker in output.speaker_diarization: print(f"start={turn.start:.1f}s stop={turn.end:.1f}s speaker_{speaker}") # start=0.2s stop=1.5s speaker_0 # start=1.8s stop=3.9s speaker_1 # start=4.2s stop=5.7s speaker_0 # ... ``` -------------------------------- ### Configure environment and download tutorial assets Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/community/eval_separation_pipeline.ipynb Sets the root directory for assets and downloads necessary audio files and RTTM files from the AMI dataset for testing. ```python import os ROOT_DIR = "/Users/hbredin/Development/pyannote/pyannote-audio" os.environ["ASSET_DIR"] = ROOT_DIR + "/tutorials/assets/separation" !mkdir -p ${ASSET_DIR}/sources !wget --continue -q -O ${ASSET_DIR}/mixture.wav https://groups.inf.ed.ac.uk/ami/AMICorpusMirror//amicorpus/ES2004a/audio/ES2004a.Mix-Headset.wav !wget --continue -q -O ${ASSET_DIR}/sources/source0.wav https://groups.inf.ed.ac.uk/ami/AMICorpusMirror//amicorpus/ES2004a/audio/ES2004a.Headset-0.wav !wget --continue -q -O ${ASSET_DIR}/sources/source1.wav https://groups.inf.ed.ac.uk/ami/AMICorpusMirror//amicorpus/ES2004a/audio/ES2004a.Headset-1.wav !wget --continue -q -O ${ASSET_DIR}/sources/source2.wav https://groups.inf.ed.ac.uk/ami/AMICorpusMirror//amicorpus/ES2004a/audio/ES2004a.Headset-2.wav !wget --continue -q -O ${ASSET_DIR}/sources/source3.wav https://groups.inf.ed.ac.uk/ami/AMICorpusMirror//amicorpus/ES2004a/audio/ES2004a.Headset-3.wav !wget --continue -q -O ${ASSET_DIR}/mixture.rttm https://raw.githubusercontent.com/pyannote/AMI-diarization-setup/main/only_words/rttms/test/ES2004a.rttm ``` -------------------------------- ### Process Specific Audio Segment with Pyannote Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/applying_a_model.ipynb This example shows how to use the `crop` method of a pyannote-audio inference object to process only a specific segment of an audio file. It takes an audio input (in-memory dictionary) and a `Segment` object defining the start and end times for processing. ```python from pyannote.core import Segment output = inference.crop(audio_in_memory, Segment(0, 20)) print(output) ``` -------------------------------- ### Listen to Audio Sample Source: https://github.com/pyannote/pyannote-audio/blob/main/notebook/example.ipynb Demonstrates how to listen to an audio sample from a loaded protocol. It uses the `listen` utility from `pyannote.audio.utils.preview` to play the first training audio file. ```python from pyannote.audio.utils.preview import listen listen(next(protocol.train())) ``` -------------------------------- ### Load and Crop Audio Excerpt Source: https://github.com/pyannote/pyannote-audio/blob/main/notebook/augmentation.ipynb Demonstrates how to initialize a protocol, load an audio file, and crop a 5-second segment using pyannote-audio utilities. It results in a torch tensor representation of the waveform. ```python from pyannote.database import get_protocol, FileFinder from pyannote.audio.core.io import Audio from pyannote.core import Segment import torch protocol = get_protocol('Debug.SpeakerDiarization.Debug', preprocessors={"audio": FileFinder()}) audio = Audio(sample_rate=16000, mono="downmix") file = next(protocol.test()) waveform, sample_rate = audio.crop(file, Segment(5, 10)) waveforms = torch.tensor(waveform)[None, :] ``` -------------------------------- ### Get Speech Timeline using Oracle OSD Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/overlapped_speech_detection.ipynb This snippet shows how to get the speech timeline from an audio file using the oracle_osd function. It assumes a test file path is defined. ```python oracle_osd(test_file).get_timeline() ``` -------------------------------- ### Initialize Protocol and Data Loading Source: https://github.com/pyannote/pyannote-audio/blob/main/notebook/inference.ipynb Configures the data protocol using FileFinder to locate audio files for processing. ```python from pyannote.database import get_protocol, FileFinder protocol = get_protocol('Debug.SpeakerDiarization.Debug', preprocessors={"audio": FileFinder()}) ``` -------------------------------- ### Load and Process Audio with Pyannote Source: https://context7.com/pyannote/pyannote-audio/llms.txt Demonstrates how to load audio files using torchaudio or numpy, and how to pass them into a pipeline using an in-memory dictionary structure. ```python import torch import torchaudio import numpy as np # Load audio with torchaudio waveform, sample_rate = torchaudio.load("audio.wav") # Create in-memory audio dictionary audio_in_memory = { "waveform": waveform, "sample_rate": sample_rate } # Process in-memory audio output = pipeline(audio_in_memory) # Process numpy array numpy_audio = np.random.randn(160000).astype(np.float32) audio_dict = { "waveform": torch.from_numpy(numpy_audio).unsqueeze(0), "sample_rate": 16000 } output = pipeline(audio_dict) ``` -------------------------------- ### GET /task/validation_sample Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/add_your_own_task.ipynb Retrieves a specific validation sample based on the provided index. ```APIDOC ## GET /task/validation_sample ### Description Retrieves a validation sample by index, calculating the corresponding file and chunk within the validation set. ### Method GET ### Endpoint /task/validation_sample ### Parameters #### Query Parameters - **sample_idx** (int) - Required - The index of the sample to retrieve. ### Response #### Success Response (200) - **X** (numpy.ndarray) - The audio features of the validation chunk. - **y** (numpy.ndarray) - The ground truth labels for the chunk. ### Response Example { "X": "[...audio_data...]", "y": [1, 0, 0] } ``` -------------------------------- ### Download and Prepare Demo Audio File Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/intro.ipynb Downloads a sample audio file from the AMI corpus and sets up a dictionary to reference it for processing. It also defines an excerpt segment for focused visualization. ```python !wget -q http://groups.inf.ed.ac.uk/ami/AMICorpusMirror/amicorpus/ES2004a/audio/ES2004a.Mix-Headset.wav DEMO_FILE = {'uri': 'ES2004a.Mix-Headset', 'audio': 'ES2004a.Mix-Headset.wav'} ``` -------------------------------- ### GET /pipeline/hooks Source: https://context7.com/pyannote/pyannote-audio/llms.txt Monitors pipeline progress, timing, and intermediate artifacts using hook callbacks. ```APIDOC ## GET /pipeline/hooks ### Description Provides mechanisms to track the progress, execution time, and internal artifacts of a diarization pipeline. ### Method GET ### Endpoint /pipeline/hooks ### Parameters #### Query Parameters - **hook_type** (string) - Required - Type of hook: 'progress', 'timing', or 'artifact'. ### Request Example { "hook_type": "timing" } ### Response #### Success Response (200) - **timing** (object) - Dictionary containing execution duration for each pipeline step. #### Response Example { "total": 5.2, "segmentation": 3.1, "embeddings": 2.1 } ``` -------------------------------- ### GET /task/training_sample Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/add_your_own_task.ipynb Retrieves a random training sample from the dataset, including audio features and target labels. ```APIDOC ## GET /task/training_sample ### Description Generates a random training sample by cropping an audio file to a specific duration and mapping active classes to a binary numpy array. ### Method GET ### Endpoint /task/training_sample ### Parameters #### Request Body - **duration** (float) - Required - The length of the audio chunk to extract. ### Response #### Success Response (200) - **X** (numpy.ndarray) - The audio features of the cropped chunk. - **y** (numpy.ndarray) - A binary array representing active classes. ### Response Example { "X": "[...audio_data...]", "y": [0, 1, 0] } ``` -------------------------------- ### POST /pipeline/vad/instantiate Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/voice_activity_detection.ipynb Instantiates the Voice Activity Detection pipeline with custom hyper-parameters for thresholding. ```APIDOC ## POST /pipeline/vad/instantiate ### Description Configures the VoiceActivityDetection pipeline with specific onset, offset, and duration thresholds to convert probabilities into final speech regions. ### Method POST ### Endpoint /pipeline/vad/instantiate ### Request Body - **onset** (float) - Required - Probability threshold to mark region as active. - **offset** (float) - Required - Probability threshold to mark region as inactive. - **min_duration_on** (float) - Required - Minimum duration to keep active regions. - **min_duration_off** (float) - Required - Minimum duration to keep inactive regions. ### Request Example { "onset": 0.6, "offset": 0.4, "min_duration_on": 0.0, "min_duration_off": 0.0 } ### Response #### Success Response (200) - **timeline** (Timeline) - The resulting speech segments. #### Response Example { "timeline": "" } ``` -------------------------------- ### GET /models/load Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/applying_a_model.ipynb Load a pretrained model from the Hugging Face Hub. Requires authentication for gated models. ```APIDOC ## GET /models/load ### Description Loads a specific pretrained model from the Hugging Face Hub. Note that some models are gated and require accepting terms on the Hugging Face website and authentication via `notebook_login`. ### Method GET (Internal Python API) ### Parameters #### Request Body - **model_id** (string) - Required - The identifier of the model on the Hugging Face Hub (e.g., "pyannote/segmentation-3.0"). - **token** (boolean/string) - Required - Set to True to use the authenticated token for gated models. ### Request Example ```python from pyannote.audio import Model model = Model.from_pretrained("pyannote/segmentation-3.0", token=True) ``` ### Response #### Success Response (200) - **model** (Object) - The loaded pyannote.audio model instance containing architecture and specifications. ``` -------------------------------- ### Get Pretrained Pipeline Hyper-parameters Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/adapting_pretrained_pipeline.ipynb Retrieves the hyper-parameters of a pretrained pyannote-speaker-diarization pipeline. These parameters are specific to the internal segmentation model used by the pipeline. ```python pretrained_hyperparameters = pretrained_pipeline.parameters(instantiated=True) pretrained_hyperparameters ``` -------------------------------- ### Process Audio from Memory with pyannote.audio Source: https://context7.com/pyannote/pyannote-audio/llms.txt Demonstrates how to process audio data directly from memory using pyannote.audio pipelines. This is useful when audio is not available as a file but as a data structure, such as from audio streams or other processing steps. It shows the initialization of a pipeline for this purpose. ```python import torch import torchaudio from pyannote.audio import Pipeline pipeline = Pipeline.from_pretrained( "pyannote/speaker-diarization-community-1", token="YOUR_HUGGINGFACE_TOKEN" ) ``` -------------------------------- ### Define Task Specifications (Python) Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/add_your_own_task.ipynb Defines the specifications for a custom task, including the problem type, resolution, expected duration, and a list of possible classes. This example is for sound event detection. ```python from pyannote.audio.core.task import Specifications specifications = Specifications( problem=problem, resolution=resolution, duration=5.0, classes=["Speech", "Dog", "Cat", "Alarm_bell_ringing", "Dishes", "Frying", "Blender", "Running_water", "Vacuum_cleaner", "Electric_shaver_toothbrush"], ) ``` -------------------------------- ### Access Training Data from Protocol Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/voice_activity_detection.ipynb Demonstrates how to access the training set from a loaded protocol and retrieve the annotation for the first training file. ```python first_training_file = next(ami.train()) reference = first_training_file["annotation"] ``` -------------------------------- ### Get Optimized Clustering Threshold Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/adapting_pretrained_pipeline.ipynb Retrieves the best clustering threshold found after the optimization process. This value, along with the optimized segmentation threshold, can be used to evaluate the finetuned pipeline's performance. ```python best_clustering_threshold = optimizer.best_params['clustering']['threshold'] ``` -------------------------------- ### Listen to Audio Segment and Visualize Annotation Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/voice_activity_detection.ipynb Plays a one-minute segment of the first training file and sets up visualization for a specific time range using pyannote.core utilities. ```python from pyannote.audio.utils.preview import listen from pyannote.core import Segment one_minute = Segment(240, 300) listen(first_training_file, one_minute) ``` ```python from pyannote.core import notebook notebook.crop = one_minute reference ``` -------------------------------- ### Customizing Optimizer and Scheduler in PyTorch Lightning Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/training_a_model.ipynb This example demonstrates how to override the default optimizer and learning rate scheduler in a PyTorch Lightning model. It defines a custom 'configure_optimizers' method using SGD and ExponentialLR. ```python from types import MethodType from torch.optim import SGD from torch.optim.lr_scheduler import ExponentialLR # Assuming 'model' is your PyTorch Lightning module def configure_optimizers(self): optimizer = SGD(self.parameters()) lr_scheduler = ExponentialLR(optimizer, gamma=0.9) return {"optimizer": optimizer, "lr_scheduler": lr_scheduler} model.configure_optimizers = MethodType(configure_optimizers, model) # Assuming 'trainer' is your PyTorch Lightning Trainer # trainer.fit(model) ``` -------------------------------- ### Get Inference Output Shape Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/applying_a_model.ipynb This Python code snippet retrieves and prints the shape of the inference output from a pyannote.audio model. The shape typically indicates the number of windows processed, the number of frames per window, and the number of output classes. ```python output.data.shape ``` -------------------------------- ### Load Dataset and Pretrained Pipeline Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/adapting_pretrained_pipeline.ipynb Registers the local database configuration and initializes the pretrained speaker diarization pipeline from Hugging Face. Requires authentication via notebook_login. ```python from pyannote.database import registry, FileFinder from huggingface_hub import notebook_login from pyannote.audio import Pipeline registry.load_database("AMI-diarization-setup/pyannote/database.yml") dataset = registry.get_protocol("AMI-SDM.SpeakerDiarization.mini", {"audio": FileFinder()}) notebook_login() pretrained_pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization", token=True) ``` -------------------------------- ### Train Speaker Diarization Model Source: https://github.com/pyannote/pyannote-audio/blob/main/notebook/example.ipynb Initializes and trains a Speaker Diarization model. It sets up the `SpeakerDiarization` task, instantiates a `SimpleSegmentationModel`, and prepares the PyTorch Lightning `Trainer` for a single epoch of training. The code snippet focuses on the setup before the training begins. ```python from pyannote.audio.tasks import SpeakerDiarization seg = SpeakerDiarization(protocol, duration=2., batch_size=32, num_workers=4) model = SimpleSegmentationModel(task=seg) trainer = Trainer(max_epochs=1) ``` -------------------------------- ### API Integration for Custom Model Training (Pyannote Audio) Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/add_your_own_model.ipynb This Python code demonstrates how to initialize and train a custom model using the pyannote.audio API. It involves loading a database protocol, defining a task, initializing the custom model, and using PyTorch Lightning's Trainer for the training process. ```python # initialize your experimental protocol from pyannote.database import registry, FileFinder registry.load_database("./AMI-diarization-setup/pyannote/database.yml") protocol = registry.get_protocol('AMI.SpeakerDiarization.mini', preprocessors={"audio": FileFinder()}) # initialize the task you want to address from pyannote.audio.tasks import VoiceActivityDetection task = VoiceActivityDetection(protocol) # initialize the model # Assuming MyCustomModel is defined and imported # from your_package_name.custom_model import MyCustomModel model = MyCustomModel(task=task) # train the model from pytorch_lightning import Trainer trainer = Trainer(max_epochs=1) trainer.fit(model) ``` -------------------------------- ### Download Sample Audio File - Shell Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/MRE_template.ipynb Downloads a sample WAV audio file from a specified URL using wget. This is useful for testing the diarization pipeline. Ensure the download link is publicly accessible for reproducibility. ```shell !wget https://github.com/pyannote/pyannote-audio/raw/develop/tutorials/assets/sample.wav ``` -------------------------------- ### Get pyannote.audio Model Specifications Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/applying_a_model.ipynb This Python code snippet retrieves and displays the specifications of a loaded pyannote.audio model. These specifications include details about the problem type, resolution, duration, minimum duration, warm-up periods, output classes, and overlap handling. ```python specs = model.specifications specs ``` -------------------------------- ### Instantiate pyannote.audio pipeline Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/community/eval_separation_pipeline.ipynb Configures the separation pipeline with specific hyper-parameters for segmentation, clustering, and leakage removal. ```python pipeline.instantiate({ "segmentation": {"min_duration_off": 0.0, "threshold": 0.5}, "clustering": { "method": "centroid", "min_cluster_size": 50, "threshold": 0.68, }, "separation": { "leakage_removal": True, "asr_collar": 0.32, } }) ``` -------------------------------- ### Contributing Custom Model to Pyannote Audio Library Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/add_your_own_model.ipynb This guide outlines the steps to contribute a custom model to the pyannote-audio library. It involves placing the model code in `pyannote.audio.models`, adding a corresponding Hydra configuration file in `pyannote.audio.cli.train_config.model`, and then using it via the CLI. ```bash # 1. Add your model in pyannote.audio.models: # pyannote/ # audio/ # models/ # custom_model.py # # 2. Check that you can import it from Python: # >>> from pyannote.audio.models.custom_model import MyCustomModel # # 3. Add the corresponding Hydra configuration file: # pyannote/ # audio/ # cli/ # train_config/ # model/ # MyCustomModel.yaml # # where the content of MyCustomModel.yaml is: # # @package _group_ # _target_: pyannote.audio.models.custom_model.MyCustomModel # param1: 32 # param2: 16 # # 4. Enjoy training: # $ pyannote-audio-train protocol=Debug.SpeakerDiarization.Debug \ # task=VoiceActivityDetection \ # model=MyCustomModel \ # model.param2=12 ``` -------------------------------- ### Load and Apply Precision-2 Premium Speaker Diarization Service Source: https://github.com/pyannote/pyannote-audio/blob/main/README.md This Python code shows how to load the 'pyannote/speaker-diarization-precision-2' premium pipeline using a pyannoteAI API key. The pipeline is executed on pyannoteAI servers, and the results, including speaker segments with start and end times, are printed. ```python from pyannote.audio import Pipeline # Precision-2 premium speaker diarization service pipeline = Pipeline.from_pretrained( "pyannote/speaker-diarization-precision-2", token="PYANNOTEAI_API_KEY") output = pipeline("audio.wav") # runs on pyannoteAI servers # print the result for turn, speaker in output.speaker_diarization: print(f"start={turn.start:.1f}s stop={turn.end:.1f}s {speaker}") # start=0.2s stop=1.6s SPEAKER_00 # start=1.8s stop=4.0s SPEAKER_01 # start=4.2s stop=5.6s SPEAKER_00 # ... ``` -------------------------------- ### Load AMI Protocol with pyannote.database Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/overlapped_speech_detection.ipynb This snippet demonstrates how to load the AMI corpus protocol using pyannote.database. It assumes the corpus is already set up and accessible. The preprocessor 'FileFinder' is used to locate audio files. ```python from pyannote.database import registry, FileFinder registry.load_database("AMI-diarization-setup/pyannote/database.yml") protocol = registry.get_protocol("AMI.SpeakerDiarization.mini", preprocessors={"audio": FileFinder()}) ``` -------------------------------- ### Load Dataset Protocol Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/training_a_model.ipynb Initializes the pyannote.database registry with a YAML configuration file and retrieves the AMI speaker diarization protocol. ```python from pyannote.database import registry, FileFinder registry.load_database("AMI-diarization-setup/pyannote/database.yml") ami = registry.get_protocol('AMI.SpeakerDiarization.mini') ``` -------------------------------- ### Load and Instantiate pyannote.audio Pipeline Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/community/eval_separation_pipeline.ipynb Loads a pre-trained speaker diarization pipeline from Hugging Face and configures its hyperparameters for optimal performance on the diarization task. It supports loading from a specified model name and uses authentication token if available. Dependencies include pyannote.audio and potentially torch. ```python from pyannote.audio import Pipeline import torch # Load the pipeline with authentication token pipeline = Pipeline.from_pretrained("pyannote/speech-separation-ami-1.0", token=True) # Instantiate with specific hyper-parameters for diarization pipeline.instantiate({ "segmentation": { "min_duration_off": 0.0, "threshold": 0.5, }, "clustering": { "method": "centroid", "threshold": 0.68, "min_cluster_size": 60, }, "separation": { "leakage_removal": True, "asr_collar": 0.0, } }) # Move pipeline to GPU if available, otherwise use CPU device = torch.device("cuda" if torch.cuda.is_available() else "cpu") pipeline.to(device=device) ``` -------------------------------- ### Evaluate Voice Activity Detection using DetectionErrorRate in Python Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/voice_activity_detection.ipynb This code snippet demonstrates how to evaluate a voice activity detection (VAD) pipeline using the DetectionErrorRate metric from pyannote.metrics. It iterates through a test dataset, applies the pipeline to get speech segments, and compares them against reference annotations. The final detection error rate is then calculated and printed. ```python from pyannote.metrics.detection import DetectionErrorRate metric = DetectionErrorRate() for file in ami.test(): # apply the voice activity detection pipeline speech = pipeline(file) # evaluate its output _ = metric( file['annotation'], # this is the reference annotation speech, # this is the hypothesized annotation uem=file['annotated']) # this is the part of the file that should be evaluated # aggregate the performance over the whole test set detection_error_rate = abs(metric) print(f'Detection error rate = {detection_error_rate * 100:.1f}%') ``` -------------------------------- ### Load Database and Protocol Source: https://github.com/pyannote/pyannote-audio/blob/main/notebook/example.ipynb Loads a specified audio database and protocol using pyannote.database. It configures audio preprocessors, such as FileFinder, and handles potential warnings regarding speaker label scope. ```python from pyannote.database import registry, FileFinder registry.load_database('../tests/data/database.yml') protocol = registry.get_protocol('Debug.SpeakerDiarization.Debug', preprocessors={"audio": FileFinder()}) ``` -------------------------------- ### Load and Use Pretrained Models Source: https://context7.com/pyannote/pyannote-audio/llms.txt Shows how to load pretrained models from HuggingFace, move them to GPU, and use them for segmentation or embedding extraction. ```python import torch from pyannote.audio import Model, Inference # Load pretrained segmentation model model = Model.from_pretrained("pyannote/segmentation-3.0", token="YOUR_HUGGINGFACE_TOKEN") model.to(torch.device("cuda")).eval() # Create inference wrapper inference = Inference(model, duration=10.0, step=1.0, batch_size=32) segmentation = inference("audio.wav") # Extract embedding embedding_model = Model.from_pretrained("pyannote/embedding", token="YOUR_HUGGINGFACE_TOKEN") waveform = torch.randn(1, 1, 16000 * 5) with torch.no_grad(): embedding = embedding_model(waveform) ``` -------------------------------- ### POST /inference/run Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/applying_a_model.ipynb Run inference on an audio file using a loaded model. ```APIDOC ## POST /inference/run ### Description Wraps a model in an Inference instance to process audio files and generate frame-wise predictions. ### Method POST (Internal Python API) ### Parameters #### Request Body - **model** (Object) - Required - The loaded model instance. - **step** (float) - Optional - The sliding window step size in seconds. - **audio_file** (string) - Required - Path to the input audio file. ### Request Example ```python from pyannote.audio import Inference inference = Inference(model, step=2.5) output = inference(AUDIO_FILE) ``` ### Response #### Success Response (200) - **output** (SlidingWindowFeature) - The model output containing probability vectors for each time frame. ``` -------------------------------- ### Load AMI Diarization Protocol with pyannote.database Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/voice_activity_detection.ipynb Loads the AMI diarization protocol and sets up preprocessors for audio files. This is the initial step for accessing and preparing the dataset for training. ```python from pyannote.database import registry, FileFinder registry.load_database("AMI-diarization-setup/pyannote/database.yml") preprocessors = {"audio": FileFinder()} ami = registry.get_protocol('AMI.SpeakerDiarization.mini', preprocessors=preprocessors) ``` -------------------------------- ### Download and Verify AMI-SDM Corpus Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/adapting_pretrained_pipeline.ipynb Downloads the AMI-SDM mini corpus for testing and verifies the dataset structure using the pyannote-database CLI tool. This ensures the data is correctly formatted for the pipeline. ```bash %cd /content/ !git clone https://github.com/pyannote/AMI-diarization-setup %cd /content/AMI-diarization-setup/pyannote/ !bash download_ami_sdm_mini.sh !PYANNOTE_DATABASE_CONFIG="/content/AMI-diarization-setup/pyannote/database.yml" pyannote-database info AMI-SDM.SpeakerDiarization.mini ``` -------------------------------- ### Process Audio from Memory Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/applying_a_pipeline.ipynb Shows how to load audio into a torch tensor and pass it to a pipeline using a dictionary format instead of a file path. ```python import torchaudio waveform, sample_rate = torchaudio.load(AUDIO_FILE) audio_in_memory = {"waveform": waveform, "sample_rate": sample_rate} from pyannote.audio import Pipeline vad = Pipeline.from_pretrained("pyannote/voice-activity-detection", token=True) vad(audio_in_memory) ``` -------------------------------- ### Load Pretrained Pipeline with pyannote.audio Source: https://context7.com/pyannote/pyannote-audio/llms.txt Loads a pretrained speaker diarization pipeline from Hugging Face Hub or a local checkpoint. This is the main entry point for using pyannote.audio's functionalities. It requires accepting user conditions and a Hugging Face token for remote models. The pipeline can be moved to a GPU for faster processing and applied to audio files with optional progress tracking. ```python import torch from pyannote.audio import Pipeline from pyannote.audio.pipelines.utils.hook import ProgressHook # Load community speaker diarization pipeline from Hugging Face Hub # Requires accepting user conditions at https://hf.co/pyannote/speaker-diarization-community-1 pipeline = Pipeline.from_pretrained( "pyannote/speaker-diarization-community-1", token="YOUR_HUGGINGFACE_TOKEN" ) # Send pipeline to GPU for faster processing pipeline.to(torch.device("cuda")) # Apply pipeline to audio file with progress tracking with ProgressHook() as hook: output = pipeline("audio.wav", hook=hook) # Access diarization results for turn, speaker in output.speaker_diarization: print(f"start={turn.start:.1f}s stop={turn.end:.1f}s {speaker}") # Access speaker embeddings (one per detected speaker) embeddings = output.speaker_embeddings # (num_speakers, dimension) numpy array # Load from local config.yaml for offline use offline_pipeline = Pipeline.from_pretrained("/path/to/config.yaml") ``` -------------------------------- ### Utilize Progress and Timing Hooks in pyannote.audio Pipelines Source: https://context7.com/pyannote/pyannote-audio/llms.txt This snippet demonstrates how to use hooks to monitor pipeline progress, collect intermediate artifacts, and measure the execution time of different pipeline steps. It covers `ProgressHook`, `TimingHook`, and `ArtifactHook`, showing how to integrate them using `with` statements. ```python from pyannote.audio import Pipeline from pyannote.audio.pipelines.utils.hook import ( ProgressHook, TimingHook, ArtifactHook, Hooks ) pipeline = Pipeline.from_pretrained( "pyannote/speaker-diarization-community-1", token="YOUR_HUGGINGFACE_TOKEN" ) # Show progress bar during processing with ProgressHook() as hook: output = pipeline("audio.wav", hook=hook) # Displays: # segmentation ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:05 # embeddings ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:02 # Measure timing of each pipeline step file = {"audio": "audio.wav", "uri": "test"} with TimingHook() as hook: output = pipeline(file, hook=hook) timing = file["timing"] print(f"Total time: {timing['total']:.2f}s") print(f"Segmentation: {timing.get('segmentation', 0):.2f}s") print(f"Embeddings: {timing.get('embeddings', 0):.2f}s") # Collect intermediate artifacts for debugging with ArtifactHook() as hook: output = pipeline(file, hook=hook) artifacts = file["artifact"] print(f"Available artifacts: {list(artifacts.keys())}") # ['segmentation', 'speaker_counting', 'embeddings', 'discrete_diarization'] # Combine multiple hooks with Hooks(ProgressHook(), TimingHook(), ArtifactHook()) as hook: output = pipeline(file, hook=hook) ``` -------------------------------- ### Listen to and Visualize Audio File Annotation Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/overlapped_speech_detection.ipynb This snippet utilizes the 'listen' utility from pyannote.audio to play the audio of a given file and visualize its reference annotation. This is useful for understanding the ground truth of the audio data. ```python from pyannote.audio.utils.preview import listen listen(first_training_file) ``` -------------------------------- ### List Available Tasks in pyannote.audio (Python) Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/add_your_own_task.ipynb Prints a list of all available tasks within the pyannote.audio.tasks module. This is useful for discovering pre-defined tasks. ```python from pyannote.audio.tasks import __all__ as TASKS; print('\n'.join(TASKS)) ``` -------------------------------- ### Prepare Inference Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/overlapped_speech_detection.ipynb Selects a test file from the protocol to be used for model inference. ```python test_file = next(protocol.test()) ``` -------------------------------- ### Load Pre-trained Pyannote Audio Model Source: https://github.com/pyannote/pyannote-audio/blob/main/notebook/sharing.ipynb Loads a pre-trained pyannote.audio model from a local checkpoint file or a URL. It uses the Model.from_pretrained method, which relies on PyTorch Lightning's loading mechanism. This is useful for resuming training or using a model for inference without retraining. ```python from pyannote.audio import Model model = Model.from_pretrained('sharing/lightning_logs/version_0/checkpoints/epoch=0-step=3.ckpt') assert isinstance(model, SimpleSegmentationModel) # checkpoint should work with a URL as well (it relies on pl_load) ``` -------------------------------- ### Perform Inference on Audio File - Python Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/voice_activity_detection.ipynb This snippet demonstrates how to load a trained model and apply it to an audio file for inference. It uses a test file provided by the AMI protocol but can be adapted for any audio file. ```python test_file = next(ami.test()) # here we use a test file provided by the protocol, but it could be any audio file ``` -------------------------------- ### Load AMI Protocol for Voice Activity Detection (Python) Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/add_your_own_task.ipynb Loads the AMI corpus protocol using pyannote.database. It assumes the AMI corpus has been set up and then retrieves the 'AMI.SpeakerDiarization.mini' protocol, configuring the audio preprocessor to use FileFinder. ```python # this assumes that the AMI corpus has been setup for diarization # according to https://github.com/pyannote/AMI-diarization-setup from pyannote.database import registry, FileFinder registry.load_database("AMI-diarization-setup/pyannote/database.yml") ami = registry.get_protocol('AMI.SpeakerDiarization.mini', preprocessors={'audio': FileFinder()}) # address voice activity detection from pyannote.audio.tasks import VoiceActivityDetection task = VoiceActivityDetection(ami) ``` -------------------------------- ### Load Audio and Annotations with pyannote.audio Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/community/eval_separation_pipeline.ipynb Loads an audio file and its corresponding reference RTTM annotations using pyannote.audio utilities. It prepares the audio data by cropping it to a specific segment and ensures the annotations align with this segment. Dependencies include pyannote.database and pyannote.audio. ```python from pyannote.database.util import load_rttm from pyannote.audio.core.io import Audio from pyannote.core import Segment import os audio = Audio() uri = "ES2004a" # original file name segment = Segment(750, 750+60) # Example segment for a 1-minute chunk starting at 750s file = os.environ.get("ASSET_DIR", ".") + "/mixture.wav" mixture, sample_rate = audio.crop(file=file, segment=segment) annotations = load_rttm(os.environ.get("ASSET_DIR", ".") + "/mixture.rttm")[uri] annotations = annotations.crop(segment) ``` -------------------------------- ### Define Custom Model - pyannote.audio Source: https://github.com/pyannote/pyannote-audio/blob/main/tutorials/add_your_own_model.ipynb Demonstrates the initial steps for defining a custom model in pyannote.audio. It shows the necessary imports and the base class structure required for a custom model, which must inherit from `pyannote.audio.Model`. ```python from typing import Optional import torch import torch.nn as nn from pyannote.audio import Model from pyannote.core import SlidingWindow from pyannote.audio.core.task import Task, Resolution from torchaudio.transforms import MFCC # Your custom model must be a subclass of `pyannote.audio.Model`, # which is a subclass of `pytorch_lightning.LightningModule`, ``` -------------------------------- ### Applying Augmented Model Source: https://github.com/pyannote/pyannote-audio/blob/main/notebook/augmentation.ipynb Shows how to pass audio through the model with the registered augmentation and play the result. ```APIDOC ## Applying Augmented Model ### Description Passes the audio waveform through the `identity` model, which now includes the `Gain` augmentation, and plays the potentially modified output. ### Method N/A (Python Script) ### Endpoint N/A ### Parameters N/A ### Request Example ```python from IPython.display import Audio as Play # Assuming 'identity' model (with augmentation) and 'waveforms', 'sample_rate' are defined Play(identity(waveforms).squeeze(), rate=sample_rate, normalize=False, autoplay=True) ``` ### Response #### Success Response (200) Plays the audio output from the augmented model. The audio may have applied gain. #### Response Example N/A (Audio playback) ``` -------------------------------- ### Loading and Processing Audio Excerpts Source: https://github.com/pyannote/pyannote-audio/blob/main/notebook/augmentation.ipynb Demonstrates how to load a dataset protocol, access audio files, and extract a specific segment of audio. ```APIDOC ## Loading and Processing Audio Excerpts ### Description This section shows how to get a specific segment of an audio file using pyannote.database and pyannote.audio. ### Method N/A (Python Script) ### Endpoint N/A ### Parameters N/A ### Request Example ```python from pyannote.database import get_protocol, FileFinder from pyannote.audio.core.io import Audio from pyannote.core import Segment protocol = get_protocol('Debug.SpeakerDiarization.Debug', preprocessors={"audio": FileFinder()}) audio = Audio(sample_rate=16000, mono="downmix") file = next(protocol.test()) waveform, sample_rate = audio.crop(file, Segment(5, 10)) import torch waveforms = torch.tensor(waveform)[None, :] ``` ### Response #### Success Response (200) - `waveform` (numpy.ndarray): The audio waveform data for the specified segment. - `sample_rate` (int): The sample rate of the audio. #### Response Example ```json { "waveform": [array of audio samples], "sample_rate": 16000 } ``` ```