


"""simple docstring"""






import argparse
import collections
import os
import re

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_table.py
__snake_case		=       '''src/transformers'''
__snake_case		=       '''docs/source/en'''
__snake_case		=       '''.'''
def       A_   (   _lowerCAmelCase     :					Any,     _lowerCAmelCase     :					str,     _lowerCAmelCase     :					List[str]       ):






     """simple docstring"""

     with open(_lowerCAmelCase,     '''r''',     encoding='''utf-8''',     newline='''\n'''       ) as f:
          _a    	=				f.readlines()
     # Find the start prompt.
     _a    	=				0
     while not lines[start_index].startswith(_lowerCAmelCase       ):
          start_index += 1
     start_index += 1

     _a    	=				start_index
     while not lines[end_index].startswith(_lowerCAmelCase       ):
          end_index += 1
     end_index -= 1

     while len(lines[start_index]       ) <= 1:
          start_index += 1
     while len(lines[end_index]       ) <= 1:
          end_index -= 1
     end_index += 1
     return "".join(lines[start_index:end_index]       ), start_index, end_index, lines


# Add here suffixes that are used to identify models, separated by |
__snake_case		=       '''Model|Encoder|Decoder|ForConditionalGeneration'''
# Regexes that match TF/Flax/PT model names.
__snake_case		=       re.compile(r'''TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''')
__snake_case		=       re.compile(r'''Flax(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''')
# Will match any TF or Flax model too so need to be in an else branch afterthe two previous regexes.
__snake_case		=       re.compile(r'''(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)''')


# This is to make sure the transformers module imported is the one in the repo.
__snake_case		=       direct_transformers_import(TRANSFORMERS_PATH)
def       A_   (   _lowerCAmelCase     :					List[Any]       ):






     """simple docstring"""

     _a    	=				re.finditer('''.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)''',     _lowerCAmelCase       )
     return [m.group(0       ) for m in matches]
def       A_   (   _lowerCAmelCase     :					List[str],     _lowerCAmelCase     :					str       ):






     """simple docstring"""

     _a    	=				2 if text == '''✅''' or text == '''❌''' else len(_lowerCAmelCase       )
     _a    	=				(width - text_length) // 2
     _a    	=				width - text_length - left_indent
     return " " * left_indent + text + " " * right_indent
def       A_   (   ):






     """simple docstring"""

     _a    	=				transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES
     _a    	=				{
         name: config_maping_names[code]
         for code, name in transformers_module.MODEL_NAMES_MAPPING.items()
         if code in config_maping_names
     }
     _a    	=				{name: config.replace('''Config''',     ''''''       ) for name, config in model_name_to_config.items()}

     # Dictionaries flagging if each model prefix has a slow/fast tokenizer, backend in PT/TF/Flax.
     _a    	=				collections.defaultdict(_lowerCAmelCase       )
     _a    	=				collections.defaultdict(_lowerCAmelCase       )
     _a    	=				collections.defaultdict(_lowerCAmelCase       )
     _a    	=				collections.defaultdict(_lowerCAmelCase       )
     _a    	=				collections.defaultdict(_lowerCAmelCase       )

     # Let's lookup through all transformers object (once).
     for attr_name in dir(_lowerCAmelCase       ):
          _a    	=				None
          if attr_name.endswith('''Tokenizer'''       ):
               _a    	=				slow_tokenizers
               _a    	=				attr_name[:-9]
          elif attr_name.endswith('''TokenizerFast'''       ):
               _a    	=				fast_tokenizers
               _a    	=				attr_name[:-13]
          elif _re_tf_models.match(_lowerCAmelCase       ) is not None:
               _a    	=				tf_models
               _a    	=				_re_tf_models.match(_lowerCAmelCase       ).groups()[0]
          elif _re_flax_models.match(_lowerCAmelCase       ) is not None:
               _a    	=				flax_models
               _a    	=				_re_flax_models.match(_lowerCAmelCase       ).groups()[0]
          elif _re_pt_models.match(_lowerCAmelCase       ) is not None:
               _a    	=				pt_models
               _a    	=				_re_pt_models.match(_lowerCAmelCase       ).groups()[0]

          if lookup_dict is not None:
               while len(_lowerCAmelCase       ) > 0:
                    if attr_name in model_name_to_prefix.values():
                         _a    	=				True
                         break
                    # Try again after removing the last word in the name
                    _a    	=				''''''.join(camel_case_split(_lowerCAmelCase       )[:-1]       )

    # Let's build that table!
     _a    	=				list(model_name_to_config.keys()       )
     model_names.sort(key=str.lower       )
     _a    	=				['''Model''', '''Tokenizer slow''', '''Tokenizer fast''', '''PyTorch support''', '''TensorFlow support''', '''Flax Support''']
     # We'll need widths to properly display everything in the center (+2 is to leave one extra space on each side).
     _a    	=				[len(_lowerCAmelCase       ) + 2 for c in columns]
     _a    	=				max([len(_lowerCAmelCase       ) for name in model_names]       ) + 2

     # Build the table per se
     _a    	=				'''|''' + '''|'''.join([_center_text(_lowerCAmelCase,     _lowerCAmelCase       ) for c, w in zip(_lowerCAmelCase,     _lowerCAmelCase       )]       ) + '''|\n'''
     # Use ":-----:" format to center-aligned table cell texts
     table += "|" + "|".join([''':''' + '''-''' * (w - 2) + ''':''' for w in widths]       ) + "|\n"

     _a    	=				{True: '''✅''', False: '''❌'''}
     for name in model_names:
          _a    	=				model_name_to_prefix[name]
          _a    	=				[
              name,
              check[slow_tokenizers[prefix]],
              check[fast_tokenizers[prefix]],
              check[pt_models[prefix]],
              check[tf_models[prefix]],
              check[flax_models[prefix]],
          ]
          table += "|" + "|".join([_center_text(_lowerCAmelCase,     _lowerCAmelCase       ) for l, w in zip(_lowerCAmelCase,     _lowerCAmelCase       )]       ) + "|\n"
     return table
def       A_   (   _lowerCAmelCase     :					List[Any]=False       ):






     """simple docstring"""

     _a	,      _a	,      _a	,      _a    	=				_find_text_in_file(
         filename=os.path.join(_lowerCAmelCase,     '''index.md'''       ),     start_prompt='''<!--This table is updated automatically from the auto modules''',     end_prompt='''<!-- End table-->''',     )
     _a    	=				get_model_table_from_auto_modules()

     if current_table != new_table:
          if overwrite:
               with open(os.path.join(_lowerCAmelCase,     '''index.md'''       ),     '''w''',     encoding='''utf-8''',     newline='''\n'''       ) as f:
                    f.writelines(lines[:start_index] + [new_table] + lines[end_index:]       )
          else:
               raise ValueError(
                   '''The model table in the `index.md` has not been updated. Run `make fix-copies` to fix this.'''       )


if __name__ == "__main__":
   __snake_case		=       argparse.ArgumentParser()
   parser.add_argument('''--fix_and_overwrite''', action='''store_true''', help='''Whether to fix inconsistencies.''')
   __snake_case		=       parser.parse_args()

   check_model_table(args.fix_and_overwrite)



"""simple docstring"""






from typing import List, Optional, Union

import numpy as np

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


__snake_case		=       logging.get_logger(__name__)


class     __lowerCamelCase  (					a__				):

     '''simple docstring'''
     A_       :	Union[str, Any]    =		['input_values', 'padding_mask']


     def __init__(      self    ,				__UpperCAmelCase = 1    ,				__UpperCAmelCase = 24000    ,				__UpperCAmelCase = 0.0    ,				__UpperCAmelCase = None    ,				__UpperCAmelCase = None    ,				**__UpperCAmelCase    ,				)       ->							List[str]:
          super().__init__(feature_size=__UpperCAmelCase    ,				sampling_rate=__UpperCAmelCase    ,				padding_value=__UpperCAmelCase    ,				**__UpperCAmelCase							)
          _a    	=				chunk_length_s
          _a    	=				overlap


     @property
     def 							_UpperCAmelCase      (      self							)       ->							Optional[int]:
          if self.chunk_length_s is None:
               return None
          else:
               return int(self.chunk_length_s * self.sampling_rate							)


     @property
     def 							_UpperCAmelCase      (      self							)       ->							Optional[int]:
          if self.chunk_length_s is None or self.overlap is None:
               return None
          else:
               return max(1    ,				int((1.0 - self.overlap) * self.chunk_length							)							)





     def __call__(      self    ,				__UpperCAmelCase    ,				__UpperCAmelCase = None    ,				__UpperCAmelCase = False    ,				__UpperCAmelCase = None    ,				__UpperCAmelCase = None    ,				__UpperCAmelCase = None    ,				)       ->							BatchFeature:
          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 audio 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.'''							)

          if padding and truncation:
               raise ValueError('''Both padding and truncation were set. Make sure you only set one.'''							)
          elif padding is None:
               # by default let's pad the inputs
               _a    	=				True

          _a    	=				bool(
              isinstance(__UpperCAmelCase    ,				(list, tuple)							) and (isinstance(raw_audio[0]    ,				(np.ndarray, tuple, list)							))							)

          if is_batched:
               _a    	=				[np.asarray(__UpperCAmelCase    ,				dtype=np.floataa							).T for audio in raw_audio]
          elif not is_batched and not isinstance(__UpperCAmelCase    ,				np.ndarray							):
               _a    	=				np.asarray(__UpperCAmelCase    ,				dtype=np.floataa							)
          elif isinstance(__UpperCAmelCase    ,				np.ndarray							) and raw_audio.dtype is np.dtype(np.floataa							):
               _a    	=				raw_audio.astype(np.floataa							)

          # always return batch
          if not is_batched:
               _a    	=				[np.asarray(__UpperCAmelCase							).T]

          # verify inputs are valid
          for idx, example in enumerate(__UpperCAmelCase							):
               if example.ndim > 2:
                    raise ValueError(F'Expected input shape (channels, length) but got shape {example.shape}'							)
               if self.feature_size == 1 and example.ndim != 1:
                    raise ValueError(F'Expected mono audio but example has {example.shape[-1]} channels'							)
               if self.feature_size == 2 and example.shape[-1] != 2:
                    raise ValueError(F'Expected stereo audio but example has {example.shape[-1]} channels'							)

          _a    	=				None
          _a    	=				BatchFeature({'''input_values''': raw_audio}							)
          if self.chunk_stride is not None and self.chunk_length is not None and max_length is None:
               if truncation:
                    _a    	=				min(array.shape[0] for array in raw_audio							)
                    _a    	=				int(np.floor(max_length / self.chunk_stride							)							)
                    _a    	=				(nb_step - 1) * self.chunk_stride + self.chunk_length
               elif padding:
                    _a    	=				max(array.shape[0] for array in raw_audio							)
                    _a    	=				int(np.ceil(max_length / self.chunk_stride							)							)
                    _a    	=				(nb_step - 1) * self.chunk_stride + self.chunk_length
                    _a    	=				'''max_length'''
               else:
                    _a    	=				input_values

        # normal padding on batch
          if padded_inputs is None:
               _a    	=				self.pad(
                   __UpperCAmelCase    ,				max_length=__UpperCAmelCase    ,				truncation=__UpperCAmelCase    ,				padding=__UpperCAmelCase    ,				return_attention_mask=__UpperCAmelCase    ,				)
               if padding:
                    _a    	=				padded_inputs.pop('''attention_mask'''							)

          _a    	=				[]
          for example in padded_inputs.pop('''input_values'''							):
               if self.feature_size == 1:
                    _a    	=				example[..., None]
               input_values.append(example.T							)

          _a    	=				input_values
          if return_tensors is not None:
               _a    	=				padded_inputs.convert_to_tensors(__UpperCAmelCase							)

          return padded_inputs