code: def make_vocab_from_params(
      params: Params,
      serialization_dir: Union[str, PathLike],
      print_statistics: bool = False,
  ) -> Vocabulary:
      vocab_params = params.pop("vocabulary", {})
      os.makedirs(serialization_dir, exist_ok=True)
      vocab_dir = os.path.join(serialization_dir, "vocabulary")
  
      if os.path.isdir(vocab_dir) and os.listdir(vocab_dir) is not None:
          raise ConfigurationError(
              "The 'vocabulary' directory in the provided serialization directory is non-empty"
          )
  
      datasets_for_vocab_creation: Optional[List[str]] = params.pop(
          "datasets_for_vocab_creation", None
      )
      # Do a quick sanity check here. There's no need to load any datasets if the vocab
      # type is "empty" or "from_files".
      if datasets_for_vocab_creation is None and vocab_params.get(
          "type"
      ) in {
          "empty",
          "from_files",
          "from_pretrained_transformer",
      }:
          datasets_for_vocab_creation = []
  
      data_loaders: Dict[str, DataLoader]
      if datasets_for_vocab_creation is None:
          # If `datasets_for_vocab_creation` was not specified, we'll use all datasets
          # from the config.
          data_loaders = data_loaders_from_params(
              params, serialization_dir=serialization_dir
          )
      else:
          for dataset_name in datasets_for_vocab_creation:
              data_path = f"{dataset_name}_data_path"
              if data_path not in params:
                  raise ConfigurationError(
                      f"invalid 'datasets_for_vocab_creation' {dataset_name}"
                  )
          data_loaders = data_loaders_from_params(
              params,
              serialization_dir=serialization_dir,
              train=("train" in datasets_for_vocab_creation),
              validation=("validation" in datasets_for_vocab_creation),
              test=("test" in datasets_for_vocab_creation),
          )
  
      instances: Iterable[Instance] = (
          instance
          for key, data_loader in data_loaders.items()
          if datasets_for_vocab_creation is None
          or key in datasets_for_vocab_creation
          for instance in data_loader.iter_instances()
      )
  
      if print_statistics:
          instances = list(instances)
  
      vocab = Vocabulary.from_params(vocab_params, instances=instances)
  
      logger.info(f"writing the vocabulary to {vocab_dir}.")
      vocab.save_to_files(vocab_dir)
      logger.info("done creating vocab")
  
      if print_statistics:
          dataset = Batch(instances)
          dataset.index_instances(vocab)
          dataset.print_statistics()
          vocab.print_statistics()
  
      return vocab
  
imports: import datetime
  import logging
  import os
  import shutil
  import json
  from os import PathLike
  from typing import Any, Dict, Iterable, Optional, Union, Tuple, Set, List
  from collections import Counter
  import torch
  from torch.nn.utils import clip_grad_norm_
  from allennlp.common.checks import check_for_gpu, ConfigurationError
  from allennlp.common.params import Params
  from allennlp.common.tqdm import Tqdm
  from allennlp.common.util import dump_metrics, sanitize, int_to_device
  from allennlp.data import Instance, Vocabulary, Batch, DataLoader
  from allennlp.data.dataset_readers import DatasetReader
  from allennlp.models.archival import CONFIG_NAME
  from allennlp.models.model import Model
  from allennlp.nn import util
label_desc: Use image node_id 13 for calling a global function with example usage: make_vocab_from_params(params, serialization_dir, print_statistics) and returns: vocab