

"""simple docstring"""





import unittest

import numpy as np
import requests

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

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
   import torch

   from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
   _a							=							False

if is_vision_available():
   from PIL import Image

   from transformers import PixaStructImageProcessor




class  _lowerCAmelCase  (     unittest.TestCase       ):







 """simple docstring"""







 def __init__(      self							:      List[str], UpperCAmelCase__							:      Any, UpperCAmelCase__							:      Optional[Any]=7, UpperCAmelCase__							:      List[str]=3, UpperCAmelCase__							:      Optional[Any]=1_8, UpperCAmelCase__							:      Union[str, Any]=3_0, UpperCAmelCase__							:      List[str]=4_0_0, UpperCAmelCase__							:      Any=None, UpperCAmelCase__							:      Optional[Any]=True, UpperCAmelCase__							:      Any=True, UpperCAmelCase__							:      Any=None, ):
    __lowercase								=						size if size is not None else {"height": 2_0, "width": 2_0}
    __lowercase								=						parent
    __lowercase								=						batch_size
    __lowercase								=						num_channels
    __lowercase								=						image_size
    __lowercase								=						min_resolution
    __lowercase								=						max_resolution
    __lowercase								=						size
    __lowercase								=						do_normalize
    __lowercase								=						do_convert_rgb
    __lowercase								=						[5_1_2, 1_0_2_4, 2_0_4_8, 4_0_9_6]
    __lowercase								=						patch_size if patch_size is not None else {"height": 1_6, "width": 1_6}


 def  _lowercase			(      self							:      int     ):
    return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}



 def  _lowercase			(      self							:      Optional[int]     ):
    __lowercase								=						"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg"
    __lowercase								=						Image.open(requests.get(UpperCAmelCase__, stream=UpperCAmelCase__     ).raw     ).convert("RGB"     )
    return raw_image







@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11					,reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`."					,)
@require_torch
@require_vision
class  _lowerCAmelCase  (     lowercase					,unittest.TestCase       ):







 """simple docstring"""







 __UpperCAmelCase     : Dict       		=     PixaStructImageProcessor if is_vision_available() else None


 def  _lowercase			(      self							:      int     ):
    __lowercase								=						PixaStructImageProcessingTester(self     )


 @property
 def  _lowercase			(      self							:      Dict     ):
    return self.image_processor_tester.prepare_image_processor_dict()


 def  _lowercase			(      self							:      Union[str, Any]     ):
    __lowercase								=						self.image_processing_class(**self.image_processor_dict     )
    self.assertTrue(hasattr(UpperCAmelCase__, "do_normalize"     )     )
    self.assertTrue(hasattr(UpperCAmelCase__, "do_convert_rgb"     )     )


 def  _lowercase			(      self							:      List[Any]     ):
    __lowercase								=						self.image_processor_tester.prepare_dummy_image()

    __lowercase								=						self.image_processing_class(**self.image_processor_dict     )
    __lowercase								=						2_0_4_8

    __lowercase								=						image_processor(UpperCAmelCase__, return_tensors="pt", max_patches=UpperCAmelCase__     )
    self.assertTrue(torch.allclose(inputs.flattened_patches.mean(), torch.tensor(0.0_606     ), atol=1E-3, rtol=1E-3     )     )


 def  _lowercase			(      self							:      Tuple     ):
    # Initialize image_processor
    __lowercase								=						self.image_processing_class(**self.image_processor_dict     )
    # create random PIL images
    __lowercase								=						prepare_image_inputs(self.image_processor_tester, equal_resolution=UpperCAmelCase__     )
    for image in image_inputs:
       self.assertIsInstance(UpperCAmelCase__, Image.Image     )

    # Test not batched input
    __lowercase								=						(
        (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
        * self.image_processor_tester.num_channels
    ) + 2

    for max_patch in self.image_processor_tester.max_patches:
       # Test not batched input
       __lowercase								=						image_processor(
           image_inputs[0], return_tensors="pt", max_patches=UpperCAmelCase__     ).flattened_patches
       self.assertEqual(
           encoded_images.shape, (1, max_patch, expected_hidden_dim), )

       # Test batched
       __lowercase								=						image_processor(
           UpperCAmelCase__, return_tensors="pt", max_patches=UpperCAmelCase__     ).flattened_patches
       self.assertEqual(
           encoded_images.shape, (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim), )


 def  _lowercase			(      self							:      List[str]     ):
    # Initialize image_processor
    __lowercase								=						self.image_processing_class(**self.image_processor_dict     )
    # create random PIL images
    __lowercase								=						prepare_image_inputs(self.image_processor_tester, equal_resolution=UpperCAmelCase__     )
    for image in image_inputs:
       self.assertIsInstance(UpperCAmelCase__, Image.Image     )

    # Test not batched input
    __lowercase								=						(
        (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
        * self.image_processor_tester.num_channels
    ) + 2

    __lowercase								=						True

    for max_patch in self.image_processor_tester.max_patches:
       # Test not batched input
       with self.assertRaises(UpperCAmelCase__     ):
          __lowercase								=						image_processor(
              image_inputs[0], return_tensors="pt", max_patches=UpperCAmelCase__     ).flattened_patches

       __lowercase								=						"Hello"

       __lowercase								=						image_processor(
           image_inputs[0], return_tensors="pt", max_patches=UpperCAmelCase__, header_text=UpperCAmelCase__     ).flattened_patches
       self.assertEqual(
           encoded_images.shape, (1, max_patch, expected_hidden_dim), )

       # Test batched
       __lowercase								=						image_processor(
           UpperCAmelCase__, return_tensors="pt", max_patches=UpperCAmelCase__, header_text=UpperCAmelCase__     ).flattened_patches
       self.assertEqual(
           encoded_images.shape, (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim), )


 def  _lowercase			(      self							:      Dict     ):
    # Initialize image_processor
    __lowercase								=						self.image_processing_class(**self.image_processor_dict     )
    # create random numpy tensors
    __lowercase								=						prepare_image_inputs(self.image_processor_tester, equal_resolution=UpperCAmelCase__, numpify=UpperCAmelCase__     )
    for image in image_inputs:
       self.assertIsInstance(UpperCAmelCase__, np.ndarray     )

    __lowercase								=						(
        (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
        * self.image_processor_tester.num_channels
    ) + 2

    for max_patch in self.image_processor_tester.max_patches:
       # Test not batched input
       __lowercase								=						image_processor(
           image_inputs[0], return_tensors="pt", max_patches=UpperCAmelCase__     ).flattened_patches
       self.assertEqual(
           encoded_images.shape, (1, max_patch, expected_hidden_dim), )

       # Test batched
       __lowercase								=						image_processor(
           UpperCAmelCase__, return_tensors="pt", max_patches=UpperCAmelCase__     ).flattened_patches
       self.assertEqual(
           encoded_images.shape, (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim), )



 def  _lowercase			(      self							:      Any     ):
    # Initialize image_processor
    __lowercase								=						self.image_processing_class(**self.image_processor_dict     )
    # create random PyTorch tensors
    __lowercase								=						prepare_image_inputs(self.image_processor_tester, equal_resolution=UpperCAmelCase__, torchify=UpperCAmelCase__     )
    for image in image_inputs:
       self.assertIsInstance(UpperCAmelCase__, torch.Tensor     )

    # Test not batched input
    __lowercase								=						(
        (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
        * self.image_processor_tester.num_channels
    ) + 2

    for max_patch in self.image_processor_tester.max_patches:
       # Test not batched input
       __lowercase								=						image_processor(
           image_inputs[0], return_tensors="pt", max_patches=UpperCAmelCase__     ).flattened_patches
       self.assertEqual(
           encoded_images.shape, (1, max_patch, expected_hidden_dim), )

       # Test batched
       __lowercase								=						image_processor(
           UpperCAmelCase__, return_tensors="pt", max_patches=UpperCAmelCase__     ).flattened_patches
       self.assertEqual(
           encoded_images.shape, (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim), )







@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11					,reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`."					,)
@require_torch
@require_vision
class  _lowerCAmelCase  (     lowercase					,unittest.TestCase       ):







 """simple docstring"""







 __UpperCAmelCase     : Tuple       		=     PixaStructImageProcessor if is_vision_available() else None


 def  _lowercase			(      self							:      Dict     ):
    __lowercase								=						PixaStructImageProcessingTester(self, num_channels=4     )
    __lowercase								=						3


 @property
 def  _lowercase			(      self							:      Tuple     ):
    return self.image_processor_tester.prepare_image_processor_dict()


 def  _lowercase			(      self							:      str     ):
    __lowercase								=						self.image_processing_class(**self.image_processor_dict     )
    self.assertTrue(hasattr(UpperCAmelCase__, "do_normalize"     )     )
    self.assertTrue(hasattr(UpperCAmelCase__, "do_convert_rgb"     )     )



 def  _lowercase			(      self							:      Optional[int]     ):
    # Initialize image_processor
    __lowercase								=						self.image_processing_class(**self.image_processor_dict     )
    # create random PIL images
    __lowercase								=						prepare_image_inputs(self.image_processor_tester, equal_resolution=UpperCAmelCase__     )
    for image in image_inputs:
       self.assertIsInstance(UpperCAmelCase__, Image.Image     )

    # Test not batched input
    __lowercase								=						(
        (self.image_processor_tester.patch_size["height"] * self.image_processor_tester.patch_size["width"])
        * (self.image_processor_tester.num_channels - 1)
    ) + 2

    for max_patch in self.image_processor_tester.max_patches:
       # Test not batched input
       __lowercase								=						image_processor(
           image_inputs[0], return_tensors="pt", max_patches=UpperCAmelCase__     ).flattened_patches
       self.assertEqual(
           encoded_images.shape, (1, max_patch, expected_hidden_dim), )

       # Test batched
       __lowercase								=						image_processor(
           UpperCAmelCase__, return_tensors="pt", max_patches=UpperCAmelCase__     ).flattened_patches
       self.assertEqual(
           encoded_images.shape, (self.image_processor_tester.batch_size, max_patch, expected_hidden_dim), )







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




