





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.
_UpperCAmelCase							:			Any        =							{"""LayoutLMv2Config""", """LayoutLMv3Config"""}

@is_pipeline_test
class   lowerCAmelCase				(   unittest.TestCase     ):
 UpperCAmelCase__						=						MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
 UpperCAmelCase__						=						TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

 if model_mapping is not None:
  UpperCAmelCase__						=						{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
 if tf_model_mapping is not None:
  UpperCAmelCase__						=						{
      config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
  }
 @require_torch
 def 	A_      ( self    : Any      )     ->      Optional[Any]:
     lowerCamelCase__       : Optional[Any]   					=						pipeline(
         task='text-classification'     ,	model='hf-internal-testing/tiny-random-distilbert'     ,	framework='pt'      )

     lowerCamelCase__       : Optional[Any]   					=						text_classifier('This is great !'      )
     self.assertEqual(nested_simplify(UpperCAmelCase      )     ,	[{'label': 'LABEL_0', 'score': 0.5_0_4}]      )

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

     lowerCamelCase__       : str   					=						text_classifier(['This is great !', 'This is bad']     ,	top_k=2      )
     self.assertEqual(
         nested_simplify(UpperCAmelCase      )     ,	[
             [{'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}],
         ]     ,	)

     lowerCamelCase__       : Optional[Any]   					=						text_classifier('This is great !'     ,	top_k=1      )
     self.assertEqual(nested_simplify(UpperCAmelCase      )     ,	[{'label': 'LABEL_0', 'score': 0.5_0_4}]      )

     # Legacy behavior
     lowerCamelCase__       : Any   					=						text_classifier('This is great !'     ,	return_all_scores=UpperCAmelCase      )
     self.assertEqual(nested_simplify(UpperCAmelCase      )     ,	[{'label': 'LABEL_0', 'score': 0.5_0_4}]      )

     lowerCamelCase__       : int   					=						text_classifier('This is great !'     ,	return_all_scores=UpperCAmelCase      )
     self.assertEqual(
         nested_simplify(UpperCAmelCase      )     ,	[[{'label': 'LABEL_0', 'score': 0.5_0_4}, {'label': 'LABEL_1', 'score': 0.4_9_6}]]      )

     lowerCamelCase__       : Tuple   					=						text_classifier(['This is great !', 'Something else']     ,	return_all_scores=UpperCAmelCase      )
     self.assertEqual(
         nested_simplify(UpperCAmelCase      )     ,	[
             [{'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}],
         ]     ,	)

     lowerCamelCase__       : str   					=						text_classifier(['This is great !', 'Something else']     ,	return_all_scores=UpperCAmelCase      )
     self.assertEqual(
         nested_simplify(UpperCAmelCase      )     ,	[
             {'label': 'LABEL_0', 'score': 0.5_0_4},
             {'label': 'LABEL_0', 'score': 0.5_0_4},
         ]     ,	)
 @require_torch
 def 	A_      ( self    : Dict      )     ->      Any:
     import torch

     lowerCamelCase__       : Dict   					=						pipeline(
         task='text-classification'     ,	model='hf-internal-testing/tiny-random-distilbert'     ,	framework='pt'     ,	device=torch.device('cpu'      )     ,	)

     lowerCamelCase__       : Tuple   					=						text_classifier('This is great !'      )
     self.assertEqual(nested_simplify(UpperCAmelCase      )     ,	[{'label': 'LABEL_0', 'score': 0.5_0_4}]      )
 @require_tf
 def 	A_      ( self    : Optional[Any]      )     ->      Dict:
     lowerCamelCase__       : Dict   					=						pipeline(
         task='text-classification'     ,	model='hf-internal-testing/tiny-random-distilbert'     ,	framework='tf'      )

     lowerCamelCase__       : Dict   					=						text_classifier('This is great !'      )
     self.assertEqual(nested_simplify(UpperCAmelCase      )     ,	[{'label': 'LABEL_0', 'score': 0.5_0_4}]      )
 @slow
 @require_torch
 def 	A_      ( self    : List[Any]      )     ->      str:
     lowerCamelCase__       : str   					=						pipeline('text-classification'      )

     lowerCamelCase__       : str   					=						text_classifier('This is great !'      )
     self.assertEqual(nested_simplify(UpperCAmelCase      )     ,	[{'label': 'POSITIVE', 'score': 1.0}]      )
     lowerCamelCase__       : str   					=						text_classifier('This is bad !'      )
     self.assertEqual(nested_simplify(UpperCAmelCase      )     ,	[{'label': 'NEGATIVE', 'score': 1.0}]      )
     lowerCamelCase__       : List[Any]   					=						text_classifier('Birds are a type of animal'      )
     self.assertEqual(nested_simplify(UpperCAmelCase      )     ,	[{'label': 'POSITIVE', 'score': 0.9_8_8}]      )
 @slow
 @require_tf
 def 	A_      ( self    : Tuple      )     ->      Optional[int]:
     lowerCamelCase__       : Any   					=						pipeline('text-classification'     ,	framework='tf'      )

     lowerCamelCase__       : Any   					=						text_classifier('This is great !'      )
     self.assertEqual(nested_simplify(UpperCAmelCase      )     ,	[{'label': 'POSITIVE', 'score': 1.0}]      )
     lowerCamelCase__       : int   					=						text_classifier('This is bad !'      )
     self.assertEqual(nested_simplify(UpperCAmelCase      )     ,	[{'label': 'NEGATIVE', 'score': 1.0}]      )
     lowerCamelCase__       : Dict   					=						text_classifier('Birds are a type of animal'      )
     self.assertEqual(nested_simplify(UpperCAmelCase      )     ,	[{'label': 'POSITIVE', 'score': 0.9_8_8}]      )
 def 	A_      ( self    : Union[str, Any]     ,	UpperCAmelCase    : List[str]     ,	UpperCAmelCase    : Dict     ,	UpperCAmelCase    : Optional[int]      )     ->      Dict:
     lowerCamelCase__       : str   					=						TextClassificationPipeline(model=UpperCAmelCase     ,	tokenizer=UpperCAmelCase      )
     return text_classifier, ["HuggingFace is in", "This is another test"]







 def 	A_      ( self    : Dict     ,	UpperCAmelCase    : Optional[Any]     ,	UpperCAmelCase    : Tuple      )     ->      Optional[Any]:
     lowerCamelCase__       : Optional[Any]   					=						text_classifier.model
     # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
     lowerCamelCase__       : List[Any]   					=						'HuggingFace is in'
     lowerCamelCase__       : Optional[Any]   					=						text_classifier(UpperCAmelCase      )

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

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

     lowerCamelCase__       : Optional[int]   					=						{'text': 'HuggingFace is in ', 'text_pair': 'Paris is in France'}
     lowerCamelCase__       : Tuple   					=						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.
     lowerCamelCase__       : Optional[int]   					=						[['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
     lowerCamelCase__       : Union[str, Any]   					=						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()      )












import unittest

import numpy as np

from transformers.testing_utils import is_flaky, 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

if is_vision_available():
    from PIL import Image

    from transformers import DonutImageProcessor

class   lowerCAmelCase				(   unittest.TestCase     ):
 def __init__( self    : Any     ,	UpperCAmelCase    : Optional[int]     ,	UpperCAmelCase    : Dict=7     ,	UpperCAmelCase    : Union[str, Any]=3     ,	UpperCAmelCase    : List[str]=18     ,	UpperCAmelCase    : Dict=30     ,	UpperCAmelCase    : List[Any]=400     ,	UpperCAmelCase    : Optional[Any]=True     ,	UpperCAmelCase    : Tuple=None     ,	UpperCAmelCase    : str=True     ,	UpperCAmelCase    : Tuple=False     ,	UpperCAmelCase    : List[str]=True     ,	UpperCAmelCase    : Optional[int]=True     ,	UpperCAmelCase    : Optional[Any]=[0.5, 0.5, 0.5]     ,	UpperCAmelCase    : List[str]=[0.5, 0.5, 0.5]     ,	)     ->      List[str]:
     lowerCamelCase__       : List[Any]   					=						parent
     lowerCamelCase__       : str   					=						batch_size
     lowerCamelCase__       : List[str]   					=						num_channels
     lowerCamelCase__       : str   					=						image_size
     lowerCamelCase__       : List[Any]   					=						min_resolution
     lowerCamelCase__       : int   					=						max_resolution
     lowerCamelCase__       : int   					=						do_resize
     lowerCamelCase__       : int   					=						size if size is not None else {'height': 18, 'width': 20}
     lowerCamelCase__       : Tuple   					=						do_thumbnail
     lowerCamelCase__       : str   					=						do_align_axis
     lowerCamelCase__       : str   					=						do_pad
     lowerCamelCase__       : Optional[Any]   					=						do_normalize
     lowerCamelCase__       : List[str]   					=						image_mean
     lowerCamelCase__       : Dict   					=						image_std







 def 	A_      ( self    : str      )     ->      Any:
     return {
         "do_resize": self.do_resize,
         "size": self.size,
         "do_thumbnail": self.do_thumbnail,
         "do_align_long_axis": self.do_align_axis,
         "do_pad": self.do_pad,
         "do_normalize": self.do_normalize,
         "image_mean": self.image_mean,
         "image_std": self.image_std,
     }


@require_torch
@require_vision
class   lowerCAmelCase				(   __UpperCamelCase,    unittest.TestCase     ):
 UpperCAmelCase__						=						DonutImageProcessor if is_vision_available() else None
 def 	A_      ( self    : List[Any]      )     ->      int:
     lowerCamelCase__       : Union[str, Any]   					=						DonutImageProcessingTester(self      )
 @property
 def 	A_      ( self    : Dict      )     ->      Union[str, Any]:
     return self.image_processor_tester.prepare_image_processor_dict()
 def 	A_      ( self    : Dict      )     ->      Any:
     lowerCamelCase__       : Tuple   					=						self.image_processing_class(**self.image_processor_dict      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'do_resize'      )      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'size'      )      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'do_thumbnail'      )      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'do_align_long_axis'      )      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'do_pad'      )      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'do_normalize'      )      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'image_mean'      )      )
     self.assertTrue(hasattr(UpperCAmelCase     ,	'image_std'      )      )
 def 	A_      ( self    : Tuple      )     ->      Union[str, Any]:
     lowerCamelCase__       : List[Any]   					=						self.image_processing_class.from_dict(self.image_processor_dict      )
     self.assertEqual(image_processor.size     ,	{'height': 18, 'width': 20}      )

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

     # Previous config had dimensions in (width, height) order
     lowerCamelCase__       : Dict   					=						self.image_processing_class.from_dict(self.image_processor_dict     ,	size=(42, 84)      )
     self.assertEqual(image_processor.size     ,	{'height': 84, 'width': 42}      )
 def 	A_      ( self    : Optional[Any]      )     ->      List[str]:
     pass
 @is_flaky()
 def 	A_      ( self    : List[str]      )     ->      Any:
     # Initialize image_processing
     lowerCamelCase__       : str   					=						self.image_processing_class(**self.image_processor_dict      )
     # create random PIL images
     lowerCamelCase__       : List[str]   					=						prepare_image_inputs(self.image_processor_tester     ,	equal_resolution=UpperCAmelCase      )
     for image in image_inputs:
         self.assertIsInstance(UpperCAmelCase     ,	Image.Image      )

     # Test not batched input
     lowerCamelCase__       : List[Any]   					=						image_processing(image_inputs[0]     ,	return_tensors='pt'      ).pixel_values
     self.assertEqual(
         encoded_images.shape     ,	(
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['height'],
             self.image_processor_tester.size['width'],
         )     ,	)

     # Test batched
     lowerCamelCase__       : Tuple   					=						image_processing(UpperCAmelCase     ,	return_tensors='pt'      ).pixel_values
     self.assertEqual(
         encoded_images.shape     ,	(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['height'],
             self.image_processor_tester.size['width'],
         )     ,	)
 @is_flaky()
 def 	A_      ( self    : int      )     ->      Tuple:
     # Initialize image_processing
     lowerCamelCase__       : Tuple   					=						self.image_processing_class(**self.image_processor_dict      )
     # create random numpy tensors
     lowerCamelCase__       : List[str]   					=						prepare_image_inputs(self.image_processor_tester     ,	equal_resolution=UpperCAmelCase     ,	numpify=UpperCAmelCase      )
     for image in image_inputs:
         self.assertIsInstance(UpperCAmelCase     ,	np.ndarray      )

     # Test not batched input
     lowerCamelCase__       : Optional[Any]   					=						image_processing(image_inputs[0]     ,	return_tensors='pt'      ).pixel_values
     self.assertEqual(
         encoded_images.shape     ,	(
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['height'],
             self.image_processor_tester.size['width'],
         )     ,	)

     # Test batched
     lowerCamelCase__       : Tuple   					=						image_processing(UpperCAmelCase     ,	return_tensors='pt'      ).pixel_values
     self.assertEqual(
         encoded_images.shape     ,	(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['height'],
             self.image_processor_tester.size['width'],
         )     ,	)







 @is_flaky()
 def 	A_      ( self    : Any      )     ->      Tuple:
     # Initialize image_processing
     lowerCamelCase__       : int   					=						self.image_processing_class(**self.image_processor_dict      )
     # create random PyTorch tensors
     lowerCamelCase__       : Tuple   					=						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
     lowerCamelCase__       : List[str]   					=						image_processing(image_inputs[0]     ,	return_tensors='pt'      ).pixel_values
     self.assertEqual(
         encoded_images.shape     ,	(
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['height'],
             self.image_processor_tester.size['width'],
         )     ,	)

     # Test batched
     lowerCamelCase__       : Dict   					=						image_processing(UpperCAmelCase     ,	return_tensors='pt'      ).pixel_values
     self.assertEqual(
         encoded_images.shape     ,	(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.size['height'],
             self.image_processor_tester.size['width'],
         )     ,	)





