"""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

       __lowerCAmelCase					: List[Any]        =True
except ImportError:
       __lowerCAmelCase					: Tuple        =False

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


def     UpperCAmelCase__ (       lowerCAmelCase__  :Namespace	)				->					List[str]:
      '''simple docstring'''


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






class 				_A		( lowerCAmelCase			):

      @staticmethod
      def        A__ (		__lowerCAmelCase		):







            """simple docstring"""



            lowercase														=			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=__lowerCAmelCase	, help="""Configuration file on which to run."""		)
            add_new_model_parser.add_argument(
                """--path"""	, type=__lowerCAmelCase	, help="""Path to cookiecutter. Should only be used for testing purposes."""		)
            add_new_model_parser.set_defaults(func=__lowerCAmelCase		)

      def __init__(		self	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase=None	, *__lowerCAmelCase		):







            """simple docstring"""



            lowercase														=			testing
            lowercase														=			testing_file
            lowercase														=			path







      def        A__ (		self		):







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

            lowercase														=			(
                Path(__lowerCAmelCase		).parent.parent.parent.parent if self._path is None else Path(self._path		).parent.parent
            )
            lowercase														=			path_to_transformer_root / """templates""" / """adding_a_new_model"""

            # Execute cookiecutter
            if not self._testing:
                  cookiecutter(str(__lowerCAmelCase		)		)
            else:
                  with open(self._testing_file	, """r"""		) as configuration_file:
                        lowercase														=			json.load(__lowerCAmelCase		)

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

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

            # Retrieve configuration
            with open(directory + """/configuration.json"""	, """r"""		) as configuration_file:
                  lowercase														=			json.load(__lowerCAmelCase		)

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

            lowercase														=			"""PyTorch""" in generate_tensorflow_pytorch_and_flax
            lowercase														=			"""TensorFlow""" in generate_tensorflow_pytorch_and_flax
            lowercase														=			"""Flax""" in generate_tensorflow_pytorch_and_flax

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

            # 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(__lowerCAmelCase		):
                  with open(__lowerCAmelCase	, """r"""		) as f:
                        lowercase														=			f.readlines()
                  with open(__lowerCAmelCase	, """w"""		) as f:
                        for line in lines:
                              if "# Copied from transformers." not in line:
                                    f.write(__lowerCAmelCase		)

            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(__lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		):
                  # Create temp file
                  lowercase							,							lowercase														=			mkstemp()
                  lowercase														=			False
                  with fdopen(__lowerCAmelCase	, """w"""		) as new_file:
                        with open(__lowerCAmelCase		) as old_file:
                              for line in old_file:
                                    new_file.write(__lowerCAmelCase		)
                                    if line_to_copy_below in line:
                                          lowercase														=			True
                                          for line_to_copy in lines_to_copy:
                                                new_file.write(__lowerCAmelCase		)

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

            def skip_units(__lowerCAmelCase		):
                  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(__lowerCAmelCase		):
                  with open(__lowerCAmelCase		) as datafile:
                        lowercase														=			[]
                        lowercase														=			False
                        lowercase														=			False
                        for line in datafile:
                              if "# To replace in: " in line and "##" not in line:
                                    lowercase														=			line.split("""\""""		)[1]
                                    lowercase														=			skip_units(__lowerCAmelCase		)
                              elif "# Below: " in line and "##" not in line:
                                    lowercase														=			line.split("""\""""		)[1]
                                    lowercase														=			skip_units(__lowerCAmelCase		)
                              elif "# End." in line and "##" not in line:
                                    if not skip_file and not skip_snippet:
                                          replace(__lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		)

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

                  remove(__lowerCAmelCase		)

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




"""simple docstring"""






import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TextClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
__lowerCAmelCase					: int        ={"""LayoutLMv2Config""", """LayoutLMv3Config"""}





@is_pipeline_test
class 				_A		( unittest.TestCase			):
      snake_case__    :     Optional[int]													=			MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
      snake_case__    :     str													=			TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

      if model_mapping is not None:
            snake_case__    :     Optional[Any]													=			{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
      if tf_model_mapping is not None:
            snake_case__    :     List[Any]													=			{
                config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
            }

      @require_torch
      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			pipeline(
                task="""text-classification"""	, model="""hf-internal-testing/tiny-random-distilbert"""	, framework="""pt"""		)

            lowercase														=			text_classifier("""This is great !"""		)
            self.assertEqual(nested_simplify(__lowerCAmelCase		)	, [{"""label""": """LABEL_0""", """score""": 0.5_0_4}]		)

            lowercase														=			text_classifier("""This is great !"""	, top_k=2		)
            self.assertEqual(
                nested_simplify(__lowerCAmelCase		)	, [{"""label""": """LABEL_0""", """score""": 0.5_0_4}, {"""label""": """LABEL_1""", """score""": 0.4_9_6}]		)

            lowercase														=			text_classifier(["""This is great !""", """This is bad"""]	, top_k=2		)
            self.assertEqual(
                nested_simplify(__lowerCAmelCase		)	, [
                    [{"""label""": """LABEL_0""", """score""": 0.5_0_4}, {"""label""": """LABEL_1""", """score""": 0.4_9_6}],
                    [{"""label""": """LABEL_0""", """score""": 0.5_0_4}, {"""label""": """LABEL_1""", """score""": 0.4_9_6}],
                ]	, )

            lowercase														=			text_classifier("""This is great !"""	, top_k=1		)
            self.assertEqual(nested_simplify(__lowerCAmelCase		)	, [{"""label""": """LABEL_0""", """score""": 0.5_0_4}]		)

            # Legacy behavior
            lowercase														=			text_classifier("""This is great !"""	, return_all_scores=__lowerCAmelCase		)
            self.assertEqual(nested_simplify(__lowerCAmelCase		)	, [{"""label""": """LABEL_0""", """score""": 0.5_0_4}]		)

            lowercase														=			text_classifier("""This is great !"""	, return_all_scores=__lowerCAmelCase		)
            self.assertEqual(
                nested_simplify(__lowerCAmelCase		)	, [[{"""label""": """LABEL_0""", """score""": 0.5_0_4}, {"""label""": """LABEL_1""", """score""": 0.4_9_6}]]		)

            lowercase														=			text_classifier(["""This is great !""", """Something else"""]	, return_all_scores=__lowerCAmelCase		)
            self.assertEqual(
                nested_simplify(__lowerCAmelCase		)	, [
                    [{"""label""": """LABEL_0""", """score""": 0.5_0_4}, {"""label""": """LABEL_1""", """score""": 0.4_9_6}],
                    [{"""label""": """LABEL_0""", """score""": 0.5_0_4}, {"""label""": """LABEL_1""", """score""": 0.4_9_6}],
                ]	, )

            lowercase														=			text_classifier(["""This is great !""", """Something else"""]	, return_all_scores=__lowerCAmelCase		)
            self.assertEqual(
                nested_simplify(__lowerCAmelCase		)	, [
                    {"""label""": """LABEL_0""", """score""": 0.5_0_4},
                    {"""label""": """LABEL_0""", """score""": 0.5_0_4},
                ]	, )

      @require_torch
      def        A__ (		self		):







            """simple docstring"""



            import torch

            lowercase														=			pipeline(
                task="""text-classification"""	, model="""hf-internal-testing/tiny-random-distilbert"""	, framework="""pt"""	, device=torch.device("""cpu"""		)	, )

            lowercase														=			text_classifier("""This is great !"""		)
            self.assertEqual(nested_simplify(__lowerCAmelCase		)	, [{"""label""": """LABEL_0""", """score""": 0.5_0_4}]		)

      @require_tf
      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			pipeline(
                task="""text-classification"""	, model="""hf-internal-testing/tiny-random-distilbert"""	, framework="""tf"""		)

            lowercase														=			text_classifier("""This is great !"""		)
            self.assertEqual(nested_simplify(__lowerCAmelCase		)	, [{"""label""": """LABEL_0""", """score""": 0.5_0_4}]		)

      @slow
      @require_torch
      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			pipeline("""text-classification"""		)

            lowercase														=			text_classifier("""This is great !"""		)
            self.assertEqual(nested_simplify(__lowerCAmelCase		)	, [{"""label""": """POSITIVE""", """score""": 1.0}]		)
            lowercase														=			text_classifier("""This is bad !"""		)
            self.assertEqual(nested_simplify(__lowerCAmelCase		)	, [{"""label""": """NEGATIVE""", """score""": 1.0}]		)
            lowercase														=			text_classifier("""Birds are a type of animal"""		)
            self.assertEqual(nested_simplify(__lowerCAmelCase		)	, [{"""label""": """POSITIVE""", """score""": 0.9_8_8}]		)

      @slow
      @require_tf
      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			pipeline("""text-classification"""	, framework="""tf"""		)

            lowercase														=			text_classifier("""This is great !"""		)
            self.assertEqual(nested_simplify(__lowerCAmelCase		)	, [{"""label""": """POSITIVE""", """score""": 1.0}]		)
            lowercase														=			text_classifier("""This is bad !"""		)
            self.assertEqual(nested_simplify(__lowerCAmelCase		)	, [{"""label""": """NEGATIVE""", """score""": 1.0}]		)
            lowercase														=			text_classifier("""Birds are a type of animal"""		)
            self.assertEqual(nested_simplify(__lowerCAmelCase		)	, [{"""label""": """POSITIVE""", """score""": 0.9_8_8}]		)

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		):







            """simple docstring"""



            lowercase														=			TextClassificationPipeline(model=__lowerCAmelCase	, tokenizer=__lowerCAmelCase		)
            return text_classifier, ["HuggingFace is in", "This is another test"]







      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase		):







            """simple docstring"""



            lowercase														=			text_classifier.model
            # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
            lowercase														=			"""HuggingFace is in"""
            lowercase														=			text_classifier(__lowerCAmelCase		)

            self.assertEqual(nested_simplify(__lowerCAmelCase		)	, [{"""label""": ANY(__lowerCAmelCase		), """score""": ANY(__lowerCAmelCase		)}]		)
            self.assertTrue(outputs[0]["""label"""] in model.config.idalabel.values()		)

            lowercase														=			["""HuggingFace is in """, """Paris is in France"""]
            lowercase														=			text_classifier(__lowerCAmelCase		)
            self.assertEqual(
                nested_simplify(__lowerCAmelCase		)	, [{"""label""": ANY(__lowerCAmelCase		), """score""": ANY(__lowerCAmelCase		)}, {"""label""": ANY(__lowerCAmelCase		), """score""": ANY(__lowerCAmelCase		)}]	, )
            self.assertTrue(outputs[0]["""label"""] in model.config.idalabel.values()		)
            self.assertTrue(outputs[1]["""label"""] in model.config.idalabel.values()		)

            # Forcing to get all results with `top_k=None`
            # This is NOT the legacy format
            lowercase														=			text_classifier(__lowerCAmelCase	, top_k=__lowerCAmelCase		)
            lowercase														=			len(model.config.idalabel.values()		)
            self.assertEqual(
                nested_simplify(__lowerCAmelCase		)	, [[{"""label""": ANY(__lowerCAmelCase		), """score""": ANY(__lowerCAmelCase		)}] * N, [{"""label""": ANY(__lowerCAmelCase		), """score""": ANY(__lowerCAmelCase		)}] * N]	, )

            lowercase														=			{"""text""": """HuggingFace is in """, """text_pair""": """Paris is in France"""}
            lowercase														=			text_classifier(__lowerCAmelCase		)
            self.assertEqual(
                nested_simplify(__lowerCAmelCase		)	, {"""label""": ANY(__lowerCAmelCase		), """score""": ANY(__lowerCAmelCase		)}	, )
            self.assertTrue(outputs["""label"""] in model.config.idalabel.values()		)

            # This might be used a text pair, but tokenizer + pipe interaction
            # makes it hard to understand that it's not using the pair properly
            # https://github.com/huggingface/transformers/issues/17305
            # We disabled this usage instead as it was outputting wrong outputs.
            lowercase														=			[["""HuggingFace is in """, """Paris is in France"""]]
            with self.assertRaises(__lowerCAmelCase		):
                  text_classifier(__lowerCAmelCase		)

            # This used to be valid for doing text pairs
            # We're keeping it working because of backward compatibility
            lowercase														=			text_classifier([[["""HuggingFace is in """, """Paris is in France"""]]]		)
            self.assertEqual(
                nested_simplify(__lowerCAmelCase		)	, [{"""label""": ANY(__lowerCAmelCase		), """score""": ANY(__lowerCAmelCase		)}]	, )
            self.assertTrue(outputs[0]["""label"""] in model.config.idalabel.values()		)



