



import argparse
import os
import re

import packaging.version


a_      :      Optional[int]    =     'examples/'
a_      :      Union[str, Any]    =     {
    'examples': (re.compile(R'^check_min_version\("[^"]+"\)\s*$', re.MULTILINE), 'check_min_version("VERSION")\n'),
    'init': (re.compile(R'^__version__\s+=\s+"([^"]+)"\s*$', re.MULTILINE), '__version__ = "VERSION"\n'),
    'setup': (re.compile(R'^(\s*)version\s*=\s*"[^"]+",', re.MULTILINE), R'\1version="VERSION",'),
    'doc': (re.compile(R'^(\s*)release\s*=\s*"[^"]+"$', re.MULTILINE), 'release = "VERSION"\n'),
}
a_      :      Union[str, Any]    =     {
    'init': 'src/transformers/__init__.py',
    'setup': 'setup.py',
}
a_      :      int    =     'README.md'






def     __a					(		__UpperCAmelCase							,  __UpperCAmelCase							,  __UpperCAmelCase       ):
 with open(__UpperCAmelCase							,  '''r'''							,  encoding='''utf-8'''							,  newline='''\n'''       ) as f:
  a__												= f.read()
 a__							,			a__												= REPLACE_PATTERNS[pattern]
 a__												= replace.replace('''VERSION'''							,  __UpperCAmelCase       )
 a__												= re_pattern.sub(__UpperCAmelCase							,  __UpperCAmelCase       )
 with open(__UpperCAmelCase							,  '''w'''							,  encoding='''utf-8'''							,  newline='''\n'''       ) as f:
  f.write(__UpperCAmelCase       )






def     __a					(		__UpperCAmelCase       ):
 for folder, directories, fnames in os.walk(__UpperCAmelCase       ):
  # Removing some of the folders with non-actively maintained examples from the walk
  if "research_projects" in directories:
   directories.remove('''research_projects'''       )
  if "legacy" in directories:
   directories.remove('''legacy'''       )
  for fname in fnames:
   if fname.endswith('''.py'''       ):
    update_version_in_file(os.path.join(__UpperCAmelCase							,  __UpperCAmelCase       )							,  __UpperCAmelCase							,  pattern='''examples'''       )






def     __a					(		__UpperCAmelCase							,  __UpperCAmelCase=False       ):
 for pattern, fname in REPLACE_FILES.items():
  update_version_in_file(__UpperCAmelCase							,  __UpperCAmelCase							,  __UpperCAmelCase       )
 if not patch:
  update_version_in_examples(__UpperCAmelCase       )






def     __a					(		):
 a__												= '''🤗 Transformers currently provides the following architectures'''
 a__												= '''1. Want to contribute a new model?'''
 with open(__UpperCAmelCase							,  '''r'''							,  encoding='''utf-8'''							,  newline='''\n'''       ) as f:
  a__												= f.readlines()

 # Find the start of the list.
 a__												= 0
 while not lines[start_index].startswith(_start_prompt       ):
  start_index += 1
 start_index += 1

 a__												= start_index
 # Update the lines in the model list.
 while not lines[index].startswith(_end_prompt       ):
  if lines[index].startswith('''1.'''       ):
   a__												= lines[index].replace(
       '''https://huggingface.co/docs/transformers/main/model_doc'''							,  '''https://huggingface.co/docs/transformers/model_doc'''							,  )
  index += 1

 with open(__UpperCAmelCase							,  '''w'''							,  encoding='''utf-8'''							,  newline='''\n'''       ) as f:
  f.writelines(__UpperCAmelCase       )






def     __a					(		):
 with open(REPLACE_FILES['''init''']							,  '''r'''       ) as f:
  a__												= f.read()
 a__												= REPLACE_PATTERNS['''init'''][0].search(__UpperCAmelCase       ).groups()[0]
 return packaging.version.parse(__UpperCAmelCase       )






def     __a					(		__UpperCAmelCase=False       ):
 a__												= get_version()
 if patch and default_version.is_devrelease:
  raise ValueError('''Can\'t create a patch version from the dev branch, checkout a released version!'''       )
 if default_version.is_devrelease:
  a__												= default_version.base_version
 elif patch:
  a__												= f"{default_version.major}.{default_version.minor}.{default_version.micro + 1}"
 else:
  a__												= f"{default_version.major}.{default_version.minor + 1}.0"

 # Now let's ask nicely if that's the right one.
 a__												= input(f"Which version are you releasing? [{default_version}]"       )
 if len(__UpperCAmelCase       ) == 0:
  a__												= default_version

 print(f"Updating version to {version}."       )
 global_version_update(__UpperCAmelCase							,  patch=__UpperCAmelCase       )
 if not patch:
  print('''Cleaning main README, don\'t forget to run `make fix-copies`.'''       )
  clean_main_ref_in_model_list()






def     __a					(		):
 a__												= get_version()
 a__												= f"{current_version.major}.{current_version.minor + 1}.0.dev0"
 a__												= current_version.base_version

 # Check with the user we got that right.
 a__												= input(f"Which version are we developing now? [{dev_version}]"       )
 if len(__UpperCAmelCase       ) == 0:
  a__												= dev_version

 print(f"Updating version to {version}."       )
 global_version_update(__UpperCAmelCase       )
 print('''Cleaning main README, don\'t forget to run `make fix-copies`.'''       )
 clean_main_ref_in_model_list()


if __name__ == "__main__":
     a_      :      Dict    =     argparse.ArgumentParser()
     parser.add_argument('--post_release', action='store_true', help='Whether this is pre or post release.')
     parser.add_argument('--patch', action='store_true', help='Whether or not this is a patch release.')
     a_      :      Any    =     parser.parse_args()
     if not args.post_release:
          pre_release_work(patch=args.patch)
     elif args.patch:
          print('Nothing to do after a patch :-)')
     else:
          post_release_work()










from typing import Dict, List, Optional, Union

import numpy as np

from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy


a_      :      Optional[int]    =     logging.get_logger(__name__)
class    __UpperCamelCase			(       _lowercase				):

 """simple docstring"""



 def __init__(	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      ) ->			int:
  a__												= feature_size
  a__												= sampling_rate
  a__												= padding_value

  a__												= kwargs.pop('''padding_side'''		,				'''right'''      )
  a__												= kwargs.pop('''return_attention_mask'''		,				SCREAMING_SNAKE_CASE      )

  super().__init__(**SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE = True		,				SCREAMING_SNAKE_CASE = None		,				SCREAMING_SNAKE_CASE = False		,				SCREAMING_SNAKE_CASE = None		,				SCREAMING_SNAKE_CASE = None		,				SCREAMING_SNAKE_CASE = None		,				) ->			BatchFeature:
  # If we have a list of dicts, let's convert it in a dict of lists
  # We do this to allow using this method as a collate_fn function in PyTorch Dataloader
  if isinstance(SCREAMING_SNAKE_CASE		,				(list, tuple)      ) and isinstance(processed_features[0]		,				(dict, BatchFeature)      ):
   a__												= {
       key: [example[key] for example in processed_features] for key in processed_features[0].keys()
   }

  # The model's main input name, usually `input_values`, has be passed for padding
  if self.model_input_names[0] not in processed_features:
   raise ValueError(
       '''You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`'''
       f" to this method that includes {self.model_input_names[0]}, but you provided"
       f" {list(processed_features.keys()      )}"      )

  a__												= processed_features[self.model_input_names[0]]
  a__												= (
      return_attention_mask if return_attention_mask is not None else self.return_attention_mask
  )

  if len(SCREAMING_SNAKE_CASE      ) == 0:
   if return_attention_mask:
    a__												= []
   return processed_features

  # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays
  # and rebuild them afterwards if no return_tensors is specified
  # Note that we lose the specific device the tensor may be on for PyTorch

  a__												= required_input[0]
  if isinstance(SCREAMING_SNAKE_CASE		,				(list, tuple)      ):
   # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element.
   a__												= 0
   while len(required_input[index]      ) == 0:
    index += 1
   if index < len(SCREAMING_SNAKE_CASE      ):
    a__												= required_input[index][0]

  if return_tensors is None:
   if is_tf_tensor(SCREAMING_SNAKE_CASE      ):
    a__												= '''tf'''
   elif is_torch_tensor(SCREAMING_SNAKE_CASE      ):
    a__												= '''pt'''
   elif isinstance(SCREAMING_SNAKE_CASE		,				(int, float, list, tuple, np.ndarray)      ):
    a__												= '''np'''
   else:
    raise ValueError(
        f"type of {first_element} unknown: {type(SCREAMING_SNAKE_CASE      )}. "
        '''Should be one of a python, numpy, pytorch or tensorflow object.'''      )

  for key, value in processed_features.items():
   if isinstance(value[0]		,				(int, float)      ):
    a__												= to_numpy(SCREAMING_SNAKE_CASE      )
   else:
    a__												= [to_numpy(SCREAMING_SNAKE_CASE      ) for v in value]

        # Convert padding_strategy in PaddingStrategy
  a__												= self._get_padding_strategies(padding=SCREAMING_SNAKE_CASE		,				max_length=SCREAMING_SNAKE_CASE      )

  a__												= processed_features[self.model_input_names[0]]

  a__												= len(SCREAMING_SNAKE_CASE      )
  if not all(len(SCREAMING_SNAKE_CASE      ) == batch_size for v in processed_features.values()      ):
   raise ValueError('''Some items in the output dictionary have a different batch size than others.'''      )

  a__												= []
  for i in range(SCREAMING_SNAKE_CASE      ):
   a__												= {k: v[i] for k, v in processed_features.items()}
   # truncation
   a__												= self._truncate(
       SCREAMING_SNAKE_CASE		,				max_length=SCREAMING_SNAKE_CASE		,				pad_to_multiple_of=SCREAMING_SNAKE_CASE		,				truncation=SCREAMING_SNAKE_CASE		,				)
   truncated_inputs.append(SCREAMING_SNAKE_CASE      )

  if padding_strategy == PaddingStrategy.LONGEST:
   # make sure that `max_length` cannot be longer than the longest truncated length
   a__												= max(len(input_slice[self.model_input_names[0]]      ) for input_slice in truncated_inputs      )
   a__												= PaddingStrategy.MAX_LENGTH

  a__												= {}
  for i in range(SCREAMING_SNAKE_CASE      ):
   # padding
   a__												= self._pad(
       truncated_inputs[i]		,				max_length=SCREAMING_SNAKE_CASE		,				padding_strategy=SCREAMING_SNAKE_CASE		,				pad_to_multiple_of=SCREAMING_SNAKE_CASE		,				return_attention_mask=SCREAMING_SNAKE_CASE		,				)

   for key, value in outputs.items():
    if key not in batch_outputs:
     a__												= []
    if value.dtype is np.dtype(np.floataa      ):
     a__												= value.astype(np.floataa      )
    batch_outputs[key].append(SCREAMING_SNAKE_CASE      )

  return BatchFeature(SCREAMING_SNAKE_CASE		,				tensor_type=SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE = None		,				SCREAMING_SNAKE_CASE = PaddingStrategy.DO_NOT_PAD		,				SCREAMING_SNAKE_CASE = None		,				SCREAMING_SNAKE_CASE = None		,				) ->			dict:
  a__												= processed_features[self.model_input_names[0]]

  if padding_strategy == PaddingStrategy.LONGEST:
   a__												= len(SCREAMING_SNAKE_CASE      )

  if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
   a__												= ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

  a__												= padding_strategy != PaddingStrategy.DO_NOT_PAD and len(SCREAMING_SNAKE_CASE      ) < max_length

  if return_attention_mask and "attention_mask" not in processed_features:
   a__												= np.ones(len(SCREAMING_SNAKE_CASE      )		,				dtype=np.intaa      )

  if needs_to_be_padded:
   a__												= max_length - len(SCREAMING_SNAKE_CASE      )
   if self.padding_side == "right":
    if return_attention_mask:
     a__												= np.pad(
         processed_features['''attention_mask''']		,				(0, difference)      )
    a__												= ((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference)
    a__												= np.pad(
        SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				'''constant'''		,				constant_values=self.padding_value      )
   elif self.padding_side == "left":
    if return_attention_mask:
     a__												= np.pad(
         processed_features['''attention_mask''']		,				(difference, 0)      )
    a__												= ((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0)
    a__												= np.pad(
        SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				'''constant'''		,				constant_values=self.padding_value      )
   else:
    raise ValueError('''Invalid padding strategy:''' + str(self.padding_side      )      )

  return processed_features

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE = None		,				SCREAMING_SNAKE_CASE = None		,				SCREAMING_SNAKE_CASE = None		,				) ->			Optional[Any]:
  if not truncation:
   return processed_features
  elif truncation and max_length is None:
   raise ValueError('''When setting ``truncation=True``, make sure that ``max_length`` is defined.'''      )

  a__												= processed_features[self.model_input_names[0]]

  # find `max_length` that fits `pad_to_multiple_of`
  if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
   a__												= ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

  a__												= len(SCREAMING_SNAKE_CASE      ) > max_length

  if needs_to_be_truncated:
   a__												= processed_features[self.model_input_names[0]][:max_length]
   if "attention_mask" in processed_features:
    a__												= processed_features['''attention_mask'''][:max_length]

  return processed_features



 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE=False		,				SCREAMING_SNAKE_CASE=None      ) ->			Optional[Any]:

  # Get padding strategy
  if padding is not False:
   if padding is True:
    a__												= PaddingStrategy.LONGEST  # Default to pad to the longest sequence in the batch
   elif not isinstance(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      ):
    a__												= PaddingStrategy(SCREAMING_SNAKE_CASE      )
   elif isinstance(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      ):
    a__												= padding
  else:
   a__												= PaddingStrategy.DO_NOT_PAD

  # Set max length if needed
  if max_length is None:
   if padding_strategy == PaddingStrategy.MAX_LENGTH:
    raise ValueError(
        f"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined"      )

        # Test if we have a padding value
  if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None):
   raise ValueError(
       '''Asking to pad but the feature_extractor does not have a padding value. Please select a value to use'''
       ''' as `padding_value`. For example: `feature_extractor.padding_value = 0.0`.'''      )

  return padding_strategy





