code: def run(
      args,
      logger,
      biencoder,
      biencoder_params,
      candidate_encoding,
      indexer,
      id2title,
      id2text,
      id2wikidata,
      test_data=None,
  ):
      if (
          not test_data
          and not getattr(args, "test_mentions", None)
          and not getattr(args, "interactive", None)
      ):
          msg = (
              "ERROR: either you start BLINK with the "
              "interactive option (-i) or you pass in input test mentions (--test_mentions)"
              "and test entities (--test_entities) or manually pass in test data"
          )
          raise ValueError(msg)
  
      if getattr(
          args, "save_preds_dir", None
      ) is not None and not os.path.exists(args.save_preds_dir):
          os.makedirs(args.save_preds_dir)
          print("Saving preds in {}".format(args.save_preds_dir))
  
      stopping_condition = False
      threshold = float(args.threshold)
      if args.threshold_type == "top_entity_by_mention":
          assert args.mention_threshold is not None
          mention_threshold = float(args.mention_threshold)
      else:
          mention_threshold = threshold
      if args.interactive:
          while not stopping_condition:
              if logger:
                  logger.info("interactive mode")
  
              # Interactive
              text = input("insert text: ")
  
              # Prepare data
              samples = [{"id": "-1", "text": text}]
              dataloader = _process_biencoder_dataloader(
                  samples,
                  biencoder.tokenizer,
                  biencoder_params,
                  logger,
              )
  
              # Run inference
              (
                  nns,
                  dists,
                  pred_mention_bounds,
                  mention_scores,
                  cand_scores,
              ) = _run_biencoder(
                  args,
                  biencoder,
                  dataloader,
                  candidate_encoding,
                  samples=samples,
                  num_cand_mentions=args.num_cand_mentions,
                  num_cand_entities=args.num_cand_entities,
                  device="cpu"
                  if biencoder_params["no_cuda"]
                  else "cuda",
                  threshold=mention_threshold,
                  indexer=indexer,
              )
  
              action = "c"
              while action == "c":
                  all_entity_preds = get_predictions(
                      args,
                      dataloader,
                      biencoder_params,
                      samples,
                      nns,
                      dists,
                      mention_scores,
                      cand_scores,
                      pred_mention_bounds,
                      id2title,
                      threshold=threshold,
                      mention_threshold=mention_threshold,
                  )[0]
  
                  pred_triples = all_entity_preds[0]["pred_triples"]
                  _print_colorful_text(
                      all_entity_preds[0]["tokens"],
                      tokenizer,
                      pred_triples,
                  )
                  _print_colorful_prediction(
                      all_entity_preds,
                      pred_triples,
                      id2text,
                      id2wikidata,
                  )
                  action = input(
                      "Next question [n] / change threshold [c]: "
                  )
                  while action != "n" and action != "c":
                      action = input(
                          "Next question [n] / change threshold [c]: "
                      )
                  if action == "c":
                      print("Current threshold {}".format(threshold))
                      while True:
                          threshold = input(
                              "New threshold (increase for less cands, decrease for more cands): "
                          )
                          try:
                              threshold = float(threshold)
                              break
                          except:
                              print(
                                  "Error! Expected float, got {}. Try again.".format(
                                      threshold
                                  )
                              )
  
      else:
          if not test_data:
              samples, num_unk = _get_test_samples(
                  args.test_mentions,
                  args.test_entities,
                  logger,
              )
          else:
              samples = test_data
  
          if logger:
              logger.info("Preparing data for biencoder")
          dataloader = _process_biencoder_dataloader(
              samples,
              biencoder.tokenizer,
              biencoder_params,
              None,
          )
  
          stopping_condition = True
  
          # prepare the data for biencoder
          # run biencoder if predictions not saved
          if not getattr(
              args, "save_preds_dir", None
          ) or not os.path.exists(
              os.path.join(
                  args.save_preds_dir, "biencoder_mention_bounds.npy"
              )
          ):
              # run biencoder
              if logger:
                  logger.info("Running biencoder...")
  
              start_time = time.time()
              (
                  nns,
                  dists,
                  pred_mention_bounds,
                  mention_scores,
                  cand_scores,
              ) = _run_biencoder(
                  args,
                  biencoder,
                  dataloader,
                  candidate_encoding,
                  samples=samples,
                  num_cand_mentions=args.num_cand_mentions,
                  num_cand_entities=args.num_cand_entities,
                  device="cpu"
                  if biencoder_params["no_cuda"]
                  else "cuda",
                  threshold=mention_threshold,
                  indexer=indexer,
              )
              end_time = time.time()
              if logger:
                  logger.info("Finished running biencoder")
  
              runtime = end_time - start_time
  
              if getattr(args, "save_preds_dir", None):
                  _save_biencoder_outs(
                      args.save_preds_dir,
                      nns,
                      dists,
                      pred_mention_bounds,
                      cand_scores,
                      mention_scores,
                      runtime,
                  )
          else:
              (
                  nns,
                  dists,
                  pred_mention_bounds,
                  cand_scores,
                  mention_scores,
                  runtime,
              ) = _load_biencoder_outs(args.save_preds_dir)
  
          assert (
              len(samples)
              == len(nns)
              == len(dists)
              == len(pred_mention_bounds)
              == len(cand_scores)
              == len(mention_scores)
          )
  
          (
              all_entity_preds,
              num_correct_weak,
              num_correct_strong,
              num_predicted,
              num_gold,
              num_correct_weak_from_input_window,
              num_correct_strong_from_input_window,
              num_gold_from_input_window,
          ) = get_predictions(
              args,
              dataloader,
              biencoder_params,
              samples,
              nns,
              dists,
              mention_scores,
              cand_scores,
              pred_mention_bounds,
              id2title,
              threshold=threshold,
              mention_threshold=mention_threshold,
          )
  
          print("*--------*")
          if num_gold > 0:
              print("WEAK MATCHING")
              display_metrics(num_correct_weak, num_predicted, num_gold)
              print("Just entities within input window...")
              display_metrics(
                  num_correct_weak_from_input_window,
                  num_predicted,
                  num_gold_from_input_window,
              )
              print("*--------*")
              print("STRONG MATCHING")
              display_metrics(
                  num_correct_strong, num_predicted, num_gold
              )
              print("Just entities within input window...")
              display_metrics(
                  num_correct_strong_from_input_window,
                  num_predicted,
                  num_gold_from_input_window,
              )
              print("*--------*")
              print("biencoder runtime = {}".format(runtime))
              print("*--------*")
  
          return all_entity_preds
  
imports: import argparse
  import json
  import sys
  from elq.index.faiss_indexer import DenseFlatIndexer, DenseHNSWFlatIndexer, DenseIVFFlatIndexer
  import logging
  import torch
  import numpy
  from colorama import init
  from termcolor import colored
  import torch.nn.functional
  import blink.ner
  from torch.utils.data import DataLoader, SequentialSampler, TensorDataset
  from elq.biencoder.biencoder import BiEncoderRanker, load_biencoder, to_bert_input
  from elq.biencoder.data_process import process_mention_data, get_context_representation_single_mention, get_candidate_representation
  import elq.candidate_ranking.utils
  import math
  from elq.vcg_utils.measures import entity_linking_tp_with_overlap
  from elq.biencoder.utils import batch_reshape_mask_left
  import os
  import sys
  from tqdm import tqdm
  import pdb
  import time
  from transformers import BertTokenizer
label_desc: Use image node_id 12 for calling a global function with example usage: run(args, logger, biencoder, biencoder_params, candidate_encoding, indexer, id2title, id2text, id2wikidata, test_data) and returns: all_entity_preds