
    &`i                         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Z	d dlm
Z
 d dlmZ d dlmZ erd dlmZ  ed	           G d
 de                      ZdS )    N)Path)TYPE_CHECKINGOptional)keras)FrameworkCheckpoint)	PublicAPI)Preprocessorbeta)	stabilityc                       e Zd ZdZdZedddej        ded         dd fd	            Z	eddd
e
ded         dd fd            Zeddde
ded         dd fd            Zdej        j        fdZdS )TensorflowCheckpointzKA :py:class:`~ray.train.Checkpoint` with TensorFlow-specific functionality._model_filenameN)preprocessormodelr   r	   returnc                *   t          j                    }d}|                    t          ||                                                     |                     |          }|r|                    |           |                    | j        |i           |S )au  Create a :py:class:`~ray.train.Checkpoint` that stores a Keras model.

        The checkpoint created with this method needs to be paired with
        `model` when used.

        Args:
            model: The Keras model, whose weights are stored in the checkpoint.
            preprocessor: A fitted preprocessor to be applied before inference.

        Returns:
            A :py:class:`TensorflowCheckpoint` containing the specified model.

        Examples:

            .. testcode::

                from ray.train.tensorflow import TensorflowCheckpoint
                import tensorflow as tf

                model = tf.keras.applications.resnet.ResNet101()
                checkpoint = TensorflowCheckpoint.from_model(model)

            .. testoutput::
                :options: +MOCK
                :hide:

                ...  # Model may or may not be downloaded

        zmodel.keras)	tempfilemkdtempsaver   as_posixfrom_directoryset_preprocessorupdate_metadataMODEL_FILENAME_KEY)clsr   r   tempdirfilename
checkpoints         ~/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/train/tensorflow/tensorflow_checkpoint.py
from_modelzTensorflowCheckpoint.from_model   s    H "$$ 

4**3355666''00
 	6''555""C$:H#EFFF    	file_pathc                   t           j                            |          r|                    d          st	          d          t          j                    }t           j                            |          }t          ||          	                                }t          j        ||           |                     |          }|r|                    |           |                    | j        |i           |S )aT  Create a :py:class:`~ray.train.Checkpoint` that stores a Keras
        model from H5 format.

        The checkpoint generated by this method contains all the information needed.
        Thus no `model` is needed to be supplied when using this checkpoint.

        Args:
            file_path: The path to the .h5 file to load model from. This is the
                same path that is used for ``model.save(path)``.
            preprocessor: A fitted preprocessor to be applied before inference.

        Returns:
            A :py:class:`TensorflowCheckpoint` converted from h5 format.

        z.h5zAPlease supply a h5 file path to `TensorflowCheckpoint.from_h5()`.)ospathisfileendswith
ValueErrorr   r   basenamer   r   shutilcopyr   r   r   r   )r   r"   r   r   r   new_checkpoint_filer   s          r   from_h5zTensorflowCheckpoint.from_h5E   s    & w~~i(( 		0B0B50I0I 	S   "$$7##I.."7H55>>@@I2333''00
 	6''555""C$:H#EFFFr!   dir_pathc                j   t           j                            |          st          d          t	          j                    }t          j        |           t          j        ||           | 	                    |          }|r|
                    |           |                    | j        di           |S )ac  Create a :py:class:`~ray.train.Checkpoint` that stores a Keras
        model from SavedModel format.

        The checkpoint generated by this method contains all the information needed.
        Thus no `model` is needed to be supplied when using this checkpoint.

        Args:
            dir_path: The directory containing the saved model. This is the same
                directory as used by ``model.save(dir_path)``.
            preprocessor: A fitted preprocessor to be applied before inference.

        Returns:
            A :py:class:`TensorflowCheckpoint` converted from SavedModel format.

        zDPlease supply a directory to `TensorflowCheckpoint.from_saved_model`.)r$   r%   isdirr(   r   r   rmdirr*   copytreer   r   r   r   )r   r.   r   r   r   s        r   from_saved_modelz%TensorflowCheckpoint.from_saved_modelg   s    & w}}X&& 	V   "$$
'***''00
 	6''555""C$:C#@AAAr!   c                 R   |                                  }| j        |vrt          d          || j                 }|                                 5 }t	          ||                                          }t          j                            |          cddd           S # 1 swxY w Y   dS )zRetrieve the model stored in this checkpoint.

        Returns:
            The Tensorflow Keras model stored in the checkpoint.
        z`TensorflowCheckpoint` cannot retrieve the model if you override the checkpoint metadata. Please use `Checkpoint.update_metadata` instead.N)	get_metadatar   r(   as_directoryr   r   r   models
load_model)selfmetadatamodel_filenamecheckpoint_dir
model_paths        r   	get_modelzTensorflowCheckpoint.get_model   s     $$&&"(22X   "$"9:   	7Nnn==FFHHJ<**:66	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7s   ABB #B )__name__
__module____qualname____doc__r   classmethodr   Modelr   r    strr-   r4   tfr?    r!   r   r   r      s'       UU*
 26	+ + +{+ ~.	+
 
 + + + [+Z IM  .6~.F	   [B HL      -5n-E 	      [ D7	7 7 7 7 7 7r!   r   )r$   r*   r   pathlibr   typingr   r   
tensorflowrG   r   (ray.train._internal.framework_checkpointr   ray.util.annotationsr   ray.data.preprocessorr	   r   rH   r!   r   <module>rO      s    				         * * * * * * * *           H H H H H H * * * * * * 3222222 VI7 I7 I7 I7 I7. I7 I7 I7 I7 I7r!   