class_docstr: TARS model for sequence tagging.
  
  In the backend, the model uses a BERT based 5-class sequence labeler which given a <label, text> pair predicts the
  probability for each word to belong to one of the BIOES classes. The input data is a usual Sentence object which
  is inflated by the model internally before pushing it through the transformer stack of BERT.
class_docstr_tok: ["TARS","model","for","sequence","tagging",".","In","the","backend",",","the","model","uses","a","BERT","based","5-class","sequence","labeler","which","given","a","<","label",",","text",">","pair","predicts","the","probability","for","each","word","to","belong","to","one","of","the","BIOES","classes",".","The","input","data","is","a","usual","Sentence","object","which","is","inflated","by","the","model","internally","before","pushing","it","through","the","transformer","stack","of","BERT","."]
docstr: Predict sequence tags for Named Entity Recognition task.
  
  Args:
      sentences: a Sentence or a List of Sentence
      mini_batch_size: size of the minibatch, usually bigger is more rapid but consume more memory,
          up to a point when it has no more effect.
      all_tag_prob: True to compute the score for each tag on each token,
          otherwise only the score of the best tag is returned
      verbose: set to True to display a progress bar
      return_loss: set to True to also compute the loss
      label_name: set this to change the name of the label type that is predicted
      embedding_storage_mode: default is 'none' which doesn't store the embeddings in RAM. Only set to 'cpu' or 'gpu'
          if you wish to not only predict, but also keep the generated embeddings in CPU or GPU memory respectively.
          'gpu' to store embeddings in GPU memory.
      return_probabilities_for_all_classes: if True, all classes will be added with their respective confidences.
      most_probable_first: if True, nested predictions will be removed, if False all predictions will be returned,
          including overlaps
docstr_tok: ["Predict","sequence","tags","for","Named","Entity","Recognition","task",".","Args",":","sentences",":","a","Sentence","or","a","List","of","Sentence","mini_batch_size",":","size","of","the","minibatch",",","usually","bigger","is","more","rapid","but","consume","more","memory",",","up","to","a","point","when","it","has","no","more","effect",".","all_tag_prob",":","True","to","compute","the","score","for","each","tag","on","each","token",",","otherwise","only","the","score","of","the","best","tag","is","returned","verbose",":","set","to","True","to","display","a","progress","bar","return_loss",":","set","to","True","to","also","compute","the","loss","label_name",":","set","this","to","change","the","name","of","the","label","type","that","is","predicted","embedding_storage_mode",":","default","is","'none","'","which","does","n't","store","the","embeddings","in","RAM",".","Only","set","to","'cpu","'","or","'gpu'","if","you","wish","to","not","only","predict",",","but","also","keep","the","generated","embeddings","in","CPU","or","GPU","memory","respectively",".","'gpu","'","to","store","embeddings","in","GPU","memory",".","return_probabilities_for_all_classes",":","if","True",",","all","classes","will","be","added","with","their","respective","confidences",".","most_probable_first",":","if","True",",","nested","predictions","will","be","removed",",","if","False","all","predictions","will","be","returned",",","including","overlaps"]
code: def predict(
      self,
      sentences: Union[List[Sentence], Sentence],
      mini_batch_size=32,
      return_probabilities_for_all_classes: bool = False,
      verbose: bool = False,
      label_name: Optional[str] = None,
      return_loss=False,
      embedding_storage_mode="none",
      most_probable_first: bool = True,
  ):
      """Predict sequence tags for Named Entity Recognition task.
  
      Args:
          sentences: a Sentence or a List of Sentence
          mini_batch_size: size of the minibatch, usually bigger is more rapid but consume more memory,
              up to a point when it has no more effect.
          all_tag_prob: True to compute the score for each tag on each token,
              otherwise only the score of the best tag is returned
          verbose: set to True to display a progress bar
          return_loss: set to True to also compute the loss
          label_name: set this to change the name of the label type that is predicted
          embedding_storage_mode: default is 'none' which doesn't store the embeddings in RAM. Only set to 'cpu' or 'gpu'
              if you wish to not only predict, but also keep the generated embeddings in CPU or GPU memory respectively.
              'gpu' to store embeddings in GPU memory.
          return_probabilities_for_all_classes: if True, all classes will be added with their respective confidences.
          most_probable_first: if True, nested predictions will be removed, if False all predictions will be returned,
              including overlaps
  
      """
      if label_name is None:
          label_name = self.get_current_label_type()
  
      if not sentences:
          return sentences
  
      if not isinstance(sentences, list):
          sentences = [sentences]
  
      Sentence.set_context_for_sentences(sentences)
  
      reordered_sentences = sorted(
          sentences, key=lambda s: len(s), reverse=True
      )
  
      dataloader = DataLoader(
          dataset=FlairDatapointDataset(reordered_sentences),
          batch_size=mini_batch_size,
      )
  
      # progress bar for verbosity
      if verbose:
          dataloader = tqdm(dataloader)
  
      all_labels = [
          label.decode("utf-8")
          for label in self.get_current_label_dictionary().idx2item
      ]
      overall_loss = 0
      overall_count = 0
      with torch.no_grad():
          for batch in dataloader:
              batch = self._filter_empty_sentences(batch)
              # stop if all sentences are empty
              if not batch:
                  continue
  
              tars_sentences: List[Sentence] = []
              all_labels_to_sentence: List[Dict[str, Sentence]] = []
              for sentence in batch:
                  # always remove tags first
                  sentence.remove_labels(label_name)
                  labels_to_sentence: Dict[str, Sentence] = {}
                  for label in all_labels:
                      tars_sentence = self._get_tars_formatted_sentence(
                          label, sentence
                      )
                      tars_sentences.append(tars_sentence)
                      labels_to_sentence[label] = tars_sentence
                  all_labels_to_sentence.append(labels_to_sentence)
  
              loss_and_count = self.tars_model.predict(
                  tars_sentences,
                  label_name=label_name,
                  mini_batch_size=mini_batch_size,
                  return_loss=return_loss,
              )
  
              if return_loss:
                  overall_loss += loss_and_count[0].item()
                  overall_count += loss_and_count[1]
  
              # go through each sentence in the batch
              for sentence, labels_to_sentence in zip(
                  batch, all_labels_to_sentence
              ):
                  # always remove tags first
                  sentence.remove_labels(label_name)
  
                  all_detected = {}
  
                  for (
                      label,
                      tars_sentence,
                  ) in labels_to_sentence.items():
                      for predicted in tars_sentence.get_labels(
                          label_name
                      ):
                          predicted.set_value(label, predicted.score)
                          all_detected[predicted] = predicted.score
  
                  if most_probable_first:
                      import operator
  
                      already_set_indices: List[int] = []
  
                      sorted_x = sorted(
                          all_detected.items(),
                          key=operator.itemgetter(1),
                      )
                      sorted_x.reverse()
                      for tuple in sorted_x:
                          # get the span and its label
                          label = tuple[0]
  
                          label_length = (
                              0
                              if not self.prefix
                              else len(label.value.split(" "))
                              + len(self.separator.split(" "))
                          )
  
                          # determine whether tokens in this span already have a label
                          tag_this = True
                          for token in label.data_point:
                              corresponding_token = sentence.get_token(
                                  token.idx - label_length
                              )
                              if corresponding_token is None:
                                  tag_this = False
                                  continue
                              if (
                                  corresponding_token.idx
                                  in already_set_indices
                              ):
                                  tag_this = False
                                  continue
  
                          # only add if all tokens have no label
                          if tag_this:
                              # make and add a corresponding predicted span
                              predicted_span = Span(
                                  [
                                      sentence.get_token(
                                          token.idx - label_length
                                      )
                                      for token in label.data_point
                                  ]
                              )
                              predicted_span.add_label(
                                  label_name,
                                  value=label.value,
                                  score=label.score,
                              )
  
                              # set indices so that no token can be tagged twice
                              already_set_indices.extend(
                                  token.idx for token in predicted_span
                              )
  
              # clearing token embeddings to save memory
              store_embeddings(
                  batch, storage_mode=embedding_storage_mode
              )
  
      if return_loss:
          return overall_loss, overall_count
      return None
  
imports: import logging
  import typing
  from abc import ABC
  from collections import OrderedDict
  from pathlib import Path
  from typing import Any, Dict, List, Optional, Set, Tuple, Union
  import numpy
  import torch
  from sklearn.metrics.pairwise import cosine_similarity
  from sklearn.preprocessing import minmax_scale
  from tqdm import tqdm
  import flair
  from flair.data import Corpus, Dictionary, Sentence, Span
  from flair.datasets import DataLoader, FlairDatapointDataset
  from flair.embeddings import TokenEmbeddings, TransformerDocumentEmbeddings, TransformerWordEmbeddings
  from flair.file_utils import cached_path
  from flair.models.sequence_tagger_model import SequenceTagger
  from flair.models.text_classification_model import TextClassifier
  from flair.training_utils import store_embeddings
label_desc: Use image node_id 7 for calling the TARSTagger obj's underlying member method code with example usage: obj.predict(sentences, mini_batch_size, return_probabilities_for_all_classes, verbose, label_name, return_loss, embedding_storage_mode, most_probable_first) and returns: None, sentences, overall_loss,  overall_count