U
    9%e<                     @   s   d dl Z d dl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 Zdd	d
Zdd ZG dd dZG dd deZG dd deZdS )    N)Optional   )TrainerCallback)IntervalStrategy
has_lengthc                 C   sZ   t | } | d | d d | d   }}}|dkrH| d|dd|dS |dd|dS )z$Format `t` (in seconds) to (h):mm:ssi  <   r   :02d)int)thms r   Z/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/transformers/utils/notebook.pyformat_time   s     r   ,  c                 C   s$   d| d|  d| d| d| dS )Nz
    <div>
      z
      <progress value='z' max='z' style='width:z<px; height:20px; vertical-align: middle;'></progress>
      z
    </div>
    r   )valuetotalprefixlabelwidthr   r   r   html_progress_bar!   s    r   c                 C   s   d}|d7 }| d D ]}|d| d7 }q|d7 }| dd D ]H}|d	7 }|D ].}t |trd|d
nt|}|d| d7 }qN|d7 }q>|d7 }|S )z*Put the texts in `items` in an HTML table.z%<table border="1" class="dataframe">
z*  <thead>
 <tr style="text-align: left;">
r   z
      <th>z</th>
z    </tr>
  </thead>
  <tbody>
   Nz	    <tr>
z.6fz
      <td>z</td>
z
    </tr>
z  </tbody>
</table><p>)
isinstancefloatstr)items	html_codeilineeltr   r   r   text_to_html_table,   s    
r"   c                   @   sf   e Zd ZdZdZdZdeee e	ed edd	d
Z
dee	edddZdddZdd Zdd ZdS )NotebookProgressBara  
    A progress par for display in a notebook.

    Class attributes (overridden by derived classes)

        - **warmup** (`int`) -- The number of iterations to do at the beginning while ignoring `update_every`.
        - **update_every** (`float`) -- Since calling the time takes some time, we only do it every presumed
          `update_every` seconds. The progress bar uses the average time passed up until now to guess the next value
          for which it will call the update.

    Args:
        total (`int`):
            The total number of iterations to reach.
        prefix (`str`, *optional*):
            A prefix to add before the progress bar.
        leave (`bool`, *optional*, defaults to `True`):
            Whether or not to leave the progress bar once it's completed. You can always call the
            [`~utils.notebook.NotebookProgressBar.close`] method to make the bar disappear.
        parent ([`~notebook.NotebookTrainingTracker`], *optional*):
            A parent object (like [`~utils.notebook.NotebookTrainingTracker`]) that spawns progress bars and handle
            their display. If set, the object passed must have a `display()` method.
        width (`int`, *optional*, defaults to 300):
            The width (in pixels) that the bar will take.

    Example:

    ```python
    import time

    pbar = NotebookProgressBar(100)
    for val in range(100):
        pbar.update(val)
        time.sleep(0.07)
    pbar.update(100)
    ```   g?NTr   NotebookTrainingTracker)r   r   leaveparentr   c                 C   s@   || _ |d krdn|| _|| _|| _|| _d | _d | _d | _d S )N )r   r   r&   r'   r   
last_valuecommentoutput)selfr   r   r&   r'   r   r   r   r   __init__e   s    zNotebookProgressBar.__init__F)r   force_updater*   c                 C   sz  || _ |dk	r|| _| jdkrbt  | _| _| | _| _d | _| _| j	| _
d| _| | n|| jkrt|stdS |s| j
dks|t| j| j | jkrv| j
dkr|  j
d8  _
t }|| j | _|| jkr| j|| j  | _nd| _|| jkr| j}d| _| js4|   n| jdk	r4| j| j|  | _| | || _|| _| jdkr^d| _ntt| j| j d| _dS )a  
        The main method to update the progress bar to `value`.

        Args:
            value (`int`):
                The value to use. Must be between 0 and `total`.
            force_update (`bool`, *optional*, defaults to `False`):
                Whether or not to force and update of the internal state and display (by default, the bar will wait for
                `value` to reach the value it predicted corresponds to a time of more than the `update_every` attribute
                since the last update to avoid adding boilerplate).
            comment (`str`, *optional*):
                A comment to add on the left of the progress bar.
        Nr   r   )r   r*   r)   time
start_timeZ	last_timeZstart_valueelapsed_timepredicted_remainingwarmupZfirst_callswait_for
update_barminr   average_time_per_itemr&   closemaxr
   update_every)r,   r   r.   r*   current_timer   r   r   updatev   sB    
(



zNotebookProgressBar.updatec              	   C   s   dt t| jt t|  t| }| jd krHd| d| j d| _nv| jd krtd| d| j dt| j | _nJd| d| j dt| j dt| j | _|  jdd| j dd	7  _|  j| jd kst | jd
krdnd| j d7  _| 	  d S )N [/z : < :z < z, r   .2fz it/sr   ])
lenr   r   r1   r   r2   r   r7   r*   display)r,   r   r*   Zspaced_valuer   r   r   r5      s    &

"(4zNotebookProgressBar.update_barc                 C   sp   t | j| j| j| j| j| _| jd k	r4| j  d S | j	d krXt
jt
| jdd| _	n| j	t
| j d S NT)Z
display_id)r   r   r   r   r   r   r   r'   rC   r+   dispHTMLr<   r,   r   r   r   rC      s    


zNotebookProgressBar.displayc                 C   s*   | j dkr&| jdk	r&| jtd dS )zCloses the progress bar.Nr(   )r'   r+   r<   rE   rF   rG   r   r   r   r8      s    zNotebookProgressBar.close)NTNr   )FN)N)__name__
__module____qualname____doc__r3   r:   r
   r   r   boolr-   r<   r5   rC   r8   r   r   r   r   r#   =   s$   $    3
r#   c                       sD   e Zd ZdZd fdd	Zdd Zdd Zdd
dZdd Z  Z	S )r%   a  
    An object tracking the updates of an ongoing training with progress bars and a nice table reporting metrics.

    Args:
        num_steps (`int`): The number of steps during training. column_names (`List[str]`, *optional*):
            The list of column names for the metrics table (will be inferred from the first call to
            [`~utils.notebook.NotebookTrainingTracker.write_line`] if not set).
    Nc                    s*   t  | |d krd n|g| _d | _d S N)superr-   inner_table	child_bar)r,   Z	num_stepscolumn_names	__class__r   r   r-      s    z NotebookTrainingTracker.__init__c                 C   s   t | j| j| j| j| j| _| jd k	r:|  jt| j7  _| j	d k	rV|  j| j	j7  _| j
d krztjt| jdd| _
n| j
t| j d S rD   )r   r   r   r   r   r   r   rO   r"   rP   r+   rE   rC   rF   r<   rG   r   r   r   rC      s    


zNotebookTrainingTracker.displayc                    s  | j dkr&t t g| _ n| j d } D ]}||kr8|| q8|| j d< t| j dkr| j d }| j d d }|d | kr| j fdd|D  nD |D ]"}|  kr|||  |< q fdd|D | j d< n| j fdd|D  dS )	z
        Write the values in the inner table.

        Args:
            values (`Dict[str, float]`): The values to display.
        Nr   r   c                    s    g | ]}| kr | nd qS )zNo Logr   .0cvaluesr   r   
<listcomp>   s     z6NotebookTrainingTracker.write_line.<locals>.<listcomp>c                    s   g | ]} | qS r   r   rU   )
new_valuesr   r   rZ      s     c                    s   g | ]} | qS r   r   rU   rX   r   r   rZ      s     )rO   listkeysrY   appendrB   index)r,   rY   columnskeylast_valuesfirst_columnrW   r   )r[   rY   r   
write_line   s$    



z"NotebookTrainingTracker.write_liner   c                 C   s   t ||| |d| _| jS )a  
        Add a child progress bar displayed under the table of metrics. The child progress bar is returned (so it can be
        easily updated).

        Args:
            total (`int`): The number of iterations for the child progress bar.
            prefix (`str`, *optional*): A prefix to write on the left of the progress bar.
            width (`int`, *optional*, defaults to 300): The width (in pixels) of the progress bar.
        )r   r'   r   )r#   rP   )r,   r   r   r   r   r   r   	add_child  s    
z!NotebookTrainingTracker.add_childc                 C   s   d| _ |   dS )z0
        Closes the child progress bar.
        N)rP   rC   rG   r   r   r   remove_child  s    z$NotebookTrainingTracker.remove_child)N)Nr   )
rH   rI   rJ   rK   r-   rC   rd   re   rf   __classcell__r   r   rR   r   r%      s   	
r%   c                   @   sV   e Zd ZdZdd Zdd Zdd Zdd	d
Zdd ZdddZ	dddZ
dd ZdS )NotebookProgressCallbackz
    A [`TrainerCallback`] that displays the progress of training or evaluation, optimized for Jupyter Notebooks or
    Google colab.
    c                 C   s   d | _ d | _d| _d S )NF)training_trackerprediction_bar_force_next_updaterG   r   r   r   r-     s    z!NotebookProgressCallback.__init__c                 K   sX   |j tjkrdnd| _d| _d| _| jgdg }|j tjkrF|d t|j	|| _
d S )NEpochStepr   Training LossValidation Loss)evaluation_strategyr   EPOCHrc   Ztraining_lossZlast_logNOr^   r%   Z	max_stepsri   )r,   argsstatecontrolkwargsrQ   r   r   r   on_train_begin"  s    
z'NotebookProgressCallback.on_train_beginc                 K   sV   t |j|jkrt |jn|jd}| jj|jd d| d|j | jd d| _d S )Nr@   r   Epoch r?   r*   r.   F)r
   epochri   r<   global_stepnum_train_epochsrk   )r,   rs   rt   ru   rv   rz   r   r   r   on_step_end+  s    $z$NotebookProgressCallback.on_step_endNc                 K   sh   t |sd S | jd krP| jd k	r4| jt|| _ntt|| _| jd n| j| jjd  d S )Nr   )r   rj   ri   re   rB   r#   r<   r   )r,   rs   rt   ru   Zeval_dataloaderrv   r   r   r   on_prediction_step4  s    

z+NotebookProgressCallback.on_prediction_stepc                 K   s   | j d k	r| j   d | _ d S rM   )rj   r8   r,   rs   rt   ru   rv   r   r   r   
on_predict@  s    

z#NotebookProgressCallback.on_predictc                 K   s:   |j tjkr6d|kr6d|d i}|j|d< | j| d S )Nlossrn   rm   )rp   r   rr   r{   ri   rd   )r,   rs   rt   ru   Zlogsrv   rY   r   r   r   on_logE  s    
zNotebookProgressCallback.on_logc                 K   s\  | j d k	rXddd}t|jD ]}d|kr |d |d<  q>q | jdkrXt|j|d< n
|j|d< d}|D ]}	|	drjt	d	d
|	}qj|
dd }
|
dd }
|
| dd }
|
| dd }
|
| dd }
|
| dd }
| D ]D\}	}|	d}ddd |dd  D }|dkr,d}|||< q| j | | j   d | _d| _d S )NzNo log)rn   ro   r   rn   rl   rm   evalZ_lossz\_loss$r(   Z
total_flosrz   Z_runtimeZ_samples_per_secondZ_steps_per_secondZ_jit_compilation_time_r=   c                 S   s   g | ]}|  qS r   )
capitalize)rV   partr   r   r   rZ   e  s     z8NotebookProgressCallback.on_evaluate.<locals>.<listcomp>r   ZLossro   T)ri   reversedZlog_historyrc   r
   rz   r{   endswithresubpopr   splitjoinrd   rf   rj   rk   )r,   rs   rt   ru   Zmetricsrv   rY   logZmetric_key_prefixkr   vZsplitsnamer   r   r   on_evaluateM  s:    







z$NotebookProgressCallback.on_evaluatec                 K   s2   | j j|jdt|j d|j dd d | _ d S )Nrx   r?   Try   )ri   r<   r{   r
   rz   r|   r   r   r   r   on_train_endp  s      z%NotebookProgressCallback.on_train_end)N)N)N)rH   rI   rJ   rK   r-   rw   r}   r~   r   r   r   r   r   r   r   r   rh     s   		


#rh   )r   )r   r/   typingr   IPython.displayrC   rE   Ztrainer_callbackr   Ztrainer_utilsr   r   r   r   r"   r#   r%   rh   r   r   r   r   <module>   s   
 N