





"""simple docstring"""





from typing import Dict, Iterable, List, Optional, Union

import numpy as np

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


_a							=							logging.get_logger(__name__)




class  _lowerCAmelCase  (     _a       ):







 """simple docstring"""







 __UpperCAmelCase     : Any       		=     ["""pixel_values"""]


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

    __lowercase								=						do_resize
    __lowercase								=						size
    __lowercase								=						resample
    __lowercase								=						do_center_crop
    __lowercase								=						crop_size
    __lowercase								=						do_rescale
    __lowercase								=						rescale_factor
    __lowercase								=						do_normalize
    __lowercase								=						image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
    __lowercase								=						image_std if image_std is not None else IMAGENET_DEFAULT_STD


 def  _lowercase			(      self							:      Union[str, Any], UpperCAmelCase__							:      str, UpperCAmelCase__							:      Optional[int], UpperCAmelCase__							:      List[str] = PILImageResampling.BICUBIC, UpperCAmelCase__							:      Optional[Any] = None, **UpperCAmelCase__							:      Dict, ):
    __lowercase								=						get_size_dict(UpperCAmelCase__, default_to_square=UpperCAmelCase__     )
    # size_dict is a dict with either keys "height" and "width" or "shortest_edge"
    if "shortest_edge" in size:
       __lowercase								=						int((2_5_6 / 2_2_4) * size["shortest_edge"]     )
       __lowercase								=						get_resize_output_image_size(UpperCAmelCase__, size=UpperCAmelCase__, default_to_square=UpperCAmelCase__     )
       __lowercase								=						{'height': output_size[0], 'width': output_size[1]}
    if "height" not in size_dict or "width" not in size_dict:
       raise ValueError(
           F"""Size dict must have keys \'height\' and \'width\' or \'shortest_edge\'. Got {size_dict.keys()}"""     )
    return resize(
        UpperCAmelCase__, size=(size_dict["height"], size_dict["width"]), resample=UpperCAmelCase__, data_format=UpperCAmelCase__, **UpperCAmelCase__     )


 def  _lowercase			(      self							:      List[str], UpperCAmelCase__							:      Any, UpperCAmelCase__							:      Any, UpperCAmelCase__							:      List[Any] = None, **UpperCAmelCase__							:      int, ):
    __lowercase								=						get_size_dict(UpperCAmelCase__     )
    if "height" not in size or "width" not in size:
       raise ValueError(F"""Size dict must have keys \'height\' and \'width\'. Got {size.keys()}"""     )
    return center_crop(UpperCAmelCase__, size=(size["height"], size["width"]), data_format=UpperCAmelCase__, **UpperCAmelCase__     )


 def  _lowercase			(      self							:      Dict, UpperCAmelCase__							:      int, UpperCAmelCase__							:      Any, UpperCAmelCase__							:      Tuple = None, **UpperCAmelCase__							:      Optional[int], ):
    return rescale(UpperCAmelCase__, scale=UpperCAmelCase__, data_format=UpperCAmelCase__, **UpperCAmelCase__     )


 def  _lowercase			(      self							:      str, UpperCAmelCase__							:      Any, UpperCAmelCase__							:      List[Any], UpperCAmelCase__							:      str, UpperCAmelCase__							:      Optional[Any] = None, **UpperCAmelCase__							:      Optional[Any], ):
    return normalize(UpperCAmelCase__, mean=UpperCAmelCase__, std=UpperCAmelCase__, data_format=UpperCAmelCase__, **UpperCAmelCase__     )



 def  _lowercase			(      self							:      List[str], UpperCAmelCase__							:      str, UpperCAmelCase__							:      Tuple = None, UpperCAmelCase__							:      int = None, UpperCAmelCase__							:      int = None, UpperCAmelCase__							:      int = None, UpperCAmelCase__							:      Dict = None, UpperCAmelCase__							:      Any = None, UpperCAmelCase__							:      Any = None, UpperCAmelCase__							:      Dict = None, UpperCAmelCase__							:      Dict = None, UpperCAmelCase__							:      List[Any] = None, UpperCAmelCase__							:      Optional[Any] = None, UpperCAmelCase__							:      List[str] = ChannelDimension.FIRST, **UpperCAmelCase__							:      int, ):
    __lowercase								=						do_resize if do_resize is not None else self.do_resize
    __lowercase								=						resample if resample is not None else self.resample
    __lowercase								=						do_center_crop if do_center_crop is not None else self.do_center_crop
    __lowercase								=						do_rescale if do_rescale is not None else self.do_rescale
    __lowercase								=						rescale_factor if rescale_factor is not None else self.rescale_factor
    __lowercase								=						do_normalize if do_normalize is not None else self.do_normalize
    __lowercase								=						image_mean if image_mean is not None else self.image_mean
    __lowercase								=						image_std if image_std is not None else self.image_std

    __lowercase								=						size if size is not None else self.size
    __lowercase								=						get_size_dict(UpperCAmelCase__, default_to_square=UpperCAmelCase__     )
    __lowercase								=						crop_size if crop_size is not None else self.crop_size
    __lowercase								=						get_size_dict(UpperCAmelCase__, param_name="crop_size"     )

    __lowercase								=						make_list_of_images(UpperCAmelCase__     )

    if not valid_images(UpperCAmelCase__     ):
       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."     )

    # All transformations expect numpy arrays.
    __lowercase								=						[to_numpy_array(UpperCAmelCase__     ) for image in images]

    if do_resize:
       __lowercase								=						[self.resize(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__     ) for image in images]

    if do_center_crop:
       __lowercase								=						[self.center_crop(UpperCAmelCase__, UpperCAmelCase__     ) for image in images]

    if do_rescale:
       __lowercase								=						[self.rescale(UpperCAmelCase__, UpperCAmelCase__     ) for image in images]

    if do_normalize:
       __lowercase								=						[self.normalize(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__     ) for image in images]

    __lowercase								=						[to_channel_dimension_format(UpperCAmelCase__, UpperCAmelCase__     ) for image in images]

    __lowercase								=						{'pixel_values': images}
    return BatchFeature(data=UpperCAmelCase__, tensor_type=UpperCAmelCase__     )







"""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.
_a							=							{'LayoutLMv2Config', 'LayoutLMv3Config'}




@is_pipeline_test
class  _lowerCAmelCase  (     unittest.TestCase       ):







 """simple docstring"""







 __UpperCAmelCase     : str       		=     MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
 __UpperCAmelCase     : str       		=     TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

 if model_mapping is not None:
  __UpperCAmelCase     : 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:
  __UpperCAmelCase     : Tuple       		=     {
      config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
  }


 @require_torch
 def  _lowercase			(      self							:      Optional[int]     ):
    __lowercase								=						pipeline(
        task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="pt"     )

    __lowercase								=						text_classifier("This is great !"     )
    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": "LABEL_0", "score": 0.504}]     )

    __lowercase								=						text_classifier("This is great !", top_k=2     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}]     )

    __lowercase								=						text_classifier(["This is great !", "This is bad"], top_k=2     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), [
            [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
            [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
        ], )

    __lowercase								=						text_classifier("This is great !", top_k=1     )
    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": "LABEL_0", "score": 0.504}]     )

    # Legacy behavior
    __lowercase								=						text_classifier("This is great !", return_all_scores=UpperCAmelCase__     )
    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": "LABEL_0", "score": 0.504}]     )

    __lowercase								=						text_classifier("This is great !", return_all_scores=UpperCAmelCase__     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), [[{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}]]     )

    __lowercase								=						text_classifier(["This is great !", "Something else"], return_all_scores=UpperCAmelCase__     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), [
            [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
            [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
        ], )

    __lowercase								=						text_classifier(["This is great !", "Something else"], return_all_scores=UpperCAmelCase__     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), [
            {"label": "LABEL_0", "score": 0.504},
            {"label": "LABEL_0", "score": 0.504},
        ], )


 @require_torch
 def  _lowercase			(      self							:      Dict     ):
    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(UpperCAmelCase__     ), [{"label": "LABEL_0", "score": 0.504}]     )


 @require_tf
 def  _lowercase			(      self							:      Union[str, Any]     ):
    __lowercase								=						pipeline(
        task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="tf"     )

    __lowercase								=						text_classifier("This is great !"     )
    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": "LABEL_0", "score": 0.504}]     )


 @slow
 @require_torch
 def  _lowercase			(      self							:      Dict     ):
    __lowercase								=						pipeline("text-classification"     )

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


 @slow
 @require_tf
 def  _lowercase			(      self							:      Tuple     ):
    __lowercase								=						pipeline("text-classification", framework="tf"     )

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


 def  _lowercase			(      self							:      Union[str, Any], UpperCAmelCase__							:      str, UpperCAmelCase__							:      int, UpperCAmelCase__							:      Tuple     ):
    __lowercase								=						TextClassificationPipeline(model=UpperCAmelCase__, tokenizer=UpperCAmelCase__     )
    return text_classifier, ["HuggingFace is in", "This is another test"]



 def  _lowercase			(      self							:      Any, UpperCAmelCase__							:      List[Any], UpperCAmelCase__							:      str     ):
    __lowercase								=						text_classifier.model
    # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
    __lowercase								=						"HuggingFace is in"
    __lowercase								=						text_classifier(UpperCAmelCase__     )

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

    __lowercase								=						["HuggingFace is in ", "Paris is in France"]
    __lowercase								=						text_classifier(UpperCAmelCase__     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), [{"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}, {"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}], )
    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(UpperCAmelCase__, top_k=UpperCAmelCase__     )
    __lowercase								=						len(model.config.idalabel.values()     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), [[{"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}] * N, [{"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}] * N], )

    __lowercase								=						{"text": "HuggingFace is in ", "text_pair": "Paris is in France"}
    __lowercase								=						text_classifier(UpperCAmelCase__     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), {"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}, )
    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(UpperCAmelCase__     ):
       text_classifier(UpperCAmelCase__     )

    # 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(UpperCAmelCase__     ), [{"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}], )
    self.assertTrue(outputs[0]["label"] in model.config.idalabel.values()     )




