
    BVht                         d 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
 ddlmZ  edg	       G d
 de             Zy)zlSignatureDef method name utility functions.

Utility functions for manipulating signature_def.method_names.
    )file_io)
tf_logging)	constants)loader_impl)compat)	tf_exportz1saved_model.signature_def_utils.MethodNameUpdater)v1c                   &    e Zd ZdZd ZddZddZy)MethodNameUpdatera  Updates the method name(s) of the SavedModel stored in the given path.

  The `MethodNameUpdater` class provides the functionality to update the method
  name field in the signature_defs of the given SavedModel. For example, it
  can be used to replace the `predict` `method_name` to `regress`.

  Typical usages of the `MethodNameUpdater`
  ```python
  ...
  updater = tf.compat.v1.saved_model.signature_def_utils.MethodNameUpdater(
      export_dir)
  # Update all signature_defs with key "foo" in all meta graph defs.
  updater.replace_method_name(signature_key="foo", method_name="regress")
  # Update a single signature_def with key "bar" in the meta graph def with
  # tags ["serve"]
  updater.replace_method_name(signature_key="bar", method_name="classify",
                              tags="serve")
  updater.save(new_export_dir)
  ```

  Note: This function will only be available through the v1 compatibility
  library as tf.compat.v1.saved_model.builder.MethodNameUpdater.
  c                 F    || _         t        j                  |      | _        y)zCreates an MethodNameUpdater object.

    Args:
      export_dir: Directory containing the SavedModel files.

    Raises:
      IOError: If the saved model file does not exist, or cannot be successfully
      parsed.
    N)_export_dirloaderparse_saved_model_saved_model)self
export_dirs     a/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/saved_model/method_name_updater.py__init__zMethodNameUpdater.__init__8   s     "D00<D    Nc                    |st        d      |st        d      |t        |t              s|g}d}| j                  j                  D ]f  }|,t        |      t        |j                  j                        k(  s1||j                  vrt        d| d| d      ||j                  |   _	        d}h |st        d| d	      y)
a'  Replaces the method_name in the specified signature_def.

    This will match and replace multiple sig defs iff tags is None (i.e when
    multiple `MetaGraph`s have a signature_def with the same key).
    If tags is not None, this will only replace a single signature_def in the
    `MetaGraph` with matching tags.

    Args:
      signature_key: Key of the signature_def to be updated.
      method_name: new method_name to replace the existing one.
      tags: A tag or sequence of tags identifying the `MetaGraph` to update. If
          None, all meta graphs will be updated.
    Raises:
      ValueError: if signature_key or method_name are not defined or
          if no metagraphs were found with the associated tags or
          if no meta graph has a signature_def that matches signature_key.
    z `signature_key` must be defined.z`method_name` must be defined.NFz"MetaGraphDef associated with tags z* does not have a signature_def with key: 'z'. This means either you specified the wrong signature key or forgot to put the signature_def with the corresponding key in your SavedModel.Tz could not be found in SavedModel. This means either you specified invalid tags or your SavedModel does not have a MetaGraphDef with the specified tags.)

ValueError
isinstancelistr   meta_graphssetmeta_info_deftagssignature_defmethod_name)r   signature_keyr   r   found_matchmeta_graph_defs         r   replace_method_namez%MethodNameUpdater.replace_method_nameE   s    $ 9::788D$!7VdK++77 
	Tc.*F*F*K*K&LL < <<24& 9::G I!!" " CN$$]3?
 .tf 5M MN N r   c                     t        j                  t        j                  t        j                  | j
                        t        j                  t        j                                    }|s| j
                  }|rtt        j                  t        j                  |      t        j                  t        j                              }t        j                  |t        | j                               nzt        j                  t        j                  |      t        j                  t        j                              }t        j                  || j                  j                  d             t        j                  dt        j                  |             y)a  Saves the updated `SavedModel`.

    Args:
      new_export_dir: Path where the updated `SavedModel` will be saved. If
          None, the input `SavedModel` will be overriden with the updates.

    Raises:
      errors.OpError: If there are errors during the file save operation.
    T)deterministiczSavedModel written to: %sN)r   file_existsjoinr   as_bytesr   r   SAVED_MODEL_FILENAME_PBTXTwrite_string_to_filestrr   SAVED_MODEL_FILENAME_PBSerializeToStringr   infoas_text)r   new_export_diris_input_text_protopaths       r   savezMethodNameUpdater.saveq   s    "--OOD,,-OOI@@A	CD ''n\\
//.
)
//)>>
?Ad ""4T->->)?@\\
//.
)
//);;
<>d ""
!!33$3GIOO/1EFr   )N)__name__
__module____qualname____doc__r   r#   r3    r   r   r   r      s    0=*NXGr   r   N)r7   tensorflow.python.lib.ior   tensorflow.python.platformr   tensorflow.python.saved_modelr   r   r   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   objectr   r8   r   r   <module>r?      sI   
 - 1 3 ? ) 6
 BCDpG pG EpGr   