o
    pi;                     @   s   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 dZG dd dZG d	d
 d
ZG dd dZdS )    N)deepcopy)AnyMappingOptionalText)	BarColumnProgressTaskProgressColumn
TextColumnTimeRemainingColumnc                   @   sf   e Zd ZdZdddefddZdd Zd	d
 Z			ddede	de
e de
e de
e f
ddZdS )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                G   s   || _ || _d S N)	artifactsr   )selfr   r    r   W/home/ubuntu/.local/lib/python3.10/site-packages/pyannote/audio/pipelines/utils/hook.py__init__8      
zArtifactHook.__init__c                 C   s   | S r   r   r   r   r   r   	__enter__<      zArtifactHook.__enter__c                 G   s   d S r   r   r   argsr   r   r   __exit__?   r   zArtifactHook.__exit__N	step_namestep_artifactfiletotal	completedc                 C   sP   |d u s| j r|| j vrd S t|tjr|jdd}t||| jt |< d S )NT)force)	r   
isinstancetorchTensornumpyr   
setdefaultr   dictr   r   r   r   r    r!   r   r   r   __call__B   s   
zArtifactHook.__call__NNN__name__
__module____qualname____doc__strr   r   r   r   r   r   r   intr*   r   r   r   r   r   %   s&    r   c                   @   sf   e Zd ZdZddedefddZdd Zd	d
 Z			ddede	de
e de
e de
e f
ddZdS )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)
    F	transienthiddenc                 C   s   || _ || _d S r   )r4   r5   )r   r4   r5   r   r   r   r   d   r   zProgressHook.__init__c                 C   s<   | j r| S ttdt t tdd| jd| _| j  | S )Nz([progress.description]{task.description}T)elapsed_when_finished)r4   )	r5   r   r
   r   r	   r   r4   progressstartr   r   r   r   r   h   s   
zProgressHook.__enter__c                 G   s   | j rd S | j  d S r   )r5   r7   stopr   r   r   r   r   v   s   
zProgressHook.__exit__Nr   r   r   r    r!   c                 C   sr   | j rd S |d u rd }}t| dr|| jkr"|| _| j| j| _| jj| j||d ||kr7| j  d S d S )N   r   )r!   r    )r5   hasattrr   r7   add_taskstepupdaterefreshr)   r   r   r   r*   }   s   zProgressHook.__call__)FFr+   )r-   r.   r/   r0   boolr   r   r   r   r   r   r   r2   r*   r   r   r   r   r3   U   s&    r3   c                   @   sb   e Zd ZdZddefddZdd Zdd	 Z	
	
	
ddede	de
e de
e de
e f
ddZd
S )
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
    timingr   c                 C   
   || _ d S r   r   )r   r   r   r   r   r         
zTimingHook.__init__c                 C   s   t   | _t | _t | _| S r   )time_pipeline_start_timer(   _start_time	_end_timer   r   r   r   r      s   
zTimingHook.__enter__c                 G   sV   t   }t }|| j |d< | j D ]\}}| j| }|| ||< q|| j| j< d S )Nr    )rE   r(   rF   rG   itemsrH   _filer   )r   r   _pipeline_end_timeprocessing_timer   rG   rH   r   r   r   r      s   
zTimingHook.__exit__Nr   r   r   r    r!   c                 C   sP   t | ds|| _|d u rd S |dkrt | j|< ||kr&t | j|< d S d S )NrJ   r   )r;   rJ   rE   rG   rH   r)   r   r   r   r*      s   
zTimingHook.__call__)rB   r+   r,   r   r   r   r   rA      s&    rA   c                   @   sZ   e Zd ZdZdd Zdd Zdd Z			dd	ed
ede	e
 de	e de	e f
ddZdS )HookszList of hooks

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

    c                 G   rC   r   hooks)r   rO   r   r   r   r      rD   zHooks.__init__c                 C   s"   | j D ]}t|dr|  q| S )Nr   )rO   r;   r   )r   hookr   r   r   r      s
   

zHooks.__enter__c                 G   s$   | j D ]}t|dr|j|  qd S )Nr   )rO   r;   r   )r   r   rP   r   r   r   r      s
   


zHooks.__exit__Nr   r   r   r    r!   c                 C   s"   | j D ]}||||||d qd S )N)r   r    r!   rN   )r   r   r   r   r    r!   rP   r   r   r   r*      s   
zHooks.__call__r+   )r-   r.   r/   r0   r   r   r   r   r   r   r   r2   r*   r   r   r   r   rM      s&    		rM   )rE   copyr   typingr   r   r   r   r$   rich.progressr   r   r	   r
   r   r   r3   rA   rM   r   r   r   r   <module>   s   	0A8