
    fTh=                         S SK r S SKrS SKrS SKJr  S SKJr  SSKJ	r	  SSK
JrJr  S rSS jrS r " S	 S
5      r " S S\5      r " S S\	5      rg)    N)Optional   )TrainerCallback)IntervalStrategy
has_lengthc                 p    [        U 5      n U S-  U S-  S-  U S-  p2nUS:w  a  U SUS SUS 3$ US SUS 3$ )z$Format `t` (in seconds) to (h):mm:ssi  <   r   :02d)int)thmss       S/var/www/auris/envauris/lib/python3.13/site-packages/transformers/utils/notebook.pyformat_timer      s[    AA4i!r'RR!A%&!VaS!C!C!CAc7!Ac71CC    c                 &    SU SU  SU SU SU S3$ )Nz
    <div>
      z
      <progress value='z' max='z' style='width:z<px; height:20px; vertical-align: middle;'></progress>
      z
    </div>
     )valuetotalprefixlabelwidths        r   html_progress_barr   !   s<    h wgeWOE7 Cg 	 r   c                     SnUS-  nU S    H  nUSU S3-  nM     US-  nU SS  HC  nUS	-  nU H0  n[        U[        5      (       a  US
 O
[        U5      nUSU S3-  nM2     US-  nME     US-  nU$ )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elts        r   text_to_html_tabler&   ,   s    <ICCI1Xz!G,,	 55Iab	\!	C",S%"8"8SIc#hC:cU'22I  	]"	  **Ir   c                       \ rS rSrSrSrSr    SS\S\\	   S\
S	\S
   S\4
S jjrSS\S\
S\\	   4S jjrSS jrS rS rSrg)NotebookProgressBar=   aP  
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?Nr   r   leaveparentNotebookTrainingTrackerr   c                     Xl         Uc  SOUU l        X0l        X@l        XPl        S U l        S U l        S U l        S U l        S U l	        S[        R                  ;   a  SU l        g g )N 
VSCODE_PIDg      ?)r   r   r+   r,   r   
last_valuecommentoutputr   r   osenvironupdate_every)selfr   r   r+   r,   r   s         r   __init__NotebookProgressBar.__init__e   sa     
"Nb



2::% #D &r   r   force_updater2   c                    Xl         Ub  X0l        U R                  cg  [        R                  " 5       =U l        U l        U=U l        U l        S=U l        U l        U R                  U l
        SU l        U R                  U5        gXR                  ::  a  U(       d  gU(       dB  U R                  S:  d2  U[        U R                  U R                  -   U R                  5      :  Gaj  U R                  S:  a  U =R                  S-  sl
        [        R                  " 5       nX@R                  -
  U l        XR                  :  a!  U R                  XR                  -
  -  U l        OSU l        XR                  :  a5  U R                  nSU l        U R                   (       d  U R#                  5         O.U R                  b!  U R                  U R                  U-
  -  U l        U R                  U5        Xl        X@l        U R                  b  U R                  S:X  a  SU l        g[%        ['        U R(                  U R                  -  5      S5      U l        gg)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   r2   r1   time
start_time	last_timestart_valueelapsed_timepredicted_remainingwarmupfirst_callswait_for
update_barminr   average_time_per_itemr+   closemaxr   r6   )r7   r   r:   r2   current_times        r   updateNotebookProgressBar.update{   s    
"L??"/3yy{:DOdn166Dt;??D 8#{{DDMOOE"oo%lT--1Uc$//TXTaTaBacgcmcm>n5n!#  A% 99;L , >D'''-1->->%JZJZBZ-[*-1*

"

+/(zzJJL++7+/+E+EV[I[+\(OOE"#O)N**28R8RVW8W ! #C(9(9D<V<V(V$WYZ [/ 6or   c           
          S[        [        U R                  5      5      [        [        U5      5      -
  -  [        U5      -   nU R                  c  SU SU R                   S3U l        OU R
                  c.  SU SU R                   S[        U R                  5       3U l        OSU SU R                   S[        U R                  5       S[        U R
                  5       3U l        U R                  S:X  a  U =R                  S-  sl        O'U =R                  SS	U R                  -  S
 S3-  sl        U =R                  U R                  b  [        U R                  5      S:X  a  SOSU R                   S3-  sl        U R                  5         g )N [/z : < :z < r   z, +inf it/sz, r   .2fz it/s])
lenr    r   r@   r   rA   r   rG   r2   display)r7   r   r2   spaced_values       r   rE   NotebookProgressBar.update_bar   s[   c#djj/2SU_DEE
R$\N!DJJ<v>DJ%%-\N!DJJ<qTEVEV9W8XYDJ L>4::,aD<M<M0N/O P 8 89:< J ))Q.

m+


1t'A'A#A#"FeLL


T\\1S5F!5KcSUVZVbVbUccdQee
r   c                    [        U R                  U R                  U R                  U R                  U R
                  5      U l        U R                  b  U R                  R                  5         g U R                  c9  [        R                  " [        R                  " U R                  5      SS9U l	        g U R                  R                  [        R                  " U R                  5      5        g NT)
display_id)r   r   r   r   r   r   r"   r,   rT   r3   dispHTMLrK   r7   s    r   rT   NotebookProgressBar.display   s    *4::tzz4;;PTPZPZ\`\f\fg;;"KK!;;,,tyy'@TRDKKKtyy89r   c                     U R                   c>  U R                  b0  U R                  R                  [        R                  " S5      5        ggg)zCloses the progress bar.Nr/   )r,   r3   rK   rZ   r[   r\   s    r   rH   NotebookProgressBar.close   s7    ;;4;;#:KKtyy}- $;r   )rG   r2   r@   rC   r"   r   r>   r1   r+   r3   r,   rA   r   r=   r?   r   r6   r   rD   r   )NTN,  )FNN)__name__
__module____qualname____firstlineno____doc__rB   r6   r   r   r    boolr8   rK   rE   rT   rH   __static_attributes__r   r   r   r(   r(   =   s    "H FL
 !%6:$$ $ 	$
 23$ $,1\C 1\t 1\hsm 1\f&	:.r   r(   c                   L   ^  \ rS rSrSrS	U 4S jjrS rS rS
S jrS r	Sr
U =r$ )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).
c                 L   > [         TU ]  U5        Uc  S OU/U l        S U l        g ra   )superr8   inner_table	child_bar)r7   	num_stepscolumn_names	__class__s      r   r8    NotebookTrainingTracker.__init__   s(    ##/#74l^r   c                 d   [        U R                  U R                  U R                  U R                  U R
                  5      U l        U R                  b(  U =R                  [        U R                  5      -  sl        U R                  b)  U =R                  U R                  R                  -  sl        U R                  c9  [        R                  " [        R                  " U R                  5      SS9U l
        g U R                  R                  [        R                  " U R                  5      5        g rX   )r   r   r   r   r   r   r"   rm   r&   rn   r3   rZ   rT   r[   rK   r\   s    r   rT   NotebookTrainingTracker.display   s    *4::tzz4;;PTPZPZ\`\f\fg'NN01A1ABBN>>%NNdnn666N;;,,tyy'@TRDKKKtyy89r   c                 F   U R                   c8  [        UR                  5       5      [        UR                  5       5      /U l         gU R                   S   nUR                  5        H  nX2;  d  M
  UR	                  U5        M     X R                   S'   [        U R                   5      S:  a  U R                   S   nU R                   S   S   nUS   X   :w  a6  U R                   R	                  U Vs/ s H  ofU;   a  X   OSPM     sn5        gUnU H-  nXgR                  5       ;  d  M  XBR                  U5         Xv'   M/     U Vs/ s H  ogU   PM	     snU R                   S'   gU R                   R	                  U Vs/ s H  oaU   PM	     sn5        gs  snf s  snf s  snf )ze
Write the values in the inner table.

Args:
    values (`Dict[str, float]`): The values to display.
Nr   r   zNo Log)rm   listkeysvaluesappendrS   index)r7   ry   columnskeylast_valuesfirst_columnc
new_valuess           r   
write_line"NotebookTrainingTracker.write_line   sx    # $V[[] 3T&--/5JKD&&q)G{{}%NN3' % #*Q4##$q("..r2#//215q>V%99$$++]d,e]dXYv+VY8-S]d,ef "(J$OO$55,7a8H,IJM % DK+K7aqM7+KD$$R(  ''G(DGqG(DE -f ,L(Ds   0FF>Fc                 8    [        XXS9U l        U R                  $ )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(   rn   )r7   r   r   r   s       r   	add_child!NotebookTrainingTracker.add_child  s     -U$\~~r   c                 2    SU l         U R                  5         g)z 
Closes the child progress bar.
N)rn   rT   r\   s    r   remove_child$NotebookTrainingTracker.remove_child  s     r   )rn   r"   rm   r3   ra   )Nr`   )rb   rc   rd   re   rf   r8   rT   r   r   r   rh   __classcell__)rq   s   @r   r-   r-      s'    
	:F> r   r-   c                   T    \ rS rSrSrS rS rS rSS jrS r	SS	 jr
SS
 jrS rSrg)NotebookProgressCallbacki   z~
A [`TrainerCallback`] that displays the progress of training or evaluation, optimized for Jupyter Notebooks or
Google colab.
c                 .    S U l         S U l        SU l        g )NF)training_trackerprediction_bar_force_next_updater\   s    r   r8   !NotebookProgressCallback.__init__&  s     $""'r   c                 $   UR                   [        R                  :X  a  SOSU l        SU l        SU l        U R                  /S/-   nUR                   [        R                  :w  a  UR                  S5        [        UR                  U5      U l
        g )NEpochStepr   Training LossValidation Loss)eval_strategyr   EPOCHr   training_losslast_logNOrz   r-   	max_stepsr   )r7   argsstatecontrolkwargsrp   s         r   on_train_begin'NotebookProgressCallback.on_train_begin+  s}    '+'9'9=M=S=S'SGY_))*o->>!1!4!44 12 7 Vr   c                 "   [        UR                  5      UR                  :X  a  [        UR                  5      OUR                  S nU R                  R                  UR                  S-   SU SUR
                   3U R                  S9  SU l        g )NrQ   r   Epoch rP   r2   r:   F)r   epochr   rK   global_stepnum_train_epochsr   )r7   r   r   r   r   r   s         r   on_step_end$NotebookProgressCallback.on_step_end4  s    $'$4$CEKK EKKX[K\$$!UG1U%;%;$<=00 	% 	

 #(r   Nc                 |   [        U5      (       d  g U R                  cl  U R                  b*  U R                  R                  [	        U5      5      U l        O[        [	        U5      5      U l        U R                  R                  S5        g U R                  R                  U R                  R                  S-   5        g )Nr   )r   r   r   r   rS   r(   rK   r   )r7   r   r   r   eval_dataloaderr   s         r   on_prediction_step+NotebookProgressCallback.on_prediction_step=  s    /**&$$0&*&;&;&E&Ec/FZ&[#&9#o:N&O#&&q)&&t':':'@'@1'DEr   c                 `    U R                   b  U R                   R                  5         S U l         g ra   )r   rH   r7   r   r   r   r   s        r   
on_predict#NotebookProgressCallback.on_predictI  s(    *%%'"r   c                     UR                   [        R                  :X  a9  SU;   a2  SUS   0nUR                  US'   U R                  R                  U5        g g g )Nlossr   r   )r   r   r   r   r   r   )r7   r   r   r   logsr   ry   s          r   on_logNotebookProgressCallback.on_logN  sW    !1!4!444%tF|4F"..F6N!!,,V4	 :H4r   c                    U R                   Gb  SSS.n[        UR                  5       H  nSU;   d  M  US   US'     O   U R                  S:X  a  [	        UR
                  5      US'   OUR                  US'   SnU H3  n	U	R                  S5      (       d  M  [        R                  " S	S
U	5      nM5     UR                  SS 5      n
UR                  SS 5      n
UR                  U S3S 5      n
UR                  U S3S 5      n
UR                  U S3S 5      n
UR                  U S3S 5      n
UR                  5        HT  u  pU	R                  S5      nSR                  USS   Vs/ s H  oR                  5       PM     sn5      nUS:X  a  SnXU'   MV     U R                   R                  U5        U R                   R!                  5         S U l        SU l        g g s  snf )NzNo log)r   r   r   r   r   r   eval_lossz\_loss$r/   
total_flosr   _runtime_samples_per_second_steps_per_second_jit_compilation_time_rN   r   Lossr   T)r   reversedlog_historyr   r   r   r   endswithresubpopr!   splitjoin
capitalizer   r   r   r   )r7   r   r   r   metricsr   ry   logmetric_key_prefixkr   vsplitspartnames                  r   on_evaluate$NotebookProgressCallback.on_evaluateV  s     ,'/HMF 1 12S=.1&kF?+ 3
   G+"%ekk"2w!&!2!2v &::g&&(*z2q(A%  L$/AGT*A01:DAA011DEtLA011BCTJA011FGNAxxvabz Jzt!2z JK6>,D t ( !!,,V4!!..0"&D&*D#A -. !Ks   #G
c                     U R                   R                  UR                  S[        UR                  5       SUR
                   3SS9  S U l         g )Nr   rP   Tr   )r   rK   r   r   r   r   r   s        r   on_train_end%NotebookProgressCallback.on_train_endy  sR    $$S-.a0F0F/GH 	% 	

 !%r   )r   r   r   r   r   r   ra   )rb   rc   rd   re   rf   r8   r   r   r   r   r   r   r   rh   r   r   r   r   r      s2    
(
W(
F#
5!+F%r   r   )r`   )r4   r   r<   typingr   IPython.displayrT   rZ   trainer_callbackr   trainer_utilsr   r   r   r   r&   r(   r-   r   r   r   r   <module>r      sW    
 	    . 8D"R. R.jK1 K\_% _%r   