
     i{$              
          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z  ez  dedz  deez  dz  defdZe
ez  ez  Z	 	 ddededz  deez  dz  de
fdZeez  Zeez  ez  Z	 	 ddededz  deez  dz  dedz  fdZeez  ez  Z	 	 ddededz  deez  dz  dedz  fdZdedefdZddedz  fdZ y)    N)Path)DictMapping)Calibration)Model)Pipeline)PLDA)BaseWaveformTransform)	from_dictpipelinetoken	cache_dirreturnc                    t        | t              r| }nt        | t              rt        j                  | ||      }nt        | t              rWd| v r:| j                  d|       | j                  d|       t        j                  d	i | }n1t        j                  | ||      }nt        dt        |        d      |t        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   	_pipelines       z/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/pyannote/audio/pipelines/utils/getter.pyget_pipeliner    %   s    
 (H%		Hc	",,XUiX		Hd	#8#/Y7 00<8<I !00I
  h 0 1( )
 	

 4XJa@AA    modelc                 h   t        | t              rnt        | t              rt        j                  | ||d      }|re|} nbt        | t              r:| j                  d|       | j                  d|       t        j                  di | } nt        dt        |        d      | j                          | 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`.r   )	r   r   r   r   r   r   r   r   eval)r"   r   r   _models       r   	get_modelr'   J   s    J %	E3	&&	
 E	E7	#%(i0%%..  e .( )
 	

 
JJLLr!   calibrationc                 D   t        | t              r| }|S t        | t              rt        j                  | ||      }|S t        | t              r;| j                  d|       | j                  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`.r   )r   r   r   r   r   r   r   r   )r(   r   r   loaded_calibrations       r   get_calibrationr+      s    : +{+(( % 
K	%(88
"  
K	&w.{I6(88G;G   k!2 3 4( )
 	
r!   pldac                 D   t        | t              r| }|S t        | t              rt        j                  | ||      }|S t        | t              r;| j                  d|       | j                  d|       t        j                  di | }|S t        dt        |        d      )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`.r   )r   r	   r   r   r   r   r   r   )r,   r   r   loaded_pldas       r   get_pldar/      s    : $   
D#	**4u	R  
D$	'Y/**2T2   d -( )
 	
r!   augmentationc                     | y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   )r0   s    r   get_augmentationr3      sW     , 56,(%l33

T,/0 17 	8 r!   needsc                 x   t         j                  j                         }|dk(  rt        j                  d      g}| |S || z  S t	        |      D cg c]  }t        j                  d|d       }}| |S t        t	        |       t        j                  |            D cg c]  \  }}|	 c}}S c c}w c c}}w )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   cpuzcuda:d)torchcudadevice_countdevicerangezip	itertoolscycle)r4   num_gpusdevicesindex_r;   s         r   get_devicesrD     s     zz&&(H1}<<&'=N<A(OLO5u||eE!9-.OGL}$'eioog6N$OP$Oyq&F$OPP M Qs    B1!B6)NN)N)!r>   pathlibr   typingr   r   r8   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   r2   r   r   r    PipelineModelr'   PipelineAugmentationPipelineCalibrationr+   PipelinePLDAr/   r3   intrD   r   r!   r   <module>rR      s  0      7 + 1 ) Q R
 #'nt#: czD  	D g%
 #'>>:> czD > 	>B -w6  "C'$. 
 #'2$2:2 czD 2 4	2j czD 
 #'.
.:. czD . 
D[	.b#7 <Q <QsTz Qr!   