



'''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


snake_case_			=     logging.get_logger(__name__)


class 	a__     (				_SCREAMING_SNAKE_CASE	):
	__magic_name__		:    Any	    =     ["audio_values", "audio_mask"]


	def __init__(self : Union[str, Any],					__UpperCAmelCase : List[Any]=2048,					__UpperCAmelCase : Any=1,					__UpperCAmelCase : str=[16, 16],					__UpperCAmelCase : Tuple=128,					__UpperCAmelCase : Union[str, Any]=44100,					__UpperCAmelCase : str=86,					__UpperCAmelCase : Union[str, Any]=2048,					__UpperCAmelCase : Tuple=0.0,					**__UpperCAmelCase : List[str],					)     ->			List[str]:

					"""simple docstring"""

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

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= spectrogram_length
					SCREAMING_SNAKE_CASE  :						Tuple      					= num_channels
					SCREAMING_SNAKE_CASE  :						Dict      					= patch_size
					SCREAMING_SNAKE_CASE  :						Tuple      					= feature_size // self.patch_size[1]
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= n_fft
					SCREAMING_SNAKE_CASE  :						Any      					= sampling_rate // hop_length_to_sampling_rate
					SCREAMING_SNAKE_CASE  :						str      					= sampling_rate
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= padding_value
					SCREAMING_SNAKE_CASE  :						Tuple      					= mel_filter_bank(
					    num_frequency_bins=1 + n_fft // 2,					num_mel_filters=A_,					min_frequency=0.0,					max_frequency=22050.0,					sampling_rate=A_,					norm='''slaney''',					mel_scale='''slaney''',					).T


	def 				lowercase__  (self : Optional[int],					__UpperCAmelCase : Optional[Any]      )     ->			np.ndarray:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= 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,					)
					SCREAMING_SNAKE_CASE  :						List[Any]      					= log_spec[:, :-1]
					SCREAMING_SNAKE_CASE  :						str      					= log_spec - 20.0
					SCREAMING_SNAKE_CASE  :						Tuple      					= np.clip(log_spec / 40.0,					-2.0,					0.0      ) + 1.0
					return log_spec






	def __call__(self : List[str],					__UpperCAmelCase : Union[str, Any],					__UpperCAmelCase : int = None,					__UpperCAmelCase : List[str] = True,					__UpperCAmelCase : Tuple = None,					__UpperCAmelCase : Any = False,					__UpperCAmelCase : Union[str, Any] = False,					**__UpperCAmelCase : List[str],					)     ->			BatchFeature:

					"""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.'''      )

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

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

					# Create audio attention mask
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= 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:
									SCREAMING_SNAKE_CASE  :						Any      					= [
									    (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
									]
									SCREAMING_SNAKE_CASE  :						Any      					= np.array(A_      ).astype(np.floataa      )

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

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

					SCREAMING_SNAKE_CASE  :						str      					= BatchFeature(data=A_,					tensor_type=A_      )
					return encoded_inputs









'''simple docstring'''

from ...configuration_utils import PretrainedConfig
from ...utils import logging


snake_case_			=     logging.get_logger(__name__)

snake_case_			=     {
    """microsoft/markuplm-base""": """https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json""",
    """microsoft/markuplm-large""": """https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json""",
}


class 	a__     (				_lowercase	):
	__magic_name__		:    List[Any]	    =     "markuplm"


	def __init__(self : List[Any],					__UpperCAmelCase : int=30522,					__UpperCAmelCase : Tuple=768,					__UpperCAmelCase : str=12,					__UpperCAmelCase : List[Any]=12,					__UpperCAmelCase : Tuple=3072,					__UpperCAmelCase : Union[str, Any]="gelu",					__UpperCAmelCase : Union[str, Any]=0.1,					__UpperCAmelCase : str=0.1,					__UpperCAmelCase : Union[str, Any]=512,					__UpperCAmelCase : Dict=2,					__UpperCAmelCase : int=0.02,					__UpperCAmelCase : Optional[Any]=1e-12,					__UpperCAmelCase : Union[str, Any]=0,					__UpperCAmelCase : Optional[Any]=0,					__UpperCAmelCase : Union[str, Any]=2,					__UpperCAmelCase : int=256,					__UpperCAmelCase : Tuple=1024,					__UpperCAmelCase : Optional[Any]=216,					__UpperCAmelCase : str=1001,					__UpperCAmelCase : Optional[Any]=32,					__UpperCAmelCase : Dict=50,					__UpperCAmelCase : Union[str, Any]="absolute",					__UpperCAmelCase : Tuple=True,					__UpperCAmelCase : List[str]=None,					**__UpperCAmelCase : Union[str, Any],					)     ->			Union[str, Any]:

					"""simple docstring"""

					super().__init__(
					    pad_token_id=__UpperCAmelCase,					bos_token_id=__UpperCAmelCase,					eos_token_id=__UpperCAmelCase,					**__UpperCAmelCase,					)
					SCREAMING_SNAKE_CASE  :						List[Any]      					= vocab_size
					SCREAMING_SNAKE_CASE  :						Dict      					= hidden_size
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= num_hidden_layers
					SCREAMING_SNAKE_CASE  :						Dict      					= num_attention_heads
					SCREAMING_SNAKE_CASE  :						Any      					= hidden_act
					SCREAMING_SNAKE_CASE  :						Tuple      					= intermediate_size
					SCREAMING_SNAKE_CASE  :						Tuple      					= hidden_dropout_prob
					SCREAMING_SNAKE_CASE  :						List[str]      					= attention_probs_dropout_prob
					SCREAMING_SNAKE_CASE  :						List[str]      					= max_position_embeddings
					SCREAMING_SNAKE_CASE  :						Dict      					= type_vocab_size
					SCREAMING_SNAKE_CASE  :						Tuple      					= initializer_range
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= layer_norm_eps
					SCREAMING_SNAKE_CASE  :						int      					= position_embedding_type
					SCREAMING_SNAKE_CASE  :						List[Any]      					= use_cache
					SCREAMING_SNAKE_CASE  :						int      					= classifier_dropout
					# additional properties
					SCREAMING_SNAKE_CASE  :						List[Any]      					= max_depth
					SCREAMING_SNAKE_CASE  :						Any      					= max_xpath_tag_unit_embeddings
					SCREAMING_SNAKE_CASE  :						int      					= max_xpath_subs_unit_embeddings
					SCREAMING_SNAKE_CASE  :						Dict      					= tag_pad_id
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= subs_pad_id
					SCREAMING_SNAKE_CASE  :						int      					= xpath_unit_hidden_size




