


from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast
from ...utils import logging


if TYPE_CHECKING:
			from ...feature_extraction_utils import FeatureExtractionMixin
			from ...tokenization_utils_base import PreTrainedTokenizerBase
			from ...utils import TensorType

snake_case_						:			Tuple													=							logging.get_logger(__name__)

snake_case_						:			Tuple													=							{
    '''openai/whisper-base''': '''https://huggingface.co/openai/whisper-base/resolve/main/config.json''',
}

# fmt: off
snake_case_						:			Any													=							[
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 3_57, 3_66, 4_38, 5_32, 6_85,
    7_05, 7_96, 9_30, 10_58, 12_20, 12_67, 12_79, 13_03, 13_43, 13_77,
    13_91, 16_35, 17_82, 18_75, 21_62, 23_61, 24_88, 34_67, 40_08, 42_11,
    46_00, 48_08, 52_99, 58_55, 63_29, 72_03, 96_09, 99_59, 1_05_63, 1_07_86,
    1_14_20, 1_17_09, 1_19_07, 1_31_63, 1_36_97, 1_37_00, 1_48_08, 1_53_06, 1_64_10, 1_67_91,
    1_79_92, 1_92_03, 1_95_10, 2_07_24, 2_23_05, 2_29_35, 2_70_07, 3_01_09, 3_04_20, 3_34_09,
    3_49_49, 4_02_83, 4_04_93, 4_05_49, 4_72_82, 4_91_46, 5_02_57, 5_03_59, 5_03_60, 5_03_61
]
snake_case_						:			Tuple													=							[
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 3_59, 5_03, 5_22, 5_42, 8_73,
    8_93, 9_02, 9_18, 9_22, 9_31, 13_50, 18_53, 19_82, 24_60, 26_27,
    32_46, 32_53, 32_68, 35_36, 38_46, 39_61, 41_83, 46_67, 65_85, 66_47,
    72_73, 90_61, 93_83, 1_04_28, 1_09_29, 1_19_38, 1_20_33, 1_23_31, 1_25_62, 1_37_93,
    1_41_57, 1_46_35, 1_52_65, 1_56_18, 1_65_53, 1_66_04, 1_83_62, 1_89_56, 2_00_75, 2_16_75,
    2_25_20, 2_61_30, 2_61_61, 2_64_35, 2_82_79, 2_94_64, 3_16_50, 3_23_02, 3_24_70, 3_68_65,
    4_28_63, 4_74_25, 4_98_70, 5_02_54, 5_02_58, 5_03_60, 5_03_61, 5_03_62
]

class 						A__							(		_UpperCAmelCase  ):
						UpperCAmelCase             =						'whisper'
						UpperCAmelCase             =						['past_key_values']
						UpperCAmelCase             =						{'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}




						def __init__(       self					:      int				,    _a					:      List[str]=5_1865				,    _a					:      Optional[Any]=80				,    _a					:      Dict=6				,    _a					:      List[str]=4				,    _a					:      List[Any]=6				,    _a					:      Optional[Any]=4				,    _a					:      Any=1536				,    _a					:      List[Any]=1536				,    _a					:      int=0.0				,    _a					:      Union[str, Any]=0.0				,    _a					:      Tuple=5_0257				,    _a					:      Optional[int]=True				,    _a					:      Any=True				,    _a					:      int="gelu"				,    _a					:      List[str]=256				,    _a					:      str=0.0				,    _a					:      Dict=0.0				,    _a					:      Optional[Any]=0.0				,    _a					:      int=0.02				,    _a					:      int=False				,    _a					:      List[Any]=1500				,    _a					:      Union[str, Any]=448				,    _a					:      int=5_0256				,    _a					:      List[str]=5_0256				,    _a					:      Tuple=5_0256				,    _a					:      int=None				,    _a					:      Optional[Any]=[220, 5_0256]				,    _a					:      Any=False				,    _a					:      Tuple=256				,    _a					:      Optional[int]=False				,    _a					:      List[str]=0.05				,    _a					:      Optional[int]=10				,    _a					:      str=2				,    _a					:      List[Any]=0.0				,    _a					:      Dict=10				,    _a					:      List[Any]=0				,    _a					:      Union[str, Any]=7				,    **_a					:      List[str]				,    )							->     Any:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =vocab_size
										_SCREAMING_SNAKE_CASE			  =num_mel_bins
										_SCREAMING_SNAKE_CASE			  =d_model
										_SCREAMING_SNAKE_CASE			  =encoder_layers
										_SCREAMING_SNAKE_CASE			  =encoder_attention_heads
										_SCREAMING_SNAKE_CASE			  =decoder_layers
										_SCREAMING_SNAKE_CASE			  =decoder_attention_heads
										_SCREAMING_SNAKE_CASE			  =decoder_ffn_dim
										_SCREAMING_SNAKE_CASE			  =encoder_ffn_dim
										_SCREAMING_SNAKE_CASE			  =dropout
										_SCREAMING_SNAKE_CASE			  =attention_dropout
										_SCREAMING_SNAKE_CASE			  =activation_dropout
										_SCREAMING_SNAKE_CASE			  =activation_function
										_SCREAMING_SNAKE_CASE			  =init_std
										_SCREAMING_SNAKE_CASE			  =encoder_layerdrop
										_SCREAMING_SNAKE_CASE			  =decoder_layerdrop
										_SCREAMING_SNAKE_CASE			  =use_cache
										_SCREAMING_SNAKE_CASE			  =encoder_layers
										_SCREAMING_SNAKE_CASE			  =scale_embedding  # scale factor will be sqrt(d_model) if True
										_SCREAMING_SNAKE_CASE			  =max_source_positions
										_SCREAMING_SNAKE_CASE			  =max_target_positions

										# Audio Classification-specific parameters. Feel free to ignore for other classes.
										_SCREAMING_SNAKE_CASE			  =classifier_proj_size
										_SCREAMING_SNAKE_CASE			  =use_weighted_layer_sum

										# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
										_SCREAMING_SNAKE_CASE			  =apply_spec_augment
										_SCREAMING_SNAKE_CASE			  =mask_time_prob
										_SCREAMING_SNAKE_CASE			  =mask_time_length
										_SCREAMING_SNAKE_CASE			  =mask_time_min_masks
										_SCREAMING_SNAKE_CASE			  =mask_feature_prob
										_SCREAMING_SNAKE_CASE			  =mask_feature_length
										_SCREAMING_SNAKE_CASE			  =mask_feature_min_masks

										_SCREAMING_SNAKE_CASE			  =median_filter_width

										super().__init__(
										    pad_token_id=__UpperCamelCase				,    bos_token_id=__UpperCamelCase				,    eos_token_id=__UpperCamelCase				,    is_encoder_decoder=__UpperCamelCase				,    decoder_start_token_id=__UpperCamelCase				,    suppress_tokens=__UpperCamelCase				,    begin_suppress_tokens=__UpperCamelCase				,    **__UpperCamelCase				,    )

class 						A__							(		_UpperCAmelCase  ):




						@property
						def 							__UpperCamelCase				(       self					:      Union[str, Any]  )							->     Mapping[str, Mapping[int, str]]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =OrderedDict(
										    [
										        ('''input_features''', {0: '''batch''', 1: '''feature_size''', 2: '''encoder_sequence'''}),
										    ]  )
										if self.use_past:
														_SCREAMING_SNAKE_CASE			  ={0: '''batch'''}
										else:
														_SCREAMING_SNAKE_CASE			  ={0: '''batch''', 1: '''decoder_sequence'''}

										if self.use_past:
														self.fill_with_past_key_values_(__UpperCamelCase				,    direction='''inputs'''  )

										return common_inputs




						def 							__UpperCamelCase				(       self					:      Tuple				,    _a					:      Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"]				,    _a					:      int = -1				,    _a					:      int = -1				,    _a					:      bool = False				,    _a					:      Optional["TensorType"] = None				,    _a					:      int = 2_2050				,    _a					:      float = 5.0				,    _a					:      int = 220				,    )							->     Mapping[str, Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =OrderedDict()
										_SCREAMING_SNAKE_CASE			  =OnnxConfig.generate_dummy_inputs(
										    self				,    preprocessor=preprocessor.feature_extractor				,    batch_size=__UpperCamelCase				,    framework=__UpperCamelCase				,    sampling_rate=__UpperCamelCase				,    time_duration=__UpperCamelCase				,    frequency=__UpperCamelCase				,    )
										_SCREAMING_SNAKE_CASE			  =encoder_inputs['''input_features'''].shape[2]
										_SCREAMING_SNAKE_CASE			  =encoder_sequence_length // 2 if self.use_past else seq_length

										_SCREAMING_SNAKE_CASE			  =super().generate_dummy_inputs(
										    preprocessor.tokenizer				,    __UpperCamelCase				,    __UpperCamelCase				,    __UpperCamelCase				,    __UpperCamelCase  )

										_SCREAMING_SNAKE_CASE			  =encoder_inputs.pop('''input_features'''  )
										_SCREAMING_SNAKE_CASE			  =decoder_inputs.pop('''decoder_input_ids'''  )

										if "past_key_values" in decoder_inputs:
														_SCREAMING_SNAKE_CASE			  =decoder_inputs.pop('''past_key_values'''  )

										return dummy_inputs




						@property
						def 							__UpperCamelCase				(       self					:      Union[str, Any]  )							->     float:


										"""simple docstring"""





										return 1E-3



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

							snake_case_						:			str													=							True
except ImportError:
							snake_case_						:			Optional[Any]													=							False

snake_case_						:			int													=							logging.get_logger(__name__)  # pylint: disable=invalid-name







def 						lowerCamelCase(   a__):
				return AddNewModelCommand(args.testing	,args.testing_file	,path=args.path)


class 						A__							(		UpperCamelCase__  ):




						@staticmethod
						def 							__UpperCamelCase				(       _a					:      ArgumentParser  )							->     int:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =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					:      List[str]				,    _a					:      bool				,    _a					:      str				,    _a					:      Any=None				,    *_a					:      Union[str, Any]  )							->     Any:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =testing
										_SCREAMING_SNAKE_CASE			  =testing_file
										_SCREAMING_SNAKE_CASE			  =path




						def 							__UpperCamelCase				(       self					:      List[Any]  )							->     int:


										"""simple docstring"""





										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
										_SCREAMING_SNAKE_CASE			  =[directory for directory in os.listdir() if '''cookiecutter-template-''' == directory[:22]]
										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.'''  )

										_SCREAMING_SNAKE_CASE			  =(
										    Path(_a  ).parent.parent.parent.parent if self._path is None else Path(self._path  ).parent.parent
										)
										_SCREAMING_SNAKE_CASE			  =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:
																		_SCREAMING_SNAKE_CASE			  =json.load(_a  )

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

										_SCREAMING_SNAKE_CASE			  =[directory for directory in os.listdir() if '''cookiecutter-template-''' in directory[:22]][0]

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

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

										_SCREAMING_SNAKE_CASE			  ='''PyTorch''' in generate_tensorflow_pytorch_and_flax
										_SCREAMING_SNAKE_CASE			  ='''TensorFlow''' in generate_tensorflow_pytorch_and_flax
										_SCREAMING_SNAKE_CASE			  ='''Flax''' in generate_tensorflow_pytorch_and_flax

										_SCREAMING_SNAKE_CASE			  =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					:      int  ):
														with open(_a				,    '''r'''  ) as f:
																		_SCREAMING_SNAKE_CASE			  =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
														_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =mkstemp()
														_SCREAMING_SNAKE_CASE			  =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:
																														_SCREAMING_SNAKE_CASE			  =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					:      str  ):
														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					:      Any  ):
														with open(_a  ) as datafile:
																		_SCREAMING_SNAKE_CASE			  =[]
																		_SCREAMING_SNAKE_CASE			  =False
																		_SCREAMING_SNAKE_CASE			  =False
																		for line in datafile:
																						if "# To replace in: " in line and "##" not in line:
																										_SCREAMING_SNAKE_CASE			  =line.split('''"'''  )[1]
																										_SCREAMING_SNAKE_CASE			  =skip_units(_a  )
																						elif "# Below: " in line and "##" not in line:
																										_SCREAMING_SNAKE_CASE			  =line.split('''"'''  )[1]
																										_SCREAMING_SNAKE_CASE			  =skip_units(_a  )
																						elif "# End." in line and "##" not in line:
																										if not skip_file and not skip_snippet:
																														replace(_a				,    _a				,    _a  )

																										_SCREAMING_SNAKE_CASE			  =[]
																						elif "# Replace with" in line and "##" not in line:
																										_SCREAMING_SNAKE_CASE			  =[]
																						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  )