class_docstr: TARS model for text classification.
  
  In the backend, the model uses a BERT based binary text classifier which given a <label, text> pair predicts the
  probability of two classes "True", and "False". 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","text","classification",".","In","the","backend",",","the","model","uses","a","BERT","based","binary","text","classifier","which","given","a","<","label",",","text",">","pair","predicts","the","probability","of","two","classes","``","True","''",",","and","``","False","''",".","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 sentences on the Text Classification task.
  
  Args:
      return_probabilities_for_all_classes: if True, all classes will be added with their respective confidences.
      sentences: a Sentence or a List of Sentence
      force_label: when multilabel is active, you can force to always get at least one prediction.
      multi_label: if True multiple labels can be predicted. Defaults to the setting of the configured task.
      label_threshold: when multi_label, specify the threshold when a class is considered as predicted.
      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.
docstr_tok: ["Predict","sentences","on","the","Text","Classification","task",".","Args",":","return_probabilities_for_all_classes",":","if","True",",","all","classes","will","be","added","with","their","respective","confidences",".","sentences",":","a","Sentence","or","a","List","of","Sentence","force_label",":","when","multilabel","is","active",",","you","can","force","to","always","get","at","least","one","prediction",".","multi_label",":","if","True","multiple","labels","can","be","predicted",".","Defaults","to","the","setting","of","the","configured","task",".","label_threshold",":","when","multi_label",",","specify","the","threshold","when","a","class","is","considered","as","predicted",".","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","."]
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",
      label_threshold: float = 0.5,
      multi_label: Optional[bool] = None,
      force_label: bool = False,
  ):
      """Predict sentences on the Text Classification task.
  
      Args:
          return_probabilities_for_all_classes: if True, all classes will be added with their respective confidences.
          sentences: a Sentence or a List of Sentence
          force_label: when multilabel is active, you can force to always get at least one prediction.
          multi_label: if True multiple labels can be predicted. Defaults to the setting of the configured task.
          label_threshold: when multi_label, specify the threshold when a class is considered as predicted.
          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.
  
  
      """
      if label_name is None:
          label_name = self.get_current_label_type()
  
      if multi_label is None:
          multi_label = self.is_current_task_multi_label()
  
      if not multi_label:
          label_threshold = 0.0
  
      # with torch.no_grad():
      if not sentences:
          return sentences
  
      if isinstance(sentences, Sentence):
          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:
          progressbar = tqdm(dataloader)
          progressbar.set_description("Batch inference")
          dataloader = progressbar
  
      overall_loss = 0
      overall_count = 0
  
      all_labels = [
          label.decode("utf-8")
          for label in self.get_current_label_dictionary().idx2item
      ]
  
      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)
  
                  best_value = ""
                  best_score = 0.0
  
                  for (
                      label,
                      tars_sentence,
                  ) in labels_to_sentence.items():
                      # add all labels that according to TARS match the text and are above threshold
                      predicted_tars_label = tars_sentence.get_label(
                          label_name
                      )
                      score = (
                          predicted_tars_label.score
                          if predicted_tars_label.value
                          == self.LABEL_MATCH
                          else 1 - predicted_tars_label.score
                      )
                      if score > label_threshold:
                          # do not add labels below confidence threshold
                          sentence.add_label(label_name, label, score)
                      if score > best_score:
                          best_score = score
                          best_value = label
  
                  # only use label with the highest confidence if enforcing single-label predictions
                  # add the label with the highest score even if below the threshold if force label is activated.
                  if not multi_label or (
                      multi_label
                      and force_label
                      and len(sentence.get_labels(label_name)) == 0
                  ):
                      # remove previously added labels and only add the best label
                      sentence.remove_labels(label_name)
                      sentence.add_label(
                          typename=label_name,
                          value=best_value,
                          score=best_score,
                      )
  
              # 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 8 for calling the TARSClassifier 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, label_threshold, multi_label, force_label) and returns: None, sentences, overall_loss,  overall_count