o
    pi{$                  
   @   s  d dl Z d dlmZ d dlmZmZ d dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZ 		dd
eeB eB dedB deeB dB defddZe
eB eB Z		ddededB deeB dB de
fddZeeB ZeeB eB Z		ddededB deeB dB dedB fddZeeB eB Z		ddededB deeB dB dedB fddZdedefddZd dedB fddZ dS )!    N)Path)DictMapping)Calibration)Model)Pipeline)PLDA)BaseWaveformTransform)	from_dictpipelinetoken	cache_dirreturnc                 C   s   t | tr| }n?t | trtj| ||d}n1t | tr=d| v r4| d| | d| tjd	i | }ntj| ||d}n
tdt|  d|d u rStd|  d|S )
Nr   r   
checkpointr   r   Unsupported type (z1) for loading pipeline: expected `str` or `dict`.zCould not load pipeline: . )	
isinstancer   strfrom_pretraineddict
setdefault	TypeErrortype
ValueError)r   r   r   	_pipeliner   r   Y/home/ubuntu/.local/lib/python3.10/site-packages/pyannote/audio/pipelines/utils/getter.pyget_pipeline%   s$   


r   modelc                 C   s   t | trn7t | trtj| ||dd}|r|} n$t | tr3| d| | d| tjdi | } n
tdt|  d|   | S )	aL  Load pretrained model and set it into `eval` mode.

    Parameter
    ---------
    model : Model, str, or dict
        When `Model`, returns `model` as is.
        When `str`, assumes that this is either the path to a checkpoint or the name of a
        pretrained model on Huggingface.co and loads with `Model.from_pretrained(model)`
        When `dict`, loads with `Model.from_pretrained(**model)`.
    token : str or bool, optional
        Huggingface token to be used for downloading from Huggingface hub.
    cache_dir: Path or str, optional
        Path to the folder where files downloaded from Huggingface hub are stored.

    Returns
    -------
    model : Model
        Model in `eval` mode.

    Examples
    --------
    >>> model = get_model("hbredin/VoiceActivityDetection-PyanNet-DIHARD")
    >>> model = get_model("/path/to/checkpoint.ckpt")
    >>> model = get_model({"checkpoint": "hbredin/VoiceActivityDetection-PyanNet-DIHARD",
    ...                    "map_location": torch.device("cuda")})

    See also
    --------
    pyannote.audio.core.model.Model.from_pretrained

    F)r   r   strictr   r   r   z.) for loading model: expected `str` or `dict`.Nr   )	r   r   r   r   r   r   r   r   eval)r   r   r   _modelr   r   r   	get_modelJ   s*   
%

r#   calibrationc                 C   z   t | tr	| }|S t | trtj| ||d}|S t | tr3| d| | d| tjdi | }|S tdt|  d)aD  Load pretrained calibration

    Parameters
    ----------
    calibration : Calibration, str, or dict
        When `Calibration`, returns `calibration` as is.
        When `str`, assumes that this is either the path to a checkpoint or the name of a
        pretrained calibration on Huggingface.co and loads with `Calibration.from_pretrained(calibration)`.
        When `dict`, loads with `Calibration.from_pretrained(**calibration)`.
    token : str or bool, optional
        Huggingface token to be used for downloading from Huggingface hub.
    cache_dir: Path or str, optional
        Path to the folder where files downloaded from Huggingface hub are stored.

    Returns
    -------
    calibration : Calibration
        Calibration.

    See also
    --------
    pyannote.audio.core.calibration.Calibration.from_pretrained
    r   r   r   r   z4) for loading calibration: expected `str` or `dict`.Nr   )r   r   r   r   r   r   r   r   )r$   r   r   loaded_calibrationr   r   r   get_calibration   s$   


r'   pldac                 C   r%   )a  Load pretrained calibration

    Parameters
    ----------
    plda : PLDA, str, or dict
        When `PLDA`, returns `plda` as is.
        When `str`, assumes that this is either the path to a checkpoint or the name of a
        pretrained PLDA on Huggingface.co and loads with `PLDA.from_pretrained(PLDA)`.
        When `dict`, loads with `PLDA.from_pretrained(**plda)`.
    token : str or bool, optional
        Huggingface token to be used for downloading from Huggingface hub.
    cache_dir: Path or str, optional
        Path to the folder where files downloaded from Huggingface hub are stored.

    Returns
    -------
    plda : PLDA
        PLDA.

    See also
    --------
    pyannote.audio.core.plda.PLDA.from_pretrained
    r   r   r   r   z-) for loading PLDA: expected `str` or `dict`.Nr   )r   r   r   r   r   r   r   r   )r(   r   r   loaded_pldar   r   r   get_plda   s   


r*   augmentationc                 C   s@   | du rdS t | tr| S t | trt| S tdt|  d)a[  Load augmentation

    Parameter
    ---------
    augmentation : BaseWaveformTransform, or dict
        When `BaseWaveformTransform`, returns `augmentation` as is.
        When `dict`, loads with `torch_audiomentations`'s `from_config` utility function.

    Returns
    -------
    augmentation : BaseWaveformTransform
        Augmentation.
    Nr   zH) for loading augmentation: expected `BaseWaveformTransform`, or `dict`.)r   r	   r   augmentation_from_dictr   r   )r+   r   r   r   get_augmentation   s   

r-   needsc                 C   sn   t j }|dkrt dg}| du r|S ||  S dd t|D }| du r(|S dd tt| t|D S )a7  Get devices that can be used by the pipeline

    Parameters
    ----------
    needs : int, optional
        Number of devices needed by the pipeline

    Returns
    -------
    devices : list of torch.device
        List of available devices.
        When `needs` is provided, returns that many devices.
    r   cpuNc                 S   s   g | ]}t d |dqS )zcuda:d)torchdevice).0indexr   r   r   
<listcomp>/  s    zget_devices.<locals>.<listcomp>c                 S   s   g | ]\}}|qS r   r   )r3   _r2   r   r   r   r5   2  s    )r1   cudadevice_countr2   rangezip	itertoolscycle)r.   num_gpusdevicesr   r   r   get_devices  s   
r?   )NN)N)!r;   pathlibr   typingr   r   r1   pyannote.audio.core.calibrationr   pyannote.audio.core.modelr   pyannote.audio.core.pipeliner   pyannote.audio.core.pldar   /torch_audiomentations.core.transforms_interfacer	   "torch_audiomentations.utils.configr
   r,   r   r   r   PipelineModelr#   PipelineAugmentationPipelineCalibrationr'   PipelinePLDAr*   r-   intr?   r   r   r   r   <module>   sx   


"

A

5

1