a
    f	g*                     @   s   zd dl Z dZW n   dZY n0 erd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZmZ G dd	 d	ZdS )
    NTF)Path)FileFineTuneFineTuningJob)numpy)assert_has_pandaspandasc                   @   s   e Zd ZdZesed ndZdZedddZ	edd	 Z
ed
d Zedd Zedd Zedd Zedd Zedd Zedd ZdS )WandbLoggerzL
    Log fine-tunes to [Weights & Biases](https://wandb.me/openai-docs)
    z@Logging requires wandb to be installed. Run `pip install wandb`.NFOpenAI-Fine-Tunec                    s   t   tsdS |rLtd r,tj|d}ntj|d}|dd |g}	nVrZt }	ntt }	|	rx|		ddu rtd dS |	d |dur| ndd }	|du r|du rdnd fd	d
|	D }
st
|
std dS )aD  
        Sync fine-tunes to Weights & Biases.
        :param id: The id of the fine-tune (optional)
        :param n_fine_tunes: Number of most recent fine-tunes to log when an id is not provided. By default, every fine-tune is synced.
        :param project: Name of the project where you're sending runs. By default, it is "GPT-3".
        :param entity: Username or team name where you're sending runs. By default, your default entity is used, which is usually your username.
        :param force: Forces logging and overwrite existing wandb run of the same fine-tune.
        NzRetrieving fine-tune job...ideventsdatazNo fine-tune has been retrievedFTc              	      s(   g | ] } j |fi qS  )_log_fine_tune).0	fine_tuneclsentityforcekwargs_wandb_initlegacyprojectshow_individual_warningsr   V/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/openai/wandb_logger.py
<listcomp>R   s   
z$WandbLogger.sync.<locals>.<listcomp>z'No new successful fine-tunes were foundu&   🎉 wandb sync completed successfully)r   WANDB_AVAILABLEprintr   retriever   poplistZauto_paging_itergetany)r   r   Zn_fine_tunesr   r   r   r   r   r   Z
fine_tunesZfine_tune_loggedr   r   r   sync    s6    

zWandbLogger.syncc              	   K   s  | d}| d}	|	dkr:|r6td| d|	 d d S z8|rR|d d d }
n|d d }
tj|
d	d
}W n"   |rtd| d Y d S 0 | d| }|d ur| d| }| |}|rP|j d}|r<|dkrtd| d|j  |std ntd| d |dks,|r<td| d |dkrP|sPd S tj	f d| 
|||||d| tt|}| D ]F\}}dd | D }|d}|d urt|}tj||d q| d}|d ur|tjd< | ||| dtjd< t  dS )Nr   statusZ	succeededz
Fine-tune z has the status "z" and will not be loggedresult_filesr   r   utf-8z& has no results and will not be logged/z) has already been logged successfully at zXUse "--force" in the CLI or "force=True" in python if you want to overwrite previous runzA run for fine-tune z3 was previously created but didn't end successfullyz.A new wandb run will be created for fine-tune z% and previous run will be overwrittenz	fine-tune)Zjob_typeconfigr   r   namer   c                 S   s    i | ]\}}t |s||qS r   )npisnan)r   kvr   r   r   
<dictcomp>       z.WandbLogger._log_fine_tune.<locals>.<dictcomp>step)r1   fine_tuned_modelT)r"   r   r   downloaddecode_get_wandb_runsummaryurlwandbinit_get_configpdZread_csvioStringIOZiterrowsitemsr    intlog_log_artifactsfinish)r   r   r   r   r   r   r   r   fine_tune_idr%   Z
results_idresultsrun_pathZ	wandb_runZwandb_statusZ
df_results_rowZmetricsr1   r2   r   r   r   r   d   s    











zWandbLogger._log_fine_tunec                 C   s"   | j st rd| _ ntdd S )NTzYou need to log in to wandb)
_logged_inr8   login	Exception)r   r   r   r   _ensure_logged_in   s    zWandbLogger._ensure_logged_inc                 C   sD   |    z"| jd u rt | _| j|W S  ty>   Y d S 0 d S N)rK   
_wandb_apir8   ApirunrJ   )r   rE   r   r   r   r5      s    

zWandbLogger._get_wandb_runc                 C   sD   |    z"| jd u rt | _| j|W S  ty>   Y d S 0 d S rL   )rK   rM   r8   rN   artifactrJ   )r   artifact_pathr   r   r   _get_wandb_artifact   s    

zWandbLogger._get_wandb_artifactc                 C   sZ   t |}dD ]*}||rt|| r|| d ||< q|drVtj|d |d< |S )N)training_filesvalidation_filesr&   r   Z
created_at)dictr"   lendatetimefromtimestamp)r   r   r)   keyr   r   r   r:      s    
zWandbLogger._get_configc                 C   s   | dr"t|d r"|d d nd }| drHt|d rH|d d nd }|ddf|ddffD ]$\}}}|d ur`| ||||| q`| d}	tjdd|d}
|
jd	d
dd }tj||dd W d    n1 s0    Y  tjj	|
d|	gd d S )NrS   r   rT   ZtrainZvalidr   Zfine_tune_detailstypemetadatazfine_tune_details.jsonwr'   modeencoding   )indentlatestaliases)
r"   rV   _log_artifact_inputsr8   Artifactnew_filejsondumprO   Zlog_artifact)r   r   r   r   Ztraining_fileZvalidation_filefileprefixartifact_typerC   rP   fr   r   r   rA      s@    


.zWandbLogger._log_artifactsc                 C   s  |d }t |d j}t |d j}| d| }	tdd|	}	|}
| d|	 d|
 }|d urn| d| }| |}|d u r`ztj|dd	}W n   t	d
| d Y d S 0 t
j|	||d}|j|dd	d}|| W d    n1 s0    Y  z>| |\}}||| t
jd| |i ||jd< W n   t	d
| d Y n0 nt
jd| |jdi t
jj|d|
gd d S )Nr   filename-z[^a-zA-Z0-9_\-.]rF   r(   :r   r'   zFile zX could not be retrieved. Make sure you are allowed to download training/validation filesrZ   r]   r^   Zn_r>   z' could not be read as a valid JSON filerc   rd   )r   r*   stemresubrR   r   r3   r4   r   r8   rg   rh   write_make_tableaddr)   updater\   r"   rO   Zuse_artifact)r   rk   rl   rm   r   r   Zfile_idro   rr   Zartifact_nameZartifact_aliasrQ   rP   file_contentrn   tableZn_itemsr   r   r   rf     s<    


(z WandbLogger._log_artifact_inputsc                 C   s*   t jt|ddd}tj|dt|fS )NrecordsT)Zorientlines)Z	dataframe)r;   	read_jsonr<   r=   r8   ZTablerV   )r   ry   Zdfr   r   r   rv   7  s    zWandbLogger._make_table)NNr
   NFF)__name__
__module____qualname____doc__r   r   rM   rH   classmethodr$   r   rK   r5   rR   r:   rA   rf   rv   r   r   r   r   r	      s:   
      C
\

	
	
	
#
+r	   )r8   r   rW   r<   ri   rs   pathlibr   Zopenair   r   r   Zopenai.datalib.numpy_helperr   r+   Zopenai.datalib.pandas_helperr   r   r;   r	   r   r   r   r   <module>   s   
