o
    ni'                     @   s  d dl Z d dlZd dl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edeeee
jf dee d	efd
dZ						d%dededeeeeee
jf f deeeee f  dee dee dee dee ded	efddZ						d&dededeeeee f  dee dee dee dee deeef d	dfddZd'deded	dfd d!Z	d(ddd"edeeeee f  d	dfd#d$ZdS ))    N)AnyDictListOptionalUnion)_SavedModel)Artifact)ArtifactManifestEntryartifactpath_or_objnamereturnc                 C   s   t |tr| ||S t |tjr| ||S t |trTtj	|r'| 
|S tj|r2| |S | |}|tj|dd W d   dS 1 sMw   Y  dS tdt| )a  Add an object to an artifact.

    High-level wrapper to add object(s) to an artifact - calls any of the .add* methods
    under Artifact depending on the type of object that's passed in. This will probably
    be moved to the Artifact class in the future.

    Args:
        artifact: `Artifact` - artifact created with `wandb.Artifact(...)`
        path_or_obj: `Union[str, ArtifactManifestEntry, data_types.WBValue]` - either a
            str or valid object which indicates what to add to an artifact.

        name: `str` - the name of the object which is added to an artifact.

    Returns:
        Type[Any] - Union[None, ArtifactManifestEntry, etc]

    T)	sort_keysNz\Expected `path_or_obj` to be instance of `ArtifactManifestEntry`, `WBValue`, or `str, found )
isinstancer	   add_reference
data_typesWBValueaddstrospathisdiradd_dirisfileadd_filenew_filewritejsondumps
ValueErrortype)r
   r   r   f r"   R/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/beta/workflows.py_add_any   s   



"r$   autor    entriesaliasesdescriptionmetadataprojectscope_projectjob_typec	                 C   s   t jdu rt j||t jddd}	nt j}	|s|  d|	j } |du r%i }t | |||dd}
|D ]
}t|
|| | q1t j|}|	j	|
|d |
S )a  Create an artifact, populate it, and log it with a run.

    If a run is not present, we create one.

    Args:
        name: `str` - name of the artifact. If not scoped to a project, name will be
            suffixed by "-{run_id}".
        type: `str` - type of the artifact, used in the UI to group artifacts of the
            same type.
        entries: `Dict` - dictionary containing the named objects we want added to this
            artifact.
        description: `str` - text description of artifact.
        metadata: `Dict` - users can pass in artifact-specific metadata here, will be
            visible in the UI.
        project: `str` - project under which to place this artifact.
        scope_project: `bool` - if True, we will not suffix `name` with "-{run_id}".
        job_type: `str` - Only applied if run is not present and we create one.
            Used to identify runs of a certain job type, i.e "evaluation".

    Returns:
        Artifact

    Ntrue)silent)r*   r,   settings-F)r'   )
wandbruninitSettingsidr   r$   util_resolve_aliaseslog_artifact)r   r    r&   r'   r(   r)   r*   r+   r,   r2   artr   r"   r"   r#   _log_artifact_version6   s   
"r:   model	model_objkwargsr   c           
      K   s6   t jj| fi |}t|dd|i|||||dd	}	|S )a  Log a model object to enable model-centric workflows in the UI.

    Supported frameworks include PyTorch, Keras, Tensorflow, Scikit-learn, etc. Under
    the hood, we create a model artifact, bind it to the run that produced this model,
    associate it with the latest metrics logged with `wandb.log(...)` and more.

    Args:
        model_obj: any model object created with the following ML frameworks: PyTorch,
            Keras, Tensorflow, Scikit-learn. name: `str` - name of the model artifact
            that will be created to house this model_obj.
        aliases: `str, List[str]` - optional alias(es) that will be applied on this
            model and allow for unique identification. The alias "latest" will always be
            applied to the latest version of a model.
        description: `str` - text description/notes about the model - will be visible in
            the Model Card UI.
        metadata: `Dict` - model-specific metadata goes here - will be visible the UI.
        project: `str` - project under which to place this artifact.
        scope_project: `bool` - If true, name of this model artifact will not be
            suffixed by `-{run_id}`.

    Returns:
        _SavedModel instance

    Example:
        ```python
        import torch.nn as nn
        import torch.nn.functional as F


        class Net(nn.Module):
            def __init__(self):
                super(Net, self).__init__()
                self.fc1 = nn.Linear(10, 10)

            def forward(self, x):
                x = self.fc1(x)
                x = F.relu(x)
                return x


        model = Net()
        sm = log_model(model, "my-simple-model", aliases=["best"])
        ```

    r;   index	log_model)	r   r    r&   r'   r(   r)   r*   r+   r,   )r   r   r3   r:   )
r<   r   r'   r(   r)   r*   r+   r=   r;   _r"   r"   r#   r?   q   s   7r?   Faliased_pathunsafec                 C   sb   |st dd| vrt dtjr-tj}|| }|d}|du s't|ts+t d|S t d)a?  Fetch a saved model from an alias.

    Under the hood, we use the alias to fetch the model artifact containing the
    serialized model files and rebuild the model object from these files. We also
    declare the fetched model artifact as an input to the run (with `run.use_artifact`).

    Args:
        aliased_path: `str` - the following forms are valid: "name:version",
            "name:alias". May be prefixed with "entity/project".
        unsafe: `bool` - must be True to indicate the user understands the risks
            associated with loading external models.

    Returns:
        _SavedModel instance

    Example:
        ```python
        # Assuming the model with the name "my-simple-model" is trusted:
        sm = use_model("my-simple-model:latest", unsafe=True)
        model = sm.model_obj()
        ```
    z;The 'unsafe' parameter must be set to True to load a model.:z@aliased_path must be of the form 'name:alias' or 'name:version'.r>   NzaDeserialization into model object failed: _SavedModel instance could not be initialized properly.zYuse_model can only be called inside a run. Please call wandb.init() before use_model(...))r   r1   r2   use_artifactgetr   r   )rA   rB   r2   r
   smr"   r"   r#   	use_model   s$   

rG   target_pathc                 C   s   t j|}t jr.t j}| jr| jj}n| jr!| jjjr!| jj}ntd|	||| dS | jdur=| jj
|| dS td)ao  Link the given model to a portfolio.

    A portfolio is a promoted collection which contains (in this case) model artifacts.
    Linking to a portfolio allows for useful model-centric workflows in the UI.

    Args:
        model: `_SavedModel` - an instance of _SavedModel, most likely from the output
            of `log_model` or `use_model`.
        target_path: `str` - the target portfolio. The following forms are valid for the
            string: {portfolio}, {project/portfolio},{entity}/{project}/{portfolio}.
        aliases: `str, List[str]` - optional alias(es) that will only be applied on this
            linked model inside the portfolio. The alias "latest" will always be applied
            to the latest version of a model.

    Returns:
        None

    Example:
        sm = use_model("my-simple-model:latest")
        link_model(sm, "my-portfolio")

    zBLinking requires that the given _SavedModel belongs to an artifactNzILinking requires that the given _SavedModel belongs to a logged artifact.)r1   r6   r7   r2   _artifact_sourcer
   _artifact_target_finalr   link_artifactlink)r;   rH   r'   r2   r
   r"   r"   r#   
link_model   s    


rN   )NNNNNr%   )r;   NNNNN)F)N)r   r   typingr   r   r   r   r   r1   wandb.data_typesr   r   wandb.sdk.artifacts.artifactr   +wandb.sdk.artifacts.artifact_manifest_entryr	   r   r   r$   dictboolr:   r?   rG   rN   r"   r"   r"   r#   <module>   s    

.	

=
	
I4