o
    i4                     @   s*  d dl Z d dlmZ d dlmZmZmZmZmZm	Z	m
Z
mZmZ 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 erHdd	lmZ d
dde	e de	e dedee	e e	e e	e f fddZ			ddedede
eeef  fddZ			dde
e dede
eeef  fddZdS )    N)Path)	IOTYPE_CHECKINGAnyCallableDictListOptionalTupleUnion)Printer   )util)Errors)registry)Language   )	max_widthcolswidthsr   returnc                 C   sr   g }g }t | |D ]%\}}t||kr|d |d  d }||  |tt|| q	||dd |D fS )N   z...c                 S      g | ]}d qS )r .0_r   r   J/home/ubuntu/.local/lib/python3.10/site-packages/spacy/training/loggers.py
<listcomp>       zsetup_table.<locals>.<listcomp>)ziplenappenduppermax)r   r   r   
final_colsfinal_widthscolwidthr   r   r   setup_table   s   r*   FTprogress_barconsole_outputoutput_filec                 C   s   t | du rdnd||dS )a  The ConsoleLogger.v2 prints out training logs in the console and/or saves them to a jsonl file.
    progress_bar (bool): Whether the logger should print a progress bar tracking the steps till the next evaluation pass.
    console_output (bool): Whether the logger should print the logs on the console.
    output_file (Optional[Union[str, Path]]): The file to save the training logs to.
    FNevalr+   r,   r-   )console_logger_v3r/   r   r   r   console_logger    s
   
r1   c                    s   d rt  rd jd  sjd jdd tjtjfdddtdtd	t	t
ttttf  gd
f t
g d
f f f fdd}|S )ax  The ConsoleLogger.v3 prints out training logs in the console and/or saves them to a jsonl file.
    progress_bar (Optional[str]): Type of progress bar to show in the console. Allowed values:
        train - Tracks the number of steps from the beginning of training until the full training run is complete (training.max_steps is reached).
        eval - Tracks the number of steps between the previous and next evaluation (training.eval_frequency is reached).
    console_output (bool): Whether the logger should print the logs on the console.
    output_file (Optional[Union[str, Path]]): The file to save the training logs to.
    FTr   )parentsnlpr   stdoutstderrr   Nc           	         s  	fddt ddd rd d d nr0d  tdd	d
dd | jD | jd d | jd d  | jd d }dd | D dd D }rdtddg|  dg ddgdd |D  dd D  dg d\}
j|d jdd D d d d}d ur|vrt	t
jj|d d!ttttf  d"d f 
fd#d$}d'fd%d&}||fS )(Nc                    s   t |  ddS )NT)fileflush)print)text)r4   r   r   <lambda>H   s    z:console_logger_v3.<locals>.setup_printer.<locals>.<lambda>T)no_printz Saving logs is disabled because z already exists.zSaving results to wzutf-8)encodingc                 S   s$   g | ]\}}t |d r|jr|qS )is_trainable)hasattrr>   )r   nameprocr   r   r   r   Y   s    z<console_logger_v3.<locals>.setup_printer.<locals>.<listcomp>training	max_stepseval_frequencyscore_weightsc                 S   s   g | ]
\}}|d ur|qS Nr   )r   r(   valuer   r   r   r   a   s    c                 S   s   g | ]}d | qS )zLoss r   )r   piper   r   r   r   b   s    r   E#Scorer      c                 S   r   )   r   r   r   r   r   r   h   r    c                 S   r   )rL   r   r   r   r   r   r   h   r    )r   r   )r   spacingc                 S   s   g | ]}d | qS )-r   )r   r)   r   r   r   r   k   s    )rN   )trainr.   )
unexpectedexpectedinfor   c              	      s  | d u rd ur d d S g }i }D ]}|dt| d |  t| d | ||< qg }i }D ]=}| d |d}zt|}W n ty\   tjj|t|d}t	|d w |dkre|d9 }|d| ||t
|< q6| d	 | d
 g| | dt| d g }	r| d	 | d
 ||t| d d}
t|
d  d ur   rj|		d rdkrψ}d| d	  }| d
 }n}d| d	 d  }d}tj|d d
|d| d S d S d S )N   z{0:.2f}lossesother_scoresg        )r@   
score_typespeedd   epochstepscore)rZ   r[   rU   scoresr\   
)r   alignsrN   rP   zLast Eval Epoch: zEpoch r   F)totaldisableleaver6   initial)updater#   formatfloatget	TypeErrorr   E916type
ValueErrorstrwritesrsly
json_dumpscloserowtqdmset_description)rS   rU   
log_losses	pipe_namer]   
log_scoresr(   r\   errdatalog_datar`   descrc   )r,   rD   logged_pipesrC   msgoutput_streamprogressr+   
score_colsrN   r5   table_alignstable_widthsrm   r   r   log_stepu   s   



z:console_logger_v3.<locals>.setup_printer.<locals>.log_stepc                      s    r    d S d S rF   )rp   r   )r}   r   r   finalize   s   z:console_logger_v3.<locals>.setup_printer.<locals>.finalize)r   N)r   warnrS   openpipelineconfigitemsr*   rq   rk   r   E1048re   r	   r   rl   r   )	r3   r4   r5   rE   	loss_colstable_headerexpected_progress_typesr   r   
_log_existr,   r-   r+   )rD   r{   rC   r|   r}   r~   r   rN   r5   r4   r   r   rm   r   setup_printerE   sP   

(<Jz(console_logger_v3.<locals>.setup_printer)r   ensure_pathexistsr2   mkdirsysr4   r5   r   r
   r   r	   r   rl   r   )r+   r,   r-   r   r   r   r   r0   1   s&   
( r0   )FTN)NTN)r   pathlibr   typingr   r   r   r   r   r   r	   r
   r   rn   rr   wasabir    r   errorsr   r   languager   rl   intr*   boolr1   r0   r   r   r   r   <module>   sR    ,

