

"""simple docstring"""




from typing import List, Optional, Union

import numpy as np

from ....audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function
from ....feature_extraction_sequence_utils import SequenceFeatureExtractor
from ....feature_extraction_utils import BatchFeature
from ....file_utils import PaddingStrategy, TensorType
from ....utils import logging


__UpperCAmelCase			=					logging.get_logger(__name__)







class   __lowercase      (     __lowerCamelCase       ):
  snake_case_												=							["""input_features""", """attention_mask"""]

  def __init__(	self		:      Any       ,A		:      str=80       ,A		:      Optional[int]=16_000       ,A		:      int=0.0       ,A		:      str=10       ,A		:      Any=25       ,A		:      str="hamming_window"       ,A		:      int=3_2_7_6_8.0       ,A		:      List[str]=0.9_7       ,A		:      Optional[int]=1.0       ,A		:      Optional[Any]=True       ,A		:      Tuple=True       ,A		:      Any=False       ,**A		:      int       ,):






   '''simple docstring'''
   super().__init__(feature_size=A       ,sampling_rate=A       ,padding_value=A       ,**A     )

   UpperCAmelCase__       :	str        =       feature_size
   UpperCAmelCase__       :	int        =       sampling_rate
   UpperCAmelCase__       :	int        =       padding_value
   UpperCAmelCase__       :	Dict        =       hop_length
   UpperCAmelCase__       :	int        =       win_length
   UpperCAmelCase__       :	Dict        =       frame_signal_scale
   UpperCAmelCase__       :	Dict        =       preemphasis_coeff
   UpperCAmelCase__       :	str        =       mel_floor
   UpperCAmelCase__       :	Any        =       normalize_means
   UpperCAmelCase__       :	str        =       normalize_vars
   UpperCAmelCase__       :	int        =       win_function
   UpperCAmelCase__       :	List[Any]        =       return_attention_mask

   UpperCAmelCase__       :	str        =       win_length * sampling_rate // 1_000
   UpperCAmelCase__       :	List[Any]        =       hop_length * sampling_rate // 1_000

   UpperCAmelCase__       :	int        =       optimal_fft_length(self.sample_size     )
   UpperCAmelCase__       :	List[Any]        =       (self.n_fft // 2) + 1

  def  __lowercase							(	self		:      Union[str, Any]       ,A		:      np.array     ):






   '''simple docstring'''
   if self.win_function == "hamming_window":
    UpperCAmelCase__       :	Any        =       window_function(window_length=self.sample_size       ,name=self.win_function       ,periodic=A     )
   else:
    UpperCAmelCase__       :	Any        =       window_function(window_length=self.sample_size       ,name=self.win_function     )

   UpperCAmelCase__       :	Union[str, Any]        =       mel_filter_bank(
       num_frequency_bins=self.n_freqs       ,num_mel_filters=self.feature_size       ,min_frequency=0.0       ,max_frequency=self.sampling_rate / 2.0       ,sampling_rate=self.sampling_rate       ,)

   UpperCAmelCase__       :	Optional[Any]        =       spectrogram(
       one_waveform * self.frame_signal_scale       ,window=A       ,frame_length=self.sample_size       ,hop_length=self.sample_stride       ,fft_length=self.n_fft       ,center=A       ,preemphasis=self.preemphasis_coeff       ,mel_filters=A       ,mel_floor=self.mel_floor       ,log_mel="""log"""       ,)
   return msfc_features.T

  def  __lowercase							(	self		:      str       ,A		:      Any       ,A		:      Optional[int]       ,A		:      str     ):






   '''simple docstring'''
   # make sure we normalize float32 arrays
   if self.normalize_means:
    UpperCAmelCase__       :	Optional[Any]        =       x[:input_length].mean(axis=0     )
    UpperCAmelCase__       :	Any        =       np.subtract(A       ,A     )
   if self.normalize_vars:
    UpperCAmelCase__       :	str        =       x[:input_length].std(axis=0     )
    UpperCAmelCase__       :	Optional[int]        =       np.divide(A       ,A     )

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

   # make sure array is in float32
   UpperCAmelCase__       :	str        =       x.astype(np.floataa     )

   return x

  def  __lowercase							(	self		:      Union[str, Any]       ,A		:      List[np.ndarray]       ,A		:      Optional[np.ndarray] = None     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Any        =       attention_mask.sum(-1     ) if attention_mask is not None else [x.shape[0] for x in input_features]
   return [self._normalize_one(A       ,A       ,self.padding_value     ) for x, n in zip(A       ,A     )]




  def __call__(	self		:      Union[str, 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[bool] = None       ,A		:      Optional[Union[str, TensorType]] = None       ,A		:      Optional[int] = None       ,**A		:      Tuple       ,):






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

   UpperCAmelCase__       :	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}"     )
   UpperCAmelCase__       :	Any        =       is_batched_numpy or (
       isinstance(A       ,(list, tuple)     ) and (isinstance(raw_speech[0]       ,(np.ndarray, tuple, list)     ))
   )

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

   # always return batch
   if not is_batched:
    UpperCAmelCase__       :	Optional[Any]        =       [raw_speech]

   # extract fbank features
   UpperCAmelCase__       :	Tuple        =       [self._extract_mfsc_features(A     ) for one_waveform in raw_speech]

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

   UpperCAmelCase__       :	Optional[Any]        =       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
   UpperCAmelCase__       :	Tuple        =       padded_inputs.get("""input_features"""     )
   if isinstance(input_features[0]       ,A     ):
    UpperCAmelCase__       :	Union[str, Any]        =       [np.asarray(A       ,dtype=np.floataa     ) for feature in input_features]

   UpperCAmelCase__       :	Dict        =       padded_inputs.get("""attention_mask"""     )
   if attention_mask is not None:
    UpperCAmelCase__       :	str        =       [np.asarray(A       ,dtype=np.intaa     ) for array in attention_mask]

   if self.normalize_means or self.normalize_vars:
    UpperCAmelCase__       :	Union[str, Any]        =       (
        np.array(A       ,dtype=np.intaa     )
        if self._get_padding_strategies(A       ,max_length=A     ) is not PaddingStrategy.DO_NOT_PAD
        and padding
        else None
    )
    UpperCAmelCase__       :	Any        =       self.normalize(
        padded_inputs["""input_features"""]       ,attention_mask=A     )

   if return_tensors is not None:
    UpperCAmelCase__       :	Union[str, Any]        =       padded_inputs.convert_to_tensors(A     )

   return padded_inputs












'''simple docstring'''




import unittest

from transformers import DonutProcessor


__UpperCAmelCase            =       """naver-clova-ix/donut-base"""




class      UpperCamelCase__					(					unittest.TestCase			):




					"""simple docstring"""


					def lowerCamelCase_			(						self				:			List[str]						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Any		=			DonutProcessor.from_pretrained(lowerCamelCase_						)



					def lowerCamelCase_			(						self				:			Tuple						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Optional[int]		=			{
							    """name""": """John Doe""",
							    """age""": """99""",
							    """city""": """Atlanta""",
							    """state""": """GA""",
							    """zip""": """30301""",
							    """phone""": """123-4567""",
							    """nicknames""": [{"""nickname""": """Johnny"""}, {"""nickname""": """JD"""}],
							}

							SCREAMING_SNAKE_CASE :				List[Any]		=			(
							    """<s_name>John Doe</s_name><s_age>99</s_age><s_city>Atlanta</s_city>"""
							    """<s_state>GA</s_state><s_zip>30301</s_zip><s_phone>123-4567</s_phone>"""
							    """<s_nicknames><s_nickname>Johnny</s_nickname>"""
							    """<sep/><s_nickname>JD</s_nickname></s_nicknames>"""
							)
							SCREAMING_SNAKE_CASE :				List[Any]		=			self.processor.tokenajson(lowerCamelCase_						)

							self.assertDictEqual(lowerCamelCase_      ,						lowerCamelCase_						)



