code: def save_spec(
      spec,
      filename,
      auto_set_specification_version=False,
      weights_dir=None,
  ):
      """
      Save a protobuf model specification to file.
  
      Parameters
      ----------
      spec: Model_pb
          Protobuf representation of the model
  
      filename: str
          File path where the spec gets saved.
  
      auto_set_specification_version: bool
          If True, will always try to set specification version automatically.
  
      weights_dir: str
          Path to the directory containing the weights.bin file. This is required
          when the spec has model type mlprogram. If the mlprogram does not contain
          any weights, this path can be an empty directory.
  
      Examples
      --------
      .. sourcecode:: python
  
          coremltools.utils.save_spec(spec, "HousePricer.mlmodel")
          coremltools.utils.save_spec(spec, "HousePricer.mlpackage")
          coremltools.utils.save_spec(
              spec, "mlprogram_model.mlpackage", weights_dir="/path/to/weights/directory"
          )
  
      See Also
      --------
      load_spec
      """
      name, ext = _os.path.splitext(filename)
  
      is_package = False
  
      if not ext:
          filename = "{}{}".format(filename, _MLMODEL_EXTENSION)
      elif ext == _MLPACKAGE_EXTENSION:
          is_package = True
      elif ext == _MLMODEL_EXTENSION:
          is_package = False
      else:
          raise Exception(
              "Extension must be {} or {} (not {})".format(
                  _MLMODEL_EXTENSION, _MLPACKAGE_EXTENSION, ext
              )
          )
  
      if auto_set_specification_version:
          try:
              # always try to downgrade the specification version to the
              # minimal version that supports everything in this mlmodel
              from ..libcoremlpython import _MLModelProxy
  
              spec = _MLModelProxy.auto_set_specification_version(spec)
          except Exception as e:
              print(e)
              _warnings.warn(
                  "Failed to automatic set specification version for this model.",
                  RuntimeWarning,
              )
  
      if is_package:
          if _ModelPackage is None:
              raise Exception(
                  "Unable to load libmodelpackage. Cannot save spec"
              )
          if (
              spec.WhichOneof("Type") == "mlProgram"
              and weights_dir is None
          ):
              raise Exception(
                  "spec of type mlProgram cannot be saved without the"
                  " weights file. Please provide the path to the weights file as well, "
                  "using the 'weights_dir' argument."
              )
          _create_mlpackage(
              spec, weights_dir=weights_dir, package_path=filename
          )
      else:
          with open(filename, "wb") as f:
              f.write(spec.SerializeToString())
  
imports: from collections.abc import Iterable
  from functools import lru_cache
  import math
  import os
  import shutil
  import subprocess
  import sys
  import tempfile
  from typing import Optional, Union
  import warnings
  import numpy
  import coremltools
  from coremltools import ComputeUnit
  from coremltools.converters.mil.mil.passes.defs.preprocess import NameSanitizer
  from coremltools.proto import Model_pb2
  import coremltools.proto.MIL_pb2
  from .._deps import _HAS_SCIPY
label_desc: Use image node_id 4 for calling a global function with example usage: save_spec(spec, filename, auto_set_specification_version, weights_dir) without return types