o
    9wi                     @   s   d dl Z d dlmZmZmZmZ d dlZd dlmZ d dl	m
Z d dlmZmZ eeeef Z	ddedeedf defd	d
Zeeeeef ZdedefddZeeef ZdedefddZddee fddZdS )    N)MappingOptionalTextUnion)BaseWaveformTransform)	from_dict)	InferenceModelmodeluse_auth_tokenreturnc                 C   sp   t | trn,t | trtj| |dd} nt | tr(| d| tjdi | } n
tdt|  d|   | S )ay  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)`.
    use_auth_token : str, optional
        When loading a private or gated huggingface.co pipeline, set `use_auth_token`
        to True or to a string containing your hugginface.co authentication
        token that can be obtained by visiting https://hf.co/settings/tokens

    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   strictr   Unsupported type (z.) for loading model: expected `str` or `dict`.N )	
isinstancer	   r   from_pretrainedr   
setdefault	TypeErrortypeeval)r
   r   r   r   b/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/pyannote/audio/pipelines/utils/getter.py	get_model#   s   
$

r   	inferencec                 C   sP   t | tr| S t | ttfrt| S t | trtdi | S tdt|  d)a  Load inference

    Parameter
    ---------
    inference : Inference, Model, str, or dict
        When `Inference`, returns `inference` as is.
        When `Model`, wraps it in `Inference(model)`.
        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 `Inference(checkpoint)`.
        When `dict`, loads with `Inference(**inference)`.

    Returns
    -------
    inference : Inference
        Inference.

    Examples
    --------
    >>> inference = get_inference("hbredin/VoiceActivityDetection-PyanNet-DIHARD")
    >>> inference = get_inference("/path/to/checkpoint.ckpt")
    >>> inference = get_inference({"model": "hbredin/VoiceActivityDetection-PyanNet-DIHARD",
    ...                            "window": "sliding"})

    See also
    --------
    pyannote.audio.core.inference.Inference

    r   z;) for loading inference: expected `Model`, `str` or `dict`.Nr   )r   r   r	   r   r   r   r   )r   r   r   r   get_inference`   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   )r"   _r!   r   r   r   r$      s    )r    cudadevice_countr!   rangezip	itertoolscycle)r   num_gpusdevicesr   r   r   get_devices   s   
r.   )N)r*   typingr   r   r   r   r    /torch_audiomentations.core.transforms_interfacer   "torch_audiomentations.utils.configr   r   pyannote.audior   r	   PipelineModelr   PipelineInferencer   PipelineAugmentationr   intr.   r   r   r   r   <module>   s(   

:-