code: def save_model(model, filepath, weights_format="h5"):
      """Save a zip-archive representing a Keras model to the given filepath.
  
      The zip-based archive contains the following structure:
  
      - JSON-based configuration file (config.json): Records of model, layer, and
          other trackables' configuration.
      - H5-based trackable state files, found in respective directories, such as
          model/states.npz, model/dense_layer/states.npz, etc.
      - Metadata file.
  
      The states of Keras trackables (layers, optimizers, loss, and metrics) are
      automatically saved as long as they can be discovered through the attributes
      returned by `dir(Model)`. Typically, the state includes the variables
      associated with the trackable, but some specially purposed layers may
      contain more such as the vocabularies stored in the hashmaps. The trackables
      define how their states are saved by exposing `save_state()` and
      `load_state()` APIs.
  
      For the case of layer states, the variables will be visited as long as
      they are either 1) referenced via layer attributes, or 2) referenced via a
      container (list, tuple, or dict), and the container is referenced via a
      layer attribute.
      """
      filepath = str(filepath)
      if not filepath.endswith(".keras"):
          raise ValueError(
              "Invalid `filepath` argument: expected a `.keras` extension. "
              f"Received: filepath={filepath}"
          )
      if weights_format == "h5" and h5py is None:
          raise ImportError(
              "h5py must be installed in order to save a model."
          )
  
      if not model.built:
          warnings.warn(
              "You are saving a model that has not yet been built. "
              "It might not contain any weights yet. "
              "Consider building the model first by calling it "
              "on some data.",
              stacklevel=2,
          )
  
      with ObjectSharingScope():
          serialized_model_dict = serialize_keras_object(model)
      config_json = json.dumps(serialized_model_dict)
      metadata_json = json.dumps(
          {
              "keras_version": keras_version,
              "date_saved": datetime.datetime.now().strftime(
                  "%Y-%m-%d@%H:%M:%S"
              ),
          }
      )
      if file_utils.is_remote_path(filepath):
          # Remote path. Zip to local memory byte io and copy to remote
          zip_filepath = io.BytesIO()
      else:
          zip_filepath = filepath
  
      with zipfile.ZipFile(zip_filepath, "w") as zf:
          with zf.open(_METADATA_FILENAME, "w") as f:
              f.write(metadata_json.encode())
          with zf.open(_CONFIG_FILENAME, "w") as f:
              f.write(config_json.encode())
  
          if weights_format == "h5":
              weights_store = H5IOStore(
                  _VARS_FNAME + ".h5", archive=zf, mode="w"
              )
          elif weights_format == "npz":
              weights_store = NpzIOStore(
                  _VARS_FNAME + ".npz", archive=zf, mode="w"
              )
          else:
              raise ValueError(
                  "Unknown `weights_format` argument. "
                  "Expected 'h5' or 'npz'. "
                  f"Received: weights_format={weights_format}"
              )
  
          asset_store = DiskIOStore(
              _ASSETS_DIRNAME, archive=zf, mode="w"
          )
  
          _save_state(
              model,
              weights_store=weights_store,
              assets_store=asset_store,
              inner_path="",
              visited_trackables=set(),
          )
          weights_store.close()
          asset_store.close()
  
      if file_utils.is_remote_path(filepath):
          with file_utils.File(filepath, "wb") as f:
              f.write(zip_filepath.getvalue())
  
imports: import datetime
  import io
  import json
  import tempfile
  import warnings
  import zipfile
  import numpy
  from keras.backend.common import global_state
  from keras.layers.layer import Layer
  from keras.losses.loss import Loss
  from keras.metrics.metric import Metric
  from keras.optimizers.optimizer import Optimizer
  from keras.saving.serialization_lib import ObjectSharingScope
  from keras.saving.serialization_lib import deserialize_keras_object
  from keras.saving.serialization_lib import serialize_keras_object
  from keras.trainers.compile_utils import CompileMetrics
  from keras.utils import file_utils
  from keras.utils import naming
  from keras.version import __version__
label_desc: Use image node_id 1 for calling a global function with example usage: save_model(model, filepath, weights_format) without return types