o
    9wi!                     @   s   d dl mZ d dlmZmZmZ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 d dlmZ d d	lmZ d
dlmZ ddlmZmZ G dd deZdS )    )partial)CallableOptionalTextUnion)
AnnotationSlidingWindowFeature)IdentificationErrorRate	ParamDictUniform)	Inference)	AudioFile)Pipeline)MacroAverageFMeasure   )Binarize   )PipelineModel	get_modelc                       s   e Zd ZdZ				ddee deded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 )MultiLabelSegmentationaP  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.
    use_auth_token : str, optional
        When loading private huggingface.co models, set `use_auth_token`
        to True or to a string containing your hugginface.co authentication
        token that can be obtained by running `huggingface-cli login`
    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use_auth_tokenc                    s   t    |d u rtd|| _|| _|| _t||d}|jj| _	t
|fi || _| jrIt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                  @c                 S   s&   i | ]}|t td dtd ddqS )r         ?)onsetoffsetr
   .0label r#   `/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/pyannote/audio/pipelines/multilabel.py
<dictcomp>l   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%   v   s    r#   )super__init__
ValueErrorr   r   r   r   specificationsclasses_classesr   _segmentationr   r'   r(   r   
thresholds)selfr   r   r   r   inference_kwargsmodel	__class__r#   r$   r*   L   s0   
	

zMultiLabelSegmentation.__init__c                 C   s   | j S N)r.   r1   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   r0   r   r'   r(   r    r7   r#   r$   r%      s    z5MultiLabelSegmentation.initialize.<locals>.<dictcomp>N)r.   	_binarizer7   r#   r7   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_SEGMENTATIONr/   r   r   	enumerater.   r   datasliding_windowr8   updaterename_labelsdictfromkeyslabels)	r1   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   r.   r	   r7   r#   r#   r$   
get_metric   s   z!MultiLabelSegmentation.get_metricc                 C   s   | j rdS dS )Nmaximizeminimize)r   r7   r#   r#   r$   get_direction   s   z$MultiLabelSegmentation.get_direction)NFFNr6   )__name__
__module____qualname____doc__r   r   boolr   r   r*   r-   r9   rA   r   r   r   rO   r   r	   rP   rS   __classcell__r#   r#   r4   r$   r   +   s*    "
6>r   N)	functoolsr   typingr   r   r   r   pyannote.corer   r   pyannote.metrics.identificationr	   pyannote.pipeline.parameterr   r   pyannote.audior   pyannote.audio.core.ior   pyannote.audio.core.pipeliner   pyannote.audio.utils.metricr   utils.signalr   utilsr   r   r   r#   r#   r#   r$   <module>   s   