code: def setup_bert(
      use_tpu,
      do_lower_case,
      bert_large,
      output_dir=None,
      tpu_name=None,
      tpu_zone=None,
      gcp_project=None,
      master=None,
      iterations_per_loop=1000,
      num_tpu_cores=8,
      max_seq_length=512,
  ):
      """
      Loads a pretrained BERT model. if not on TPU then we'll download it if necessary
      :param use_tpu:
      :param do_lower_case:
      :param bert_large:
      :param output_dir:
      :param tpu_name: tpu hyperparm
      :param tpu_zone: tpu hyperparm
      :param gcp_project: ok if None
      :param master: ok if None
      :param iterations_per_loop:
      :param num_tpu_cores: 8 should be ok
      :param max_seq_length: just for debugging
      :return:
      """
  
      #####
      if output_dir is None:
          raise ValueError(
              "Need to set output dir when running on a TPU"
          )
      if tf.gfile.Exists(output_dir):
          tf.gfile.DeleteRecursively(output_dir)
      tf.gfile.MakeDirs(output_dir)
  
      if use_tpu:
          # Cloud storage has bert-base cased, bert-base uncased, and bert-large uncased, but NOT bert-large cased.
          if bert_large and (not do_lower_case):
              raise ValueError(
                  "You need to download bert-large cased and upload it to your own google cloud storage bucket!"
              )
  
              # Info on how you do this:
              # gsutil cp -R gs://cloud-tpu-checkpoints/bert/cased_L-12_H-768_A-12/ gs://MYBUCKET/bert_models/
              # gsutil cp -R gs://cloud-tpu-checkpoints/bert/uncased_L-12_H-768_A-12/ gs://MYBUCKET/bert_models/
              # gsutil cp -R gs://cloud-tpu-checkpoints/bert/uncased_L-24_H-1024_A-16/ gs://MYBUCKET/bert_models/
              # wget https://storage.googleapis.com/bert_models/2018_10_18/cased_L-24_H-1024_A-16.zip && unzip cased_L-24_H-1024_A-16.zip && gsutil cp -R cased_L-24_H-1024_A-16/ gs://MYBUCKET/bert_models/
  
          path = "gs://cloud-tpu-checkpoints/bert/{}_{}".format(
              "uncased" if do_lower_case else "cased",
              "L-24_H-1024_A-16" if bert_large else "L-12_H-768_A-12",
          )
  
          if tpu_name is None:
              raise ValueError(
                  "Need to set TPU name when running on a TPU"
              )
          tpu_cluster_resolver = (
              tf.contrib.cluster_resolver.TPUClusterResolver(
                  tpu_name, zone=tpu_zone, project=gcp_project
              )
          )
      else:
          if bert_large:
              if do_lower_case:
                  url = "https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-24_H-1024_A-16.zip"
              else:
                  url = "https://storage.googleapis.com/bert_models/2018_10_18/cased_L-24_H-1024_A-16.zip"
          else:
              if do_lower_case:
                  url = "https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-12_H-768_A-12.zip"
              else:
                  url = "https://storage.googleapis.com/bert_models/2018_10_18/cased_L-12_H-768_A-12.zip"
          download_path = url.split("/")[-1]
          unzip_path = download_path.split(".")[0]
  
          ####
          if not os.path.exists(unzip_path):
              response = requests.get(url, stream=True)
              with open(download_path, "wb") as handle:
                  for chunk in response.iter_content(chunk_size=512):
                      if chunk:  # filter out keep-alive new chunks
                          handle.write(chunk)
              with zipfile.ZipFile(download_path) as zf:
                  zf.extractall()
  
          print("BERT HAS BEEN DOWNLOADED")
          path = os.path.join(os.getcwd(), unzip_path)
  
          # TPU shit
          tpu_cluster_resolver = None
  
      # Not sure yet if os.path.join works on gs:// files.
      vocab_file = f"{path}/vocab.txt"
      init_checkpoint = f"{path}/bert_model.ckpt"
      bert_config = modeling.BertConfig.from_json_file(
          f"{path}/bert_config.json"
      )
      tokenization.validate_case_matches_checkpoint(
          do_lower_case, init_checkpoint
      )
  
      tokenizer = tokenization.FullTokenizer(
          vocab_file=vocab_file, do_lower_case=do_lower_case
      )
  
      run_config = tf.contrib.tpu.RunConfig(
          cluster=tpu_cluster_resolver,
          master=master,
          model_dir=output_dir,
          save_checkpoints_steps=iterations_per_loop,
          tpu_config=tf.contrib.tpu.TPUConfig(
              iterations_per_loop=iterations_per_loop,
              num_shards=num_tpu_cores,
              per_host_input_for_training=tf.contrib.tpu.InputPipelineConfig.PER_HOST_V2,
          ),
      )
  
      if max_seq_length > bert_config.max_position_embeddings:
          raise ValueError(
              "Cannot use sequence length %d because the BERT model "
              "was only trained up to sequence length %d"
              % (max_seq_length, bert_config.max_position_embeddings)
          )
  
      return run_config, bert_config, tokenizer, init_checkpoint
  
imports: import collections
  import csv
  import os
  import zipfile
  import requests
  import tensorflow
  from adversarial_filtering.bert import modeling
  from adversarial_filtering.bert import tokenization
  import random
  import gcsfs
  import numpy
  from tensorflow.python.lib.io import file_io
label_desc: Use image node_id 4 for calling a global function with example usage: setup_bert(use_tpu, do_lower_case, bert_large, output_dir, tpu_name, tpu_zone, gcp_project, master, iterations_per_loop, num_tpu_cores, max_seq_length) and returns: run_config,  bert_config,  tokenizer,  init_checkpoint