
import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


lowercase__       =logging.get_logger(__name__)

lowercase__       ={
    'salesforce/blip2-opt-2.7b': 'https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json',
}



class 		UpperCamelCase__  (       __lowercase    ):
   _SCREAMING_SNAKE_CASE      :							Union[str, Any]	       =      "blip_2_vision_model"
   def __init__(self :					Union[str, Any]					,				snake_case_ :					Tuple=1_4_0_8					,				snake_case_ :					List[str]=6_1_4_4					,				snake_case_ :					Dict=3_9					,				snake_case_ :					Any=1_6					,				snake_case_ :					Tuple=2_2_4					,				snake_case_ :					Optional[Any]=1_4					,				snake_case_ :					Dict="gelu"					,				snake_case_ :					Any=0.0_0001					,				snake_case_ :					Union[str, Any]=0.0					,				snake_case_ :					Optional[Any]=1E-10					,				snake_case_ :					Dict=True					,				**snake_case_ :					Tuple					,				):
    super().__init__(**snake_case_   )

    __a :  int						=  hidden_size
    __a :  List[str]						=  intermediate_size
    __a :  int						=  num_hidden_layers
    __a :  str						=  num_attention_heads
    __a :  Tuple						=  patch_size
    __a :  Union[str, Any]						=  image_size
    __a :  List[str]						=  initializer_range
    __a :  Optional[int]						=  attention_dropout
    __a :  int						=  layer_norm_eps
    __a :  Optional[int]						=  hidden_act
    __a :  List[Any]						=  qkv_bias







   @classmethod
   def       lowerCAmelCase			(cls :					Tuple					,				snake_case_ :					Union[str, os.PathLike]					,				**snake_case_ :					int   ):
    cls._set_token_in_kwargs(snake_case_   )

    __a				,       __a :  Tuple						=  cls.get_config_dict(snake_case_					,				**snake_case_   )

    # get the vision config dict if we are loading from Blip2Config
    if config_dict.get('''model_type'''   ) == "blip-2":
     __a :  Union[str, Any]						=  config_dict['''vision_config''']

    if "model_type" in config_dict and hasattr(cls					,				'''model_type'''   ) and config_dict["model_type"] != cls.model_type:
     logger.warning(
         f"You are using a model of type {config_dict['model_type']} to instantiate a model of type "
         f"{cls.model_type}. This is not supported for all configurations of models and can yield errors."   )

    return cls.from_dict(snake_case_					,				**snake_case_   )





class 		UpperCamelCase__  (       __lowercase    ):
   _SCREAMING_SNAKE_CASE      :							Union[str, Any]	       =      "blip_2_qformer"
   def __init__(self :					Dict					,				snake_case_ :					Optional[Any]=3_0_5_2_2					,				snake_case_ :					Tuple=7_6_8					,				snake_case_ :					Optional[int]=1_2					,				snake_case_ :					int=1_2					,				snake_case_ :					Any=3_0_7_2					,				snake_case_ :					Union[str, Any]="gelu"					,				snake_case_ :					Dict=0.1					,				snake_case_ :					List[str]=0.1					,				snake_case_ :					Any=5_1_2					,				snake_case_ :					Tuple=0.02					,				snake_case_ :					Tuple=1E-12					,				snake_case_ :					int=0					,				snake_case_ :					int="absolute"					,				snake_case_ :					Optional[Any]=2					,				snake_case_ :					List[str]=1_4_0_8					,				**snake_case_ :					str					,				):
    super().__init__(pad_token_id=snake_case_					,				**snake_case_   )

    __a :  Dict						=  vocab_size
    __a :  List[str]						=  hidden_size
    __a :  Union[str, Any]						=  num_hidden_layers
    __a :  int						=  num_attention_heads
    __a :  str						=  hidden_act
    __a :  Optional[Any]						=  intermediate_size
    __a :  Dict						=  hidden_dropout_prob
    __a :  List[str]						=  attention_probs_dropout_prob
    __a :  Dict						=  max_position_embeddings
    __a :  Optional[int]						=  initializer_range
    __a :  Optional[int]						=  layer_norm_eps
    __a :  str						=  position_embedding_type
    __a :  Optional[int]						=  cross_attention_frequency
    __a :  Any						=  encoder_hidden_size







   @classmethod
   def       lowerCAmelCase			(cls :					int					,				snake_case_ :					Union[str, os.PathLike]					,				**snake_case_ :					Tuple   ):
    cls._set_token_in_kwargs(snake_case_   )

    __a				,       __a :  Union[str, Any]						=  cls.get_config_dict(snake_case_					,				**snake_case_   )

    # get the qformer config dict if we are loading from Blip2Config
    if config_dict.get('''model_type'''   ) == "blip-2":
     __a :  Tuple						=  config_dict['''qformer_config''']

    if "model_type" in config_dict and hasattr(cls					,				'''model_type'''   ) and config_dict["model_type"] != cls.model_type:
     logger.warning(
         f"You are using a model of type {config_dict['model_type']} to instantiate a model of type "
         f"{cls.model_type}. This is not supported for all configurations of models and can yield errors."   )

    return cls.from_dict(snake_case_					,				**snake_case_   )





class 		UpperCamelCase__  (       __lowercase    ):
   _SCREAMING_SNAKE_CASE      :							Optional[Any]	       =      "blip-2"
   _SCREAMING_SNAKE_CASE      :							Any	       =      True
   def __init__(self :					Any					,				snake_case_ :					Tuple=None					,				snake_case_ :					str=None					,				snake_case_ :					Tuple=None					,				snake_case_ :					Any=3_2					,				**snake_case_ :					Dict   ):
    super().__init__(**snake_case_   )

    if vision_config is None:
     __a :  Any						=  {}
     logger.info('''vision_config is None. initializing the Blip2VisionConfig with default values.'''   )

    if qformer_config is None:
     __a :  Dict						=  {}
     logger.info('''qformer_config is None. Initializing the Blip2QFormerConfig with default values.'''   )

    if text_config is None:
     __a :  Dict						=  {}
     logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).'''   )

    __a :  Tuple						=  BlipaVisionConfig(**snake_case_   )
    __a :  int						=  BlipaQFormerConfig(**snake_case_   )
    __a :  Dict						=  text_config['''model_type'''] if '''model_type''' in text_config else '''opt'''
    __a :  Tuple						=  CONFIG_MAPPING[text_model_type](**snake_case_   )

    __a :  Optional[Any]						=  self.text_config.tie_word_embeddings
    __a :  Any						=  self.text_config.is_encoder_decoder

    __a :  Optional[int]						=  num_query_tokens
    __a :  Dict						=  self.vision_config.hidden_size
    __a :  Optional[int]						=  self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
    __a :  Dict						=  1.0
    __a :  List[Any]						=  0.02
   @classmethod
   def       lowerCAmelCase			(cls :					Optional[int]					,				snake_case_ :					BlipaVisionConfig					,				snake_case_ :					BlipaQFormerConfig					,				snake_case_ :					PretrainedConfig					,				**snake_case_ :					Optional[int]					,				):
    return cls(
        vision_config=vision_config.to_dict()					,				qformer_config=qformer_config.to_dict()					,				text_config=text_config.to_dict()					,				**snake_case_					,				)







   def       lowerCAmelCase			(self :					Optional[Any]   ):
    __a :  Optional[Any]						=  copy.deepcopy(self.__dict__   )
    __a :  Any						=  self.vision_config.to_dict()
    __a :  Tuple						=  self.qformer_config.to_dict()
    __a :  int						=  self.text_config.to_dict()
    __a :  Tuple						=  self.__class__.model_type
    return output


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

      lowercase__       =True
except ImportError:
      lowercase__       =False

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




def    __UpperCamelCase     (				lowerCAmelCase__							:				Namespace  ):
 return AddNewModelCommand(args.testing  ,   args.testing_file  ,   path=args.path  )



class 		UpperCamelCase__  (       __lowercase    ):
   @staticmethod
   def       lowerCAmelCase			(snake_case_ :					ArgumentParser   ):
    __a :  List[Any]						=  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=snake_case_					,				help='''Configuration file on which to run.'''   )
    add_new_model_parser.add_argument(
        '''--path'''					,				type=snake_case_					,				help='''Path to cookiecutter. Should only be used for testing purposes.'''   )
    add_new_model_parser.set_defaults(func=snake_case_   )
   def __init__(self :					Dict					,				snake_case_ :					bool					,				snake_case_ :					str					,				snake_case_ :					Dict=None					,				*snake_case_ :					Optional[Any]   ):
    __a :  Union[str, Any]						=  testing
    __a :  List[Any]						=  testing_file
    __a :  Any						=  path







   def       lowerCAmelCase			(self :					int   ):
    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
    __a :  Union[str, Any]						=  [directory for directory in os.listdir() if '''cookiecutter-template-''' == directory[:2_2]]
    if len(snake_case_   ) > 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.'''   )

    __a :  Union[str, Any]						=  (
        Path(snake_case_   ).parent.parent.parent.parent if self._path is None else Path(self._path   ).parent.parent
    )
    __a :  Union[str, Any]						=  path_to_transformer_root / '''templates''' / '''adding_a_new_model'''

    # Execute cookiecutter
    if not self._testing:
     cookiecutter(str(snake_case_   )   )
    else:
     with open(self._testing_file					,				'''r'''   ) as configuration_file:
      __a :  List[Any]						=  json.load(snake_case_   )

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

    __a :  List[str]						=  [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:
     __a :  Optional[Any]						=  json.load(snake_case_   )

    __a :  str						=  configuration['''lowercase_modelname''']
    __a :  int						=  configuration['''generate_tensorflow_pytorch_and_flax''']
    os.remove(f"{directory}/configuration.json"   )

    __a :  Any						=  '''PyTorch''' in generate_tensorflow_pytorch_and_flax
    __a :  Dict						=  '''TensorFlow''' in generate_tensorflow_pytorch_and_flax
    __a :  Optional[int]						=  '''Flax''' in generate_tensorflow_pytorch_and_flax

    __a :  Dict						=  f"{path_to_transformer_root}/src/transformers/models/{lowercase_model_name}"
    os.makedirs(snake_case_					,				exist_ok=snake_case_   )
    os.makedirs(f"{path_to_transformer_root}/tests/models/{lowercase_model_name}"					,				exist_ok=snake_case_   )

    # 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(snake_case_ :					Union[str, Any]   ):
     with open(snake_case_					,				'''r'''   ) as f:
      __a :  Union[str, Any]						=  f.readlines()
     with open(snake_case_					,				'''w'''   ) as f:
      for line in lines:
       if "# Copied from transformers." not in line:
        f.write(snake_case_   )

    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(snake_case_ :					str					,				snake_case_ :					str					,				snake_case_ :					List[str]   ):
     # Create temp file
     __a				,       __a :  Tuple						=  mkstemp()
     __a :  Optional[Any]						=  False
     with fdopen(snake_case_					,				'''w'''   ) as new_file:
      with open(snake_case_   ) as old_file:
       for line in old_file:
        new_file.write(snake_case_   )
        if line_to_copy_below in line:
         __a :  Tuple						=  True
         for line_to_copy in lines_to_copy:
          new_file.write(snake_case_   )

     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(snake_case_					,				snake_case_   )
     # Remove original file
     remove(snake_case_   )
     # Move new file
     move(snake_case_					,				snake_case_   )

    def skip_units(snake_case_ :					Any   ):
     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(snake_case_ :					int   ):
     with open(snake_case_   ) as datafile:
      __a :  List[Any]						=  []
      __a :  int						=  False
      __a :  Tuple						=  False
      for line in datafile:
       if "# To replace in: " in line and "##" not in line:
        __a :  Optional[Any]						=  line.split('''"'''   )[1]
        __a :  Dict						=  skip_units(snake_case_   )
       elif "# Below: " in line and "##" not in line:
        __a :  str						=  line.split('''"'''   )[1]
        __a :  Any						=  skip_units(snake_case_   )
       elif "# End." in line and "##" not in line:
        if not skip_file and not skip_snippet:
         replace(snake_case_					,				snake_case_					,				snake_case_   )

        __a :  str						=  []
       elif "# Replace with" in line and "##" not in line:
        __a :  Optional[int]						=  []
       elif "##" not in line:
        lines_to_copy.append(snake_case_   )

     remove(snake_case_   )

    replace_in_files(f"{directory}/to_replace_{lowercase_model_name}.py"   )
    os.rmdir(snake_case_   )
