o
    pi!                     @   s   d dl mZ d dlmZ d dlmZmZmZ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mZ d d	lmZ d d
lmZmZ ddlmZ ddlmZmZ G dd deZdS )    )partial)Path)CallableOptionalTextUnion)	Inference)	AudioFile)Pipeline)MacroAverageFMeasure)
AnnotationSlidingWindowFeature)IdentificationErrorRate	ParamDictUniform   )Binarize   )PipelineModel	get_modelc                       s   e Zd ZdZ					ddee dededeedf dee	edf f
 fd	d
Z
dd Zdd ZdZddedee defddZdeeef fddZdd Z  ZS )MultiLabelSegmentationa7  Generic multi-label segmentation

    Parameters
    ----------
    segmentation : Model, str, or dict
        Pretrained multi-label segmentation model.
        See pyannote.audio.pipelines.utils.get_model for supported format.
    fscore : bool, optional
        Optimize for average (precision/recall) fscore, over all classes.
        Defaults to optimizing identification error rate.
    share_min_duration : bool, optional
        If True, `min_duration_on` and `min_duration_off` are shared among labels.
    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.
    inference_kwargs : dict, optional
        Keywords arguments passed to Inference.

    Hyper-parameters
    ----------------
    Each {label} of the segmentation model is assigned four hyper-parameters:
    onset, offset : float
        Onset/offset detection thresholds
    min_duration_on : float
        Remove {label} regions shorter than that many seconds.
        Shared between labels if `share_min_duration` is `True`.
    min_duration_off : float
        Fill non-{label} regions shorter than that many seconds.
        Shared between labels if `share_min_duration` is `True`.
    NFsegmentationfscoreshare_min_durationtoken	cache_dirc                    s   t    |d u rtd|| _|| _|| _t|||d}|jj| _	t
|fi || _| jrJtdd| _tdd| _tdi dd | j	D | _d S tdi dd | j	D | _d S )	NzMMultiLabelSegmentation pipeline must be provided with a `segmentation` model.)r   r                  @c                 S   s&   i | ]}|t td dtd ddqS )r         ?)onsetoffsetr   .0label r%   W/home/ubuntu/.local/lib/python3.10/site-packages/pyannote/audio/pipelines/multilabel.py
<dictcomp>m   s    z3MultiLabelSegmentation.__init__.<locals>.<dictcomp>c              
   S   s6   i | ]}|t td dtd dtd dtd ddqS )r   r   r   r    r!   min_duration_onmin_duration_offr   r"   r%   r%   r&   r'   w   s    r%   )super__init__
ValueErrorr   r   r   r   specificationsclasses_classesr   _segmentationr   r)   r*   r   
thresholds)selfr   r   r   r   r   inference_kwargsmodel	__class__r%   r&   r,   M   s0   
	

zMultiLabelSegmentation.__init__c                 C   s   | j S N)r0   r3   r%   r%   r&   r/      s   zMultiLabelSegmentation.classesc                    s    fdd j D  _dS )z2Initialize pipeline with current set of parametersc              	      s^   i | ]+}|t  j| d   j| d  js j| d n j js( j| d n jdqS )r    r!   r)   r*   r(   )r   r2   r   r)   r*   r"   r9   r%   r&   r'      s    z5MultiLabelSegmentation.initialize.<locals>.<dictcomp>N)r0   	_binarizer9   r%   r9   r&   
initialize   s   
z!MultiLabelSegmentation.initializezcache/segmentationfilehookreturnc           	      C   s   | j ||d}| jr&| j|v r|| j }n| j|t|ddd}||| j< n| j|t|ddd}|d| t|d d}t| jD ],\}}t|j	dd||d f |j
}| j| |}||jt| |dd qB|S )	a  Apply multi-label detection

        Parameters
        ----------
        file : AudioFile
            Processed file.
        hook : callable, optional
            Callback called after each major steps of the pipeline as follows:
                hook(step_name,      # human-readable name of current step
                     step_artefact,  # artifact generated by current step
                     file=file)      # file being processed
            Time-consuming steps call `hook` multiple times with the same `step_name`
            and additional `completed` and `total` keyword arguments usable to track
            progress of current step.

        Returns
        -------
        detection : Annotation
            Detected regions.
        )r=   r   Nuri)r?   r   F)copy)
setup_hooktrainingCACHED_SEGMENTATIONr1   r   r   	enumerater0   r   datasliding_windowr:   updaterename_labelsdictfromkeyslabels)	r3   r<   r=   segmentations	detectionir$   label_segmentationlabel_annotationr%   r%   r&   apply   s0   

zMultiLabelSegmentation.applyc                 C   s   | j r	t| jdS t S )z,Return new instance of identification metric)r/   )r   r   r0   r   r9   r%   r%   r&   
get_metric   s   z!MultiLabelSegmentation.get_metricc                 C   s   | j rdS dS )Nmaximizeminimize)r   r9   r%   r%   r&   get_direction   s   z$MultiLabelSegmentation.get_direction)NFFNNr8   )__name__
__module____qualname____doc__r   r   boolr   r   r   r,   r/   r;   rC   r	   r   r   rQ   r   r   rR   rU   __classcell__r%   r%   r6   r&   r   ,   s0    "
6>r   N)	functoolsr   pathlibr   typingr   r   r   r   pyannote.audior   pyannote.audio.core.ior	   pyannote.audio.core.pipeliner
   pyannote.audio.utils.metricr   pyannote.corer   r   pyannote.metrics.identificationr   pyannote.pipeline.parameterr   r   utils.signalr   utilsr   r   r   r%   r%   r%   r&   <module>   s   