o
    2h-                     @   s   d dl 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 d dl
mZ zd dlZW n ey=   dZY nw edd	gdddZeddgdddZed	dddZeddddZdS )    N)logging)keras_export)legacy_h5_format)
saving_lib)
file_utils)io_utilszkeras.saving.save_modelzkeras.models.save_modelTc           
      K   sN  | dd}| dd}|r,t|dst|dr%td|  ntd| |r9td	t|  t|drEtd
 t|d}|du rS| }z| o\t	j
|}W n tyi   d}Y nw |rw|swt|}	|	swdS |rt|drt| |S |stj| |ddS t|drt| |||S td| d)a  Saves a model as a `.keras` file.

    Args:
        model: Keras model instance to be saved.
        filepath: `str` or `pathlib.Path` object. Path where to save the model.
        overwrite: Whether we should overwrite any existing model at the target
            location, or instead ask the user via an interactive prompt.
        zipped: Whether to save the model as a zipped `.keras`
            archive (default when saving locally), or as an unzipped directory
            (default when saving on the Hugging Face Hub).

    Example:

    ```python
    model = keras.Sequential(
        [
            keras.layers.Dense(5, input_shape=(3,)),
            keras.layers.Softmax(),
        ],
    )
    model.save("model.keras")
    loaded_model = keras.saving.load_model("model.keras")
    x = keras.random.uniform((10, 3))
    assert np.allclose(model.predict(x), loaded_model.predict(x))
    ```

    Note that `model.save()` is an alias for `keras.saving.save_model()`.

    The saved `.keras` file is a `zip` archive that contains:

    - The model's configuration (architecture)
    - The model's weights
    - The model's optimizer's state (if any)

    Thus models can be reinstantiated in the exact same state.
    include_optimizerTsave_formatF.h5.hdf5.keraszThe `save_format` argument is deprecated in Keras 3. We recommend removing this argument as it can be inferred from the file path. Received: save_format=zThe `save_format` argument is deprecated in Keras 3. Please remove this argument and pass a file path with either `.keras` or `.h5` extension.Received: save_format=z-The following argument(s) are not supported: a  You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`. hf://N)zippeda  Invalid filepath extension for saving. Please add either a `.keras` extension for the native Keras format (recommended) or a `.h5` extension. Use `model.export(filepath)` if you want to export a SavedModel for use with TFLite/TFServing/etc. Received: filepath=.)popstrendswithr   warning
ValueErrorlistkeys
startswithospathexists	TypeErrorr   ask_to_proceed_with_overwriter   
save_modelr   save_model_to_hdf5)
modelfilepath	overwriter   kwargsr   r	   is_hfr   proceed r&   V/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/saving/saving_api.pyr      sf   &
	
r   zkeras.saving.load_modelzkeras.models.load_modelc                 C   s  t | dot| }t| ott| d}t | d}t	| rJt| sJ|sJ|sJtt
 tj| }t| | t|rJ|} d}|sP|sP|rYt
j| |||dS t | dritj| |||dS t | drxtd|  dtd	|  d
|  d)a  Loads a model saved via `model.save()`.

    Args:
        filepath: `str` or `pathlib.Path` object, path to the saved model file.
        custom_objects: Optional dictionary mapping names
            (strings) to custom classes or functions to be
            considered during deserialization.
        compile: Boolean, whether to compile the model after loading.
        safe_mode: Boolean, whether to disallow unsafe `lambda` deserialization.
            When `safe_mode=False`, loading an object has the potential to
            trigger arbitrary code execution. This argument is only
            applicable to the Keras v3 model format. Defaults to `True`.

    Returns:
        A Keras model instance. If the original model was compiled,
        and the argument `compile=True` is set, then the returned model
        will be compiled. Otherwise, the model will be left uncompiled.

    Example:

    ```python
    model = keras.Sequential([
        keras.layers.Dense(5, input_shape=(3,)),
        keras.layers.Softmax()])
    model.save("model.keras")
    loaded_model = keras.saving.load_model("model.keras")
    x = np.random.random((10, 3))
    assert np.allclose(model.predict(x), loaded_model.predict(x))
    ```

    Note that the model variables may have different name values
    (`var.name` property, e.g. `"dense_1/kernel:0"`) after being reloaded.
    It is recommended that you use layer attributes to
    access specific variables, e.g. `model.get_layer("dense_1").kernel`.
    r   zconfig.jsonr   T)custom_objectscompile	safe_moder
   zFile not found: filepath=z<. Please ensure the file is an accessible `.keras` zip file.$File format not supported: filepath=a  . Keras 3 only supports V3 `.keras` files and legacy H5 format files (`.h5` extension). Note that the legacy SavedModel format is not supported by `load_model()` in Keras 3. In order to reload a TensorFlow SavedModel as an inference-only layer in Keras 3, use `keras.layers.TFSMLayer(za, call_endpoint='serving_default')` (note that your `call_endpoint` might have a different name).)r   r   zipfile
is_zipfiler   isdirr   joinr   is_remote_pathr   get_temp_dirr   r   basenamecopy
load_modelr   load_model_from_hdf5r   )r!   r(   r)   r*   is_keras_zipis_keras_dirr$   
local_pathr&   r&   r'   r4   |   sZ   %


r4   zkeras.saving.save_weightsc                 K   s   t |}|d u r|dstd| |d ur$|ds$td| ztj|}W n ty7   d}Y nw |rE|sEt|}|sEd S t	j
| ||fi | d S )N.weights.h5z;The filename must end in `.weights.h5`. Received: filepath=)z
weights.h5zweights.jsonz`The filename must end in `.weights.json` when `max_shard_size` is specified. Received: filepath=F)r   r   r   r   r   r   r   r   r   r   save_weights_only)r    r!   r"   max_shard_sizer#   filepath_strr   r%   r&   r&   r'   save_weights   s2   
r=   zkeras.saving.load_weightsFc                 K   s  t |}|dd }|dd }|rtd| |dr>|d ur)td| |d ur4td| tj| ||d d S |dsH|d	r^|d urStd| tj| |||d
 d S |dsh|drtsntd|d urytd| t|d/}d|j	vrd|v r|d }|rt
|| | nt
|| | W d    d S W d    d S 1 sw   Y  d S td| d)Nobjects_to_skipby_namezInvalid keyword arguments: r   zF`objects_to_skip` only supports loading '.weights.h5' files.Received: zI`by_name` only supports loading legacy '.h5' or '.hdf5' files. Received: )skip_mismatchr9   z.weights.json)r@   r>   r   r   z2Loading a H5 file requires `h5py` to be installed.rlayer_namesmodel_weightsr+   zY. Keras 3 only supports V3 `.keras` and `.weights.h5` files, or legacy V1/V2 `.h5` files.)r   r   r   r   r   load_weights_onlyh5pyImportErrorFileattrsr   $load_weights_from_hdf5_group_by_nameload_weights_from_hdf5_group)r    r!   r@   r#   r<   r>   r?   fr&   r&   r'   load_weights   s~   


"
rL   )TN)NTT)F)r   r,   abslr   keras.src.api_exportr   keras.src.legacy.savingr   keras.src.savingr   keras.src.utilsr   r   rE   rF   r   r4   r=   rL   r&   r&   r&   r'   <module>   s,    

ic