code: def _create_mlpackage(
      proto_spec: _Model_pb2,
      weights_dir: _Optional[str] = None,
      package_path: _Optional[str] = None,
  ) -> str:
      """
      Args:
          proto_spec: The proto spec of the model.
          weights_dir: Copy weights from this path to the mlpackage.
          package_path: Place the created mlpackage at this path. Error out if this path is a non-empty directory.
  
      Returns:
          path to the mlpackage
      """
      if package_path is None:
          package_path = _tempfile.mkdtemp(suffix=_MLPACKAGE_EXTENSION)
      if _os.path.exists(package_path):
          if _os.listdir(package_path):
              raise FileExistsError(
                  f"The package_path is invalid because it's a non-empty directory: {package_path}"
              )
          # If package_path is an empty dir, the ModelPackage load will error out with `manifest.json not found` issue.
          _shutil.rmtree(package_path)
  
      _, ext = _os.path.splitext(package_path)
      if ext != _MLPACKAGE_EXTENSION:
          raise Exception(
              f"For an ML Package, extension must be {_MLPACKAGE_EXTENSION} (not {ext})"
          )
  
      package = _ModelPackage(package_path)
  
      # Save proto to disk as the root model file, and copy into the model package.
      spec_file = _tempfile.NamedTemporaryFile(
          suffix=_MLMODEL_EXTENSION
      )
      spec_file.write(proto_spec.SerializeToString())
      spec_file.flush()
      package.setRootModel(
          spec_file.name,
          _MODEL_FILE_NAME,
          _MLPACKAGE_AUTHOR_NAME,
          "CoreML Model Specification",
      )
      # Spec file is auto cleaned after close, which is fine because it is already added to the model package.
      spec_file.close()
  
      # Add weights bundle into the model package.
      if weights_dir is not None:
          package.addItem(
              weights_dir,
              _WEIGHTS_DIR_NAME,
              _MLPACKAGE_AUTHOR_NAME,
              "CoreML Model Weights",
          )
  
      return package_path
  
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 3 for calling a global function with example usage: _create_mlpackage(proto_spec, weights_dir, package_path) and returns: package_path