o
    RTh(                     @   s   zd dl Z dZW n   dZY er;d 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
 d dlmZ d dlmZ G dd	 d	ZdS )
    NTF)Path)FileFineTune)numpy)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GPT-3c           
         s   t sdS |rtj|d}|dd |g}n"t }|r#|ddu r)td dS |d |dur3| ndd }|du rB|du rBdnd fdd	|D }	s\t|	s\t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.
        N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projectshow_individual_warningsr   V/home/air/segue/gemini/backup/venv/lib/python3.10/site-packages/openai/wandb_logger.py
<listcomp>I   s    	z$WandbLogger.sync.<locals>.<listcomp>z'No new successful fine-tunes were foundu&   🎉 wandb sync completed successfully)WANDB_AVAILABLEr   retrievepoplistgetprintany)
r   r
   n_fine_tunesr   r   r   r   r   
fine_tunesfine_tune_loggedr   r   r   sync    s*   	zWandbLogger.syncc              	   K   s  | d}| d}|dkr|rtd| d| d d S z|d d d }	tj|	d	d
}
W n   |r>td| d Y d S | d| }|d urS| d| }| |}|r|j d}|r|dkrxtd| d|j  |swtd ntd| d |dks|rtd| d |dkr|sd S tj	dd| 
|||||d| tt|
}| D ]!\}}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
   status	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)job_typeconfigr   r   namer
   c                 S   s    i | ]\}}t |s||qS r   )npisnan)r   kvr   r   r   
<dictcomp>   s     z.WandbLogger._log_fine_tune.<locals>.<dictcomp>step)r2   fine_tuned_modelTr   )r   r   r   downloaddecode_get_wandb_runsummaryurlwandbinit_get_configpdread_csvioStringIOiterrowsitemsr   intlog_log_artifactsfinish)r   r   r   r   r   r   r   fine_tune_idr%   
results_idresultsrun_path	wandb_runwandb_status
df_results_rowmetricsr2   r3   r   r   r   r   Z   s   









zWandbLogger._log_fine_tunec                 C   s$   | j st rd| _ d S tdd S )NTzYou need to log in to wandb)
_logged_inr9   login	Exception)r   r   r   r   _ensure_logged_in   s
   
zWandbLogger._ensure_logged_inc                 C   @   |    z| jd u rt | _| j|W S  ty   Y d S w N)rS   
_wandb_apir9   ApirunrR   )r   rI   r   r   r   r6         

zWandbLogger._get_wandb_runc                 C   rT   rU   )rS   rV   r9   rW   artifactrR   )r   artifact_pathr   r   r   _get_wandb_artifact   rY   zWandbLogger._get_wandb_artifactc                 C   sZ   t |}dD ]}||rt|| r|| d ||< q|dr+tj|d |d< |S )N)training_filesvalidation_filesr'   r   
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r$t|d r$|d d nd }|ddf|ddffD ]\}}}|d urB| ||||| q0| d}	tjdd|d}
|
jd	d
dd}tj||dd W d    n1 skw   Y  tjj	|
d|	gd d S )Nr]   r   r^   trainvalidr
   fine_tune_detailstypemetadatazfine_tune_details.jsonwr(   modeencoding   )indentlatestaliases)
r   ra   _log_artifact_inputsr9   Artifactnew_filejsondumprX   log_artifact)r   r   r   r   training_filevalidation_filefileprefixartifact_typerF   rZ   fr   r   r   rD      sD   



zWandbLogger._log_artifactsc                 C   s  |d }t |d j}t |d j}| d| }	tdd|	}	|}
| d|	 d|
 }|d ur7| d| }| |}|d u rztj|dd	}W n   t	d
| d Y d S t
j|	||d}|j|dd	d}|| W d    n1 szw   Y  z| |\}}||| t
jd| |i ||jd< W n   t	d
| d Y nt
jd| |jdi t
jj|d|
gd d S )Nr
   filename-z[^a-zA-Z0-9_\-.]rM   r)   :r	   r(   zFile zX could not be retrieved. Make sure you are allowed to download training/validation filesrh   rk   rl   n_rA   z' could not be read as a valid JSON filerq   rr   )r   r,   stemresubr\   r   r4   r5   r   r9   ru   rv   write_make_tableaddr+   updaterj   r   rX   use_artifact)r   r|   r}   r~   r   r   file_idr   r   artifact_nameartifact_aliasr[   rZ   file_contentr   tablen_itemsr   r   r   rt      s>   

z WandbLogger._log_artifact_inputsc                 C   s*   t jt|ddd}tj|dt|fS )NrecordsT)orientlines)	dataframe)r<   	read_jsonr>   r?   r9   Tablera   )r   r   dfr   r   r   r   )  s   zWandbLogger._make_table)NNr   NF)__name__
__module____qualname____doc__r   r   rV   rP   classmethodr$   r   rS   r6   r\   r;   rD   rt   r   r   r   r   r   r      s:    
9
X

	
	
	
#
+r   )r9   r   rb   r>   rw   r   pathlibr   openair   r   openai.datalib.numpy_helperr   r-   openai.datalib.pandas_helperr   r<   r   r   r   r   r   <module>   s    