code: def compute_metrics(self):
      import collections
  
      import evaluate
      import numpy as np
      from transformers import AutoModelForQuestionAnswering
  
      func = None
      if self._record_class == TextClassificationRecord:
          accuracy = evaluate.load("accuracy")
          f1 = evaluate.load("f1", config_name="multilabel")
  
          def compute_metrics_text_classification_multi_label(
              eval_pred,
          ):
              logits, labels = eval_pred
              # apply sigmoid
              predictions = (1.0 / (1 + np.exp(-logits))) > 0.5
  
              # f1 micro averaged
              metrics = f1.compute(
                  predictions=predictions,
                  references=labels,
                  average="micro",
              )
              # f1 per label
              per_label_metric = f1.compute(
                  predictions=predictions,
                  references=labels,
                  average=None,
              )
              for label, f1_score in zip(
                  self._label_list, per_label_metric["f1"]
              ):
                  metrics[f"f1_{label}"] = f1_score
  
              return metrics
  
          def compute_metrics_text_classification(eval_pred):
              predictions, labels = eval_pred
              predictions = np.argmax(predictions, axis=1)
              return accuracy.compute(
                  predictions=predictions, references=labels
              )
  
          if self._multi_label:
              func = compute_metrics_text_classification_multi_label
          else:
              func = compute_metrics_text_classification
  
      elif self._record_class == TokenClassificationRecord:
          seqeval = evaluate.load("seqeval")
  
          def compute_metrics(p):
              predictions, labels = p
              predictions = np.argmax(predictions, axis=2)
  
              true_predictions = [
                  [
                      self._label_list[p]
                      for (p, l) in zip(prediction, label)
                      if l != -100
                  ]
                  for prediction, label in zip(predictions, labels)
              ]
              true_labels = [
                  [
                      self._label_list[l]
                      for (p, l) in zip(prediction, label)
                      if l != -100
                  ]
                  for prediction, label in zip(predictions, labels)
              ]
  
              results = seqeval.compute(
                  predictions=true_predictions, references=true_labels
              )
              return {
                  "precision": results["overall_precision"],
                  "recall": results["overall_recall"],
                  "f1": results["overall_f1"],
                  "accuracy": results["overall_accuracy"],
              }
  
          func = compute_metrics
      elif AutoModelForQuestionAnswering:
          squad = evaluate.load("squad")
  
          # Copy from https://huggingface.co/learn/nlp-course/chapter7/7?fw=pt#fine-tuning-the-model-with-the-trainer-api
          n_best = 20
  
          def compute_metrics_question_answering(pred):
              start_logits, end_logits = pred.predictions
              features = self._tokenized_eval_dataset
              examples = self._eval_dataset
  
              example_to_features = collections.defaultdict(list)
              for idx, feature in enumerate(features):
                  example_to_features[feature["example_id"]].append(idx)
  
              predicted_answers = []
              for example in examples:
                  example_id = example["id"]
                  context = example["context"]
                  answers = []
  
                  # Loop through all features associated with that example
                  for feature_index in example_to_features[example_id]:
                      start_logit = start_logits[feature_index]
                      end_logit = end_logits[feature_index]
                      offsets = features[feature_index][
                          "offset_mapping"
                      ]
  
                      start_indexes = np.argsort(start_logit)[
                          -1 : -n_best - 1 : -1
                      ].tolist()
                      end_indexes = np.argsort(end_logit)[
                          -1 : -n_best - 1 : -1
                      ].tolist()
                      for start_index in start_indexes:
                          for end_index in end_indexes:
                              # Skip answers that are not fully in the context
                              if (
                                  offsets[start_index] is None
                                  or offsets[end_index] is None
                              ):
                                  continue
                              # Skip answers with a length that is either < 0 or > max_answer_length
                              if (
                                  end_index < start_index
                                  or end_index - start_index + 1
                                  > self.trainer_tokenizer.model_max_length
                              ):
                                  continue
  
                              answer = {
                                  "text": context[
                                      offsets[start_index][0] : offsets[
                                          end_index
                                      ][1]
                                  ],
                                  "logit_score": start_logit[
                                      start_index
                                  ]
                                  + end_logit[end_index],
                              }
                              answers.append(answer)
  
                  # Select the answer with the best score
                  if len(answers) > 0:
                      best_answer = max(
                          answers, key=lambda x: x["logit_score"]
                      )
                      predicted_answers.append(
                          {
                              "id": example_id,
                              "prediction_text": best_answer["text"],
                          }
                      )
                  else:
                      predicted_answers.append(
                          {"id": example_id, "prediction_text": ""}
                      )
  
              theoretical_answers = [
                  {"id": ex["id"], "answers": ex["answers"]}
                  for ex in examples
              ]
  
              return squad.compute(
                  predictions=predicted_answers,
                  references=theoretical_answers,
              )
  
          func = compute_metrics_question_answering
      else:
          raise NotImplementedError(
              "Other record types are not supported yet."
          )
      return func
  
file_path: argilla/src/argilla/training/transformers.py
imports: import logging
  from typing import List, Union
  from datasets import DatasetDict
  from argilla.client.models import TextClassificationRecord, TokenClassificationRecord
  from argilla.training.base import ArgillaTrainerSkeleton
  from argilla.training.utils import _apply_column_mapping, filter_allowed_args, get_default_args
  from argilla.utils.dependency import require_dependencies
label_desc: Use image node_id 8 for calling the ArgillaTransformersTrainer obj's underlying member method code with example usage: obj.compute_metrics() and returns: func, metrics, accuracy, dict, squad