





import collections
import importlib.util
import os
import re
from pathlib import Path


__lowercase  :   Optional[int]						      ="""src/transformers"""


# Matches is_xxx_available()
__lowercase  :   Union[str, Any]						      =re.compile(R"""is\_([a-z_]*)_available()""")
# Catches a one-line _import_struct = {xxx}
__lowercase  :   List[str]						      =re.compile(R"""^_import_structure\s+=\s+\{([^\}]+)\}""")
# Catches a line with a key-values pattern: "bla": ["foo", "bar"]
__lowercase  :   str						      =re.compile(R"""\s+\"\S*\":\s+\[([^\]]*)\]""")
# Catches a line if not is_foo_available
__lowercase  :   List[Any]						      =re.compile(R"""^\s*if\s+not\s+is\_[a-z_]*\_available\(\)""")
# Catches a line _import_struct["bla"].append("foo")
__lowercase  :   Optional[int]						      =re.compile(R"""^\s*_import_structure\[\"\S*\"\]\.append\(\"(\S*)\"\)""")
# Catches a line _import_struct["bla"].extend(["foo", "bar"]) or _import_struct["bla"] = ["foo", "bar"]
__lowercase  :   Optional[int]						      =re.compile(R"""^\s*_import_structure\[\S*\](?:\.extend\(|\s*=\s+)\[([^\]]*)\]""")
# Catches a line with an object between quotes and a comma:     "MyModel",
__lowercase  :   Optional[int]						      =re.compile("""^\s+\"([^\"]+)\",""")
# Catches a line with objects between brackets only:    ["foo", "bar"],
__lowercase  :   Tuple						      =re.compile("""^\s+\[([^\]]+)\]""")
# Catches a line with from foo import bar, bla, boo
__lowercase  :   Optional[Any]						      =re.compile(R"""\s+from\s+\S*\s+import\s+([^\(\s].*)\n""")
# Catches a line with try:
__lowercase  :   str						      =re.compile(R"""^\s*try:""")
# Catches a line with else:
__lowercase  :   Dict						      =re.compile(R"""^\s*else:""")




def     a__ (					lowercase__  ):






   '''simple docstring'''






   if _re_test_backend.search(lowercase__  ) is None:
      return None
   UpperCAmelCase_											=[b[0] for b in _re_backend.findall(lowercase__  )]
   backends.sort()
   return "_and_".join(lowercase__  )




def     a__ (					lowercase__  ):






   '''simple docstring'''






   with open(lowercase__  ,    "r"  ,    encoding="utf-8"  ,    newline="\n"  ) as f:
      UpperCAmelCase_											=f.readlines()

   UpperCAmelCase_											=0
   while line_index < len(lowercase__  ) and not lines[line_index].startswith("_import_structure = {"  ):
      line_index += 1

   # If this is a traditional init, just return.
   if line_index >= len(lowercase__  ):
      return None

   # First grab the objects without a specific backend in _import_structure
   UpperCAmelCase_											=[]
   while not lines[line_index].startswith("if TYPE_CHECKING"  ) and find_backend(lines[line_index]  ) is None:
      UpperCAmelCase_											=lines[line_index]
      # If we have everything on a single line, let's deal with it.
      if _re_one_line_import_struct.search(lowercase__  ):
         UpperCAmelCase_											=_re_one_line_import_struct.search(lowercase__  ).groups()[0]
         UpperCAmelCase_											=re.findall("\[([^\]]+)\]"  ,    lowercase__  )
         for imp in imports:
            objects.extend([obj[1:-1] for obj in imp.split(", "  )]  )
         line_index += 1
         continue
      UpperCAmelCase_											=_re_import_struct_key_value.search(lowercase__  )
      if single_line_import_search is not None:
         UpperCAmelCase_											=[obj[1:-1] for obj in single_line_import_search.groups()[0].split(", "  ) if len(lowercase__  ) > 0]
         objects.extend(lowercase__  )
      elif line.startswith(" " * 8 + "\""  ):
         objects.append(line[9:-3]  )
      line_index += 1

   UpperCAmelCase_											={"none": objects}
   # Let's continue with backend-specific objects in _import_structure
   while not lines[line_index].startswith("if TYPE_CHECKING"  ):
      # If the line is an if not is_backend_available, we grab all objects associated.
      UpperCAmelCase_											=find_backend(lines[line_index]  )
      # Check if the backend declaration is inside a try block:
      if _re_try.search(lines[line_index - 1]  ) is None:
         UpperCAmelCase_											=None

      if backend is not None:
         line_index += 1

         # Scroll until we hit the else block of try-except-else
         while _re_else.search(lines[line_index]  ) is None:
            line_index += 1

         line_index += 1

         UpperCAmelCase_											=[]
         # Until we unindent, add backend objects to the list
         while len(lines[line_index]  ) <= 1 or lines[line_index].startswith(" " * 4  ):
            UpperCAmelCase_											=lines[line_index]
            if _re_import_struct_add_one.search(lowercase__  ) is not None:
               objects.append(_re_import_struct_add_one.search(lowercase__  ).groups()[0]  )
            elif _re_import_struct_add_many.search(lowercase__  ) is not None:
               UpperCAmelCase_											=_re_import_struct_add_many.search(lowercase__  ).groups()[0].split(", "  )
               UpperCAmelCase_											=[obj[1:-1] for obj in imports if len(lowercase__  ) > 0]
               objects.extend(lowercase__  )
            elif _re_between_brackets.search(lowercase__  ) is not None:
               UpperCAmelCase_											=_re_between_brackets.search(lowercase__  ).groups()[0].split(", "  )
               UpperCAmelCase_											=[obj[1:-1] for obj in imports if len(lowercase__  ) > 0]
               objects.extend(lowercase__  )
            elif _re_quote_object.search(lowercase__  ) is not None:
               objects.append(_re_quote_object.search(lowercase__  ).groups()[0]  )
            elif line.startswith(" " * 8 + "\""  ):
               objects.append(line[9:-3]  )
            elif line.startswith(" " * 1_2 + "\""  ):
               objects.append(line[1_3:-3]  )
            line_index += 1

         UpperCAmelCase_											=objects
      else:
         line_index += 1

    # At this stage we are in the TYPE_CHECKING part, first grab the objects without a specific backend
   UpperCAmelCase_											=[]
   while (
       line_index < len(lowercase__  )
       and find_backend(lines[line_index]  ) is None
       and not lines[line_index].startswith("else"  )
   ):
      UpperCAmelCase_											=lines[line_index]
      UpperCAmelCase_											=_re_import.search(lowercase__  )
      if single_line_import_search is not None:
         objects.extend(single_line_import_search.groups()[0].split(", "  )  )
      elif line.startswith(" " * 8  ):
         objects.append(line[8:-2]  )
      line_index += 1

   UpperCAmelCase_											={"none": objects}
   # Let's continue with backend-specific objects
   while line_index < len(lowercase__  ):
      # If the line is an if is_backend_available, we grab all objects associated.
      UpperCAmelCase_											=find_backend(lines[line_index]  )
      # Check if the backend declaration is inside a try block:
      if _re_try.search(lines[line_index - 1]  ) is None:
         UpperCAmelCase_											=None

      if backend is not None:
         line_index += 1

         # Scroll until we hit the else block of try-except-else
         while _re_else.search(lines[line_index]  ) is None:
            line_index += 1

         line_index += 1

         UpperCAmelCase_											=[]
         # Until we unindent, add backend objects to the list
         while len(lines[line_index]  ) <= 1 or lines[line_index].startswith(" " * 8  ):
            UpperCAmelCase_											=lines[line_index]
            UpperCAmelCase_											=_re_import.search(lowercase__  )
            if single_line_import_search is not None:
               objects.extend(single_line_import_search.groups()[0].split(", "  )  )
            elif line.startswith(" " * 1_2  ):
               objects.append(line[1_2:-2]  )
            line_index += 1

         UpperCAmelCase_											=objects
      else:
         line_index += 1

   return import_dict_objects, type_hint_objects




def     a__ (					lowercase__  ,    lowercase__  ):






   '''simple docstring'''






   def find_duplicates(lowercase__  ):
      return [k for k, v in collections.Counter(lowercase__  ).items() if v > 1]

   if list(import_dict_objects.keys()  ) != list(type_hint_objects.keys()  ):
      return ["Both sides of the init do not have the same backends!"]

   UpperCAmelCase_											=[]
   for key in import_dict_objects.keys():
      UpperCAmelCase_											=find_duplicates(import_dict_objects[key]  )
      if duplicate_imports:
         errors.append(F'Duplicate _import_structure definitions for: {duplicate_imports}'  )
      UpperCAmelCase_											=find_duplicates(type_hint_objects[key]  )
      if duplicate_type_hints:
         errors.append(F'Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}'  )

      if sorted(set(import_dict_objects[key]  )  ) != sorted(set(type_hint_objects[key]  )  ):
         UpperCAmelCase_											="base imports" if key == "none" else F'{key} backend'
         errors.append(F'Differences for {name}:'  )
         for a in type_hint_objects[key]:
            if a not in import_dict_objects[key]:
               errors.append(F'  {a} in TYPE_HINT but not in _import_structure.'  )
         for a in import_dict_objects[key]:
            if a not in type_hint_objects[key]:
               errors.append(F'  {a} in _import_structure but not in TYPE_HINT.'  )
   return errors




def     a__ (					):






   '''simple docstring'''






   UpperCAmelCase_											=[]
   for root, _, files in os.walk(lowercase__  ):
      if "__init__.py" in files:
         UpperCAmelCase_											=os.path.join(lowercase__  ,    "__init__.py"  )
         UpperCAmelCase_											=parse_init(lowercase__  )
         if objects is not None:
            UpperCAmelCase_											=analyze_results(*lowercase__  )
            if len(lowercase__  ) > 0:
               UpperCAmelCase_											=F'Problem in {fname}, both halves do not define the same objects.\n{errors[0]}'
               failures.append("\n".join(lowercase__  )  )
   if len(lowercase__  ) > 0:
      raise ValueError("\n\n".join(lowercase__  )  )




def     a__ (					):






   '''simple docstring'''






   UpperCAmelCase_											=[]
   for path, directories, files in os.walk(lowercase__  ):
      for folder in directories:
         # Ignore private modules
         if folder.startswith("_"  ):
            directories.remove(lowercase__  )
            continue
         # Ignore leftovers from branches (empty folders apart from pycache)
         if len(list((Path(lowercase__  ) / folder).glob("*.py"  )  )  ) == 0:
            continue
         UpperCAmelCase_											=str((Path(lowercase__  ) / folder).relative_to(lowercase__  )  )
         UpperCAmelCase_											=short_path.replace(os.path.sep  ,    "."  )
         submodules.append(lowercase__  )
      for fname in files:
         if fname == "__init__.py":
            continue
         UpperCAmelCase_											=str((Path(lowercase__  ) / fname).relative_to(lowercase__  )  )
         UpperCAmelCase_											=short_path.replace(".py"  ,    ""  ).replace(os.path.sep  ,    "."  )
         if len(submodule.split("."  )  ) == 1:
            submodules.append(lowercase__  )
   return submodules


__lowercase  :   List[str]						      =[
    """convert_pytorch_checkpoint_to_tf2""",
    """modeling_flax_pytorch_utils""",
]




def     a__ (					):






   '''simple docstring'''






   UpperCAmelCase_											=importlib.util.spec_from_file_location(
       "transformers"  ,    os.path.join(lowercase__  ,    "__init__.py"  )  ,    submodule_search_locations=[PATH_TO_TRANSFORMERS]  ,    )
   UpperCAmelCase_											=spec.loader.load_module()

   UpperCAmelCase_											=[
       module
       for module in get_transformers_submodules()
       if module not in IGNORE_SUBMODULES and module not in transformers._import_structure.keys()
   ]
   if len(lowercase__  ) > 0:
      UpperCAmelCase_											="\n".join(F'- {module}' for module in module_not_registered  )
      raise ValueError(
          "The following submodules are not properly registered in the main init of Transformers:\n"
          F'{list_of_modules}\n'
          "Make sure they appear somewhere in the keys of `_import_structure` with an empty list as value."  )


if __name__ == "__main__":
     check_all_inits()
     check_submodules()











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


__lowercase  :   Union[str, Any]						      =logging.get_logger(__name__)

class      A       (	__lowercase						):
      _snake_case							     =['''input_features''', '''attention_mask''']



      def __init__(	self:						Dict		,  _lowerCAmelCase:						List[str]=80		,  _lowerCAmelCase:						Optional[Any]=1_6000		,  _lowerCAmelCase:						Any=0.0		,  _lowerCAmelCase:						Optional[Any]=10		,  _lowerCAmelCase:						Optional[Any]=25		,  _lowerCAmelCase:						Union[str, Any]="hamming_window"		,  _lowerCAmelCase:						Optional[int]=3_27_68.0		,  _lowerCAmelCase:						Optional[Any]=0.97		,  _lowerCAmelCase:						Any=1.0		,  _lowerCAmelCase:						List[str]=True		,  _lowerCAmelCase:						Dict=True		,  _lowerCAmelCase:						Any=False		,  **_lowerCAmelCase:						Any		,  )						->    Dict:
         '''simple docstring'''

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

         UpperCAmelCase_											=feature_size
         UpperCAmelCase_											=sampling_rate
         UpperCAmelCase_											=padding_value
         UpperCAmelCase_											=hop_length
         UpperCAmelCase_											=win_length
         UpperCAmelCase_											=frame_signal_scale
         UpperCAmelCase_											=preemphasis_coeff
         UpperCAmelCase_											=mel_floor
         UpperCAmelCase_											=normalize_means
         UpperCAmelCase_											=normalize_vars
         UpperCAmelCase_											=win_function
         UpperCAmelCase_											=return_attention_mask

         UpperCAmelCase_											=win_length * sampling_rate // 1000
         UpperCAmelCase_											=hop_length * sampling_rate // 1000

         UpperCAmelCase_											=optimal_fft_length(self.sample_size       )
         UpperCAmelCase_											=(self.n_fft // 2) + 1



      def 	lowerCAmelCase__    (	self:						Optional[Any]		,  _lowerCAmelCase:						np.array       )						->    np.ndarray:
         '''simple docstring'''

         if self.win_function == "hamming_window":
            UpperCAmelCase_											=window_function(window_length=self.sample_size		,  name=self.win_function		,  periodic=_lowerCAmelCase       )
         else:
            UpperCAmelCase_											=window_function(window_length=self.sample_size		,  name=self.win_function       )

         UpperCAmelCase_											=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_											=spectrogram(
             one_waveform * self.frame_signal_scale		,  window=_lowerCAmelCase		,  frame_length=self.sample_size		,  hop_length=self.sample_stride		,  fft_length=self.n_fft		,  center=_lowerCAmelCase		,  preemphasis=self.preemphasis_coeff		,  mel_filters=_lowerCAmelCase		,  mel_floor=self.mel_floor		,  log_mel="log"		,  )
         return msfc_features.T



      def 	lowerCAmelCase__    (	self:						Dict		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						Dict		,  _lowerCAmelCase:						Any       )						->    Any:
         '''simple docstring'''

         if self.normalize_means:
            UpperCAmelCase_											=x[:input_length].mean(axis=0       )
            UpperCAmelCase_											=np.subtract(_lowerCAmelCase		,  _lowerCAmelCase       )
         if self.normalize_vars:
            UpperCAmelCase_											=x[:input_length].std(axis=0       )
            UpperCAmelCase_											=np.divide(_lowerCAmelCase		,  _lowerCAmelCase       )

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

         # make sure array is in float32
         UpperCAmelCase_											=x.astype(np.floataa       )

         return x



      def 	lowerCAmelCase__    (	self:						List[str]		,  _lowerCAmelCase:						List[np.ndarray]		,  _lowerCAmelCase:						Optional[np.ndarray] = None       )						->    List[np.ndarray]:
         '''simple docstring'''

         UpperCAmelCase_											=attention_mask.sum(-1       ) if attention_mask is not None else [x.shape[0] for x in input_features]
         return [self._normalize_one(_lowerCAmelCase		,  _lowerCAmelCase		,  self.padding_value       ) for x, n in zip(_lowerCAmelCase		,  _lowerCAmelCase       )]






      def __call__(	self:						int		,  _lowerCAmelCase:						Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]		,  _lowerCAmelCase:						Union[bool, str, PaddingStrategy] = False		,  _lowerCAmelCase:						Optional[int] = None		,  _lowerCAmelCase:						bool = False		,  _lowerCAmelCase:						Optional[int] = None		,  _lowerCAmelCase:						Optional[bool] = None		,  _lowerCAmelCase:						Optional[Union[str, TensorType]] = None		,  _lowerCAmelCase:						Optional[int] = None		,  **_lowerCAmelCase:						List[Any]		,  )						->    BatchFeature:
         '''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_											=isinstance(_lowerCAmelCase		,  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_											=is_batched_numpy or (
             isinstance(_lowerCAmelCase		,  (list, tuple)       ) and (isinstance(raw_speech[0]		,  (np.ndarray, tuple, list)       ))
         )

         if is_batched:
            UpperCAmelCase_											=[np.asarray(_lowerCAmelCase		,  dtype=np.floataa       ) for speech in raw_speech]
         elif not is_batched and not isinstance(_lowerCAmelCase		,  np.ndarray       ):
            UpperCAmelCase_											=np.asarray(_lowerCAmelCase		,  dtype=np.floataa       )
         elif isinstance(_lowerCAmelCase		,  np.ndarray       ) and raw_speech.dtype is np.dtype(np.floataa       ):
            UpperCAmelCase_											=raw_speech.astype(np.floataa       )

         # always return batch
         if not is_batched:
            UpperCAmelCase_											=[raw_speech]

         # extract fbank features
         UpperCAmelCase_											=[self._extract_mfsc_features(_lowerCAmelCase       ) for one_waveform in raw_speech]

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

         UpperCAmelCase_											=self.pad(
             _lowerCAmelCase		,  padding=_lowerCAmelCase		,  max_length=_lowerCAmelCase		,  truncation=_lowerCAmelCase		,  pad_to_multiple_of=_lowerCAmelCase		,  return_attention_mask=_lowerCAmelCase		,  **_lowerCAmelCase		,  )
         # make sure list is in array format
         UpperCAmelCase_											=padded_inputs.get("input_features"       )
         if isinstance(input_features[0]		,  _lowerCAmelCase       ):
            UpperCAmelCase_											=[np.asarray(_lowerCAmelCase		,  dtype=np.floataa       ) for feature in input_features]

         UpperCAmelCase_											=padded_inputs.get("attention_mask"       )
         if attention_mask is not None:
            UpperCAmelCase_											=[np.asarray(_lowerCAmelCase		,  dtype=np.intaa       ) for array in attention_mask]

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

         if return_tensors is not None:
            UpperCAmelCase_											=padded_inputs.convert_to_tensors(_lowerCAmelCase       )

         return padded_inputs




