





from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


_A :   Optional[int]  			=  logging.get_logger(__name__)


if is_vision_available():
       import PIL


class        __SCREAMING_SNAKE_CASE      (			lowerCAmelCase_ ):
       _UpperCAmelCase				:  Any			   =   ["pixel_values"]







       def __init__(				self					:     Union[str, Any]		, A					:     bool = True		, A					:     Dict[str, int] = None		, A					:     PILImageResampling = PILImageResampling.BICUBIC		, A					:     bool = True		, A					:     Dict[str, int] = None		, A					:     bool = True		, A					:     Union[int, float] = 1 / 2_5_5		, A					:     bool = True		, A					:     Optional[Union[float, List[float]]] = None		, A					:     Optional[Union[float, List[float]]] = None		, A					:     bool = True		, **A					:     List[str]		, ):
          super().__init__(**A      )
          lowerCamelCase__       :						str        =							size if size is not None else {'''shortest_edge''': 2_2_4}
          lowerCamelCase__       :						List[str]        =							get_size_dict(A		, default_to_square=A      )
          lowerCamelCase__       :						List[Any]        =							crop_size if crop_size is not None else {'''height''': 2_2_4, '''width''': 2_2_4}
          lowerCamelCase__       :						Dict        =							get_size_dict(A		, default_to_square=A		, param_name='''crop_size'''      )

          lowerCamelCase__       :						Optional[int]        =							do_resize
          lowerCamelCase__       :						Optional[int]        =							size
          lowerCamelCase__       :						List[str]        =							resample
          lowerCamelCase__       :						Tuple        =							do_center_crop
          lowerCamelCase__       :						Tuple        =							crop_size
          lowerCamelCase__       :						List[Any]        =							do_rescale
          lowerCamelCase__       :						str        =							rescale_factor
          lowerCamelCase__       :						List[str]        =							do_normalize
          lowerCamelCase__       :						str        =							image_mean if image_mean is not None else OPENAI_CLIP_MEAN
          lowerCamelCase__       :						Tuple        =							image_std if image_std is not None else OPENAI_CLIP_STD
          lowerCamelCase__       :						int        =							do_convert_rgb







       def 						__lowerCamelCase   (				self					:     int		, A					:     np.ndarray		, A					:     Dict[str, int]		, A					:     PILImageResampling = PILImageResampling.BICUBIC		, A					:     Optional[Union[str, ChannelDimension]] = None		, **A					:     Any		, ):
          lowerCamelCase__       :						Union[str, Any]        =							get_size_dict(A		, default_to_square=A      )
          if "shortest_edge" not in size:
             raise ValueError(F"The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}"      )
          lowerCamelCase__       :						Optional[Any]        =							get_resize_output_image_size(A		, size=size['''shortest_edge''']		, default_to_square=A      )
          return resize(A		, size=A		, resample=A		, data_format=A		, **A      )







       def 						__lowerCamelCase   (				self					:     List[str]		, A					:     np.ndarray		, A					:     Dict[str, int]		, A					:     Optional[Union[str, ChannelDimension]] = None		, **A					:     str		, ):
          lowerCamelCase__       :						Optional[Any]        =							get_size_dict(A      )
          if "height" not in size or "width" not in size:
             raise ValueError(F"The `size` parameter must contain the keys (height, width). Got {size.keys()}"      )
          return center_crop(A		, size=(size['''height'''], size['''width'''])		, data_format=A		, **A      )







       def 						__lowerCamelCase   (				self					:     Optional[Any]		, A					:     np.ndarray		, A					:     Union[int, float]		, A					:     Optional[Union[str, ChannelDimension]] = None		, **A					:     Dict		, ):
          return rescale(A		, scale=A		, data_format=A		, **A      )







       def 						__lowerCamelCase   (				self					:     Optional[int]		, A					:     np.ndarray		, A					:     Union[float, List[float]]		, A					:     Union[float, List[float]]		, A					:     Optional[Union[str, ChannelDimension]] = None		, **A					:     Union[str, Any]		, ):
          return normalize(A		, mean=A		, std=A		, data_format=A		, **A      )







       def 						__lowerCamelCase   (				self					:     Optional[Any]		, A					:     ImageInput		, A					:     bool = None		, A					:     Dict[str, int] = None		, A					:     PILImageResampling = None		, A					:     bool = None		, A					:     int = None		, A					:     bool = None		, A					:     float = None		, A					:     bool = None		, A					:     Optional[Union[float, List[float]]] = None		, A					:     Optional[Union[float, List[float]]] = None		, A					:     bool = None		, A					:     Optional[Union[str, TensorType]] = None		, A					:     Optional[ChannelDimension] = ChannelDimension.FIRST		, **A					:     Optional[int]		, ):
          lowerCamelCase__       :						Any        =							do_resize if do_resize is not None else self.do_resize
          lowerCamelCase__       :						Any        =							size if size is not None else self.size
          lowerCamelCase__       :						Union[str, Any]        =							get_size_dict(A		, param_name='''size'''		, default_to_square=A      )
          lowerCamelCase__       :						Union[str, Any]        =							resample if resample is not None else self.resample
          lowerCamelCase__       :						List[Any]        =							do_center_crop if do_center_crop is not None else self.do_center_crop
          lowerCamelCase__       :						Any        =							crop_size if crop_size is not None else self.crop_size
          lowerCamelCase__       :						List[str]        =							get_size_dict(A		, param_name='''crop_size'''		, default_to_square=A      )
          lowerCamelCase__       :						Tuple        =							do_rescale if do_rescale is not None else self.do_rescale
          lowerCamelCase__       :						Tuple        =							rescale_factor if rescale_factor is not None else self.rescale_factor
          lowerCamelCase__       :						Optional[int]        =							do_normalize if do_normalize is not None else self.do_normalize
          lowerCamelCase__       :						Union[str, Any]        =							image_mean if image_mean is not None else self.image_mean
          lowerCamelCase__       :						str        =							image_std if image_std is not None else self.image_std
          lowerCamelCase__       :						Union[str, Any]        =							do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

          lowerCamelCase__       :						Dict        =							make_list_of_images(A      )

          if not valid_images(A      ):
             raise ValueError(
                 '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
                 '''torch.Tensor, tf.Tensor or jax.ndarray.'''      )

          if do_resize and size is None:
             raise ValueError('''Size must be specified if do_resize is True.'''      )

          if do_center_crop and crop_size is None:
             raise ValueError('''Crop size must be specified if do_center_crop is True.'''      )

          if do_rescale and rescale_factor is None:
             raise ValueError('''Rescale factor must be specified if do_rescale is True.'''      )

          if do_normalize and (image_mean is None or image_std is None):
             raise ValueError('''Image mean and std must be specified if do_normalize is True.'''      )

          # PIL RGBA images are converted to RGB
          if do_convert_rgb:
             lowerCamelCase__       :						Any        =							[convert_to_rgb(A      ) for image in images]

          # All transformations expect numpy arrays.
          lowerCamelCase__       :						Tuple        =							[to_numpy_array(A      ) for image in images]

          if do_resize:
             lowerCamelCase__       :						Optional[Any]        =							[self.resize(image=A		, size=A		, resample=A      ) for image in images]

          if do_center_crop:
             lowerCamelCase__       :						List[Any]        =							[self.center_crop(image=A		, size=A      ) for image in images]

          if do_rescale:
             lowerCamelCase__       :						Dict        =							[self.rescale(image=A		, scale=A      ) for image in images]

          if do_normalize:
             lowerCamelCase__       :						Dict        =							[self.normalize(image=A		, mean=A		, std=A      ) for image in images]

          lowerCamelCase__       :						Union[str, Any]        =							[to_channel_dimension_format(A		, A      ) for image in images]

          lowerCamelCase__       :						str        =							{'''pixel_values''': images}
          return BatchFeature(data=A		, tensor_type=A      )










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.
_A :   List[Any]  			=  {'LayoutLMv2Config', 'LayoutLMv3Config'}


@is_pipeline_test
class        __SCREAMING_SNAKE_CASE      (			unittest.TestCase ):
       _UpperCAmelCase				:  Optional[int]			   =   MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
       _UpperCAmelCase				:  Optional[Any]			   =   TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

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







       def 						__lowerCamelCase   (				self					:     str		, A					:     List[str]		, A					:     Tuple		, A					:     str      )       ->List[str]:
          lowerCamelCase__       :						List[Any]        =							ZeroShotClassificationPipeline(
              model=A		, tokenizer=A		, candidate_labels=['''polics''', '''health''']      )
          return classifier, ["Who are you voting for in 2020?", "My stomach hurts."]







       def 						__lowerCamelCase   (				self					:     List[str]		, A					:     Union[str, Any]		, A					:     Optional[Any]      )       ->Union[str, Any]:
          lowerCamelCase__       :						Any        =							classifier('''Who are you voting for in 2020?'''		, candidate_labels='''politics'''      )
          self.assertEqual(A		, {'''sequence''': ANY(A      ), '''labels''': [ANY(A      )], '''scores''': [ANY(A      )]}      )

          # No kwarg
          lowerCamelCase__       :						str        =							classifier('''Who are you voting for in 2020?'''		, ['''politics''']      )
          self.assertEqual(A		, {'''sequence''': ANY(A      ), '''labels''': [ANY(A      )], '''scores''': [ANY(A      )]}      )

          lowerCamelCase__       :						Dict        =							classifier('''Who are you voting for in 2020?'''		, candidate_labels=['''politics''']      )
          self.assertEqual(A		, {'''sequence''': ANY(A      ), '''labels''': [ANY(A      )], '''scores''': [ANY(A      )]}      )

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

          lowerCamelCase__       :						Optional[int]        =							classifier('''Who are you voting for in 2020?'''		, candidate_labels=['''politics''', '''public health''']      )
          self.assertEqual(
              A		, {'''sequence''': ANY(A      ), '''labels''': [ANY(A      ), ANY(A      )], '''scores''': [ANY(A      ), ANY(A      )]}      )
          self.assertAlmostEqual(sum(nested_simplify(outputs['''scores''']      )      )		, 1.0      )

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

          # https://github.com/huggingface/transformers/issues/13846
          lowerCamelCase__       :						Optional[int]        =							classifier(['''I am happy''']		, ['''positive''', '''negative''']      )
          self.assertEqual(
              A		, [
                  {'''sequence''': ANY(A      ), '''labels''': [ANY(A      ), ANY(A      )], '''scores''': [ANY(A      ), ANY(A      )]}
                  for i in range(1      )
              ]		, )
          lowerCamelCase__       :						Any        =							classifier(['''I am happy''', '''I am sad''']		, ['''positive''', '''negative''']      )
          self.assertEqual(
              A		, [
                  {'''sequence''': ANY(A      ), '''labels''': [ANY(A      ), ANY(A      )], '''scores''': [ANY(A      ), ANY(A      )]}
                  for i in range(2      )
              ]		, )

          with self.assertRaises(A      ):
             classifier(''''''		, candidate_labels='''politics'''      )

          with self.assertRaises(A      ):
             classifier(A		, candidate_labels='''politics'''      )

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

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

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

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

          self.run_entailment_id(A      )







       def 						__lowerCamelCase   (				self					:     Optional[int]		, A					:     Pipeline      )       ->Dict:
          lowerCamelCase__       :						str        =							zero_shot_classifier.model.config
          lowerCamelCase__       :						Optional[int]        =							config.labelaid
          lowerCamelCase__       :						Tuple        =							zero_shot_classifier.entailment_id

          lowerCamelCase__       :						Optional[Any]        =							{'''LABEL_0''': 0, '''LABEL_1''': 1, '''LABEL_2''': 2}
          self.assertEqual(zero_shot_classifier.entailment_id		, -1      )

          lowerCamelCase__       :						Optional[int]        =							{'''entailment''': 0, '''neutral''': 1, '''contradiction''': 2}
          self.assertEqual(zero_shot_classifier.entailment_id		, 0      )

          lowerCamelCase__       :						List[str]        =							{'''ENTAIL''': 0, '''NON-ENTAIL''': 1}
          self.assertEqual(zero_shot_classifier.entailment_id		, 0      )

          lowerCamelCase__       :						Optional[Any]        =							{'''ENTAIL''': 2, '''NEUTRAL''': 1, '''CONTR''': 0}
          self.assertEqual(zero_shot_classifier.entailment_id		, 2      )

          lowerCamelCase__       :						Tuple        =							original_labelaid
          self.assertEqual(A		, zero_shot_classifier.entailment_id      )







       @require_torch
       def 						__lowerCamelCase   (				self					:     Any      )       ->str:
          lowerCamelCase__       :						Optional[int]        =							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?''' * 1_0_0		, candidate_labels=['''politics''', '''public health''', '''science''']      )







       @require_torch
       def 						__lowerCamelCase   (				self					:     List[Any]      )       ->Optional[Any]:
          lowerCamelCase__       :						List[Any]        =							pipeline(
              '''zero-shot-classification'''		, model='''sshleifer/tiny-distilbert-base-cased-distilled-squad'''		, framework='''pt'''		, )
          lowerCamelCase__       :						Dict        =							zero_shot_classifier(
              '''Who are you voting for in 2020?'''		, candidate_labels=['''politics''', '''public health''', '''science''']      )

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







       @require_tf
       def 						__lowerCamelCase   (				self					:     str      )       ->List[str]:
          lowerCamelCase__       :						Any        =							pipeline(
              '''zero-shot-classification'''		, model='''sshleifer/tiny-distilbert-base-cased-distilled-squad'''		, framework='''tf'''		, )
          lowerCamelCase__       :						Optional[Any]        =							zero_shot_classifier(
              '''Who are you voting for in 2020?'''		, candidate_labels=['''politics''', '''public health''', '''science''']      )

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







       @slow
       @require_torch
       def 						__lowerCamelCase   (				self					:     List[Any]      )       ->Optional[Any]:
          lowerCamelCase__       :						int        =							pipeline('''zero-shot-classification'''		, model='''roberta-large-mnli'''		, framework='''pt'''      )
          lowerCamelCase__       :						int        =							zero_shot_classifier(
              '''Who are you voting for in 2020?'''		, candidate_labels=['''politics''', '''public health''', '''science''']      )

          self.assertEqual(
              nested_simplify(A      )		, {
                  '''sequence''': '''Who are you voting for in 2020?''',
                  '''labels''': ['''politics''', '''public health''', '''science'''],
                  '''scores''': [0.9_76, 0.0_15, 0.0_09],
              }		, )
          lowerCamelCase__       :						Any        =							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=A		, )
          self.assertEqual(
              nested_simplify(A      )		, {
                  '''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_17, 0.7_13, 0.0_18, 0.0_18],
              }		, )







       @slow
       @require_tf
       def 						__lowerCamelCase   (				self					:     Union[str, Any]      )       ->Optional[int]:
          lowerCamelCase__       :						List[Any]        =							pipeline('''zero-shot-classification'''		, model='''roberta-large-mnli'''		, framework='''tf'''      )
          lowerCamelCase__       :						Dict        =							zero_shot_classifier(
              '''Who are you voting for in 2020?'''		, candidate_labels=['''politics''', '''public health''', '''science''']      )

          self.assertEqual(
              nested_simplify(A      )		, {
                  '''sequence''': '''Who are you voting for in 2020?''',
                  '''labels''': ['''politics''', '''public health''', '''science'''],
                  '''scores''': [0.9_76, 0.0_15, 0.0_09],
              }		, )
          lowerCamelCase__       :						Union[str, Any]        =							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=A		, )
          self.assertEqual(
              nested_simplify(A      )		, {
                  '''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_17, 0.7_13, 0.0_18, 0.0_18],
              }		, )



