class_docstr: A training job represents a Sagemaker training job within an experiment.
class_docstr_tok: ["A","training","job","represents","a","Sagemaker","training","job","within","an","experiment","."]
docstr: Retrieves the model artifact from S3 and stores it locally in a
  temporary directory.
  
  Args:
      cache: Whether to cache the extracted artifact.
  
  Returns:
      The artifact which contains the untarred model artifact directory. The artifact should
          be wrapped in a `with` statement such that the directory is cleaned up after usage.
code: def artifact(self, cache: bool = True) -> Artifact:
      """
      Retrieves the model artifact from S3 and stores it locally in a
      temporary directory.
  
      Args:
          cache: Whether to cache the extracted artifact.
  
      Returns:
          The artifact which contains the untarred model artifact directory. The artifact should
              be wrapped in a `with` statement such that the directory is cleaned up after usage.
      """
      cache_dir = self._cache_dir() / "artifacts"
  
      # First, we check whether the model is already available locally. For this, the `cache`
      # flag is irrelevant
      if cache_dir.exists():
          return Artifact(cache_dir, cleanup=False)
  
      # If not, we need to download the artifact. For that, we need to get the bucket and object
      # path
      regex = r"^s3://([A-z0-9-_]*)/(.*)$"
      bucket_name, object_path = re.findall(
          regex, self.info["ModelArtifacts"]["S3ModelArtifacts"]
      )[0]
  
      # Then, we can download the model
      s3 = default_session().client("s3")
      with tempfile.NamedTemporaryFile(suffix=".tar.gz") as tmp:
          s3.download_fileobj(bucket_name, object_path, tmp)
          tmp.seek(0)
          # As soon as it is downloaded, we can unpack the tar into the cache directory or a
          # temporary one
          if cache:
              cache_dir.mkdir(exist_ok=True, parents=True)
              target = cache_dir
          else:
              target = Path(tempfile.mkdtemp())
          shutil.unpack_archive(tmp.name, target)
  
      # And return the artifact
      return Artifact(target, cleanup=not cache)
  
imports: from __future__ import annotations
  import datetime
  import json
  import logging
  import re
  import shutil
  import tempfile
  import time
  from collections import Counter
  from functools import cached_property, lru_cache
  from pathlib import Path
  from typing import Any, Callable, cast, Dict, Iterator, List, Tuple, Union
  import boto3
  import numpy
  from botocore.exceptions import ClientError
  from .session import default_session
label_desc: Use image node_id 9 for calling the TrainingJob obj's underlying member method code with example usage: obj.artifact(cache) and returns: Artifact, Artifact