'''simple docstring'''






import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    Pipeline,
    ZeroShotClassificationPipeline,
    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.
SCREAMING_SNAKE_CASE_	   =			{'LayoutLMv2Config', 'LayoutLMv3Config'}




@is_pipeline_test
class    snake_case_ (       unittest.TestCase				):



  """simple docstring"""
  __lowerCAmelCase				      =					MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
  __lowerCAmelCase				      =					TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

  if model_mapping is not None:
    __lowerCAmelCase				      =					{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
  if tf_model_mapping is not None:
    __lowerCAmelCase				      =					{
        config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
    }







  def    lowercase_  (							self   ,    snake_case_   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					List[str]		= ZeroShotClassificationPipeline(
        model=snake_case_   ,    tokenizer=snake_case_   ,    candidate_labels=["""polics""", """health"""]					)
    return classifier, ["Who are you voting for in 2020?", "My stomach hurts."]







  def    lowercase_  (							self   ,    snake_case_   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					Optional[int]		= classifier("""Who are you voting for in 2020?"""   ,    candidate_labels="""politics"""					)
    self.assertEqual(snake_case_   ,    {"""sequence""": ANY(snake_case_					), """labels""": [ANY(snake_case_					)], """scores""": [ANY(snake_case_					)]}					)

    # No kwarg
    __UpperCAmelCase:					Any		= classifier("""Who are you voting for in 2020?"""   ,    ["""politics"""]					)
    self.assertEqual(snake_case_   ,    {"""sequence""": ANY(snake_case_					), """labels""": [ANY(snake_case_					)], """scores""": [ANY(snake_case_					)]}					)

    __UpperCAmelCase:					Optional[int]		= classifier("""Who are you voting for in 2020?"""   ,    candidate_labels=["""politics"""]					)
    self.assertEqual(snake_case_   ,    {"""sequence""": ANY(snake_case_					), """labels""": [ANY(snake_case_					)], """scores""": [ANY(snake_case_					)]}					)

    __UpperCAmelCase:					Any		= classifier("""Who are you voting for in 2020?"""   ,    candidate_labels="""politics, public health"""					)
    self.assertEqual(
        snake_case_   ,    {"""sequence""": ANY(snake_case_					), """labels""": [ANY(snake_case_					), ANY(snake_case_					)], """scores""": [ANY(snake_case_					), ANY(snake_case_					)]}					)
    self.assertAlmostEqual(sum(nested_simplify(outputs["""scores"""]					)					)   ,    1.0					)

    __UpperCAmelCase:					Union[str, Any]		= classifier("""Who are you voting for in 2020?"""   ,    candidate_labels=["""politics""", """public health"""]					)
    self.assertEqual(
        snake_case_   ,    {"""sequence""": ANY(snake_case_					), """labels""": [ANY(snake_case_					), ANY(snake_case_					)], """scores""": [ANY(snake_case_					), ANY(snake_case_					)]}					)
    self.assertAlmostEqual(sum(nested_simplify(outputs["""scores"""]					)					)   ,    1.0					)

    __UpperCAmelCase:					Any		= classifier(
        """Who are you voting for in 2020?"""   ,    candidate_labels="""politics"""   ,    hypothesis_template="""This text is about {}"""					)
    self.assertEqual(snake_case_   ,    {"""sequence""": ANY(snake_case_					), """labels""": [ANY(snake_case_					)], """scores""": [ANY(snake_case_					)]}					)

    # https://github.com/huggingface/transformers/issues/13846
    __UpperCAmelCase:					Tuple		= classifier(["""I am happy"""]   ,    ["""positive""", """negative"""]					)
    self.assertEqual(
        snake_case_   ,    [
            {"""sequence""": ANY(snake_case_					), """labels""": [ANY(snake_case_					), ANY(snake_case_					)], """scores""": [ANY(snake_case_					), ANY(snake_case_					)]}
            for i in range(1					)
        ]   ,    )
    __UpperCAmelCase:					Dict		= classifier(["""I am happy""", """I am sad"""]   ,    ["""positive""", """negative"""]					)
    self.assertEqual(
        snake_case_   ,    [
            {"""sequence""": ANY(snake_case_					), """labels""": [ANY(snake_case_					), ANY(snake_case_					)], """scores""": [ANY(snake_case_					), ANY(snake_case_					)]}
            for i in range(2					)
        ]   ,    )

    with self.assertRaises(snake_case_					):
      classifier(""""""   ,    candidate_labels="""politics"""					)

    with self.assertRaises(snake_case_					):
      classifier(snake_case_   ,    candidate_labels="""politics"""					)

    with self.assertRaises(snake_case_					):
      classifier("""Who are you voting for in 2020?"""   ,    candidate_labels=""""""					)

    with self.assertRaises(snake_case_					):
      classifier("""Who are you voting for in 2020?"""   ,    candidate_labels=snake_case_					)

    with self.assertRaises(snake_case_					):
      classifier(
          """Who are you voting for in 2020?"""   ,    candidate_labels="""politics"""   ,    hypothesis_template="""Not formatting template"""   ,    )

    with self.assertRaises(snake_case_					):
      classifier(
          """Who are you voting for in 2020?"""   ,    candidate_labels="""politics"""   ,    hypothesis_template=snake_case_   ,    )

    self.run_entailment_id(snake_case_					)







  def    lowercase_  (							self   ,    snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					Dict		= zero_shot_classifier.model.config
    __UpperCAmelCase:					List[Any]		= config.labelaid
    __UpperCAmelCase:					str		= zero_shot_classifier.entailment_id

    __UpperCAmelCase:					List[str]		= {"""LABEL_0""": 0, """LABEL_1""": 1, """LABEL_2""": 2}
    self.assertEqual(zero_shot_classifier.entailment_id   ,    -1					)

    __UpperCAmelCase:					List[Any]		= {"""entailment""": 0, """neutral""": 1, """contradiction""": 2}
    self.assertEqual(zero_shot_classifier.entailment_id   ,    0					)

    __UpperCAmelCase:					str		= {"""ENTAIL""": 0, """NON-ENTAIL""": 1}
    self.assertEqual(zero_shot_classifier.entailment_id   ,    0					)

    __UpperCAmelCase:					str		= {"""ENTAIL""": 2, """NEUTRAL""": 1, """CONTR""": 0}
    self.assertEqual(zero_shot_classifier.entailment_id   ,    2					)

    __UpperCAmelCase:					Optional[int]		= original_labelaid
    self.assertEqual(snake_case_   ,    zero_shot_classifier.entailment_id					)







  @require_torch
  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					str		= pipeline(
        """zero-shot-classification"""   ,    model="""sshleifer/tiny-distilbert-base-cased-distilled-squad"""   ,    framework="""pt"""   ,    )
    # There was a regression in 4.10 for this
    # Adding a test so we don't make the mistake again.
    # https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499
    zero_shot_classifier(
        """Who are you voting for in 2020?""" * 100   ,    candidate_labels=["""politics""", """public health""", """science"""]					)







  @require_torch
  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Optional[int]		= pipeline(
        """zero-shot-classification"""   ,    model="""sshleifer/tiny-distilbert-base-cased-distilled-squad"""   ,    framework="""pt"""   ,    )
    __UpperCAmelCase:					Optional[Any]		= zero_shot_classifier(
        """Who are you voting for in 2020?"""   ,    candidate_labels=["""politics""", """public health""", """science"""]					)

    self.assertEqual(
        nested_simplify(snake_case_					)   ,    {
            """sequence""": """Who are you voting for in 2020?""",
            """labels""": ["""science""", """public health""", """politics"""],
            """scores""": [0.3_3_3, 0.3_3_3, 0.3_3_3],
        }   ,    )







  @require_tf
  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Optional[int]		= pipeline(
        """zero-shot-classification"""   ,    model="""sshleifer/tiny-distilbert-base-cased-distilled-squad"""   ,    framework="""tf"""   ,    )
    __UpperCAmelCase:					Dict		= zero_shot_classifier(
        """Who are you voting for in 2020?"""   ,    candidate_labels=["""politics""", """public health""", """science"""]					)

    self.assertEqual(
        nested_simplify(snake_case_					)   ,    {
            """sequence""": """Who are you voting for in 2020?""",
            """labels""": ["""science""", """public health""", """politics"""],
            """scores""": [0.3_3_3, 0.3_3_3, 0.3_3_3],
        }   ,    )







  @slow
  @require_torch
  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Optional[Any]		= pipeline("""zero-shot-classification"""   ,    model="""roberta-large-mnli"""   ,    framework="""pt"""					)
    __UpperCAmelCase:					Optional[int]		= zero_shot_classifier(
        """Who are you voting for in 2020?"""   ,    candidate_labels=["""politics""", """public health""", """science"""]					)

    self.assertEqual(
        nested_simplify(snake_case_					)   ,    {
            """sequence""": """Who are you voting for in 2020?""",
            """labels""": ["""politics""", """public health""", """science"""],
            """scores""": [0.9_7_6, 0.0_1_5, 0.0_0_9],
        }   ,    )
    __UpperCAmelCase:					Dict		= zero_shot_classifier(
        """The dominant sequence transduction models are based on complex recurrent or convolutional neural networks"""
        """ in an encoder-decoder configuration. The best performing models also connect the encoder and decoder"""
        """ through an attention mechanism. We propose a new simple network architecture, the Transformer, based"""
        """ solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two"""
        """ machine translation tasks show these models to be superior in quality while being more parallelizable"""
        """ and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014"""
        """ English-to-German translation task, improving over the existing best results, including ensembles by"""
        """ over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new"""
        """ single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small"""
        """ fraction of the training costs of the best models from the literature. We show that the Transformer"""
        """ generalizes well to other tasks by applying it successfully to English constituency parsing both with"""
        """ large and limited training data."""   ,    candidate_labels=["""machine learning""", """statistics""", """translation""", """vision"""]   ,    multi_label=snake_case_   ,    )
    self.assertEqual(
        nested_simplify(snake_case_					)   ,    {
            """sequence""": (
                """The dominant sequence transduction models are based on complex recurrent or convolutional neural"""
                """ networks in an encoder-decoder configuration. The best performing models also connect the"""
                """ encoder and decoder through an attention mechanism. We propose a new simple network"""
                """ architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence"""
                """ and convolutions entirely. Experiments on two machine translation tasks show these models to be"""
                """ superior in quality while being more parallelizable and requiring significantly less time to"""
                """ train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,"""
                """ improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014"""
                """ English-to-French translation task, our model establishes a new single-model state-of-the-art"""
                """ BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training"""
                """ costs of the best models from the literature. We show that the Transformer generalizes well to"""
                """ other tasks by applying it successfully to English constituency parsing both with large and"""
                """ limited training data."""
            ),
            """labels""": ["""translation""", """machine learning""", """vision""", """statistics"""],
            """scores""": [0.8_1_7, 0.7_1_3, 0.0_1_8, 0.0_1_8],
        }   ,    )







  @slow
  @require_tf
  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Optional[Any]		= pipeline("""zero-shot-classification"""   ,    model="""roberta-large-mnli"""   ,    framework="""tf"""					)
    __UpperCAmelCase:					str		= zero_shot_classifier(
        """Who are you voting for in 2020?"""   ,    candidate_labels=["""politics""", """public health""", """science"""]					)

    self.assertEqual(
        nested_simplify(snake_case_					)   ,    {
            """sequence""": """Who are you voting for in 2020?""",
            """labels""": ["""politics""", """public health""", """science"""],
            """scores""": [0.9_7_6, 0.0_1_5, 0.0_0_9],
        }   ,    )
    __UpperCAmelCase:					int		= zero_shot_classifier(
        """The dominant sequence transduction models are based on complex recurrent or convolutional neural networks"""
        """ in an encoder-decoder configuration. The best performing models also connect the encoder and decoder"""
        """ through an attention mechanism. We propose a new simple network architecture, the Transformer, based"""
        """ solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two"""
        """ machine translation tasks show these models to be superior in quality while being more parallelizable"""
        """ and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014"""
        """ English-to-German translation task, improving over the existing best results, including ensembles by"""
        """ over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new"""
        """ single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small"""
        """ fraction of the training costs of the best models from the literature. We show that the Transformer"""
        """ generalizes well to other tasks by applying it successfully to English constituency parsing both with"""
        """ large and limited training data."""   ,    candidate_labels=["""machine learning""", """statistics""", """translation""", """vision"""]   ,    multi_label=snake_case_   ,    )
    self.assertEqual(
        nested_simplify(snake_case_					)   ,    {
            """sequence""": (
                """The dominant sequence transduction models are based on complex recurrent or convolutional neural"""
                """ networks in an encoder-decoder configuration. The best performing models also connect the"""
                """ encoder and decoder through an attention mechanism. We propose a new simple network"""
                """ architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence"""
                """ and convolutions entirely. Experiments on two machine translation tasks show these models to be"""
                """ superior in quality while being more parallelizable and requiring significantly less time to"""
                """ train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,"""
                """ improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014"""
                """ English-to-French translation task, our model establishes a new single-model state-of-the-art"""
                """ BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training"""
                """ costs of the best models from the literature. We show that the Transformer generalizes well to"""
                """ other tasks by applying it successfully to English constituency parsing both with large and"""
                """ limited training data."""
            ),
            """labels""": ["""translation""", """machine learning""", """vision""", """statistics"""],
            """scores""": [0.8_1_7, 0.7_1_3, 0.0_1_8, 0.0_1_8],
        }   ,    )
'''simple docstring'''






import json
import os
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import ImageGPTImageProcessor




class    a (       unittest.TestCase				):



  """simple docstring"""







  def __init__(							self   ,    snake_case_   ,    snake_case_=7   ,    snake_case_=3   ,    snake_case_=18   ,    snake_case_=30   ,    snake_case_=400   ,    snake_case_=True   ,    snake_case_=None   ,    snake_case_=True   ,    ):

    '''simple docstring'''







    __UpperCAmelCase:					Optional[int]		= size if size is not None else {"""height""": 18, """width""": 18}
    __UpperCAmelCase:					Tuple		= parent
    __UpperCAmelCase:					Any		= batch_size
    __UpperCAmelCase:					str		= num_channels
    __UpperCAmelCase:					Any		= image_size
    __UpperCAmelCase:					Optional[int]		= min_resolution
    __UpperCAmelCase:					Tuple		= max_resolution
    __UpperCAmelCase:					Any		= do_resize
    __UpperCAmelCase:					Any		= size
    __UpperCAmelCase:					Optional[Any]		= do_normalize







  def    lowercase_  (							self					):

    '''simple docstring'''







    return {
        # here we create 2 clusters for the sake of simplicity
        "clusters": np.asarray(
            [
                [0.8_8_6_6_4_4_3_6_3_4_0_3_3_2_0_3, 0.6_6_1_8_8_2_9_3_6_9_5_4_4_9_8_3, 0.3_8_9_1_7_4_6_4_0_1_7_8_6_8_0_4],
                [-0.6_0_4_2_5_5_9_1_4_6_8_8_1_1_0_4, -0.0_2_2_9_5_0_0_8_8_6_0_5_2_8_4_6_9, 0.5_4_2_3_7_9_7_3_6_9_0_0_3_2_9_6],
            ]					),
        "do_resize": self.do_resize,
        "size": self.size,
        "do_normalize": self.do_normalize,
    }




@require_torch
@require_vision
class    a (       __lowerCAmelCase						,  unittest.TestCase				):



  """simple docstring"""
  __lowerCAmelCase				      =					ImageGPTImageProcessor if is_vision_available() else None







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					str		= ImageGPTImageProcessingTester(self					)







  @property
  def    lowercase_  (							self					):

    '''simple docstring'''







    return self.image_processor_tester.prepare_image_processor_dict()







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Tuple		= self.image_processing_class(**self.image_processor_dict					)
    self.assertTrue(hasattr(snake_case_   ,    """clusters"""					)					)
    self.assertTrue(hasattr(snake_case_   ,    """do_resize"""					)					)
    self.assertTrue(hasattr(snake_case_   ,    """size"""					)					)
    self.assertTrue(hasattr(snake_case_   ,    """do_normalize"""					)					)







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					List[Any]		= self.image_processing_class.from_dict(self.image_processor_dict					)
    self.assertEqual(image_processor.size   ,    {"""height""": 18, """width""": 18}					)

    __UpperCAmelCase:					int		= self.image_processing_class.from_dict(self.image_processor_dict   ,    size=42					)
    self.assertEqual(image_processor.size   ,    {"""height""": 42, """width""": 42}					)







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					int		= self.image_processing_class(**self.image_processor_dict					)
    __UpperCAmelCase:					str		= json.loads(image_processor.to_json_string()					)
    for key, value in self.image_processor_dict.items():
      if key == "clusters":
        self.assertTrue(np.array_equal(snake_case_   ,    obj[key]					)					)
      else:
        self.assertEqual(obj[key]   ,    snake_case_					)







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Any		= self.image_processing_class(**self.image_processor_dict					)

    with tempfile.TemporaryDirectory() as tmpdirname:
      __UpperCAmelCase:					List[Any]		= os.path.join(snake_case_   ,    """image_processor.json"""					)
      image_processor_first.to_json_file(snake_case_					)
      __UpperCAmelCase:					List[str]		= self.image_processing_class.from_json_file(snake_case_					).to_dict()

    __UpperCAmelCase:					str		= image_processor_first.to_dict()
    for key, value in image_processor_first.items():
      if key == "clusters":
        self.assertTrue(np.array_equal(snake_case_   ,    image_processor_second[key]					)					)
      else:
        self.assertEqual(image_processor_first[key]   ,    snake_case_					)







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Any		= self.image_processing_class(**self.image_processor_dict					)

    with tempfile.TemporaryDirectory() as tmpdirname:
      image_processor_first.save_pretrained(snake_case_					)
      __UpperCAmelCase:					Any		= self.image_processing_class.from_pretrained(snake_case_					).to_dict()

    __UpperCAmelCase:					str		= image_processor_first.to_dict()
    for key, value in image_processor_first.items():
      if key == "clusters":
        self.assertTrue(np.array_equal(snake_case_   ,    image_processor_second[key]					)					)
      else:
        self.assertEqual(image_processor_first[key]   ,    snake_case_					)







  @unittest.skip("""ImageGPT requires clusters at initialization"""					)
  def    lowercase_  (							self					):

    '''simple docstring'''







    pass






def  UpperCamelCase__					(					)							->	List[str]:
  __UpperCAmelCase:					Any		= load_dataset("""hf-internal-testing/fixtures_image_utils"""   ,    split="""test"""     )

  __UpperCAmelCase:					List[Any]		= Image.open(dataset[4]["""file"""]     )
  __UpperCAmelCase:					Dict		= Image.open(dataset[5]["""file"""]     )

  __UpperCAmelCase:					Dict		= [imagea, imagea]

  return images







@require_vision
@require_torch
class    a (       unittest.TestCase				):



  """simple docstring"""







  @slow
  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Union[str, Any]		= ImageGPTImageProcessor.from_pretrained("""openai/imagegpt-small"""					)

    __UpperCAmelCase:					Dict		= prepare_images()

    # test non-batched
    __UpperCAmelCase:					Tuple		= image_processing(images[0]   ,    return_tensors="""pt"""					)

    self.assertIsInstance(encoding.input_ids   ,    torch.LongTensor					)
    self.assertEqual(encoding.input_ids.shape   ,    (1, 1024)					)

    __UpperCAmelCase:					int		= [306, 191, 191]
    self.assertEqual(encoding.input_ids[0, :3].tolist()   ,    snake_case_					)

    # test batched
    __UpperCAmelCase:					Union[str, Any]		= image_processing(snake_case_   ,    return_tensors="""pt"""					)

    self.assertIsInstance(encoding.input_ids   ,    torch.LongTensor					)
    self.assertEqual(encoding.input_ids.shape   ,    (2, 1024)					)

    __UpperCAmelCase:					Tuple		= [303, 13, 13]
    self.assertEqual(encoding.input_ids[1, -3:].tolist()   ,    snake_case_					)