o
    xi                     @   s   	 	dddZ d S )NTc                    s  ddl ddlddlm ddlm}m} ddlm ddl	m
}m}m}m}m}m}	m}
m} ddlddlm ||	|
|f||||fdd	 d
d fddd fdd	
d fdd		d fdd	d fdd	d fdd	 |}g }g }|j D ]}|j|ju r|| q|| qtg ||d|
 |jd|R }|j|ddd |D djv rňjd d<  	
fdd}du r|S |S )!z/Wrap a standard python function and log to W&B.    N)wraps)	Parameter	signature)
components)InputArtifactInputBinaryFile	InputPathInputTextFileOutputArtifactOutputBinaryFile
OutputPathOutputTextFile)	telemetryc                 S   sV   t | }|j}t|dks|d turdS t|dd }t|ts"dS tdd |D S )N   r   F_fieldsc                 s   s    | ]}t |tV  qd S N)
isinstancestr).0n r   W/home/ubuntu/.local/lib/python3.10/site-packages/wandb/integration/kfp/wandb_logging.py	<genexpr>&   s    z;wandb_log.<locals>.isinstance_namedtuple.<locals>.<genexpr>)type	__bases__lentuplegetattrr   all)xtbfr   r   r   isinstance_namedtuple   s   
z(wandb_log.<locals>.isinstance_namedtuplec                 S   s   d| j  dS )Nz<iframe src="za?kfp=true" style="border:none;width:100%;height:100%;min-width:900px;min-height:600px;"></iframe>)urlrunr   r   r   get_iframe_html(   s   z"wandb_log.<locals>.get_iframe_htmlc                     s     d} |  dS )NWANDB_KUBEFLOW_URLz/#/runs/details/{workflow.uid})getenv)wandb_kubeflow_url)osr   r   get_link_back_to_kubeflow+   s   

z,wandb_log.<locals>.get_link_back_to_kubeflowc                    s$   ||j | <  d|  d|  d S )NzSetting config: z to )configtermlog)namedatar&   wandbr   r   log_input_scalar/   s   
z#wandb_log.<locals>.log_input_scalarc                    6    j | |d}|| ||  d|   d S )Nr   zUsing artifact: )Artifactadd_fileuse_artifactr.   r/   r0   r   r&   artifactr1   r   r   log_input_artifact3      

z%wandb_log.<locals>.log_input_artifactc                    sN   |rt |j|D ]\}}| j d| |i q
d S || |i d S )N.)zipr   log__name__)r/   r0   r&   kv)funcr#   r   r   log_output_scalar9   s
   z$wandb_log.<locals>.log_output_scalarc                    r4   )Nr5   zLogging artifact: )r6   r7   log_artifactr.   r9   r1   r   r   log_output_artifact@   r<   z&wandb_log.<locals>.log_output_artifactc                    sT   | j }| d} j| | j|dd}|| || d|  d S )Nz.ymlkubeflow_component_filer5   zLogging component file: )r@   
_python_opfunc_to_component_filer6   r7   rE   r.   )rC   r&   r/   output_component_filer:   )r   r2   r   r   _log_component_fileF   s   


z&wandb_log.<locals>._log_component_filemlpipeline_ui_metadata_path)
annotationkind)
parametersc                 S   s   i | ]}|j |jqS r   )r/   rM   )r   paramr   r   r   
<dictcomp>f   s    zwandb_log.<locals>.<dictcomp>returnc                    s   i i i i  j  D ]%\}}|dkr||< qt|r$||< qt|r.||< q||< q  	
fdd}|_|_ |S )NrR   c               	      s  j | i |}|  |jd }|jd= jjdd} }||_||jd< |}ddd|dgi}t|d	}|| W d    n1 sLw   Y  rY |d
 	 D ]\}	}
	|	||	 | q]	 D ]\}	}|	||	 |j
| qnj|d
}d|j_W d    n1 sw   Y  |ji |j}	 D ]
\}	}
|	|| q	 D ]\}	}
|	||	 |j
| qW d    |S 1 sw   Y  |S )NrL   z8{{workflow.annotations.pipelines.kubeflow.org/run_name}})job_typegroupLINK_TO_KUBEFLOW_RUNoutputsmarkdowninline)r   storagesourcewr%   T)bindapply_defaults	argumentsinitr@   notesr-   opendumpitemsr   contextfeaturekfp_wandb_logargskwargs)rg   rh   boundrL   r&   kubeflow_urliframe_htmlmetadatametadata_filer/   _anntelresult)rK   rC   r'   r,   input_artifactsinput_scalarsjsonlog_component_filer;   r3   rF   rD   new_sigoutput_artifactsoutput_scalarsr2   wb_telemetryr   r   wrapperz   sT   




++z-wandb_log.<locals>.decorator.<locals>.wrapper)__annotations__rc   r   __signature__)rC   r/   ro   rz   )rK   r'   r,   input_typesrt   ru   r;   r3   rF   rD   new_annsrv   output_typesr2   ry   r   )rC   rr   rs   rw   rx   r   	decoratorj   s"   





.4zwandb_log.<locals>.decoratorr   )rt   r+   	functoolsr   inspectr   r   kfpr   kfp.componentsr   r   r   r	   r
   r   r   r   r2   wandb.sdk.libr   rO   valuesdefaultemptyappendr   POSITIONAL_OR_KEYWORDreplacer{   )rC   ru   r   r   r   r   r   r	   r
   r   r   r   sig
no_defaulthas_defaultrP   
new_paramsr   r   )rK   r   rC   r'   r,   r}   r#   rt   ru   r;   r3   rF   rD   r~   rv   r+   r   r2   ry   r   r   	wandb_log   s\   (


*Ir   )NT)r   r   r   r   r   <module>   s   