






'''simple docstring'''
from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


UpperCamelCase__			=      logging.get_logger(__name__)


class 							_UpperCAmelCase    (		snake_case      ):
    __lowerCamelCase: str									=     ['input_features', 'attention_mask']




    def __init__(      self	:							Tuple	,  a	:							Optional[Any]=8_0	,  a	:							Optional[int]=1_6_0_0_0	,  a	:							List[Any]=8_0	,  a	:							Any=0.0	,  a	:							Optional[int]=True	,  a	:							Optional[Any]=True	,  a	:							Any=True	,  **a	:							Tuple	,  ):






       '''simple docstring'''






       super().__init__(feature_size=a	,  sampling_rate=a	,  padding_value=a	,  **a      )
       lowercase_						:     List[str]							     =  num_mel_bins
       lowercase_						:     int							     =  do_ceptral_normalize
       lowercase_						:     Optional[Any]							     =  normalize_means
       lowercase_						:     Tuple							     =  normalize_vars
       lowercase_						:     Dict							     =  True




    def 					lowerCAmelCase__					(      self	:							Optional[int]	,  a	:							np.ndarray	,  ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  waveform * (2**1_5)  # Kaldi compliance: 16-bit signed integers
       lowercase_						:     List[Any]							     =  torch.from_numpy(a      ).unsqueeze(0      )
       lowercase_						:     Tuple							     =  ta_kaldi.fbank(a	,  num_mel_bins=self.num_mel_bins	,  sample_frequency=self.sampling_rate      )
       return features.numpy()




    @staticmethod
    def 					lowerCAmelCase__					(      a	:							np.ndarray	,  a	:							int	,  a	:							Optional[bool] = True	,  a	:							Optional[bool] = True	,  a	:							float = 0.0	,  ):






       '''simple docstring'''






       if normalize_means:
          lowercase_						:     Optional[int]							     =  x[:input_length].mean(axis=0      )
          lowercase_						:     Union[str, Any]							     =  np.subtract(a	,  a      )
       if normalize_vars:
          lowercase_						:     int							     =  x[:input_length].std(axis=0      )
          lowercase_						:     Any							     =  np.divide(a	,  a      )

       if input_length < x.shape[0]:
          lowercase_						:     int							     =  padding_value

       # make sure array is in float32
       lowercase_						:     Optional[Any]							     =  x.astype(np.floataa      )

       return x




    def 					lowerCAmelCase__					(      self	:							Dict	,  a	:							List[np.ndarray]	,  a	:							Optional[np.ndarray] = None      ):






       '''simple docstring'''






       lowercase_						:     Optional[int]							     =  attention_mask.sum(-1      ) if attention_mask is not None else [x.shape[0] for x in input_features]
       return [
           self.utterance_cmvn(a	,  a	,  self.normalize_means	,  self.normalize_vars	,  self.padding_value      )
           for x, n in zip(a	,  a      )
       ]





    def __call__(      self	:							Optional[Any]	,  a	:							Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]	,  a	:							Union[bool, str, PaddingStrategy] = False	,  a	:							Optional[int] = None	,  a	:							bool = False	,  a	:							Optional[int] = None	,  a	:							Optional[Union[str, TensorType]] = None	,  a	:							Optional[int] = None	,  a	:							Optional[bool] = None	,  **a	:							Optional[int]	,  ):






       '''simple docstring'''






       if sampling_rate is not None:
          if sampling_rate != self.sampling_rate:
             raise ValueError(
                 f"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of"""
                 f""" {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with"""
                 f""" {self.sampling_rate} and not {sampling_rate}."""      )
       else:
          logger.warning(
              "It is strongly recommended to pass the `sampling_rate` argument to this function. "
              "Failing to do so can result in silent errors that might be hard to debug."      )

       lowercase_						:     Optional[Any]							     =  isinstance(a	,  np.ndarray      ) and len(raw_speech.shape      ) > 1
       if is_batched_numpy and len(raw_speech.shape      ) > 2:
          raise ValueError(f"""Only mono-channel audio is supported for input to {self}"""      )
       lowercase_						:     Optional[Any]							     =  is_batched_numpy or (
           isinstance(a	,  (list, tuple)      ) and (isinstance(raw_speech[0]	,  (np.ndarray, tuple, list)      ))
       )

       if is_batched:
          lowercase_						:     Optional[int]							     =  [np.asarray(a	,  dtype=np.floataa      ) for speech in raw_speech]
       elif not is_batched and not isinstance(a	,  np.ndarray      ):
          lowercase_						:     Union[str, Any]							     =  np.asarray(a	,  dtype=np.floataa      )
       elif isinstance(a	,  np.ndarray      ) and raw_speech.dtype is np.dtype(np.floataa      ):
          lowercase_						:     List[str]							     =  raw_speech.astype(np.floataa      )

       # always return batch
       if not is_batched:
          lowercase_						:     List[Any]							     =  [raw_speech]

       # extract fbank features
       lowercase_						:     Tuple							     =  [self._extract_fbank_features(a      ) for waveform in raw_speech]

       # convert into correct format for padding
       lowercase_						:     Dict							     =  BatchFeature({"input_features": features}      )

       lowercase_						:     str							     =  self.pad(
           a	,  padding=a	,  max_length=a	,  truncation=a	,  pad_to_multiple_of=a	,  return_attention_mask=a	,  **a	,  )

       # make sure list is in array format
       lowercase_						:     Dict							     =  padded_inputs.get("input_features"      )
       if isinstance(input_features[0]	,  a      ):
          lowercase_						:     Dict							     =  [np.asarray(a	,  dtype=np.floataa      ) for feature in input_features]

       lowercase_						:     List[str]							     =  padded_inputs.get("attention_mask"      )
       if attention_mask is not None:
          lowercase_						:     int							     =  [np.asarray(a	,  dtype=np.intaa      ) for array in attention_mask]

       # Utterance-level cepstral mean and variance normalization
       if self.do_ceptral_normalize:
          lowercase_						:     Optional[Any]							     =  (
              np.array(a	,  dtype=np.intaa      )
              if self._get_padding_strategies(a	,  max_length=a      ) is not PaddingStrategy.DO_NOT_PAD
              else None
          )
          lowercase_						:     int							     =  self.normalize(
              padded_inputs["input_features"]	,  attention_mask=a      )

       if return_tensors is not None:
          lowercase_						:     Optional[int]							     =  padded_inputs.convert_to_tensors(a      )

       return padded_inputs












'''simple docstring'''
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


UpperCamelCase__			=      {
    'configuration_layoutlmv3': [
        'LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'LayoutLMv3Config',
        'LayoutLMv3OnnxConfig',
    ],
    'processing_layoutlmv3': ['LayoutLMv3Processor'],
    'tokenization_layoutlmv3': ['LayoutLMv3Tokenizer'],
}

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__			=      ['LayoutLMv3TokenizerFast']

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__			=      [
     'LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST',
     'LayoutLMv3ForQuestionAnswering',
     'LayoutLMv3ForSequenceClassification',
     'LayoutLMv3ForTokenClassification',
     'LayoutLMv3Model',
     'LayoutLMv3PreTrainedModel',
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__			=      [
     'TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST',
     'TFLayoutLMv3ForQuestionAnswering',
     'TFLayoutLMv3ForSequenceClassification',
     'TFLayoutLMv3ForTokenClassification',
     'TFLayoutLMv3Model',
     'TFLayoutLMv3PreTrainedModel',
 ]

try:
 if not is_vision_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__			=      ['LayoutLMv3FeatureExtractor']
 UpperCamelCase__			=      ['LayoutLMv3ImageProcessor']


if TYPE_CHECKING:
 from .configuration_layoutlmva import (
     LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP,
     LayoutLMvaConfig,
     LayoutLMvaOnnxConfig,
 )
 from .processing_layoutlmva import LayoutLMvaProcessor
 from .tokenization_layoutlmva import LayoutLMvaTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_layoutlmva import (
      LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
      LayoutLMvaForQuestionAnswering,
      LayoutLMvaForSequenceClassification,
      LayoutLMvaForTokenClassification,
      LayoutLMvaModel,
      LayoutLMvaPreTrainedModel,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_layoutlmva import (
      TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFLayoutLMvaForQuestionAnswering,
      TFLayoutLMvaForSequenceClassification,
      TFLayoutLMvaForTokenClassification,
      TFLayoutLMvaModel,
      TFLayoutLMvaPreTrainedModel,
  )

 try:
  if not is_vision_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor
  from .image_processing_layoutlmva import LayoutLMvaImageProcessor

else:
 import sys

 UpperCamelCase__			=      _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




