code: def _set_api_key(
      api_key_file: Optional[str] = None, api_key: Optional[str] = None
  ):
      """Set WandB API key from `wandb_config`. Will pop the
      `api_key_file` and `api_key` keys from `wandb_config` parameter.
  
      The order of fetching the API key is:
        1) From `api_key` or `api_key_file` arguments
        2) From WANDB_API_KEY environment variables
        3) User already logged in to W&B (wandb.api.api_key set)
        4) From external hook WANDB_SETUP_API_KEY_HOOK
      """
      if os.environ.get(WANDB_MODE_ENV_VAR) in {"offline", "disabled"}:
          return
  
      if api_key_file:
          if api_key:
              raise ValueError(
                  "Both WandB `api_key_file` and `api_key` set."
              )
          with open(api_key_file, "rt") as fp:
              api_key = fp.readline().strip()
  
      if not api_key and not os.environ.get(WANDB_ENV_VAR):
          # Check if user is already logged into wandb.
          try:
              wandb.ensure_configured()
              if wandb.api.api_key:
                  logger.info("Already logged into W&B.")
                  return
          except AttributeError:
              pass
          # Try to get API key from external hook
          if WANDB_SETUP_API_KEY_HOOK in os.environ:
              try:
                  api_key = _load_class(
                      os.environ[WANDB_SETUP_API_KEY_HOOK]
                  )()
              except Exception as e:
                  logger.exception(
                      f"Error executing {WANDB_SETUP_API_KEY_HOOK} to setup API key: {e}",
                      exc_info=e,
                  )
      if api_key:
          os.environ[WANDB_ENV_VAR] = api_key
      elif not os.environ.get(WANDB_ENV_VAR):
          raise ValueError(
              "No WandB API key found. Either set the {} environment "
              "variable, pass `api_key` or `api_key_file` to the"
              "`WandbLoggerCallback` class as arguments, "
              "or run `wandb login` from the command line".format(
                  WANDB_ENV_VAR
              )
          )
  
imports: import enum
  import os
  import pickle
  import urllib
  import warnings
  import numpy
  from numbers import Number
  import pyarrow.fs
  from types import ModuleType
  from typing import Any, Dict, List, Optional, Sequence, Tuple, Union
  import ray
  from ray import logger
  from ray.air import session
  from ray.air._internal import usage
  from ray.air.util.node import _force_on_current_node
  from ray.tune.logger import LoggerCallback
  from ray.tune.utils import flatten_dict
  from ray.tune.experiment import Trial
  from ray.train._internal.syncer import DEFAULT_SYNC_TIMEOUT
  from ray._private.storage import _load_class
  from ray.util import PublicAPI
  from ray.util.queue import Queue
label_desc: Use image node_id 6 for calling a global function with example usage: _set_api_key(api_key_file, api_key) without return types