code: def create_tarred_dataset(
      text_file: Union[os.PathLike, str],
      labels_file: Union[os.PathLike, str],
      output_dir: Union[os.PathLike, str],
      max_seq_length: int,
      tokens_in_batch: int,
      lines_per_dataset_fragment: int,
      num_batches_per_tarfile: int,
      tokenizer_name: str,
      tokenizer_model: Optional[Union[os.PathLike, str]] = None,
      vocab_file: Optional[Union[os.PathLike, str]] = None,
      merges_file: Optional[Union[os.PathLike, str]] = None,
      special_tokens: Optional[Dict[str, str]] = None,
      use_fast_tokenizer: Optional[bool] = False,
      pad_label: str = "O",
      punct_label_ids: Optional[Dict[str, int]] = None,
      capit_label_ids: Optional[Dict[str, int]] = None,
      punct_label_vocab_file: Optional[Union[os.PathLike, str]] = None,
      capit_label_vocab_file: Optional[Union[os.PathLike, str]] = None,
      tar_file_prefix: Optional[str] = "punctuation_capitalization",
      n_jobs: Optional[int] = None,
      audio_file: Optional[Path] = None,
      use_audio: Optional[bool] = False,
      sample_rate: Optional[int] = 16000,
  ) -> None:
      """
      Creates tarred dataset from ``text_file`` and ``labels_file``. A tarred dataset allows to train on large amounts of
      data without storing it all into memory simultaneously. You may use these function directly or try script
      `examples/nlp/token_classification/data/create_punctuation_capitalization_tarred_dataset.py
      <https://github.com/NVIDIA/NeMo/blob/main/examples/nlp/token_classification/data/create_punctuation_capitalization_tarred_dataset.py>`_.
  
      Tarred dataset is a directory which contains metadata file, tar files with batches,
      ``punct_label_vocab.csv`` and ``capit_label_vocab.csv`` files.
  
      Metadata file is a JSON file with 4 items: ``'num_batches'``, ``'tar_files'``, ``'punct_label_vocab_file'``,
      ``'capit_label_vocab_file'``. The item ``'num_batches'`` (``int``) is a total number of batches in tarred dataset.
      ``'tar_files'`` is a list of paths to tar files relative to directory containing the metadata file. The items
      ``'punct_label_vocab_file'`` and ``'capit_label_vocab_file'`` are correspondingly paths to punctuation and
      capitalization label vocabulary files. These paths are relative to directory containing the metadata file.
  
      Every tar file contains objects written using ``webdataset.TarWriter``. Each object is a dictionary with two items:
      ``'__key__'`` and ``'batch.pyd'``. ``'__key__'`` is a name of a batch and ``'batch.pyd'`` is a pickled dictionary
      which contains ``'input_ids'``, ``'subtokens_mask'``, ``'punct_labels'``, ``'capit_labels'``. ``'input_ids'`` is an
      array containing ids of source tokens, ``'subtokens_mask'`` is a boolean array showing first tokens in words,
      ``'punct_labels'`` and ``'capit_labels'`` are arrays with ids of labels.
  
      Metadata file should be passed to constructor of :class:`BertPunctuationCapitalizationTarredDataset` and the
      instance of the class will handle iteration and constructing masks and token types for BERT model.
  
      Args:
          text_file (:obj:`Union[os.PathLike, str]`): a path to a file with dataset source. Dataset source is lowercase
              text without punctuation. Number of lines in ``text_file`` has to be equal to the number of lines in
              ``labels_file``.
          labels_file (:obj:`Union[os.PathLike, str]`): a path to a file with labels. Labels are given in the format
              described in :ref:`NeMo Data Format<nemo-data-format-label>`.
          output_dir (:obj:`Union[os.PathLike, str]`): a path to a directory where metadata file, tar files and
              ``'punct_label_ids.csv'`` and ``'capit_label_ids.csv'`` files are saved.
          max_seq_length (:obj:`int`): Maximum number of subtokens in an input sequence. A source sequence which contains
              too many subtokens is clipped to ``max_seq_length - 2`` subtokens and then [CLS] token is prepended to the
              clipped sequence and [SEP] token is appended to the clipped sequence. The clipping is performed via removal
              of subtokens in the end of a source sequence.
          tokens_in_batch (:obj:`int`): maximum number of tokens in a batch including [CLS], [SEP], [UNK], and [PAD]
              tokens. Before packing into batches source sequences are sorted by number of tokens in order to reduce
              number of pad tokens. So the number of samples in a batch may vary.
          lines_per_dataset_fragment (:obj:`int`): a number of lines processed by one worker during creation of tarred
              dataset. A worker tokenizes ``lines_per_dataset_fragment`` lines and keeps in RAM tokenized text labels
              before packing them into batches. Reducing ``lines_per_dataset_fragment`` leads to reducing of the amount
              of memory used by this function.
          num_batches_per_tarfile (:obj:`int`): a number of batches saved in a tar file. If you increase
              ``num_batches_per_tarfile``, then there will be less tar files in the dataset. There cannot be less than
              ``num_batches_per_tarfile`` batches in a tar file, and all excess batches are removed. Maximum number of
              discarded batches is ``num_batches_per_tarfile - 1``.
          tokenizer_name (:obj:`str`): a name of the tokenizer used for tokenization of source sequences. Possible
              options are ``'sentencepiece'``, ``'word'``, ``'char'``, HuggingFace tokenizers. For more options see
              function ``nemo.collections.nlp.modules.common.get_tokenizer``. The tokenizer must have properties
              ``cls_id``, ``pad_id``, ``sep_id``, ``unk_id``.
          tokenizer_model (:obj:`Union[os.PathLike, str]`, `optional`): a path to a tokenizer model required for
              ``'sentencepiece'`` tokenizer.
          vocab_file (:obj:`Union[os.PathLike, str]`, `optional`): a path to a vocabulary file which can be used in
              ``'word'``, ``'char'``, and HuggingFace tokenizers.
          merges_file (:obj:`Union[os.PathLike, str]`, `optional`): a path to merges file which can be used in
              HuggingFace tokenizers.
          special_tokens (:obj:`Dict[str, str]`, `optional`): a dictionary with special tokens passed to constructors of
              ``'char'``, ``'word'``, ``'sentencepiece'``, and various HuggingFace tokenizers.
          use_fast_tokenizer (:obj:`bool`, `optional`, defaults to :obj:`False`): whether to use fast HuggingFace
              tokenizer.
          pad_label (:obj:`str`, `optional`, defaults to :obj:`'O'`): a pad label both for punctuation and
              capitalization. This label is also a neutral label (used for marking words which do not need punctuation
              and capitalization).
          punct_label_ids (:obj:`Dict[str, int]`, `optional`): a dictionary which keys are punctuation labels and values
              are label ids. The pad label ``pad_label`` has to have id ``0``. You can provide at most one of parameters
              ``punct_label_ids`` and ``punct_label_vocab_file``. If none of parameters ``punct_label_ids`` and
              ``punct_label_vocab_file`` is provided, then punctuation label ids will be inferred from ``labels_file``
              file.
          capit_label_ids (:obj:`Dict[str, int]`, `optional`): same as ``punct_label_ids`` for capitalization labels.
          punct_label_vocab_file (:obj:`Union[os.PathLike, str]`, `optional`): a path to a file with punctuation labels.
              These labels include pad label. The pad label has to be the first label in the file. Each label is written
              on a separate line. Alternatively you can use ``punct_labels_ids`` parameter. If none of parameters
              ``punct_labels_ids`` and ``punct_label_vocab_file`` is provided, then punctuation label ids will be
              inferred from ``labels_file`` file.
          capit_label_vocab_file (:obj:`Union[os.PathLike, str]`, `optional`): same as ``punct_label_vocab_file`` for
              capitalization labels.
          tar_file_prefix (:obj:`str`, `optional`, defaults :obj:`'punctuation_capitalization'`): a string from which tar
              file names start. The string can contain only characters ``A-Z``, ``a-z``, ``0-9``, ``_``, ``-``, ``.``.
          n_jobs (:obj:`int`, `optional`): a number of workers for creating tarred dataset. If ``None``, then ``n_jobs``
              is equal to number of CPUs.
          audio_file (:obj:`Optional[Union[os.PathLike, str]]`, defaults to :obj:`None`): a path to a file with audio dataset file paths if dataset is lexical and audio. Must contain one path per line.
          use_audio (:obj:`bool`, `optional`, defaults to :obj:`False`): If set to ``True`` dataset becomes lexical and audio rather than only lexical.
          sample_rate (:obj:`int`, `optional`, defaults to :obj:`16000`) Targeted sample rate of audios If ``use_audio`` set to ``True``.
      """
      check_tar_file_prefix(
          tar_file_prefix, ValueError, "tar_file_prefix"
      )
      if n_jobs is None:
          n_jobs = mp.cpu_count()
      text_file, labels_file = (
          Path(text_file).expanduser(),
          Path(labels_file).expanduser(),
      )
      output_dir = Path(output_dir).expanduser()
      ds_params_str = DATASET_PARAMETERS_TMPL.format(
          prefix=tar_file_prefix,
          tokens_in_batch=tokens_in_batch,
          max_seq_length=max_seq_length,
          tokenizer=REPLACE_NOT_ALLOWED_CHARACTERS_IN_FILE_NAME.sub(
              "-", tokenizer_name
          ),
      )
      output_file_tmpl = ds_params_str + TAR_FINAL_TMPL
      metadata_file_name = output_dir / (
          "metadata." + ds_params_str + ".json"
      )
      remove_unexpected_files_and_dirs(
          output_dir, output_file_tmpl, metadata_file_name
      )
      audio_start_bytes = None
      if use_audio:
          (
              num_lines,
              text_start_bytes,
              label_start_bytes,
              audio_start_bytes,
          ) = get_fragment_start_bytes(
              text_file,
              labels_file,
              lines_per_dataset_fragment,
              audio_file,
          )
      else:
          (
              num_lines,
              text_start_bytes,
              label_start_bytes,
          ) = get_fragment_start_bytes(
              text_file, labels_file, lines_per_dataset_fragment
          )
      if text_start_bytes:
          output_dir.mkdir(parents=True, exist_ok=True)
      else:
          raise ValueError(
              f"Both {labels_file} and {text_file} are empty. Tarred dataset cannot be created."
          )
      punct_label_ids, capit_label_ids = get_label_dictionaries(
          labels_file,
          label_start_bytes,
          num_lines,
          lines_per_dataset_fragment,
          pad_label,
          punct_label_ids,
          capit_label_ids,
          punct_label_vocab_file,
          capit_label_vocab_file,
          n_jobs,
      )
  
      with Progress(
          num_lines,
          [
              "Tokenization",
              "Batch mark up",
              "Batch building",
              "Writing tarred dataset",
          ],
          "query",
      ) as progress_queues:
          Parallel(n_jobs=min(n_jobs, len(text_start_bytes)))(
              delayed(process_fragment)(
                  text_file,
                  labels_file,
                  output_dir,
                  text_start_pos,
                  label_start_pos,
                  lines_per_dataset_fragment,
                  max_seq_length,
                  tokens_in_batch,
                  num_batches_per_tarfile,
                  tokenizer_name,
                  None
                  if tokenizer_model is None
                  else Path(tokenizer_model).expanduser(),
                  None
                  if vocab_file is None
                  else Path(vocab_file).expanduser(),
                  None
                  if merges_file is None
                  else Path(merges_file).expanduser(),
                  special_tokens,
                  use_fast_tokenizer,
                  pad_label,
                  punct_label_ids,
                  capit_label_ids,
                  fragment_idx,
                  *progress_queues,
                  audio_file,
                  sample_rate,
                  audio_file_start_pos,
                  use_audio,
              )
              for fragment_idx, (
                  text_start_pos,
                  label_start_pos,
                  audio_file_start_pos,
              ) in enumerate(
                  zip(
                      text_start_bytes,
                      label_start_bytes,
                      audio_start_bytes
                      if use_audio
                      else [None for _ in range(len(text_start_bytes))],
                  )
              )
          )
      repack_tar_files_with_not_enough_batches(
          output_dir, num_batches_per_tarfile
      )
      create_metadata_file(
          output_dir,
          output_file_tmpl,
          metadata_file_name,
          num_batches_per_tarfile,
      )
  
imports: import inspect
  import json
  import multiprocessing
  import os
  import pickle
  import re
  import shutil
  import tempfile
  from collections import deque
  from pathlib import Path
  from typing import Any, Callable, Dict, Iterator, List, Optional, Set, Tuple, Type, Union
  import numpy
  import torch
  import webdataset
  from joblib import Parallel, delayed
  from omegaconf import DictConfig
  from torch.utils.data import IterableDataset
  from nemo.collections.common.tokenizers import TokenizerSpec
  from nemo.collections.nlp.data.token_classification.punctuation_capitalization_dataset import LABEL_ID_DIR_FOR_NEMO_CHECKPOINT, BertPunctuationCapitalizationDataset, Progress, create_label_ids, create_masks_and_segment_ids, load_label_ids, raise_not_equal_labels_error
  from nemo.collections.nlp.modules.common.tokenizer_utils import get_tokenizer
  from nemo.core.neural_types import AudioSignal, ChannelType, LabelsType, LengthsType, MaskType, NeuralType
  from nemo.utils import logging
label_desc: Use image node_id 16 for calling a global function with example usage: create_tarred_dataset(text_file, labels_file, output_dir, max_seq_length, tokens_in_batch, lines_per_dataset_fragment, num_batches_per_tarfile, tokenizer_name, tokenizer_model, vocab_file, merges_file, special_tokens, use_fast_tokenizer, pad_label, punct_label_ids, capit_label_ids, punct_label_vocab_file, capit_label_vocab_file, tar_file_prefix, n_jobs, audio_file, use_audio, sample_rate) without return types