### torch.device Examples Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/types.md Examples demonstrating how to create a torch.device object to specify computation devices like GPU or CPU. ```python device = torch.device("cuda") # GPU ``` ```python device = torch.device("cpu") # CPU ``` ```python device = torch.device("cuda:0") # Specific GPU ``` -------------------------------- ### Optimization with Custom Base Configuration Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/optimization.md This example shows how to initialize the `Optimizer` with a custom base configuration. This allows you to start the optimization process from a specific set of pre-defined pipeline parameters. ```APIDOC ## Usage Example: With Custom Base Configuration ```python from diart import SpeakerDiarization, SpeakerDiarizationConfig from diart.optim import Optimizer from diart.models import SegmentationModel # Start from a custom base configuration base_config = SpeakerDiarizationConfig( duration=3.0, # Shorter chunks for faster processing latency=1.0, max_speakers=10, ) optimizer = Optimizer( pipeline_class=SpeakerDiarization, speech_path="/audio", reference_path="/rttm", study_or_path="/results/study", base_config=base_config, do_kickstart_hparams=True, # Start with base config values ) optimizer(num_iter=50) ``` ``` -------------------------------- ### Basic Optimization Example Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/optimization.md This example demonstrates how to perform basic hyper-parameter optimization using the `Optimizer` class. It initializes the optimizer with a pipeline class, data paths, and a study path, then runs the optimization for a specified number of iterations and prints the best results. ```APIDOC ## Usage Example: Basic Optimization ```python from diart import SpeakerDiarization, SpeakerDiarizationConfig from diart.optim import Optimizer from pathlib import Path # Run optimization optimizer = Optimizer( pipeline_class=SpeakerDiarization, speech_path="/audio/training", reference_path="/rttm/training", study_or_path="/results/study", # Will create sqlite database batch_size=32, ) # Optimize for 100 iterations optimizer(num_iter=100) # Get best results print(f"Best DER: {optimizer.best_performance}%") print(f"Best params: {optimizer.best_hparams}") ``` ``` -------------------------------- ### Distributed Optimization Example Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/optimization.md This example illustrates how to set up distributed optimization across multiple machines. It involves creating an Optuna study in a shared database (e.g., MySQL/PostgreSQL) and then having each machine load and contribute to the same study. ```APIDOC ## Usage Example: Distributed Optimization Optimize across multiple machines using a shared database: ```python from diart.optim import Optimizer import optuna # Create study in MySQL/PostgreSQL db = "mysql://user:password@localhost/optimization" study = optuna.load_study( "my_study", db, optuna.samplers.TPESampler() ) # Each machine runs this with the same database optimizer = Optimizer( SpeakerDiarization, speech_path="/audio", reference_path="/rttm", study_or_path=study, ) optimizer(num_iter=25) # Each machine does 25 iterations ``` ``` -------------------------------- ### Optimizer with Custom Base Configuration Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/optimization.md Initialize the Optimizer starting from a custom base configuration, such as adjusting duration, latency, or max speakers. This is useful for guiding the optimization process. ```python base_config = SpeakerDiarizationConfig( duration=3.0, # Shorter chunks for faster processing latency=1.0, max_speakers=10, ) optimizer = Optimizer( pipeline_class=SpeakerDiarization, speech_path="/audio", reference_path="/rttm", study_or_path="/results/study", base_config=base_config, do_kickstart_hparams=True, # Start with base config values ) ``` -------------------------------- ### WebSocketAudioSource.read Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Starts the WebSocket server and begins listening for incoming audio chunks from clients. ```APIDOC ## WebSocketAudioSource.read ### Description Starts the WebSocket server and begins listening for incoming audio chunks from clients. ### Method POST ### Endpoint N/A (Method) ### Parameters None ### Request Example ```python source.read() ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Initialize MicrophoneAudioSource (Default Device) Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Example of initializing MicrophoneAudioSource using the default microphone device. ```python from diart.sources import MicrophoneAudioSource from diart import SpeakerDiarization from diart.inference import StreamingInference # Use default microphone source = MicrophoneAudioSource() ``` -------------------------------- ### FileAudioSource Usage Example Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Demonstrates how to create and use a FileAudioSource with SpeakerDiarization and StreamingInference. ```APIDOC ## FileAudioSource Usage Example ### Description Demonstrates how to create and use a FileAudioSource with SpeakerDiarization and StreamingInference. ### Usage Example ```python from diart.sources import FileAudioSource from diart import SpeakerDiarization from diart.inference import StreamingInference pipeline = SpeakerDiarization() # Create file source with 1 second left padding, 2 second right padding source = FileAudioSource( "audio.wav", sample_rate=16000, padding=(1.0, 2.0), block_duration=0.5 ) inference = StreamingInference(pipeline, source) predictions = inference() ``` ``` -------------------------------- ### Full Speaker Diarization Configuration Example Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/configuration.md An exhaustive example showing all possible parameters for SpeakerDiarizationConfig, including model loading, duration, step, latency, and various tuning thresholds. This demonstrates the full range of customization available. ```python # Equivalent to: config = SpeakerDiarizationConfig( segmentation=SegmentationModel.from_pyannote("pyannote/segmentation"), embedding=EmbeddingModel.from_pyannote("pyannote/embedding"), duration=3.0, step=0.5, latency=0.5, # Defaults to step tau_active=0.6, rho_update=0.3, delta_new=1.0, gamma=3.0, beta=10.0, max_speakers=20, normalize_embedding_weights=False, device=torch.device("cuda" if torch.cuda.is_available() else "cpu"), sample_rate=16000, ) ``` -------------------------------- ### Install Hugging Face CLI and Login Source: https://github.com/juanmc2005/diart/blob/main/README.md Before using pyannote models with Diart, install the huggingface-cli and log in with your Hugging Face user access token. This is required for accessing pre-trained models. ```shell huggingface-cli login ``` -------------------------------- ### FileAudioSource Usage Example Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Demonstrates how to initialize and use FileAudioSource with padding and block duration for streaming audio from a file into a SpeakerDiarization pipeline. ```python from diart.sources import FileAudioSource from diart import SpeakerDiarization from diart.inference import StreamingInference pipeline = SpeakerDiarization() # Create file source with 1 second left padding, 2 second right padding source = FileAudioSource( "audio.wav", sample_rate=16000, padding=(1.0, 2.0), block_duration=0.5 ) inference = StreamingInference(pipeline, source) predictions = inference() ``` -------------------------------- ### Install Diart with Conda Source: https://github.com/juanmc2005/diart/blob/main/README.md Use this command to create and activate a pre-configured conda environment for Diart. Ensure you have the necessary system dependencies installed first. ```shell conda env create -f diart/environment.yml conda activate diart ``` -------------------------------- ### Initialize MicrophoneAudioSource (Specific Device) Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Example of initializing MicrophoneAudioSource with a specific device ID. List available devices using 'python -m sounddevice'. ```python # Or specify a device (list devices with: python -m sounddevice) source = MicrophoneAudioSource(device=2) # Use device with ID 2 ``` -------------------------------- ### Install Diart with Pip Source: https://github.com/juanmc2005/diart/blob/main/README.md Install the Diart package using pip after ensuring system dependencies are met. This is a straightforward installation method. ```shell pip install diart ``` -------------------------------- ### Benchmark Usage Example Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/inference-and-streaming.md Demonstrates how to initialize and use the Benchmark class to run a SpeakerDiarization pipeline with a custom configuration and print the resulting performance report. ```python from diart import SpeakerDiarization, SpeakerDiarizationConfig from diart.inference import Benchmark # Create benchmark benchmark = Benchmark( speech_path="/audio/files", reference_path="/rttm/references", output_path="/output/predictions", batch_size=32 ) # Run with specific configuration config = SpeakerDiarizationConfig( tau_active=0.555, rho_update=0.422, delta_new=1.517 ) report = benchmark(SpeakerDiarization, config) print(report) ``` -------------------------------- ### Benchmark Diart Configuration (Shell) Source: https://github.com/juanmc2005/diart/blob/main/README.md Run a benchmark for Diart using the command line. This example configures parameters for DIHARD III and specifies the segmentation model. ```shell diart.benchmark /wav/dir --reference /rttm/dir --tau-active=0.555 --rho-update=0.422 --delta-new=1.517 --segmentation pyannote/segmentation@Interspeech2021 ``` -------------------------------- ### Start Diart WebSocket Server Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/INDEX.md Launch a WebSocket server for Diart to handle streaming audio input and return diarization results. Configure host and port for accessibility. ```bash # Start WebSocket server diart.serve --host 0.0.0.0 --port 7007 ``` -------------------------------- ### Initialize AppleDeviceAudioSource for Streaming Inference Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Example of initializing AppleDeviceAudioSource and integrating it with SpeakerDiarization and StreamingInference for real-time speaker diarization. ```python from diart.sources import AppleDeviceAudioSource from diart import SpeakerDiarization from diart.inference import StreamingInference source = AppleDeviceAudioSource(sample_rate=16000, device="0:0") pipeline = SpeakerDiarization() inference = StreamingInference(pipeline, source) predictions = inference() ``` -------------------------------- ### Recommended Starting Points for SpeakerDiarizationConfig Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/optimization.md Provides recommended hyper-parameter configurations for different datasets to optimize speaker diarization. ```APIDOC ## Recommended Starting Points ### DIHARD III (any latency) ```python config = SpeakerDiarizationConfig( tau_active=0.555, rho_update=0.422, delta_new=1.517, ) ``` ### AMI (any latency) ```python config = SpeakerDiarizationConfig( tau_active=0.507, rho_update=0.006, delta_new=1.057, ) ``` ### VoxConverse (any latency) ```python config = SpeakerDiarizationConfig( tau_active=0.576, rho_update=0.915, delta_new=0.648, ) ``` ### DIHARD II (1 second latency) ```python config = SpeakerDiarizationConfig( step=0.5, latency=1.0, tau_active=0.619, rho_update=0.326, delta_new=0.997, ) ``` ### DIHARD II (5 second latency) ```python config = SpeakerDiarizationConfig( tau_active=0.555, rho_update=0.422, delta_new=1.517, ) ``` ``` -------------------------------- ### DelayedAggregation Usage Example Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/blocks-and-operators.md Demonstrates how to use DelayedAggregation to aggregate overlapping windows. Requires pyannote.core and numpy for creating sample buffers. ```python from diart.blocks import DelayedAggregation from pyannote.core import SlidingWindow, SlidingWindowFeature, Segment import numpy as np dagg = DelayedAggregation(step=0.5, latency=1.0, strategy="hamming") # Create sample buffers frames = 500 speakers = 2 resolution = 5.0 / frames # 5 second duration with 500 frames buffers = [ SlidingWindowFeature( np.random.rand(frames, speakers), SlidingWindow(start=i*0.5, duration=resolution, step=resolution) ) for i in range(dagg.num_overlapping_windows) ] result = dagg(buffers) print(result.data.shape) # Aggregated result ``` -------------------------------- ### Optimization with Custom Hyper-parameters Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/optimization.md This example shows how to specify a custom list of `HyperParameter` objects to optimize. This allows you to fine-tune specific parameters of the pipeline rather than optimizing all available parameters. ```APIDOC ## Usage Example: Custom Hyper-parameters ```python from diart import SpeakerDiarization, SpeakerDiarizationConfig from diart.blocks.base import HyperParameter from diart.optim import Optimizer # Define custom hyper-parameters to optimize custom_params = [ HyperParameter(name="tau_active", low=0.4, high=0.8), HyperParameter(name="rho_update", low=0.0, high=0.5), # Don't optimize delta_new ] optimizer = Optimizer( pipeline_class=SpeakerDiarization, speech_path="/audio", reference_path="/rttm", study_or_path="/results/study", hparams=custom_params, ) optimizer(num_iter=100) ``` ``` -------------------------------- ### AMI Configuration Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/optimization.md Recommended starting point for SpeakerDiarizationConfig for AMI dataset with any latency. ```python config = SpeakerDiarizationConfig( tau_active=0.507, rho_update=0.006, delta_new=1.057, ) ``` -------------------------------- ### VoxConverse Configuration Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/optimization.md Recommended starting point for SpeakerDiarizationConfig for VoxConverse dataset with any latency. ```python config = SpeakerDiarizationConfig( tau_active=0.576, rho_update=0.915, delta_new=0.648, ) ``` -------------------------------- ### MicrophoneAudioSource Read Method Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Starts the microphone stream to capture and emit audio chunks. ```python def read(self) ``` -------------------------------- ### SpeakerSegmentation Usage Example Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/blocks-and-operators.md Demonstrates how to initialize and use the SpeakerSegmentation block for processing single audio files or batches. Ensure the model is loaded and moved to the appropriate device (e.g., 'cuda'). ```python from diart.blocks import SpeakerSegmentation import torch segmentation = SpeakerSegmentation.from_pretrained("pyannote/segmentation") segmentation = segmentation.to("cuda") # Single audio waveform = torch.randn(1, 16000*5) # 5 seconds at 16kHz output = segmentation(waveform) print(output.shape) # (batch=1, frames, num_speakers) # Batch of audio batch = torch.randn(4, 1, 16000*5) # 4 samples output = segmentation(batch) print(output.shape) # (4, frames, num_speakers) ``` -------------------------------- ### DIHARD III Configuration Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/optimization.md Recommended starting point for SpeakerDiarizationConfig for DIHARD III dataset with any latency. ```python config = SpeakerDiarizationConfig( tau_active=0.555, rho_update=0.422, delta_new=1.517, ) ``` -------------------------------- ### EmbeddingModel.from_pyannote Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/models.md Creates an EmbeddingModel instance from a pyannote.audio model available on Hugging Face Hub. Requires pyannote.audio to be installed. ```APIDOC ## EmbeddingModel.from_pyannote ### Description Create an EmbeddingModel from a pyannote.audio model on Hugging Face Hub. ### Method `EmbeddingModel.from_pyannote( model: str, use_hf_token: Union[Text, bool, None] = True, ) -> EmbeddingModel` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **model** (str) - Required - Model identifier (e.g., "pyannote/embedding"). - **use_hf_token** (str | bool | None) - Optional - Hugging Face access token. Defaults to True. ### Returns `EmbeddingModel` instance. ### Raises `AssertionError` if pyannote.audio is not installed. ``` -------------------------------- ### Benchmark Speaker Diarization on Multiple Files Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/inference-and-streaming.md This example shows how to benchmark the speaker diarization pipeline across multiple audio files. It requires specifying paths for speech, reference RTTM files, and an output directory for results. ```python # Option 2: Benchmark on multiple files benchmark = Benchmark( speech_path="/data/audio", reference_path="/data/rttm", output_path="/results", batch_size=32 ) report = benchmark(SpeakerDiarization, config) print(report) ``` -------------------------------- ### Create SegmentationModel from Hugging Face Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/models.md Use this method to load a pre-trained segmentation model from Hugging Face Hub. Ensure pyannote.audio is installed. The model is moved to CUDA if available and set to evaluation mode. ```python from diart.models import SegmentationModel import torch # Load from Hugging Face model = SegmentationModel.from_pretrained("pyannote/segmentation") model = model.to("cuda") model.eval() # Run on audio batch waveform = torch.randn(2, 1, 16000*5) # 2 samples, 5 seconds at 16kHz segmentation = model(waveform) print(segmentation.shape) # (2, frames, num_speakers) ``` -------------------------------- ### Create EmbeddingModel from Hugging Face Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/models.md Use this method to load a pre-trained speaker embedding model from Hugging Face Hub. Ensure pyannote.audio is installed. The model can be moved to a CUDA-enabled GPU for faster inference. ```python from diart.models import EmbeddingModel import torch # Load from Hugging Face model = EmbeddingModel.from_pretrained("pyannote/embedding") model.to("cuda") ``` -------------------------------- ### Initialize StreamingInference with Observers Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/inference-and-streaming.md Shows how to initialize `StreamingInference` and attach RxPY observers for processing outputs. This example attaches an `RTTMWriter` to save predictions to an RTTM file and a `PredictionAccumulator` to collect all predictions in memory. ```python from diart.inference import StreamingInference from diart.sinks import RTTMWriter, PredictionAccumulator inference = StreamingInference(pipeline, source) # Write RTTM file inference.attach_observers(RTTMWriter(source.uri, "output.rttm")) # Accumulate all predictions accumulator = PredictionAccumulator() inference.attach_observers(accumulator) predictions = inference() final_prediction = accumulator.get_prediction() ``` -------------------------------- ### Create Optuna Study for Distributed Tuning Source: https://github.com/juanmc2005/diart/blob/main/README.md Before distributed tuning, create a database and an Optuna study using a DBMS like MySQL. Ensure study and database names match. ```shell mysql -u root -e "CREATE DATABASE IF NOT EXISTS example" optuna create-study --study-name "example" --storage "mysql://root@localhost/example" ``` -------------------------------- ### Initialize WebSocketAudioSource Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Instantiate WebSocketAudioSource to receive audio over WebSockets. Configure sample rate, host, port, and optional SSL certificates for secure connections. ```python source = WebSocketAudioSource( sample_rate=16000, host="0.0.0.0", port=7007 ) ``` -------------------------------- ### SpeakerDiarizationPipeline.get_config_class Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/speaker-diarization-pipeline.md Get the configuration class for the speaker diarization pipeline. ```APIDOC ## SpeakerDiarizationPipeline.get_config_class ### Description Get the configuration class for this pipeline. ### Method `@staticmethod def get_config_class() -> type` ### Returns `type` - The configuration class for this pipeline. ``` -------------------------------- ### WebSocketAudioSource.duration Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Gets the duration of the audio source. For WebSocket streams, the duration is unknown. ```APIDOC ## WebSocketAudioSource.duration ### Description Gets the duration of the audio source. For WebSocket streams, the duration is unknown. ### Method GET ### Endpoint N/A (Property) ### Parameters None ### Request Example ```python duration = source.duration ``` ### Response #### Success Response (200) - **duration** (Optional[float]) - None (WebSocket stream has unknown duration). #### Response Example ```json null ``` ``` -------------------------------- ### Distributed Tuning from Command Line Source: https://github.com/juanmc2005/diart/blob/main/README.md Run `diart.tune` with the `--storage` option pointing to your distributed Optuna database to enable parallel optimization processes. ```shell diart.tune /wav/dir --reference /rttm/dir --storage mysql://root@localhost/example ``` -------------------------------- ### Initialize Optimizer Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/optimization.md Instantiate the Optimizer with pipeline class, data paths, and study configuration. Specify batch size and optionally provide hyper-parameters, base configuration, metric, and optimization direction. ```python optimizer = Optimizer( pipeline_class=SpeakerDiarization, speech_path="/audio/training", reference_path="/rttm/training", study_or_path="/results/study", # Will create sqlite database batch_size=32, ) ``` -------------------------------- ### PredictionAccumulator Get Prediction Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/inference-and-streaming.md Retrieves the accumulated annotation with all predictions stitched together. ```python def get_prediction(self) -> Annotation: ``` -------------------------------- ### Get VoiceActivityDetection Configuration Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/voice-activity-detection.md Retrieves the configuration object associated with the VoiceActivityDetection pipeline. ```python @property def config(self) -> VoiceActivityDetectionConfig ``` -------------------------------- ### WebSocketAudioSource Constructor Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Initializes a new instance of the WebSocketAudioSource class. This sets up the WebSocket server to listen for incoming audio data. ```APIDOC ## WebSocketAudioSource Constructor ### Description Initializes a new instance of the WebSocketAudioSource class. This sets up the WebSocket server to listen for incoming audio data. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **sample_rate** (int) - Required - Sample rate of chunks emitted (must match client rate). - **host** (str) - Optional - Host address to bind the WebSocket server. Use "0.0.0.0" for all interfaces. Default: "127.0.0.1". - **port** (int) - Optional - Port for the WebSocket server. Default: 7007. - **key** (`str` | `Path` | `None`) - Optional - Path to SSL private key for secure connections. Default: None. - **certificate** (`str` | `Path` | `None`) - Optional - Path to SSL certificate for secure connections. Default: None. ### Request Example ```python WebSocketAudioSource(sample_rate=16000, host="0.0.0.0", port=7007) ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Get Configuration Class for VoiceActivityDetection Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/voice-activity-detection.md Returns the configuration class used by the VoiceActivityDetection pipeline. ```python @staticmethod def get_config_class() -> type ``` -------------------------------- ### Build Custom Pipeline with Microphone Source Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/blocks-and-operators.md Demonstrates how to create a custom audio processing pipeline using microphone input, segmentation, and embedding blocks. Ensure necessary models are downloaded before running. ```python import rx.operators as ops import diart.operators as dops from diart.blocks import SpeakerSegmentation, OverlapAwareSpeakerEmbedding from diart.sources import MicrophoneAudioSource # Create blocks segmentation = SpeakerSegmentation.from_pretrained("pyannote/segmentation") embedding = OverlapAwareSpeakerEmbedding.from_pretrained("pyannote/embedding") source = MicrophoneAudioSource() # Build custom pipeline stream = source.stream.pipe( # Rearrange to 5-second windows with 0.5s step dops.rearrange_audio_stream(5, 0.5, 16000), # Compute segmentation ops.map(lambda wav: (wav, segmentation(wav.unsqueeze(0)))), # Compute embeddings ops.starmap(embedding), ).subscribe(on_next=lambda emb: print(emb.shape)) source.read() ``` -------------------------------- ### Benchmark Class Constructor Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/inference-and-streaming.md Initializes the Benchmark class with paths for audio, references, and output, along with configuration options for progress display, report generation, and batch size. ```python class Benchmark: def __init__( self, speech_path: Union[Text, Path], reference_path: Optional[Union[Text, Path]] = None, output_path: Optional[Union[Text, Path]] = None, show_progress: bool = True, show_report: bool = True, batch_size: int = 32, ): ... ``` -------------------------------- ### EmbeddingModel.from_onnx Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/models.md Creates an EmbeddingModel instance from an ONNX model file. Requires ONNX runtime to be installed. ```APIDOC ## EmbeddingModel.from_onnx ### Description Create an EmbeddingModel from an ONNX model file. ### Method `EmbeddingModel.from_onnx( model_path: Union[str, Path], input_names: List[str] | None = None, output_name: str = "embedding", ) -> EmbeddingModel` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **model_path** (str | Path) - Required - Path to the ONNX model file. - **input_names** (List[str] | None) - Optional - Names of input tensors in the ONNX model. Defaults to ["waveform", "weights"]. - **output_name** (str) - Optional - Name of the output tensor. Defaults to "embedding". ### Returns `EmbeddingModel` instance. ### Raises `AssertionError` if ONNX runtime is not installed. ``` -------------------------------- ### Distributed Tuning from Python Source: https://github.com/juanmc2005/diart/blob/main/README.md Load an existing Optuna study from a database and initialize the `Optimizer` with the study object for distributed hyper-parameter tuning. ```python from diart.optim import Optimizer from optuna.samplers import TPESampler import optuna db = "mysql://root@localhost/example" study = optuna.load_study("example", db, TPESampler()) optimizer = Optimizer("/wav/dir", "/rttm/dir", study) optimizer(num_iter=100) ``` -------------------------------- ### Real-time Application Configuration Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/configuration.md Configure for low latency and frequent updates. Use shorter duration and step values, and minimize latency. ```python from diart import SpeakerDiarization, SpeakerDiarizationConfig config = SpeakerDiarizationConfig( duration=2.0, # Shorter chunks step=0.2, # More frequent updates latency=0.2, # Minimize latency tau_active=0.6, # Default threshold max_speakers=4, # Limited speakers ) pipeline = SpeakerDiarization(config) ``` -------------------------------- ### Get Hyper-parameters for VoiceActivityDetection Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/voice-activity-detection.md Lists the tunable hyper-parameters for the VoiceActivityDetection pipeline, which currently includes 'tau_active'. ```python @staticmethod def hyper_parameters() -> Sequence[HyperParameter] ``` -------------------------------- ### Get Speaker Diarization Pipeline Configuration Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/speaker-diarization-pipeline.md Access the current configuration object of the pipeline. This is a read-only property. ```python pipeline.config ``` -------------------------------- ### Parameter Tuning with Optimizer Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/README.md Sets up and runs an optimizer to tune speaker diarization parameters. Ensure audio and reference paths are valid. ```python from diart.optim import Optimizer optimizer = Optimizer( pipeline_class=SpeakerDiarization, speech_path="/audio", reference_path="/rttm", study_or_path="/results/study", ) optimizer(num_iter=100) ``` -------------------------------- ### Basic Diart Optimization Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/optimization.md Run basic optimization from the command line, specifying input audio, reference RTTM, and output directories. ```bash diart.tune /audio/dir --reference /rttm/dir --output /results/dir ``` -------------------------------- ### Tune Hyper-parameters from Python Source: https://github.com/juanmc2005/diart/blob/main/README.md Instantiate the `Optimizer` class with input, reference, and output directories. Call the optimizer instance with the desired number of iterations. ```python from diart.optim import Optimizer optimizer = Optimizer("/wav/dir", "/rttm/dir", "/output/dir") optimizer(num_iter=100) ``` -------------------------------- ### Synchronize Pipeline with Padding Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Shows how to use padding with FileAudioSource for pipeline synchronization. This is crucial when dealing with streaming audio or when specific processing durations are required. ```python from diart import SpeakerDiarization, SpeakerDiarizationConfig from diart.sources import FileAudioSource from diart.inference import StreamingInference config = SpeakerDiarizationConfig(duration=5.0, latency=1.0) pipeline = SpeakerDiarization(config) # Get padding for this file padding = pipeline.config.get_file_padding("audio.wav") # Create source with padding source = FileAudioSource("audio.wav", 16000, padding=padding) inference = StreamingInference(pipeline, source) predictions = inference() ``` -------------------------------- ### Benchmark __call__ Method Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/inference-and-streaming.md Runs a specified pipeline class with a given configuration on all audio files. Optionally evaluates performance using a provided metric. Returns a DataFrame with performance metrics or a list of predictions. ```python def __call__( self, pipeline_class: type, config: PipelineConfig, metric: Optional[BaseMetric] = None, ) -> Union[pd.DataFrame, List[Annotation]]: ... ``` -------------------------------- ### Get Tunable Hyper-parameters for Speaker Diarization Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/speaker-diarization-pipeline.md Lists the tunable hyper-parameters for the speaker diarization pipeline. These parameters can be adjusted to optimize performance. ```python SpeakerDiarizationPipeline.hyper_parameters() ``` -------------------------------- ### DIHARD II (1s Latency) Configuration Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/optimization.md Recommended starting point for SpeakerDiarizationConfig for DIHARD II dataset with 1 second latency. ```python config = SpeakerDiarizationConfig( step=0.5, latency=1.0, tau_active=0.619, rho_update=0.326, delta_new=0.997, ) ``` -------------------------------- ### Tune Hyper-parameters from Command Line Source: https://github.com/juanmc2005/diart/blob/main/README.md Use the `diart.tune` command to optimize pipeline hyper-parameters. Specify directories for audio, references, and output. See `diart.tune -h` for more options. ```shell diart.tune /wav/dir --reference /rttm/dir --output /output/dir ``` -------------------------------- ### Import Audio Input Sources Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/README.md Imports various audio source classes for reading audio data. ```python from diart.sources import ( FileAudioSource, MicrophoneAudioSource, WebSocketAudioSource, ) ``` -------------------------------- ### Create EmbeddingModel from ONNX Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/models.md Load an EmbeddingModel from an ONNX file. Specify input and output tensor names if they differ from the defaults. Ensure ONNX runtime is installed. ```python # Load ONNX embedding model onnx_embedding = EmbeddingModel.from_onnx( "embedding.onnx", input_names=["audio", "speaker_weights"], output_name="output" ) output = onnx_embedding(waveform, weights) ``` -------------------------------- ### Extract Speaker Embeddings Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/models.md Call the model with a waveform tensor to extract speaker embeddings. Optionally, provide weights for weighted pooling to get per-speaker embeddings. ```python # Extract embeddings without weights waveform = torch.randn(1, 1, 16000*5) embeddings = model(waveform) print(embeddings.shape) # (1, 512) - single embedding # Extract per-speaker embeddings with weights weights = torch.rand(1, 500) # (batch=1, frames=500) per_speaker_embeddings = model(waveform, weights) print(per_speaker_embeddings.shape) # (1, num_speakers, 512) ``` -------------------------------- ### Benchmark run_single Method Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/inference-and-streaming.md Executes a diarization pipeline on a single audio file without altering the pipeline's internal state. Requires a pipeline instance, the audio file path, and a progress bar object. ```python def run_single( self, pipeline: Pipeline, filepath: Path, progress_bar: ProgressBar, ) -> Annotation: ... ``` -------------------------------- ### SpeakerEmbedding Usage Example Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/blocks-and-operators.md Shows how to use the SpeakerEmbedding block to extract speaker embeddings from audio. It covers both single embedding extraction and per-speaker embeddings using weights. ```python from diart.blocks import SpeakerEmbedding import torch embedding = SpeakerEmbedding.from_pretrained("pyannote/embedding") waveform = torch.randn(1, 1, 16000*5) # Single embedding for entire audio emb = embedding(waveform) print(emb.shape) # (1, 512) # Per-speaker embeddings with weights weights = torch.rand(1, 500) # Soft weights per frame per_speaker_emb = embedding(waveform, weights) print(per_speaker_emb.shape) # (1, num_speakers, 512) ``` -------------------------------- ### Initialize StreamingInference with Hooks Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/inference-and-streaming.md Demonstrates how to initialize `StreamingInference` with a pipeline and audio source, and attach a custom hook function to process each output. The hook receives the diarization annotation and the corresponding audio chunk. ```python from diart import SpeakerDiarization from diart.sources import FileAudioSource from diart.inference import StreamingInference pipeline = SpeakerDiarization() source = FileAudioSource("audio.wav", 16000) inference = StreamingInference( pipeline, source, batch_size=1, do_profile=True, show_progress=True ) # Add custom processing hook def log_output(output): annotation, audio = output print(f"Got {len(annotation.itertracks())} speakers") inference.attach_hooks(log_output) # Run inference predictions = inference() ``` -------------------------------- ### Get Speaker Diarization Configuration Class Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/speaker-diarization-pipeline.md Retrieves the configuration class used by the speaker diarization pipeline. This static method allows inspection or instantiation of the configuration object. ```python SpeakerDiarizationPipeline.get_config_class() ``` -------------------------------- ### Distributed Diart Optimization with Database Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/optimization.md Configure distributed optimization using a database backend for storage. ```bash diart.tune /audio/dir --reference /rttm/dir \ --storage mysql://user:pass@localhost/optimization ``` -------------------------------- ### SegmentationModel.from_pyannote Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/models.md Create a SegmentationModel from a pyannote.audio model on Hugging Face Hub. ```APIDOC ## SegmentationModel.from_pyannote ### Description Create a SegmentationModel from a pyannote.audio model on Hugging Face Hub. ### Method `@staticmethod` ### Parameters #### Path Parameters - **model** (str) - Required - Model identifier (e.g., "pyannote/segmentation", "pyannote/segmentation-3.0"). - **use_hf_token** (str | bool | None) - Optional - Hugging Face access token. If True, use CLI login token. Default: True ### Returns `SegmentationModel` instance. ### Raises `AssertionError` if pyannote.audio is not installed. ``` -------------------------------- ### Create SegmentationModel from ONNX file Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/models.md Use this method to load a segmentation model saved in ONNX format. Ensure ONNX runtime is installed. The model can then be used to process audio waveforms. ```python onnx_model = SegmentationModel.from_onnx("model.onnx") output = onnx_model(waveform) ``` -------------------------------- ### Optimization Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/README.md Documentation for hyper-parameter tuning with `Optimizer` and distributed optimization strategies. ```APIDOC ## Optimization ### Description This section covers advanced optimization techniques within diart, focusing on hyper-parameter tuning using the `Optimizer` class and strategies for distributed optimization. ### Components - `Optimizer`: For hyper-parameter tuning. - Distributed optimization with databases. - Custom hyper-parameter definitions. ### Recommendations Recommended starting points per dataset are provided. ### Usage Refer to the `api-reference/optimization.md` for detailed guidance on using the `Optimizer` and implementing distributed optimization. ``` -------------------------------- ### AudioBufferState Dataclass Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/types.md This dataclass represents the internal state for accumulating audio stream data. It holds the current chunk, the buffered audio, start time, and a flag indicating if changes have occurred. ```python from dataclasses import dataclass from typing import Optional import numpy as np @dataclass class AudioBufferState: chunk: Optional[np.ndarray] buffer: Optional[np.ndarray] start_time: float changed: bool ``` -------------------------------- ### Serve Diart Pipeline via Command Line Source: https://github.com/juanmc2005/diart/blob/main/README.md This command-line interface allows you to serve a Diart speaker diarization pipeline over WebSockets. Ensure the client uses the same `step` and `sample_rate` as the server. ```shell diart.serve --host 0.0.0.0 --port 7007 diart.client microphone --host --port 7007 ``` -------------------------------- ### Inference and Streaming Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/README.md Documentation for real-time processing with `StreamingInference`, benchmarking with `Benchmark`, and distributed benchmarking with `Parallelize`. ```APIDOC ## Inference and Streaming ### Description This section covers advanced inference capabilities, including real-time processing using `StreamingInference`, dataset evaluation with `Benchmark`, and distributed benchmarking with `Parallelize`. It also details output sink options. ### Classes - `StreamingInference`: For real-time processing. - `Benchmark`: For dataset evaluation. - `Parallelize`: For distributed benchmarking. ### Output Sinks - RTTM - Accumulator - Visualization ### Usage Refer to the `api-reference/inference-and-streaming.md` for detailed information on these components and their usage. ``` -------------------------------- ### MicrophoneAudioSource Constructor Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Initializes MicrophoneAudioSource with optional block duration and device. ```python def __init__( self, block_duration: float = 0.5, device: Optional[Union[int, Text, Tuple[int, Text]]] = None, ) ``` -------------------------------- ### Stream Audio from Command Line (Live Microphone) Source: https://github.com/juanmc2005/diart/blob/main/README.md Use the `diart.stream` command to process live audio from a microphone. Use `microphone:ID` to select a specific device. See `python -m sounddevice` for available devices. ```shell # Use "microphone:ID" to select a non-default device # See `python -m sounddevice` for available devices diart.stream microphone ``` -------------------------------- ### buffer_output Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/blocks-and-operators.md Buffer pipeline outputs with audio for visualization. ```APIDOC ## buffer_output ### Description Buffer pipeline outputs with audio for visualization. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **duration** (float) - Required - Duration of the buffer in seconds. - **step** (float) - Required - Step between buffered chunks in seconds. - **latency** (float) - Required - Latency of the buffer in seconds. - **sample_rate** (int) - Required - Sample rate in Hz. ### Returns RxPY Operator for buffering outputs. ``` -------------------------------- ### Basic Voice Activity Detection Configuration Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/configuration.md Initialize VoiceActivityDetection with default configuration. Use this for standard voice activity detection tasks. ```python from diart import VoiceActivityDetection, VoiceActivityDetectionConfig config = VoiceActivityDetectionConfig() pipeline = VoiceActivityDetection(config) ``` -------------------------------- ### VoiceActivityDetection Constructor Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/voice-activity-detection.md Initializes the VoiceActivityDetection pipeline. If no configuration is provided, default settings are used. Ensure latency is within the valid range [step, duration]. ```python def __init__(self, config: VoiceActivityDetectionConfig | None = None) ``` -------------------------------- ### AppleDeviceAudioSource Constructor Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Initializes an AppleDeviceAudioSource for reading from Apple hardware devices using AVFoundation. ```APIDOC ## AppleDeviceAudioSource Audio source for reading from Apple hardware devices (input/output) using AVFoundation. ### Constructor ```python def __init__( self, sample_rate: int, device: str = "0:0", stream_index: int = 0, block_duration: float = 0.5, ) ``` #### Parameters - **sample_rate** (int) - Required - Sample rate in Hz. - **device** (str) - Optional - Device identifier in format "input:output" (e.g., "0:0" for first input). Defaults to "0:0". - **stream_index** (int) - Optional - Stream index within the device. Defaults to 0. - **block_duration** (float) - Optional - Duration of each emitted chunk in seconds. Defaults to 0.5. ### Usage Example ```python from diart.sources import AppleDeviceAudioSource from diart import SpeakerDiarization from diart.inference import StreamingInference source = AppleDeviceAudioSource(sample_rate=16000, device="0:0") pipeline = SpeakerDiarization() inference = StreamingInference(pipeline, source) predictions = inference() ``` ``` -------------------------------- ### MicrophoneAudioSource Constructor Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Initializes a MicrophoneAudioSource. You can specify the block duration and the audio device to use. ```APIDOC ## MicrophoneAudioSource Constructor ### Description Initializes a MicrophoneAudioSource with a specified block duration and audio device. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **block_duration** (float) - Optional - Duration of each emitted chunk in seconds. Defaults to 0.5. - **device** (`int | str | (int, str) | None`) - Optional - Device identifier. Use None for the default device. Format: device ID as int, name as str, or tuple of (ID, name). Defaults to None. ### Request Example ```python from diart.sources import MicrophoneAudioSource # Use default microphone source = MicrophoneAudioSource() # Or specify a device (list devices with: python -m sounddevice) source = MicrophoneAudioSource(device=2) # Use device with ID 2 ``` ### Response None ### Notes The sample rate is automatically detected from the device. The highest supported rate from [16000, 32000, 44100, 48000] Hz is used. ``` -------------------------------- ### Benchmark get_file_paths Method Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/inference-and-streaming.md Retrieves a list of paths for all audio files within the specified speech directory. ```python def get_file_paths(self) -> List[Path]: ... ``` -------------------------------- ### Parallelize Constructor Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/inference-and-streaming.md Wrapper to run a Benchmark across multiple CPU cores in parallel. ```APIDOC ## Class: Parallelize ```python class Parallelize: ``` Wrapper to run a Benchmark across multiple CPU cores in parallel. ### Constructor ```python def __init__(self, benchmark: Benchmark, num_workers: int) ``` | Parameter | Type | Required | Default | Description | |-----------|------|----------|---------|-------------| | benchmark | `Benchmark` | Yes | — | Benchmark instance to parallelize. | | num_workers | int | No | 0 | Number of parallel worker processes. 0 means no parallelism. | ``` -------------------------------- ### Hugging Face Authentication for Model Downloads Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/configuration.md Authenticate with Hugging Face CLI or set the HF_TOKEN environment variable to download models required by Hugging Face. This is necessary if models are not already cached locally. ```bash # Set Hugging Face token for model downloads huggingface-cli login # Or set HF_TOKEN export HF_TOKEN="hf_xxxxxxxxxxxxx" ``` -------------------------------- ### Chain Operations with FileAudioSource Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Demonstrates chaining operations on an audio stream from a FileAudioSource using RxPy operators. Useful for applying transformations or aggregations to audio chunks. ```python import rx.operators as ops from diart.sources import FileAudioSource from diart.blocks import SpeakerSegmentation source = FileAudioSource("audio.wav", sample_rate=16000) segmentation = SpeakerSegmentation.from_pretrained("pyannote/segmentation") # Subscribe to stream source.stream.pipe( ops.buffer(5), # Buffer 5 chunks ).subscribe(on_next=lambda chunks: print(f"Got {len(chunks)} chunks")) source.read() ``` -------------------------------- ### FileAudioSource Constructor Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/api-reference/audio-sources.md Initializes a FileAudioSource to read audio from a specified file. Allows configuration of sample rate, padding, and block duration for streaming. ```APIDOC ## FileAudioSource Constructor ### Description Initializes a FileAudioSource to read audio from a specified file. Allows configuration of sample rate, padding, and block duration for streaming. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Constructor Signature ```python def __init__( self, file: FilePath, sample_rate: int, padding: Tuple[float, float] = (0, 0), block_duration: float = 0.5, ) ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **file** (FilePath) - Required - Path to the audio file to stream. - **sample_rate** (int) - Required - Sample rate for resampling output in Hz. - **padding** (Tuple[float, float]) - Optional - Left and right padding in seconds to add to the file. Defaults to (0, 0). - **block_duration** (float) - Optional - Duration of each emitted chunk in seconds. Defaults to 0.5. ``` -------------------------------- ### Conference/Meeting Configuration Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/configuration.md Configure for scenarios with multiple speakers, expecting many participants. Uses moderate latency and lower thresholds for sensitivity. ```python config = SpeakerDiarizationConfig( duration=5.0, step=0.5, latency=2.0, # Moderate latency tau_active=0.5, # Lower threshold for sensitivity gamma=2.0, # Weaker penalty for overlaps beta=5.0, # More tolerant of simultaneous speakers max_speakers=10, # Expect many speakers ) pipeline = SpeakerDiarization(config) ``` -------------------------------- ### Configuration with Custom Models Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/configuration.md Configure the pipeline to use custom segmentation and embedding models, specifying the device (e.g., a specific GPU). ```python from diart.models import SegmentationModel, EmbeddingModel segmentation = SegmentationModel.from_pretrained("pyannote/segmentation-3.0") embedding = EmbeddingModel.from_pretrained( "speechbrain/spkrec-ecapa-voxceleb" ) config = SpeakerDiarizationConfig( segmentation=segmentation, embedding=embedding, device=torch.device("cuda:0"), # Specific GPU ) ``` -------------------------------- ### Load and Use Custom Configuration Source: https://github.com/juanmc2005/diart/blob/main/_autodocs/configuration.md Import and utilize a custom configuration object defined in a separate Python file. This demonstrates how to apply saved settings to the speaker diarization pipeline. ```python from config import CONFIG from diart import SpeakerDiarization pipeline = SpeakerDiarization(CONFIG) ```