o
    9wip                     @   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   `/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/pyannote/audio/pipelines/utils/hook.py__init__8   s   
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                   @   b   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 )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	transientc                 C   
   || _ d S r   r4   )r   r4   r   r   r   r   d      
zProgressHook.__init__c                 C   s2   t tdt t tdd| jd| _| j  | S )Nz([progress.description]{task.description}T)elapsed_when_finishedr6   )r   r
   r   r	   r   r4   progressstartr   r   r   r   r   g   s   
zProgressHook.__enter__c                 G   s   | j   d S r   )r9   stopr   r   r   r   r   r   s   zProgressHook.__exit__Nr   r   r   r   r    c                 C   sh   |d u rd }}t | dr|| jkr|| _| j| j| _| jj| j||d ||kr2| j  d S d S )N   r   )r    r   )hasattrr   r9   add_taskstepupdaterefreshr(   r   r   r   r)   u   s   zProgressHook.__call__)Fr*   )r,   r-   r.   r/   boolr   r   r   r   r   r   r   r1   r)   r   r   r   r   r3   U   s&    r3   c                   @   r2   )
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   r5   r   r   )r   r   r   r   r   r      r7   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__)rD   r*   r+   r   r   r   r   rC      s&    rC   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   r5   r   hooks)r   rO   r   r   r   r      r7   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.   r/   r   r   r   r   r   r   r   r1   r)   r   r   r   r   rM      s&    		rM   )rE   copyr   typingr   r   r   r   r#   rich.progressr   r   r	   r
   r   r   r3   rC   rM   r   r   r   r   <module>   s   	068