code: def dataio_prepare(hparams, tokenizer):
      """This function prepares the datasets to be used in the brain class.
      It also defines the data processing pipeline through user-defined functions.
      """
  
      # 1. Define datasets
      data_folder = hparams["data_folder"]
  
      train_data = sb.dataio.dataset.DynamicItemDataset.from_csv(
          csv_path=hparams["train_csv"],
          replacements={"data_root": data_folder},
      )
  
      if hparams["sorting"] == "ascending":
          # we sort training data to speed up training and get better results.
          # train_data = train_data.filtered_sorted(sort_key="duration",)
  
          # we sort training data to speed up training and get better results.
          train_data = train_data.filtered_sorted(
              sort_key="duration",
              key_max_value={
                  "duration": hparams["avoid_if_longer_than"]
              },
          )
  
          # when sorting do not shuffle in dataloader ! otherwise is pointless
          hparams["dataloader_options"]["shuffle"] = False
  
      elif hparams["sorting"] == "descending":
          # train_data = train_data.filtered_sorted(
          #     sort_key="duration", reverse=True,
          # )
          train_data = train_data.filtered_sorted(
              sort_key="duration",
              reverse=True,
              key_max_value={
                  "duration": hparams["avoid_if_longer_than"]
              },
          )
          # when sorting do not shuffle in dataloader ! otherwise is pointless
          hparams["dataloader_options"]["shuffle"] = False
  
      elif hparams["sorting"] == "random":
          pass
  
      else:
          raise NotImplementedError(
              "sorting must be random, ascending or descending"
          )
      valid_data = sb.dataio.dataset.DynamicItemDataset.from_csv(
          csv_path=hparams["valid_csv"],
          replacements={"data_root": data_folder},
      )
      # We also sort the validation data so it is faster to validate
      valid_data = valid_data.filtered_sorted(sort_key="duration")
  
      test_datasets = {}
      for csv_file in hparams["test_csv"]:
          name = Path(csv_file).stem
          test_datasets[
              name
          ] = sb.dataio.dataset.DynamicItemDataset.from_csv(
              csv_path=csv_file, replacements={"data_root": data_folder}
          )
          test_datasets[name] = test_datasets[name].filtered_sorted(
              sort_key="duration"
          )
      datasets = [train_data, valid_data] + [
          i for _, i in test_datasets.items()
      ]
  
      # 2. Define audio pipeline:
      @sb.utils.data_pipeline.takes("wav", "channel", "start", "stop")
      @sb.utils.data_pipeline.provides("sig")
      def audio_pipeline(wav, channel, start, stop):
          # Select a speech segment from the sph file
          # start and end times are already frames.
          # This is done in data preparation stage.
          start = int(start)
          stop = int(stop)
          num_frames = stop - start
          sig, fs = torchaudio.load(
              wav, num_frames=num_frames, frame_offset=start
          )
          info = torchaudio.info(wav)
  
          resampled = sig
          # Maybe resample to 16kHz
          if int(info.sample_rate) != int(hparams["sample_rate"]):
              resampled = torchaudio.transforms.Resample(
                  info.sample_rate,
                  hparams["sample_rate"],
              )(sig)
  
          resampled = resampled.transpose(0, 1).squeeze(1)
  
          if info.num_channels > 1:
              # Select the proper audio channel of the segment
              if channel == "A":
                  resampled = resampled[:, 0]
              else:
                  resampled = resampled[:, 1]
          return resampled
  
      sb.dataio.dataset.add_dynamic_item(datasets, audio_pipeline)
  
      # 3. Define text pipeline:
      @sb.utils.data_pipeline.takes("words")
      @sb.utils.data_pipeline.provides(
          "tokens_list", "tokens_bos", "tokens_eos", "tokens"
      )
      def text_pipeline(wrd):
          tokens_list = tokenizer.sp.encode_as_ids(wrd)
          yield tokens_list
          tokens_bos = torch.LongTensor(
              [hparams["bos_index"]] + (tokens_list)
          )
          yield tokens_bos
          tokens_eos = torch.LongTensor(
              tokens_list + [hparams["eos_index"]]
          )
          yield tokens_eos
          tokens = torch.LongTensor(tokens_list)
          yield tokens
  
      sb.dataio.dataset.add_dynamic_item(datasets, text_pipeline)
  
      # 4. Set output:
      sb.dataio.dataset.set_output_keys(
          datasets,
          ["id", "sig", "tokens_bos", "tokens_eos", "tokens"],
      )
      return train_data, valid_data, test_datasets
  
imports: import functools
  import os
  import sys
  from pathlib import Path
  import torch
  import logging
  import speechbrain
  import torchaudio
  from hyperpyyaml import load_hyperpyyaml
  from speechbrain.tokenizers.SentencePiece import SentencePiece
  from speechbrain.utils.data_utils import undo_padding
  from speechbrain.utils.distributed import run_on_main, if_main_process
label_desc: Use image node_id 2 for calling a global function with example usage: dataio_prepare(hparams, tokenizer) and returns: train_data,  valid_data,  test_datasets, resampled