pyannote-audio

repository·main·Indexed 26 days ago

https://github.com/pyannote/pyannote-audio

A Python-based toolkit for state-of-the-art speaker diarization built on PyTorch. It provides pretrained models and pipelines, including the open-source community-1 pipeline and the premium precision-2 service via pyannoteAI. The toolkit includes features for sliding window inference, model fine-tuning, layer freezing, and a CLI for optimizing, benchmarking, and applying pipelines to audio files.

Tokens
19K
Snippets
60
Records
90
Agent score
94%

What's inside pyannote-audio

  1. Use gated models and pipelines offline

    main

    Gated models and pipelines can be used offline. While the initial authentication process (filling out gating forms) is required to access them, you do not need to go through the authentication process every time you run your application (e.g., in a docker run command) once they are accessed.

    For specific instructions on offline usage:

    • For models: see tutorials/applying_a_model.ipynb.
    • For pipelines: see tutorials/applying_a_pipeline.ipynb.
  2. Use the `precision-2` premium speaker diarization service

    main

    The precision-2 pipeline is a premium service that runs on pyannoteAI servers. To use it:

    1. Create a pyannoteAI API key at dashboard.pyannote.ai.
    2. Pass the API key to Pipeline.from_pretrained using the pyannote/speaker-diarization-precision-2 identifier.
    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}")
  3. Use the `community-1` open-source speaker diarization pipeline

    main

    The community-1 pipeline runs locally. To use it, you must:

    1. Accept the user conditions for pyannote/speaker-diarization-community-1 on Hugging Face.
    2. Create a Hugging Face access token at hf.co/settings/tokens.
    3. Pass the token to Pipeline.from_pretrained.

    You can optionally move the pipeline to a GPU using .to(torch.device("cuda")) and use a ProgressHook to monitor progress.

    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}")
  4. Set up pyannote.audio for development

    main

    To set up the environment for developing the pyannote.audio library, install the package in editable mode with development and testing dependencies, then install the pre-commit hooks.

    pip install -e .[dev,testing]
    pre-commit install
  5. Improve diarization performance via fine-tuning

    main

    To improve performance beyond the default pretrained models, you can fine-tune the pipeline using your own data. The recommended workflow is:

    1. Manually annotate dozens of conversations with high precision.
    2. Split the annotated data into training (80%), development (10%), and test (10%) subsets.
    3. Format the data for use with pyannote.database.
    4. Follow the recipe in tutorials/adapting_pretrained_pipeline.ipynb to adapt the pretrained pipeline to your specific domain.
  6. Apply pretrained pipelines to audio in memory

    main
    You can apply pretrained pipelines to audio data that is already loaded in memory rather than reading from a file. Refer to the tutorials/applying_a_pipeline.ipynb tutorial for implementation details.
  7. Configure telemetry settings

    main

    You can control the optional anonymous telemetry feature using environment variables, within a Python session, or globally across sessions. Telemetry tracks pipeline origin, class, and file duration/speaker counts, but does not track user identity.

    Environment Variable: Set PYANNOTE_METRICS_ENABLED to 1 to enable or 0 to disable.

    Python Session: Use set_telemetry_metrics(bool) from pyannote.audio.telemetry.

    Global Configuration: Use set_telemetry_metrics(bool, save_choice_as_default=True) to persist settings across sessions.

    # enable metrics
    export PYANNOTE_METRICS_ENABLED=1
    
    # disable metrics
    export PYANNOTE_METRICS_ENABLED=0
    from pyannote.audio.telemetry import set_telemetry_metrics
    
    # enable metrics for current session
    set_telemetry_metrics(True)
    
    # disable metrics globally
    set_telemetry_metrics(False, save_choice_as_default=True)
  8. Finetune a segmentation model for Overlapped Speech Detection

    main

    To finetune a model like pyannote/segmentation-3.0 for OSD, follow these steps:

    1. Initialize the OSD Task: Define the protocol, chunk duration, and batch size.
    2. Authenticate: Since official models are gated, use huggingface_hub.notebook_login() to access weights.
    3. Load Model and Assign Task: Load the pretrained model and explicitly assign the OSD task to it.
    4. Train: Use pytorch-lightning to fit the model.
    from pyannote.audio.tasks import OverlappedSpeechDetection
    from pyannote.audio.core.model import Model
    import pytorch_lightning as pl
    
    # 1. Initialize task
    osd = OverlappedSpeechDetection(protocol, duration=2., batch_size=16)
    
    # 2. Load model and assign task
    pretrained_model = Model.from_pretrained("pyannote/segmentation-3.0", token=True)
    pretrained_model.task = osd
    
    # 3. Train
    trainer = pl.Trainer(max_epochs=1)
    trainer.fit(pretrained_model)
    from pyannote.audio.tasks import OverlappedSpeechDetection
    from pyannote.audio.core.model import Model
    import pytorch_lightning as pl
    
    osd = OverlappedSpeechDetection(protocol, duration=2., batch_size=16)
    pretrained_model = Model.from_pretrained("pyannote/segmentation-3.0", token=True)
    pretrained_model.task = osd
    
    trainer = pl.Trainer(max_epochs=1)
    trainer.fit(pretrained_model)