code: def cmd():
      """Function called when invoking from the terminal."""
  
      parser = argparse.ArgumentParser(
          description="Easily retrain OpenAI's GPT-2 text-generating model on new texts. (https://github.com/minimaxir/gpt-2-simple)"
      )
  
      # Explicit arguments
  
      parser.add_argument(
          "--mode",
          help='Mode for using the CLI (either "finetune" or "generate") [Required]',
          nargs="?",
      )
      parser.add_argument(
          "--run_name",
          help="[finetune/generate] Run number to save/load the model",
          nargs="?",
          default="run1",
      )
      parser.add_argument(
          "--checkpoint_dir",
          help="[finetune] Path of the checkpoint directory",
          nargs="?",
          default="checkpoint",
      )
      parser.add_argument(
          "--model_name",
          help="[finetune] Name of the GPT-2 model to finetune",
          nargs="?",
          default="124M",
      )
      parser.add_argument(
          "--model_dir",
          help="[finetune] Path of directory of the GPT-2 model to finetune",
          nargs="?",
          default="models",
      )
      parser.add_argument(
          "--dataset",
          help="[finetune] Path to the source text.",
          nargs="?",
          default=None,
      )
      parser.add_argument(
          "--steps",
          help="[finetune] Number of steps to train (-1 for infinite)",
          nargs="?",
          default=-1,
      )
      parser.add_argument(
          "--restore_from",
          help="[finetune] Whether to load model 'fresh' or from 'latest' checkpoint.",
          nargs="?",
          default="latest",
      )
      parser.add_argument(
          "--sample_every",
          help="[finetune] After how many steps to print sample",
          nargs="?",
          default=1000000,
          type=int,
      )
      parser.add_argument(
          "--save_every",
          help="[finetune] After how many steps to save checkpoint",
          nargs="?",
          default=100,
          type=int,
      )
      parser.add_argument(
          "--print_every",
          help="[finetune] After how many steps to print progress",
          nargs="?",
          default=10,
          type=int,
      )
      parser.add_argument(
          "--optimizer",
          help="[finetune] Optimizer to use for finetuning (adam or sgd)",
          nargs="?",
          default="adam",
      )
      parser.add_argument(
          "--overwrite",
          help="[finetune] Overwrite existing model when continuing training",
          nargs="?",
          default=False,
          type=lambda x: (str(x).lower() == "true"),
      )
      parser.add_argument(
          "--nfiles",
          help="[generate] How many files to generate.",
          nargs="?",
          default=1,
          type=int,
      )
      parser.add_argument(
          "--nsamples",
          help="[generate] How many texts to generate.",
          nargs="?",
          default=1,
          type=int,
      )
      parser.add_argument(
          "--folder",
          help="[generate] Folder to save the generated files",
          nargs="?",
          default="gen",
          type=str,
      )
      parser.add_argument(
          "--length",
          help="[generate] Length (tokens) of the generated texts",
          nargs="?",
          default=1023,
          type=int,
      )
      parser.add_argument(
          "--temperature",
          help="[generate] Temperature of the generated texts",
          nargs="?",
          default=0.7,
          type=float,
      )
      parser.add_argument(
          "--top_k",
          help="[generate] Sample only from top k tokens",
          nargs="?",
          default=0,
          type=int,
      )
      parser.add_argument(
          "--top_p",
          help="[generate] Sample from top p prob (overrides top_k if nonzero)",
          nargs="?",
          default=0.0,
          type=float,
      )
      parser.add_argument(
          "--batch_size",
          help="[generate] Batch size for generation (increase for GPUs)",
          nargs="?",
          default=1,
          type=int,
      )
      parser.add_argument(
          "--prefix",
          help="[generate] Prefix for generated texts",
          nargs="?",
          default=None,
      )
      parser.add_argument(
          "--truncate",
          help="[generate] Truncation for generated texts",
          nargs="?",
          default=None,
      )
      # https://stackoverflow.com/a/46951029
      parser.add_argument(
          "--include_prefix",
          help="[generate] Include prefix when truncating.",
          nargs="?",
          default=True,
          type=lambda x: (str(x).lower() == "true"),
      )
      parser.add_argument(
          "--sample_delim",
          help="[generate] Delimiter between each generated sample.",
          nargs="?",
          default="=" * 20 + "\n",
          type=str,
      )
      parser.add_argument(
          "--multi_gpu",
          help="[generate/finetune] Attempt to allocate multiple GPUs for running.",
          nargs="?",
          default=True,
          type=lambda x: (str(x).lower() == "true"),
      )
  
      # Positional arguments
      parser.add_argument("mode", nargs="?")
      parser.add_argument("dataset", nargs="?")
  
      args = parser.parse_args()
      assert args.mode in [
          "finetune",
          "generate",
      ], "Mode must be 'finetune' or 'generate'"
  
      if args.mode == "finetune":
          assert (
              args.dataset is not None
          ), "You need to provide a dataset."
  
          cmd_finetune(
              dataset=args.dataset,
              run_name=args.run_name,
              checkpoint_dir=args.checkpoint_dir,
              model_name=args.model_name,
              model_dir=args.model_dir,
              steps=args.steps,
              restore_from=args.restore_from,
              sample_every=args.sample_every,
              save_every=args.save_every,
              print_every=args.print_every,
              optimizer=args.optimizer,
              overwrite=args.overwrite,
              multi_gpu=args.multi_gpu,
          )
      if args.mode == "generate":
          cmd_generate(
              nfiles=args.nfiles,
              nsamples=args.nsamples,
              folder=args.folder,
              length=args.length,
              temperature=args.temperature,
              batch_size=args.batch_size,
              prefix=args.prefix,
              truncate=args.truncate,
              include_prefix=args.include_prefix,
              sample_delim=args.sample_delim,
              run_name=args.run_name,
              checkpoint_dir=args.checkpoint_dir,
              top_k=args.top_k,
              top_p=args.top_p,
              multi_gpu=args.multi_gpu,
          )
  
imports: import tarfile
  import os
  import json
  import requests
  import sys
  import shutil
  import re
  from tqdm import tqdm, trange
  import numpy
  import tensorflow
  from tensorflow.core.protobuf import rewriter_config_pb2
  from tensorflow.python.client import device_lib
  import time
  from datetime import datetime
  import csv
  import argparse
  from sys import exit
  import codecs
  from gpt_2_simple.src import model, sample, encoder, memory_saving_gradients
  from gpt_2_simple.src.load_dataset import load_dataset, Sampler
  from gpt_2_simple.src.accumulate import AccumulatingOptimizer
label_desc: Use image node_id 20 for calling a global function with example usage: cmd() without return types