o
    mi{H                     @   s4  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
mZmZmZm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 d dlmZ ejdd	d
dZeejedk rmedej dd dlmZ d dlmZ d dl mZm!Z! ejddd
dZ"ejddd
dZ#G dd dZ$dS )    N)AnyDictListOptionalTupleUnion)util)Table)	telemetry)Run)parse_versionopenaizOThis integration requires `openai`. To install, please run `pip install openai`F)namerequiredlazyz1.12.0zSThis integration requires openai version 1.12.0 and above. Your current version is z' To fix, please `pip install -U openai`)OpenAI)FineTuningJob)ErrorHyperparametersnumpyz]`numpy` not installed >> This integration requires numpy!  To fix, please `pip install numpy`pandasz``pandas` not installed >> This integration requires pandas!  To fix, please `pip install pandas`c                   @   s  e Zd ZU dZdZeej ed< dZ	e
ed< dZee ed< dZee ed< e											
	d9dee dee dee dedee de
de
de
dededeeef defddZededefddZedededee de
de
de
dededeeef fddZedd  Zed!efd"d#Zed$efd%d&Zededeeef fd'd(Zed)efd*d+Zed,edeee ef fd-d.Z!edededee de
de
dededdfd/d0Z"ed1ee d2ed3ededee de
ddfd4d5Z#ed6ede$e%ef fd7d8Z&dS ):WandbLoggerzJLog OpenAI fine-tunes to [Weights & Biases](https://wandb.me/openai-docs).N
_wandb_apiF
_logged_inopenai_client_runOpenAI-Fine-TuneTmodel-metadatamodelfine_tune_job_idnum_fine_tunesprojectentity	overwritewait_for_job_successlog_datasetsmodel_artifact_namemodel_artifact_typekwargs_wandb_initreturnc              
   K   s  |du rt  }|| _|rtd |jjj|d}|g}n"|jj }|r*|jdu r1t	d dS |j|dur:| ndd }|dupF|du}g }|D ]}|j
}| d| }|durb| d| }| |}|r|jd}|r|dkr|st	d| d	|j d
 n|dks|r|dkrt	d| d td| d d}|dkr|s dS tjdu rtjdd||||d|| _ntj| _|r| |}| j|||||||	|
fi | qK|st|st	d dS )a  Sync fine-tunes to Weights & Biases.

        :param fine_tune_job_id: The id of the fine-tune (optional)
        :param openai_client: Pass the `OpenAI()` client (optional)
        :param num_fine_tunes: Number of most recent fine-tunes to log when an fine_tune_job_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 overwrite: Forces logging and overwrite existing wandb run of the same fine-tune.
        :param wait_for_job_success: Waits for the fine-tune to be complete and then log metrics to W&B. By default, it is True.
        :param model_artifact_name: Name of the model artifact that is logged
        :param model_artifact_type: Type of the model artifact that is logged
        NzRetrieving fine-tune job...fine_tuning_job_idzNo fine-tune has been retrieved/status	succeeded
Fine-tune z) has already been logged successfully at z<. Use `overwrite=True` 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 overwrittenTz	fine-tune)job_typer!   r"   r   idz'No new successful fine-tunes were foundu&   🎉 wandb sync completed successfully )r   r   wandbtermlogfine_tuningjobsretrievelistdatatermwarnr1   _get_wandb_runsummarygeturlruninitr   _wait_for_job_success_log_fine_tuneany)clsr   r   r    r!   r"   r#   r$   r%   r&   r'   r(   	fine_tune
fine_tunesshow_individual_warningsfine_tune_loggedfine_tune_idrun_path	wandb_runwandb_statusr2   r2   b/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/integration/openai/fine_tuning.pysync:   s   








	
zWandbLogger.syncrE   c                 C   s   t d t d 	 |jdkrt d |S |jdkr(t d|j d |S |jd	kr9t d|j d
 |S td | jjj	j
|jd}q)Nz<Waiting for the OpenAI fine-tuning job to finish training...zuTo avoid blocking, you can call `WandbLogger.sync` with `wait_for_job_success=False` after OpenAI training completes.Tr.   z[Fine-tuning finished, logging metrics, model metadata, and run metadata to Weights & Biasesfailedr/   z" has failed and will not be logged	cancelledz% was cancelled and will not be logged
   r*   )r3   r4   r-   r:   r1   timesleepr   r5   r6   r7   )rD   rE   r2   r2   rM   rA      s2   





z!WandbLogger._wait_for_job_successrG   c	              	   K   s  |j }
|j}tj| jd}d|j_W d    n1 sw   Y  |dkr5|r3td|
 d| d d S z.|j	d }z| j
jj|d }t|d	}W n tya   | j
jj|dj}Y nw W n tjyy   |rvtd|
 d
 Y d S w | jj| | tt|}| D ]"\}}dd | D }|d}|d urt|}| jj ||d q|j!}|d ur|| jj"d< | #||||||| d| jj"d< | j$  dS )N)r?   Tr.   r/   z has the status "z" and will not be loggedr   file_idutf-8z& has no results and will not be loggedc                 S   s    i | ]\}}t |s||qS r2   )npisnan.0kvr2   r2   rM   
<dictcomp>   s     z.WandbLogger._log_fine_tune.<locals>.<dictcomp>step)r^   fine_tuned_modelr-   )%r1   r-   r
   contextr   featureopenai_finetuningr3   r:   result_filesr   filescontentreadbase64	b64decodedecode	Exceptiontextr   NotFoundErrorconfigupdate_get_configpdread_csvioStringIOiterrowsitemspopintlogr_   r<   _log_artifactsfinish)rD   rE   r!   r"   r#   rG   r%   r&   r'   r(   rI   r-   tel
results_idencoded_resultsresults
df_results_rowmetricsr^   r_   r2   r2   rM   rB      sl   




zWandbLogger._log_fine_tunec                 C   s$   | j st rd| _ d S tdd S )NTzIt appears you are not currently logged in to Weights & Biases. Please run `wandb login` in your terminal or `wandb.login()` in a notebook.When prompted, you can obtain your API key by visiting wandb.ai/authorize.)r   r3   loginrj   )rD   r2   r2   rM   _ensure_logged_in  s   
zWandbLogger._ensure_logged_inrJ   c                 C   @   |    z| jd u rt | _| j|W S  ty   Y d S w N)r   r   r3   Apir?   rj   )rD   rJ   r2   r2   rM   r;   #     

zWandbLogger._get_wandb_runartifact_pathc                 C   r   r   )r   r   r3   r   artifactrj   )rD   r   r2   r2   rM   _get_wandb_artifact-  r   zWandbLogger._get_wandb_artifactc                 C   s   t |}|d d |d< |drtj|d d|d< |dr0tj|d d|d< |dr>| |d |d< |drL| |d |d< |S )Nrc   r   
created_atz%Y-%m-%d %H:%M:%Sfinished_athyperparameterserror)dictr=   datetimefromtimestampstrftimesanitize)rD   rE   rm   r2   r2   rM   ro   7  s&   



zWandbLogger._get_configr   c                 C   s>   i }z|j |d< |j|d< |j|d< W |S  ty   Y d S w )Nn_epochs
batch_sizelearning_rate_multiplier)r   r   r   rj   )rD   r   hyperparamsr2   r2   rM   _unpack_hyperparametersI  s   

z#WandbLogger._unpack_hyperparametersinputc                    sf   t tttg t| ttfrt| S t| tr! fdd|  D S t| t	r/ fdd| D S t| S )Nc                    s*   i | ]\}}|t | v r|nt|qS r2   typestrrY   valid_typesr2   rM   r]   `  s    z(WandbLogger.sanitize.<locals>.<dictcomp>c                    s$   g | ]}t | v r|nt|qS r2   r   )rZ   r\   r   r2   rM   
<listcomp>d  s   $ z(WandbLogger.sanitize.<locals>.<listcomp>)
boolrw   floatr   
isinstancer   r   r   ru   r8   )r   r2   r   rM   r   Z  s   


zWandbLogger.sanitizec              	   C   s  |r5t d |jr|jnd }|jr|jnd }	|ddf|	ddffD ]\}
}}|
d ur4| |
||||| q!|j}t j||t|d}|jddd	d
+}t|}| 	|d |d< | 	|d |d< | 	|}t
j||dd W d    n1 sxw   Y  | jj|d|gd d S )Nz$Logging training/validation files...traintraining_filesvalidvalidation_files)r   metadatazmodel_metadata.jsonwrV   )modeencodingr   r      )indentlatestaliases)r3   r4   training_filevalidation_file_log_artifact_inputsr1   Artifactr   new_filer   jsondumpr   log_artifact)rD   rE   r!   r"   r%   r#   r&   r'   r   r   fileprefixartifact_typerI   r   fdict_fine_tuner2   r2   rM   ry   h  s@   


zWandbLogger._log_artifactsrU   r   r   c              
   C   s  | d| }t dd|}|}| d| d| }	|d ur%| d|	 }	| |	}
|
d u s0|rz
| jjj|d}W n tjyN   t	d| d Y d S w tj
||d	}
tjd
d}||j |j}W d    n1 spw   Y  |
|| t| z,| |j\}}|
|| | j| d|i | jjd| |i ||
jd< W n. ty } zt	d| d| d W Y d }~nd }~ww | jjd| |
jdi | jj|
d|gd d S )N-z[^a-zA-Z0-9_\-.]r   r,   :rT   zFile zd could not be retrieved. Make sure you have OpenAI permissions to download training/validation files)r   F)delete_datan_ru   zIssue saving z( as a Table to Artifacts, exception:
  ''r   r   )resubr   r   rd   re   r   rl   r3   	termerrorr   tempfileNamedTemporaryFilewriter   add_fileosunlink_make_tablerk   addr   rx   rm   rn   r   rj   r=   use_artifact)rD   rU   r   r   r!   r"   r#   artifact_nameartifact_aliasr   r   file_contenttmp_filetmp_file_pathtablen_itemser2   r2   rM   r     sJ   


 z WandbLogger._log_artifact_inputsr   c                 C   s   t jg dd}tjt|ddd}| D ]"\}}|j}t|dks&J |	|d d |d	 d |d
 d  q|t|fS )N)zrole: systemz
role: userzrole: assistant)columnsrecordsT)orientlines   r   re      r   )
r3   r	   rp   	read_jsonrr   rs   rt   messageslenadd_data)rD   r   r   df_idxmessager   r2   r2   rM   r     s   


zWandbLogger._make_table)
NNNr   NFTTr   r   )'__name__
__module____qualname____doc__r   r   r3   r   __annotations__r   r   r   r   r   r   classmethodr   rw   r   r   rN   r   rA   rB   r   r;   r   ro   r   r   staticmethodr   r   r   ry   r   r   r	   r   r2   r2   r2   rM   r   2   s   
 	

t	

L
			/9 r   )%rg   r   rr   r   r   r   r   rR   typingr   r   r   r   r   r   r3   r   wandb.data_typesr	   wandb.sdk.libr
   wandb.sdk.wandb_runr   
wandb.utilr   
get_moduler   __version__r   r   openai.types.fine_tuningr   (openai.types.fine_tuning.fine_tuning_jobr   rW   rp   r   r2   r2   r2   rM   <module>   sL     