class_docstr: This class implements a model-specific automatic speech recognizer using the end-to-end speech recognizer in
  Espresso.
  
  | Paper link: https://arxiv.org/abs/1909.08723
class_docstr_tok: ["This","class","implements","a","model-specific","automatic","speech","recognizer","using","the","end-to-end","speech","recognizer","in","Espresso",".","|","Paper","link",":","https",":","\/\/arxiv.org\/abs\/1909.08723"]
docstr: Initialization of an instance PyTorchEspresso
  
  :param espresso_config_filepath: The path of the espresso config file (yaml)
  :param model: The choice of pretrained model if a pretrained model is required.
  :param clip_values: Tuple of the form `(min, max)` of floats or `np.ndarray` representing the minimum and
         maximum values allowed for features. If floats are provided, these will be used as the range of all
         features. If arrays are provided, each value will be considered the bound for a feature, thus
         the shape of clip values needs to match the total number of features.
  :param preprocessing_defences: Preprocessing defence(s) to be applied by the estimator.
  :param postprocessing_defences: Postprocessing defence(s) to be applied by the estimator.
  :param preprocessing: Tuple of the form `(subtrahend, divisor)` of floats or `np.ndarray` of values to be
         used for data preprocessing. The first value will be subtracted from the input. The input will then
         be divided by the second one.
  :param device_type: Type of device to be used for model and tensors, if `cpu` run on CPU, if `gpu` run on GPU
                      if available otherwise run on CPU.
docstr_tok: ["Initialization","of","an","instance","PyTorchEspresso",":","param","espresso_config_filepath",":","The","path","of","the","espresso","config","file","(","yaml",")",":","param","model",":","The","choice","of","pretrained","model","if","a","pretrained","model","is","required",".",":","param","clip_values",":","Tuple","of","the","form","`","(","min",",","max",")","`","of","floats","or","`","np.ndarray","`","representing","the","minimum","and","maximum","values","allowed","for","features",".","If","floats","are","provided",",","these","will","be","used","as","the","range","of","all","features",".","If","arrays","are","provided",",","each","value","will","be","considered","the","bound","for","a","feature",",","thus","the","shape","of","clip","values","needs","to","match","the","total","number","of","features",".",":","param","preprocessing_defences",":","Preprocessing","defence","(","s",")","to","be","applied","by","the","estimator",".",":","param","postprocessing_defences",":","Postprocessing","defence","(","s",")","to","be","applied","by","the","estimator",".",":","param","preprocessing",":","Tuple","of","the","form","`","(","subtrahend",",","divisor",")","`","of","floats","or","`","np.ndarray","`","of","values","to","be","used","for","data","preprocessing",".","The","first","value","will","be","subtracted","from","the","input",".","The","input","will","then","be","divided","by","the","second","one",".",":","param","device_type",":","Type","of","device","to","be","used","for","model","and","tensors",",","if","`","cpu","`","run","on","CPU",",","if","`","gpu","`","run","on","GPU","if","available","otherwise","run","on","CPU","."]
code: def __init__(
      self,
      espresso_config_filepath: Optional[str] = None,
      model: Optional[str] = None,
      clip_values: Optional["CLIP_VALUES_TYPE"] = None,
      preprocessing_defences: Union[
          "Preprocessor", List["Preprocessor"], None
      ] = None,
      postprocessing_defences: Union[
          "Postprocessor", List["Postprocessor"], None
      ] = None,
      preprocessing: "PREPROCESSING_TYPE" = None,
      device_type: str = "gpu",
      verbose: bool = True,
  ):
      """
      Initialization of an instance PyTorchEspresso
  
      :param espresso_config_filepath: The path of the espresso config file (yaml)
      :param model: The choice of pretrained model if a pretrained model is required.
      :param clip_values: Tuple of the form `(min, max)` of floats or `np.ndarray` representing the minimum and
             maximum values allowed for features. If floats are provided, these will be used as the range of all
             features. If arrays are provided, each value will be considered the bound for a feature, thus
             the shape of clip values needs to match the total number of features.
      :param preprocessing_defences: Preprocessing defence(s) to be applied by the estimator.
      :param postprocessing_defences: Postprocessing defence(s) to be applied by the estimator.
      :param preprocessing: Tuple of the form `(subtrahend, divisor)` of floats or `np.ndarray` of values to be
             used for data preprocessing. The first value will be subtracted from the input. The input will then
             be divided by the second one.
      :param device_type: Type of device to be used for model and tensors, if `cpu` run on CPU, if `gpu` run on GPU
                          if available otherwise run on CPU.
      """
      import torch
      import yaml
      from fairseq import checkpoint_utils, tasks, utils
      from fairseq.data import encoders
      import sentencepiece as spm
  
      # Super initialization
      super().__init__(
          model=None,
          clip_values=clip_values,
          channels_first=None,
          preprocessing_defences=preprocessing_defences,
          postprocessing_defences=postprocessing_defences,
          preprocessing=preprocessing,
      )
      self.verbose = verbose
  
      # Check clip values
      if self.clip_values is not None:
          if not np.all(self.clip_values[0] == -1):  # pragma: no cover
              raise ValueError(
                  "This estimator requires normalized input audios with clip_vales=(-1, 1)."
              )
          if not np.all(self.clip_values[1] == 1):  # pragma: no cover
              raise ValueError(
                  "This estimator requires normalized input audios with clip_vales=(-1, 1)."
              )
  
      # Check postprocessing defences
      if self.postprocessing_defences is not None:  # pragma: no cover
          raise ValueError(
              "This estimator does not support `postprocessing_defences`."
          )
  
      # Set cpu/gpu device
      self._device: torch.device
      if device_type == "cpu" or not torch.cuda.is_available():
          self._device = torch.device("cpu")
      else:  # pragma: no cover
          cuda_idx = torch.cuda.current_device()
          self._device = torch.device(f"cuda:{cuda_idx}")
  
      # Load config/model
      if espresso_config_filepath is None:
          if model == "librispeech_transformer":
              config_filename, config_url = (
                  "libri960_transformer.yaml",
                  "https://github.com/YiwenShaoStephen/espresso/releases/download/v0.1-alpha/"
                  "libri960_transformer.yaml",
              )
              model_filename, model_url = (
                  "checkpoint_best.pt",
                  "https://github.com/YiwenShaoStephen/espresso/releases/download/v0.1-alpha/checkpoint_best.pt",
              )
              sp_filename, sp_url = (
                  "train_960_unigram5000.model",
                  "https://github.com/YiwenShaoStephen/espresso/releases/download/v0.1-alpha/"
                  "train_960_unigram5000.model",
              )
              dict_filename, dict_url = (
                  "train_960_unigram5000_units.txt",
                  "https://github.com/YiwenShaoStephen/espresso/releases/download/v0.1-alpha/"
                  "train_960_unigram5000_units.txt",
              )
          else:  # pragma: no cover
              raise ValueError("Model not recognised.")
  
          # Download files
          config_path = get_file(
              filename=config_filename,
              path=config.ART_DATA_PATH,
              url=config_url,
              extract=False,
              verbose=self.verbose,
          )
          model_path = get_file(
              filename=model_filename,
              path=config.ART_DATA_PATH,
              url=model_url,
              extract=False,
              verbose=self.verbose,
          )
          sp_path = get_file(
              filename=sp_filename,
              path=config.ART_DATA_PATH,
              url=sp_url,
              extract=False,
              verbose=self.verbose,
          )
          dict_path = get_file(
              filename=dict_filename,
              path=config.ART_DATA_PATH,
              url=dict_url,
              extract=False,
              verbose=self.verbose,
          )
      else:
          raise ValueError(
              "Value for `espresso_config_filepath` has to `None` for now."
          )
  
      # construct espresso args
      with open(config_path, encoding="utf8") as file:
          esp_args_dict = yaml.load(file, Loader=yaml.FullLoader)
          esp_args = Namespace(**esp_args_dict)
          if (
              espresso_config_filepath is None
          ):  # overwrite paths in downloaded config with the actual ones
              esp_args.path = model_path
              esp_args.sentencepiece_model = sp_path
              esp_args.dict = dict_path
      self.esp_args = esp_args
  
      # setup espresso/fairseq task
      self.task = tasks.setup_task(self.esp_args)
      self.task.feat_dim = self.esp_args.feat_dim
  
      # load_model_ensemble
      (
          self._models,
          self._model_args,
      ) = checkpoint_utils.load_model_ensemble(
          utils.split_paths(self.esp_args.path),
          arg_overrides=ast.literal_eval(self.esp_args.model_overrides),
          task=self.task,
          suffix=getattr(self.esp_args, "checkpoint_suffix", ""),
      )
      for m in self._models:
          m.to(self._device)
  
      self._model = self._models[0]
  
      self.dictionary = self.task.target_dictionary
      self.generator = self.task.build_generator(
          self._models, self.esp_args
      )
      self.tokenizer = encoders.build_tokenizer(self.esp_args)
      self.bpe = encoders.build_bpe(self.esp_args)  # bpe encoder
      self.spp = spm.SentencePieceProcessor()  # sentence piece model
      self.spp.Load(self.esp_args.sentencepiece_model)
  
      self.criterion = self.task.build_criterion(self.esp_args)
      self._sampling_rate = self.esp_args.sampling_rate
  
imports: import ast
  from argparse import Namespace
  import logging
  from typing import Dict, List, Optional, Tuple, TYPE_CHECKING, Union
  import numpy
  from art import config
  from art.estimators.pytorch import PyTorchEstimator
  from art.estimators.speech_recognition.speech_recognizer import SpeechRecognizerMixin, PytorchSpeechRecognizerMixin
  from art.utils import get_file
label_desc: Use image node_id 1 to create a new PyTorchEspresso object from inherited base classes: PytorchSpeechRecognizerMixin, SpeechRecognizerMixin, PyTorchEstimator with example: obj = PyTorchEspresso(espresso_config_filepath, model, clip_values, preprocessing_defences, postprocessing_defences, preprocessing, device_type, verbose)