code: def generate_labels(
      answers,
      passage_len_t,
      question_len_t,
      max_answers,
      sp_toks_start,
      sp_toks_mid,
      answer_type_list=None,
  ):
      """
      Creates QA label vector for each answer in answers. The labels are the index of the start and end token
      relative to the passage. They are contained in an array of size (max_answers, 2).
      -1 is used to fill array since there the number of answers is often less than max_answers.
      The index values take in to consideration the question tokens, and also special tokens such as [CLS].
      When the answer is not fully contained in the passage, or the question
      is impossible to answer, the start_idx and end_idx are 0 i.e. start and end are on the very first token
      (in most models, this is the [CLS] token). Note that in our implementation NQ has 4 answer types
      ["no_answer", "yes", "no", "span"] and this is what answer_type_list should look like
      """
  
      # Note here that label_idxs get passed to the QuestionAnsweringHead and answer_types get passed to the text
      # classification head. label_idxs may contain multiple start, end labels since SQuAD dev and test sets
      # can have multiple annotations. By contrast, Natural Questions only has one annotation per sample hence
      # why answer_types is only of length 1
      label_idxs = np.full((max_answers, 2), fill_value=-1)
      answer_types = np.asarray([-1])
      answer_str = ""
  
      # If there are no answers
      if len(answers) == 0:
          label_idxs[0, :] = 0
          answer_types[:] = 0
          return label_idxs, answer_types
  
      # Iterate over the answers for the one sample
      for i, answer in enumerate(answers):
          start_idx = answer["start_t"]
          end_idx = answer["end_t"]
  
          # Check that the start and end are contained within this passage
          if answer_in_passage(start_idx, end_idx, passage_len_t):
              label_idxs[i][0] = (
                  sp_toks_start
                  + question_len_t
                  + sp_toks_mid
                  + start_idx
              )
              label_idxs[i][1] = (
                  sp_toks_start + question_len_t + sp_toks_mid + end_idx
              )
              answer_str = answer["answer_type"]
          # If the start or end of the span answer is outside the passage, treat passage as no_answer
          else:
              label_idxs[i][0] = 0
              label_idxs[i][1] = 0
              answer_str = "no_answer"
  
      if answer_type_list:
          answer_types[0] = answer_type_list.index(answer_str)
  
      return label_idxs, answer_types
  
file_path: FARM/farm/data_handler/nq_utils.py
imports: import logging
  import re
  import numpy
  from farm.data_handler.samples import Sample
label_desc: Use image node_id 2 for calling a global function with example usage: generate_labels(answers, passage_len_t, question_len_t, max_answers, sp_toks_start, sp_toks_mid, answer_type_list) and returns: label_idxs,  answer_types, label_idxs,  answer_types