





'''simple docstring'''
import json
import os
import shutil
import warnings
from argparse import ArgumentParser, Namespace
from pathlib import Path
from typing import List

from ..utils import logging
from . import BaseTransformersCLICommand


try:
    from cookiecutter.main import cookiecutter

    a__							:							str =True
except ImportError:
    a__							:							Tuple =False

a__							:							int =logging.get_logger(__name__)  # pylint: disable=invalid-name
def 				lowercase__   (   __lowercase						:  Namespace     ) ->      List[str]:



       """simple docstring"""






       return AddNewModelCommand(args.testing				,  args.testing_file				,  path=args.path     )


class      snake_case     (    __lowerCamelCase       ):




 """simple docstring"""



 @staticmethod
 def        _lowerCamelCase					(       __A       :							ArgumentParser	):
        __UpperCamelCase       =  parser.add_parser('add-new-model'	)
        add_new_model_parser.add_argument('--testing'							,	action='store_true'							,	help='If in testing mode.'	)
        add_new_model_parser.add_argument('--testing_file'							,	type=__A							,	help='Configuration file on which to run.'	)
        add_new_model_parser.add_argument(
            '--path'							,	type=__A							,	help='Path to cookiecutter. Should only be used for testing purposes.'	)
        add_new_model_parser.set_defaults(func=__A	)






 def __init__(       self       :							Dict							,	__A       :							bool							,	__A       :							str							,	__A       :							Any=None							,	*__A       :							Optional[int]	):
        __UpperCamelCase       =  testing
        __UpperCamelCase       =  testing_file
        __UpperCamelCase       =  path






 def        _lowerCamelCase					(       self       :							str	):
        warnings.warn(
            'The command `transformers-cli add-new-model` is deprecated and will be removed in v5 of Transformers. '
            'It is not actively maintained anymore, so might give a result that won\'t pass all tests and quality '
            'checks, you should use `transformers-cli add-new-model-like` instead.'	)
        if not _has_cookiecutter:
               raise ImportError(
                   'Model creation dependencies are required to use the `add_new_model` command. Install them by running '
                   'the following at the root of your `transformers` clone:\n\n\t$ pip install -e .[modelcreation]\n'	)
        # Ensure that there is no other `cookiecutter-template-xxx` directory in the current working directory
        __UpperCamelCase       =  [directory for directory in os.listdir() if 'cookiecutter-template-' == directory[:2_2]]
        if len(__A	) > 0:
               raise ValueError(
                   'Several directories starting with `cookiecutter-template-` in current working directory. '
                   'Please clean your directory by removing all folders starting with `cookiecutter-template-` or '
                   'change your working directory.'	)

        __UpperCamelCase       =  (
            Path(__A	).parent.parent.parent.parent if self._path is None else Path(self._path	).parent.parent
        )
        __UpperCamelCase       =  path_to_transformer_root / 'templates' / 'adding_a_new_model'

        # Execute cookiecutter
        if not self._testing:
               cookiecutter(str(__A	)	)
        else:
               with open(self._testing_file							,	'r'	) as configuration_file:
                      __UpperCamelCase       =  json.load(__A	)

               cookiecutter(
                   str(path_to_cookiecutter if self._path is None else self._path	)							,	no_input=__A							,	extra_context=__A							,	)

        __UpperCamelCase       =  [directory for directory in os.listdir() if 'cookiecutter-template-' in directory[:2_2]][0]

        # Retrieve configuration
        with open(directory + '/configuration.json'							,	'r'	) as configuration_file:
               __UpperCamelCase       =  json.load(__A	)

        __UpperCamelCase       =  configuration['lowercase_modelname']
        __UpperCamelCase       =  configuration['generate_tensorflow_pytorch_and_flax']
        os.remove(f'''{directory}/configuration.json'''	)

        __UpperCamelCase       =  'PyTorch' in generate_tensorflow_pytorch_and_flax
        __UpperCamelCase       =  'TensorFlow' in generate_tensorflow_pytorch_and_flax
        __UpperCamelCase       =  'Flax' in generate_tensorflow_pytorch_and_flax

        __UpperCamelCase       =  f'''{path_to_transformer_root}/src/transformers/models/{lowercase_model_name}'''
        os.makedirs(__A							,	exist_ok=__A	)
        os.makedirs(f'''{path_to_transformer_root}/tests/models/{lowercase_model_name}'''							,	exist_ok=__A	)

        # Tests require submodules as they have parent imports
        with open(f'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/__init__.py'''							,	'w'	):
               pass

        shutil.move(
            f'''{directory}/__init__.py'''							,	f'''{model_dir}/__init__.py'''							,	)
        shutil.move(
            f'''{directory}/configuration_{lowercase_model_name}.py'''							,	f'''{model_dir}/configuration_{lowercase_model_name}.py'''							,	)

        def remove_copy_lines(__A       :							Any	):
               with open(__A							,	'r'	) as f:
                      __UpperCamelCase       =  f.readlines()
               with open(__A							,	'w'	) as f:
                      for line in lines:
                             if "# Copied from transformers." not in line:
                                    f.write(__A	)

        if output_pytorch:
               if not self._testing:
                      remove_copy_lines(f'''{directory}/modeling_{lowercase_model_name}.py'''	)

               shutil.move(
                   f'''{directory}/modeling_{lowercase_model_name}.py'''							,	f'''{model_dir}/modeling_{lowercase_model_name}.py'''							,	)

               shutil.move(
                   f'''{directory}/test_modeling_{lowercase_model_name}.py'''							,	f'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_{lowercase_model_name}.py'''							,	)
        else:
               os.remove(f'''{directory}/modeling_{lowercase_model_name}.py'''	)
               os.remove(f'''{directory}/test_modeling_{lowercase_model_name}.py'''	)

        if output_tensorflow:
               if not self._testing:
                      remove_copy_lines(f'''{directory}/modeling_tf_{lowercase_model_name}.py'''	)

               shutil.move(
                   f'''{directory}/modeling_tf_{lowercase_model_name}.py'''							,	f'''{model_dir}/modeling_tf_{lowercase_model_name}.py'''							,	)

               shutil.move(
                   f'''{directory}/test_modeling_tf_{lowercase_model_name}.py'''							,	f'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_tf_{lowercase_model_name}.py'''							,	)
        else:
               os.remove(f'''{directory}/modeling_tf_{lowercase_model_name}.py'''	)
               os.remove(f'''{directory}/test_modeling_tf_{lowercase_model_name}.py'''	)

        if output_flax:
               if not self._testing:
                      remove_copy_lines(f'''{directory}/modeling_flax_{lowercase_model_name}.py'''	)

               shutil.move(
                   f'''{directory}/modeling_flax_{lowercase_model_name}.py'''							,	f'''{model_dir}/modeling_flax_{lowercase_model_name}.py'''							,	)

               shutil.move(
                   f'''{directory}/test_modeling_flax_{lowercase_model_name}.py'''							,	f'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_flax_{lowercase_model_name}.py'''							,	)
        else:
               os.remove(f'''{directory}/modeling_flax_{lowercase_model_name}.py'''	)
               os.remove(f'''{directory}/test_modeling_flax_{lowercase_model_name}.py'''	)

        shutil.move(
            f'''{directory}/{lowercase_model_name}.md'''							,	f'''{path_to_transformer_root}/docs/source/en/model_doc/{lowercase_model_name}.md'''							,	)

        shutil.move(
            f'''{directory}/tokenization_{lowercase_model_name}.py'''							,	f'''{model_dir}/tokenization_{lowercase_model_name}.py'''							,	)

        shutil.move(
            f'''{directory}/tokenization_fast_{lowercase_model_name}.py'''							,	f'''{model_dir}/tokenization_{lowercase_model_name}_fast.py'''							,	)

        from os import fdopen, remove
        from shutil import copymode, move
        from tempfile import mkstemp

        def replace(__A       :							str							,	__A       :							str							,	__A       :							List[str]	):
               # Create temp file
               __UpperCamelCase       ,						__UpperCamelCase       =  mkstemp()
               __UpperCamelCase       =  False
               with fdopen(__A							,	'w'	) as new_file:
                      with open(__A	) as old_file:
                             for line in old_file:
                                    new_file.write(__A	)
                                    if line_to_copy_below in line:
                                           __UpperCamelCase       =  True
                                           for line_to_copy in lines_to_copy:
                                                  new_file.write(__A	)

               if not line_found:
                      raise ValueError(f'''Line {line_to_copy_below} was not found in file.'''	)

               # Copy the file permissions from the old file to the new file
               copymode(__A							,	__A	)
               # Remove original file
               remove(__A	)
               # Move new file
               move(__A							,	__A	)

        def skip_units(__A       :							Dict	):
               return (
                   ("generating PyTorch" in line and not output_pytorch)
                   or ("generating TensorFlow" in line and not output_tensorflow)
                   or ("generating Flax" in line and not output_flax)
               )

        def replace_in_files(__A       :							Optional[Any]	):
               with open(__A	) as datafile:
                      __UpperCamelCase       =  []
                      __UpperCamelCase       =  False
                      __UpperCamelCase       =  False
                      for line in datafile:
                             if "# To replace in: " in line and "##" not in line:
                                    __UpperCamelCase       =  line.split('"'	)[1]
                                    __UpperCamelCase       =  skip_units(__A	)
                             elif "# Below: " in line and "##" not in line:
                                    __UpperCamelCase       =  line.split('"'	)[1]
                                    __UpperCamelCase       =  skip_units(__A	)
                             elif "# End." in line and "##" not in line:
                                    if not skip_file and not skip_snippet:
                                           replace(__A							,	__A							,	__A	)

                                    __UpperCamelCase       =  []
                             elif "# Replace with" in line and "##" not in line:
                                    __UpperCamelCase       =  []
                             elif "##" not in line:
                                    lines_to_copy.append(__A	)

               remove(__A	)

        replace_in_files(f'''{directory}/to_replace_{lowercase_model_name}.py'''	)
        os.rmdir(__A	)







'''simple docstring'''
from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

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


a__							:							str =logging.get_logger(__name__)

class      snake_case     (    __lowerCamelCase       ):




 """simple docstring"""



 SCREAMING_SNAKE_CASE_	:					str					=["input_features", "attention_mask"]






 def __init__(       self       :							Union[str, Any]							,	__A       :							Optional[int]=8_0							,	__A       :							Tuple=1_6_0_0_0							,	__A       :							Optional[Any]=8_0							,	__A       :							Any=0.0							,	__A       :							Any=True							,	__A       :							List[str]=True							,	__A       :							str=True							,	**__A       :							List[Any]							,	):
        super().__init__(feature_size=__A							,	sampling_rate=__A							,	padding_value=__A							,	**__A	)
        __UpperCamelCase       =  num_mel_bins
        __UpperCamelCase       =  do_ceptral_normalize
        __UpperCamelCase       =  normalize_means
        __UpperCamelCase       =  normalize_vars
        __UpperCamelCase       =  True






 def        _lowerCamelCase					(       self       :							Union[str, Any]							,	__A       :							np.ndarray							,	):
        __UpperCamelCase       =  waveform * (2**1_5)  # Kaldi compliance: 16-bit signed integers
        __UpperCamelCase       =  torch.from_numpy(__A	).unsqueeze(0	)
        __UpperCamelCase       =  ta_kaldi.fbank(__A							,	num_mel_bins=self.num_mel_bins							,	sample_frequency=self.sampling_rate	)
        return features.numpy()






 @staticmethod
 def        _lowerCamelCase					(       __A       :							np.ndarray							,	__A       :							int							,	__A       :							Optional[bool] = True							,	__A       :							Optional[bool] = True							,	__A       :							float = 0.0							,	):
        # make sure we normalize float32 arrays
        if normalize_means:
               __UpperCamelCase       =  x[:input_length].mean(axis=0	)
               __UpperCamelCase       =  np.subtract(__A							,	__A	)
        if normalize_vars:
               __UpperCamelCase       =  x[:input_length].std(axis=0	)
               __UpperCamelCase       =  np.divide(__A							,	__A	)

        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       :							int							,	__A       :							List[np.ndarray]							,	__A       :							Optional[np.ndarray] = None	):
        __UpperCamelCase       =  attention_mask.sum(-1	) if attention_mask is not None else [x.shape[0] for x in input_features]
        return [
            self.utterance_cmvn(__A							,	__A							,	self.normalize_means							,	self.normalize_vars							,	self.padding_value	)
            for x, n in zip(__A							,	__A	)
        ]






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

        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(__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       =  is_batched_numpy or (
            isinstance(__A							,	(list, tuple)	) and (isinstance(raw_speech[0]							,	(np.ndarray, tuple, list)	))
        )

        if is_batched:
               __UpperCamelCase       =  [np.asarray(__A							,	dtype=np.floataa	) for speech in raw_speech]
        elif not is_batched and not isinstance(__A							,	np.ndarray	):
               __UpperCamelCase       =  np.asarray(__A							,	dtype=np.floataa	)
        elif isinstance(__A							,	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_fbank_features(__A	) for waveform in raw_speech]

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

        __UpperCamelCase       =  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       =  padded_inputs.get('input_features'	)
        if isinstance(input_features[0]							,	__A	):
               __UpperCamelCase       =  [np.asarray(__A							,	dtype=np.floataa	) for feature in input_features]

        __UpperCamelCase       =  padded_inputs.get('attention_mask'	)
        if attention_mask is not None:
               __UpperCamelCase       =  [np.asarray(__A							,	dtype=np.intaa	) for array in attention_mask]

        # Utterance-level cepstral mean and variance normalization
        if self.do_ceptral_normalize:
               __UpperCamelCase       =  (
                   np.array(__A							,	dtype=np.intaa	)
                   if self._get_padding_strategies(__A							,	max_length=__A	) is not PaddingStrategy.DO_NOT_PAD
                   else None
               )
               __UpperCamelCase       =  self.normalize(
                   padded_inputs['input_features']							,	attention_mask=__A	)

        if return_tensors is not None:
               __UpperCamelCase       =  padded_inputs.convert_to_tensors(__A	)

        return padded_inputs
