class_docstr: Transformers handler class for sequence, token classification and question answering.
docstr: Predict the class (or classes) of the received text using the
  serialized transformers checkpoint.
  Args:
      input_batch (list): List of Text Tensors from the pre-process function is passed here
  Returns:
      list : It returns a list of the predicted value for the input text
docstr_tok: ["Predict","the","class","(","or","classes",")","of","the","received","text","using","the","serialized","transformers","checkpoint",".","Args",":","input_batch","(","list",")",":","List","of","Text","Tensors","from","the","pre-process","function","is","passed","here","Returns",":","list",":","It","returns","a","list","of","the","predicted","value","for","the","input","text"]
code: def inference(self, input_batch):
      """Predict the class (or classes) of the received text using the
      serialized transformers checkpoint.
      Args:
          input_batch (list): List of Text Tensors from the pre-process function is passed here
      Returns:
          list : It returns a list of the predicted value for the input text
      """
      input_ids_batch, attention_mask_batch = input_batch
      inferences = []
      # Handling inference for sequence_classification.
      if self.setup_config["mode"] == "sequence_classification":
          predictions = self.model(
              input_ids_batch, attention_mask_batch
          )
          print(
              "This the output size from the Seq classification model",
              predictions[0].size(),
          )
          print(
              "This the output from the Seq classification model",
              predictions,
          )
  
          num_rows, num_cols = predictions[0].shape
          for i in range(num_rows):
              out = predictions[0][i].unsqueeze(0)
              y_hat = out.argmax(1).item()
              predicted_idx = str(y_hat)
              inferences.append(self.mapping[predicted_idx])
      # Handling inference for question_answering.
      elif self.setup_config["mode"] == "question_answering":
          # the output should be only answer_start and answer_end
          # we are outputing the words just for demonstration.
          if self.setup_config["save_mode"] == "pretrained":
              outputs = self.model(
                  input_ids_batch, attention_mask_batch
              )
              answer_start_scores = outputs.start_logits
              answer_end_scores = outputs.end_logits
          else:
              answer_start_scores, answer_end_scores = self.model(
                  input_ids_batch, attention_mask_batch
              )
          print(
              "This the output size for answer start scores from the question answering model",
              answer_start_scores.size(),
          )
          print(
              "This the output for answer start scores from the question answering model",
              answer_start_scores,
          )
          print(
              "This the output size for answer end scores from the question answering model",
              answer_end_scores.size(),
          )
          print(
              "This the output for answer end scores from the question answering model",
              answer_end_scores,
          )
  
          num_rows, num_cols = answer_start_scores.shape
          # inferences = []
          for i in range(num_rows):
              answer_start_scores_one_seq = answer_start_scores[
                  i
              ].unsqueeze(0)
              answer_start = torch.argmax(answer_start_scores_one_seq)
              answer_end_scores_one_seq = answer_end_scores[
                  i
              ].unsqueeze(0)
              answer_end = torch.argmax(answer_end_scores_one_seq) + 1
              prediction = self.tokenizer.convert_tokens_to_string(
                  self.tokenizer.convert_ids_to_tokens(
                      input_ids_batch[i].tolist()[
                          answer_start:answer_end
                      ]
                  )
              )
              inferences.append(prediction)
          logger.info("Model predicted: '%s'", prediction)
      # Handling inference for token_classification.
      elif self.setup_config["mode"] == "token_classification":
          outputs = self.model(input_ids_batch, attention_mask_batch)[0]
          print(
              "This the output size from the token classification model",
              outputs.size(),
          )
          print(
              "This the output from the token classification model",
              outputs,
          )
          num_rows = outputs.shape[0]
          for i in range(num_rows):
              output = outputs[i].unsqueeze(0)
              predictions = torch.argmax(output, dim=2)
              tokens = self.tokenizer.tokenize(
                  self.tokenizer.decode(input_ids_batch[i])
              )
              if self.mapping:
                  label_list = self.mapping["label_list"]
              label_list = label_list.strip("][").split(", ")
              prediction = [
                  (token, label_list[prediction])
                  for token, prediction in zip(
                      tokens, predictions[0].tolist()
                  )
              ]
              inferences.append(prediction)
          logger.info("Model predicted: '%s'", prediction)
  
      # Handling inference for text_generation.
      if self.setup_config["mode"] == "text_generation":
          if self.setup_config["model_parallel"]:
              # Need to move the first device, as the trasnformer model has been placed there
              # https://github.com/huggingface/transformers/blob/v4.17.0/src/transformers/models/gpt2/modeling_gpt2.py#L970
              input_ids_batch = input_ids_batch.to("cuda:0")
          outputs = self.model.generate(
              input_ids_batch,
              max_length=50,
              do_sample=True,
              top_p=0.95,
              top_k=60,
          )
          for i, x in enumerate(outputs):
              inferences.append(
                  self.tokenizer.decode(
                      outputs[i], skip_special_tokens=True
                  )
              )
  
          logger.info("Generated text: '%s'", inferences)
  
      print("Generated text", inferences)
      return inferences
  
imports: import ast
  import json
  import logging
  import os
  from abc import ABC
  import torch
  import transformers
  from captum.attr import LayerIntegratedGradients
  from transformers import AutoModelForCausalLM, AutoModelForQuestionAnswering, AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoTokenizer, GPT2TokenizerFast
  from ts.torch_handler.base_handler import BaseHandler
label_desc: Use image node_id 4 for calling the TransformersSeqClassifierHandler obj's underlying member method code with example usage: obj.inference(input_batch) and returns: inferences