code: def clean_up_spot(
      stack_name: str,
      boto3_session: boto3.session.Session,
      disable_tqdm: bool = False,
  ) -> None:
      # The spot API is eventually consistent and the only way to guarantee
      # that we don't leave any spot requests alive (that may eventually be
      # fulfilled and lead to running EC2 instances) is to wait a long enough
      # period that any created spot requests will have shown up in the API.
      # 60 seconds seems like a relatively safe amount of time.
      SPOT_WAIT_SECONDS = 60
  
      start_time = time.time()
  
      all_terminated_instance_ids = set()
  
      format_str = "{l_bar}{bar}| (remaining time: {remaining})"
      pbar = tqdm.tqdm(
          total=SPOT_WAIT_SECONDS,
          desc="Cleaning up spot instances and spot instance requests",
          bar_format=format_str,
          disable=disable_tqdm,
      )
      progress_bar_state = 0.0
      while True:
          elapsed_time = time.time() - start_time
          if elapsed_time >= SPOT_WAIT_SECONDS:
              pbar.update(
                  SPOT_WAIT_SECONDS - progress_bar_state
              )  # Exit TQDM with it showing 100%
              pbar.close()
              break
  
          tqdm_update = elapsed_time - progress_bar_state
          pbar.update(tqdm_update)
          progress_bar_state = elapsed_time
  
          instance_ids = delete_spot_requests_and_agents(
              stack_name, boto3_session
          )
          for i in instance_ids:
              all_terminated_instance_ids.add(i)
  
      # Final cleanup
      instance_ids = delete_spot_requests_and_agents(
          stack_name, boto3_session
      )
      for i in instance_ids:
          all_terminated_instance_ids.add(i)
  
      if len(instance_ids) > 0:
          ec2 = boto3_session.client("ec2")
          waiter = ec2.get_waiter("instance_terminated")
          for n in range(NUM_WAITS):
              print("Waiting For Spot Agents To Terminate")
              try:
                  waiter.wait(
                      InstanceIds=instance_ids,
                      WaiterConfig={"Delay": 10},
                  )
                  break
              except WaiterError as e:
                  if n == NUM_WAITS - 1:
                      raise e
  
imports: import sys
  import threading
  import time
  from typing import Any, Callable, Dict, List, Optional, Tuple
  import boto3
  import tqdm
  from botocore.exceptions import ClientError, WaiterError
  from determined.deploy.aws import constants
label_desc: Use image node_id 21 for calling a global function with example usage: clean_up_spot(stack_name, boto3_session, disable_tqdm) without return types