
     i;                         d dl Z d dlmZ d dlmZmZmZmZ d dlZd dl	m
Z
mZmZmZmZ  G d d      Z G d d      Z G d	 d
      Z G d d      Zy)    N)deepcopy)AnyMappingOptionalText)	BarColumnProgressTaskProgressColumn
TextColumnTimeRemainingColumnc                   d    e Zd ZdZdddefdZd Zd Z	 	 	 dd	ed
e	de
e   de
e   de
e   f
dZy)ArtifactHooka  Hook to save artifacts of each internal step

    Parameters
    ----------
    artifacts: list of str, optional
        List of steps to save. Defaults to all steps.
    file_key: str, optional
        Key used to store artifacts in `file`.
        Defaults to "artifact".

    Usage
    -----
    >>> with ArtifactHook() as hook:
    ...     output = pipeline(file, hook=hook)
    # file["artifact"] contains a dict with artifacts of each step

    artifactfile_keyr   c                     || _         || _        y N)	artifactsr   )selfr   r   s      x/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/pyannote/audio/pipelines/utils/hook.py__init__zArtifactHook.__init__8   s    "     c                     | S r    r   s    r   	__enter__zArtifactHook.__enter__<   s    r   c                      y r   r   r   argss     r   __exit__zArtifactHook.__exit__?   s    r   N	step_namestep_artifactfiletotal	completedc                     || j                   r|| j                   vry t        |t        j                        r|j	                  d      }t        |      |j                  | j                  t                     |<   y )NT)force)	r   
isinstancetorchTensornumpyr   
setdefaultr   dictr   r!   r"   r#   r$   r%   s         r   __call__zArtifactHook.__call__B   s`     !NNy>mU\\2)//d/;M<D]<Stv.y9r   NNN__name__
__module____qualname____doc__strr   r   r    r   r   r   r   intr/   r   r   r   r   r   %   su    $ 4> !S ! #'##'TT T w	T
 }T C=Tr   r   c                   d    e Zd ZdZddedefdZd Zd Z	 	 	 dded	e	d
e
e   de
e   de
e   f
dZy)ProgressHookaP  Hook to show progress of each internal step

    Parameters
    ----------
    transient: bool, optional
        Clear the progress on exit. Defaults to False.

    Example
    -------
    pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization")
    with ProgressHook() as hook:
       output = pipeline(file, hook=hook)
    	transienthiddenc                      || _         || _        y r   )r:   r;   )r   r:   r;   s      r   r   zProgressHook.__init__d   s    "r   c                     | j                   r| S t        t        d      t               t	               t        d      | j                        | _        | j                  j                          | S )Nz([progress.description]{task.description}T)elapsed_when_finished)r:   )	r;   r	   r   r   r
   r   r:   progressstartr   s    r   r   zProgressHook.__enter__h   sT    ;;K ABK d;nn
 	r   c                 R    | j                   ry | j                  j                          y r   )r;   r?   stopr   s     r   r    zProgressHook.__exit__v   s    ;;r   Nr!   r"   r#   r$   r%   c                 R   | j                   ry |dx}}t        | d      r|| j                  k7  r1|| _        | j                  j	                  | j                        | _        | j                  j                  | j
                  ||       ||k\  r| j                  j                          y y )N   r!   )r%   r$   )r;   hasattrr!   r?   add_taskstepupdaterefreshr.   s         r   r/   zProgressHook.__call__}   s     ;; !!It[)Y$..-H&DN..t~~>DITYY)5I MM!!# r   )FFr0   )r2   r3   r4   r5   boolr   r   r    r   r   r   r   r7   r/   r   r   r   r9   r9   U   sq    $   #'##'$$ $ w	$
 }$ C=$r   r9   c                   `    e Zd ZdZddefdZd Zd Z	 	 	 ddede	d	e
e   d
e
e   de
e   f
dZy)
TimingHookar  Hook to compute processing time of internal steps

    Parameters
    ----------
    file_key: str, optional
        Key used to store processing time in `file`.
        Defaults to "timing_hook".

    Usage
    -----
    >>> with TimingHook() as hook:
    ...     output = pipeline(file, hook=hook)
    # file["timing_hook"]  contains processing time for each step
    r   c                     || _         y r   r   )r   r   s     r   r   zTimingHook.__init__   s	     r   c                 t    t        j                          | _        t               | _        t               | _        | S r   )time_pipeline_start_timer-   _start_time	_end_timer   s    r   r   zTimingHook.__enter__   s(    $(IIK!6r   c                    t        j                          }t               }|| j                  z
  |d<   | j                  j	                         D ]  \  }}| j
                  |   }||z
  ||<    || j                  | j                  <   y )Nr$   )rO   r-   rP   rQ   itemsrR   _filer   )r   r   _pipeline_end_timeprocessing_timer!   rQ   rR   s          r   r    zTimingHook.__exit__   sy    !YY[&#58Q8Q#Q &*&6&6&<&<&>"I{y1I)2[)@OI& '? %4

4==!r   Nr!   r"   r#   r$   r%   c                     t        | d      s|| _        |y |dk(  r!t        j                         | j                  |<   ||k\  r"t        j                         | j                  |<   y y )NrU   r   )rE   rU   rO   rQ   rR   r.   s         r   r/   zTimingHook.__call__   s\     tW%DJ>*.))+DY'(,		DNN9% r   )timingr0   r1   r   r   r   rL   rL      sj    ! !4 #'##'44 4 w	4
 }4 C=4r   rL   c                   X    e Zd ZdZd Zd Zd Z	 	 	 ddedede	e
   d	e	e   d
e	e   f
dZy)HookszList of hooks

    Usage
    -----
    >>> with Hooks(ProgessHook(), TimingHook(), ArtifactHook()) as hook:
    ...     output = pipeline("audio.wav", hook=hook)

    c                     || _         y r   hooks)r   r^   s     r   r   zHooks.__init__   s	    
r   c                 b    | j                   D ]  }t        |d      s|j                          ! | S )Nr   )r^   rE   r   )r   hooks     r   r   zHooks.__enter__   s+    JJDt[)   r   c                 ^    | j                   D ]  }t        |d      s |j                  |    y )Nr    )r^   rE   r    )r   r   r`   s      r   r    zHooks.__exit__   s(    JJDtZ(t$ r   Nr!   r"   r#   r$   r%   c                 @    | j                   D ]  } ||||||        y )N)r#   r$   r%   r]   )r   r!   r"   r#   r$   r%   r`   s          r   r/   zHooks.__call__   s"     JJDMEYW r   r0   )r2   r3   r4   r5   r   r   r    r   r   r   r   r7   r/   r   r   r   r[   r[      si    % #'##'	X	X 	X w		X
 }	X C=	Xr   r[   )rO   copyr   typingr   r   r   r   r)   rich.progressr   r	   r
   r   r   r   r9   rL   r[   r   r   r   <module>rf      sO   .   / /  -T -T`>$ >$B54 54p!X !Xr   