'''simple docstring'''




import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from .tokenization_lxmert import LxmertTokenizer


__UpperCAmelCase					={"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

__UpperCAmelCase					={
    "vocab_file": {
        "unc-nlp/lxmert-base-uncased": "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/vocab.txt",
    },
    "tokenizer_file": {
        "unc-nlp/lxmert-base-uncased": (
            "https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/tokenizer.json"
        ),
    },
}

__UpperCAmelCase					={
    "unc-nlp/lxmert-base-uncased": 5_1_2,
}

__UpperCAmelCase					={
    "unc-nlp/lxmert-base-uncased": {"do_lower_case": True},
}




class        a__ (  UpperCAmelCase__					):
     lowerCamelCase    :	List[str]				=VOCAB_FILES_NAMES
     lowerCamelCase    :	Optional[int]				=PRETRAINED_VOCAB_FILES_MAP
     lowerCamelCase    :	List[str]				=PRETRAINED_INIT_CONFIGURATION
     lowerCamelCase    :	List[str]				=PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     lowerCamelCase    :	Dict				=LxmertTokenizer






     def __init__(					self						:						str    ,							a						:						Tuple=None    ,							a						:						Optional[Any]=None    ,							a						:						List[Any]=True    ,							a						:						Optional[int]="[UNK]"    ,							a						:						str="[SEP]"    ,							a						:						str="[PAD]"    ,							a						:						int="[CLS]"    ,							a						:						Optional[Any]="[MASK]"    ,							a						:						Optional[int]=True    ,							a						:						Any=None    ,							**a						:						Optional[Any]    ,							):





         """simple docstring"""



         super().__init__(
             a    ,							tokenizer_file=a    ,							do_lower_case=a    ,							unk_token=a    ,							sep_token=a    ,							pad_token=a    ,							cls_token=a    ,							mask_token=a    ,							tokenize_chinese_chars=a    ,							strip_accents=a    ,							**a    ,							)

         __lowerCamelCase            =    json.loads(self.backend_tokenizer.normalizer.__getstate__()       )
         if (
             normalizer_state.get('''lowercase'''    ,							a       ) != do_lower_case
             or normalizer_state.get('''strip_accents'''    ,							a       ) != strip_accents
             or normalizer_state.get('''handle_chinese_chars'''    ,							a       ) != tokenize_chinese_chars
         ):
             __lowerCamelCase            =    getattr(a    ,							normalizer_state.pop('''type'''       )       )
             __lowerCamelCase            =    do_lower_case
             __lowerCamelCase            =    strip_accents
             __lowerCamelCase            =    tokenize_chinese_chars
             __lowerCamelCase            =    normalizer_class(**a       )

         __lowerCamelCase            =    do_lower_case






     def   SCREAMING_SNAKE_CASE__  (					self						:						Optional[int]    ,							a						:						int    ,							a						:						List[Any]=None       ):





         """simple docstring"""



         __lowerCamelCase            =    [self.cls_token_id] + token_ids_a + [self.sep_token_id]

         if token_ids_a:
             output += token_ids_a + [self.sep_token_id]

         return output






     def   SCREAMING_SNAKE_CASE__  (					self						:						int    ,							a						:						List[int]    ,							a						:						Optional[List[int]] = None       ):





         """simple docstring"""



         __lowerCamelCase            =    [self.sep_token_id]
         __lowerCamelCase            =    [self.cls_token_id]
         if token_ids_a is None:
             return len(cls + token_ids_a + sep       ) * [0]
         return len(cls + token_ids_a + sep       ) * [0] + len(token_ids_a + sep       ) * [1]






     def   SCREAMING_SNAKE_CASE__  (					self						:						List[Any]    ,							a						:						str    ,							a						:						Optional[str] = None       ):





         """simple docstring"""



         __lowerCamelCase            =    self._tokenizer.model.save(a    ,							name=a       )
         return tuple(a       )

'''simple docstring'''




from math import ceil
from typing import List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor
from ...utils import TensorType, logging


__UpperCAmelCase					=logging.get_logger(__name__)




class        a__ (  UpperCAmelCase__					):
     lowerCamelCase    :	Union[str, Any]				=["audio_values", "audio_mask"]






     def __init__(					self						:						List[str]    ,							a						:						List[Any]=20_48    ,							a						:						Optional[int]=1    ,							a						:						List[str]=[16, 16]    ,							a						:						int=1_28    ,							a						:						Dict=4_41_00    ,							a						:						str=86    ,							a						:						int=20_48    ,							a						:						int=0.0    ,							**a						:						Dict    ,							):





         """simple docstring"""



         super().__init__(
             feature_size=a    ,							sampling_rate=a    ,							padding_value=a    ,							**a    ,							)

         __lowerCamelCase            =    spectrogram_length
         __lowerCamelCase            =    num_channels
         __lowerCamelCase            =    patch_size
         __lowerCamelCase            =    feature_size // self.patch_size[1]
         __lowerCamelCase            =    n_fft
         __lowerCamelCase            =    sampling_rate // hop_length_to_sampling_rate
         __lowerCamelCase            =    sampling_rate
         __lowerCamelCase            =    padding_value
         __lowerCamelCase            =    mel_filter_bank(
             num_frequency_bins=1 + n_fft // 2    ,							num_mel_filters=a    ,							min_frequency=0.0    ,							max_frequency=2_20_50.0    ,							sampling_rate=a    ,							norm='''slaney'''    ,							mel_scale='''slaney'''    ,							).T






     def   SCREAMING_SNAKE_CASE__  (					self						:						Tuple    ,							a						:						np.array       ):





         """simple docstring"""



         __lowerCamelCase            =    spectrogram(
             a    ,							window_function(self.n_fft    ,							'''hann'''       )    ,							frame_length=self.n_fft    ,							hop_length=self.hop_length    ,							power=2.0    ,							mel_filters=self.mel_filters.T    ,							log_mel='''dB'''    ,							db_range=80.0    ,							)
         __lowerCamelCase            =    log_spec[:, :-1]
         __lowerCamelCase            =    log_spec - 20.0
         __lowerCamelCase            =    np.clip(log_spec / 40.0    ,							-2.0    ,							0.0       ) + 1.0
         return log_spec






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





         """simple docstring"""



         if sampling_rate is not None:
             if sampling_rate != self.sampling_rate:
                 raise ValueError(
                     '''This feature extractor is set to support sampling rate'''
                     f""" of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled"""
                     f""" with {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.'''       )

         __lowerCamelCase            =    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}"""       )
         __lowerCamelCase            =    is_batched_numpy or (
             isinstance(a    ,							(list, tuple)       ) and (isinstance(raw_speech[0]    ,							(np.ndarray, tuple, list)       ))
         )
         if is_batched:
             __lowerCamelCase            =    [np.asarray([speech]    ,							dtype=np.floataa       ).T for speech in raw_speech]
         elif not is_batched and not isinstance(a    ,							np.ndarray       ):
             __lowerCamelCase            =    np.asarray(a    ,							dtype=np.floataa       )
         elif isinstance(a    ,							np.ndarray       ) and raw_speech.dtype is np.dtype(np.floataa       ):
             __lowerCamelCase            =    raw_speech.astype(np.floataa       )
         # always return batch
         if not is_batched:
             __lowerCamelCase            =    [np.asarray([raw_speech]       ).T]

         # Convert audio signals to log mel spectrograms, truncate by time axis
         __lowerCamelCase            =    [
             self._np_extract_fbank_features(waveform.squeeze()       ).T[: self.spectrogram_length] for waveform in raw_speech
         ]
         if isinstance(audio_features[0]    ,							a       ):
             __lowerCamelCase            =    [np.asarray(a    ,							dtype=np.floataa       ) for feature in audio_features]

         # Create audio attention mask
         __lowerCamelCase            =    max(
             [ceil(feature.shape[0] / self.patch_size[0]       ) * self.freq_len for feature in audio_features]       )  # The maximum number of audio patches in a batch
         if return_attention_mask:
             __lowerCamelCase            =    [
                 (ceil(feature.shape[0] / self.patch_size[0]       ) * self.freq_len) * [1]
                 + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0]       ) * self.freq_len) * [0]
                 for feature in audio_features
             ]
             __lowerCamelCase            =    np.array(a       ).astype(np.floataa       )

         # convert into correct format for padding
         __lowerCamelCase            =    max_patch_len // self.freq_len * self.patch_size[0]  # The maximum audio size in a batch
         __lowerCamelCase            =    np.ones([len(a       ), 1, max_time_len, self.feature_size]       ).astype(np.floataa       )
         __lowerCamelCase            =    padded_audio_features * self.padding_value
         for i in range(len(a       )       ):
             __lowerCamelCase            =    audio_features[i]
             __lowerCamelCase            =    feature

         # return as BatchFeature
         if return_attention_mask:
             __lowerCamelCase            =    {'''audio_values''': padded_audio_features, '''audio_mask''': audio_mask}
         else:
             __lowerCamelCase            =    {'''audio_values''': padded_audio_features}

         __lowerCamelCase            =    BatchFeature(data=a    ,							tensor_type=a       )
         return encoded_inputs
