




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.
lowerCamelCase__			:       Dict         =							{'LayoutLMv2Config', 'LayoutLMv3Config'}





@is_pipeline_test
class 	lowerCamelCase_							(				unittest.TestCase ):


	'''simple docstring'''







	lowercase_       =   MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
	lowercase_       =   TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

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



	@require_torch
	def 				lowerCAmelCase_  (							self  :		List[str]						):
							SCREAMING_SNAKE_CASE_     						=      pipeline(
							    task='text-classification'    ,    model='hf-internal-testing/tiny-random-distilbert'    ,    framework='pt'						)

							SCREAMING_SNAKE_CASE_     						=      text_classifier('This is great !'						)
							self.assertEqual(nested_simplify(_lowerCAmelCase						)    ,    [{'label': 'LABEL_0', 'score': 0.504}]						)

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

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

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

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

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

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

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



	@require_torch
	def 				lowerCAmelCase_  (							self  :		Union[str, Any]						):
							import torch

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

							SCREAMING_SNAKE_CASE_     						=      text_classifier('This is great !'						)
							self.assertEqual(nested_simplify(_lowerCAmelCase						)    ,    [{'label': 'LABEL_0', 'score': 0.504}]						)



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

							SCREAMING_SNAKE_CASE_     						=      text_classifier('This is great !'						)
							self.assertEqual(nested_simplify(_lowerCAmelCase						)    ,    [{'label': 'LABEL_0', 'score': 0.504}]						)



	@slow
	@require_torch
	def 				lowerCAmelCase_  (							self  :		List[str]						):
							SCREAMING_SNAKE_CASE_     						=      pipeline('text-classification'						)

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



	@slow
	@require_tf
	def 				lowerCAmelCase_  (							self  :		int						):
							SCREAMING_SNAKE_CASE_     						=      pipeline('text-classification'    ,    framework='tf'						)

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



	def 				lowerCAmelCase_  (							self  :		List[Any]    ,    _lowerCAmelCase  :		Optional[Any]    ,    _lowerCAmelCase  :		int    ,    _lowerCAmelCase  :		Any						):
							SCREAMING_SNAKE_CASE_     						=      TextClassificationPipeline(model=_lowerCAmelCase    ,    tokenizer=_lowerCAmelCase						)
							return text_classifier, ["HuggingFace is in", "This is another test"]




	def 				lowerCAmelCase_  (							self  :		Optional[Any]    ,    _lowerCAmelCase  :		Union[str, Any]    ,    _lowerCAmelCase  :		str						):
							SCREAMING_SNAKE_CASE_     						=      text_classifier.model
							# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
							SCREAMING_SNAKE_CASE_     						=      'HuggingFace is in'
							SCREAMING_SNAKE_CASE_     						=      text_classifier(_lowerCAmelCase						)

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

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

							SCREAMING_SNAKE_CASE_     						=      {'text': 'HuggingFace is in ', 'text_pair': 'Paris is in France'}
							SCREAMING_SNAKE_CASE_     						=      text_classifier(_lowerCAmelCase						)
							self.assertEqual(
							    nested_simplify(_lowerCAmelCase						)    ,    {'label': ANY(_lowerCAmelCase						), 'score': ANY(_lowerCAmelCase						)}    ,    )
							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.
							SCREAMING_SNAKE_CASE_     						=      [['HuggingFace is in ', 'Paris is in France']]
							with self.assertRaises(_lowerCAmelCase						):
													text_classifier(_lowerCAmelCase						)

							# This used to be valid for doing text pairs
							# We're keeping it working because of backward compatibility
							SCREAMING_SNAKE_CASE_     						=      text_classifier([[['HuggingFace is in ', 'Paris is in France']]]						)
							self.assertEqual(
							    nested_simplify(_lowerCAmelCase						)    ,    [{'label': ANY(_lowerCAmelCase						), 'score': ANY(_lowerCAmelCase						)}]    ,    )
							self.assertTrue(outputs[0]['label'] in model.config.idalabel.values()						)





import unittest

import numpy as np

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

if is_vision_available():
   from PIL import Image

   from transformers import LevitImageProcessor





class 	lowerCamelCase_							(				unittest.TestCase ):


 '''simple docstring'''







 def __init__(							self  :		str    ,    _lowerCAmelCase  :		Optional[Any]    ,    _lowerCAmelCase  :		Any=7    ,    _lowerCAmelCase  :		Tuple=3    ,    _lowerCAmelCase  :		List[str]=18    ,    _lowerCAmelCase  :		Any=30    ,    _lowerCAmelCase  :		List[Any]=400    ,    _lowerCAmelCase  :		Dict=True    ,    _lowerCAmelCase  :		Tuple=None    ,    _lowerCAmelCase  :		Tuple=True    ,    _lowerCAmelCase  :		Union[str, Any]=None    ,    _lowerCAmelCase  :		Tuple=True    ,    _lowerCAmelCase  :		List[Any]=[0.5, 0.5, 0.5]    ,    _lowerCAmelCase  :		Union[str, Any]=[0.5, 0.5, 0.5]    ,    ):
       SCREAMING_SNAKE_CASE_     						=      size if size is not None else {'shortest_edge': 18}
       SCREAMING_SNAKE_CASE_     						=      crop_size if crop_size is not None else {'height': 18, 'width': 18}
       SCREAMING_SNAKE_CASE_     						=      parent
       SCREAMING_SNAKE_CASE_     						=      batch_size
       SCREAMING_SNAKE_CASE_     						=      num_channels
       SCREAMING_SNAKE_CASE_     						=      image_size
       SCREAMING_SNAKE_CASE_     						=      min_resolution
       SCREAMING_SNAKE_CASE_     						=      max_resolution
       SCREAMING_SNAKE_CASE_     						=      do_resize
       SCREAMING_SNAKE_CASE_     						=      size
       SCREAMING_SNAKE_CASE_     						=      do_center_crop
       SCREAMING_SNAKE_CASE_     						=      crop_size
       SCREAMING_SNAKE_CASE_     						=      do_normalize
       SCREAMING_SNAKE_CASE_     						=      image_mean
       SCREAMING_SNAKE_CASE_     						=      image_std




 def 				lowerCAmelCase_  (							self  :		List[Any]						):
       return {
           "image_mean": self.image_mean,
           "image_std": self.image_std,
           "do_normalize": self.do_normalize,
           "do_resize": self.do_resize,
           "do_center_crop": self.do_center_crop,
           "size": self.size,
           "crop_size": self.crop_size,
       }





@require_torch
@require_vision
class 	lowerCamelCase_							(				_SCREAMING_SNAKE_CASE	,       unittest.TestCase ):


 '''simple docstring'''







 lowercase_       =   LevitImageProcessor if is_vision_available() else None



 def 				lowerCAmelCase_  (							self  :		List[str]						):
       SCREAMING_SNAKE_CASE_     						=      LevitImageProcessingTester(self						)



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



 def 				lowerCAmelCase_  (							self  :		str						):
       SCREAMING_SNAKE_CASE_     						=      self.image_processing_class(**self.image_processor_dict						)
       self.assertTrue(hasattr(_lowerCAmelCase    ,    'image_mean'						)						)
       self.assertTrue(hasattr(_lowerCAmelCase    ,    'image_std'						)						)
       self.assertTrue(hasattr(_lowerCAmelCase    ,    'do_normalize'						)						)
       self.assertTrue(hasattr(_lowerCAmelCase    ,    'do_resize'						)						)
       self.assertTrue(hasattr(_lowerCAmelCase    ,    'do_center_crop'						)						)
       self.assertTrue(hasattr(_lowerCAmelCase    ,    'size'						)						)



 def 				lowerCAmelCase_  (							self  :		Union[str, Any]						):
       SCREAMING_SNAKE_CASE_     						=      self.image_processing_class.from_dict(self.image_processor_dict						)
       self.assertEqual(image_processor.size    ,    {'shortest_edge': 18}						)
       self.assertEqual(image_processor.crop_size    ,    {'height': 18, 'width': 18}						)

       SCREAMING_SNAKE_CASE_     						=      self.image_processing_class.from_dict(self.image_processor_dict    ,    size=42    ,    crop_size=84						)
       self.assertEqual(image_processor.size    ,    {'shortest_edge': 42}						)
       self.assertEqual(image_processor.crop_size    ,    {'height': 84, 'width': 84}						)



 def 				lowerCAmelCase_  (							self  :		Dict						):
       pass



 def 				lowerCAmelCase_  (							self  :		int						):
       # Initialize image_processing
       SCREAMING_SNAKE_CASE_     						=      self.image_processing_class(**self.image_processor_dict						)
       # create random PIL images
       SCREAMING_SNAKE_CASE_     						=      prepare_image_inputs(self.image_processor_tester    ,    equal_resolution=_lowerCAmelCase						)
       for image in image_inputs:
             self.assertIsInstance(_lowerCAmelCase    ,    Image.Image						)

       # Test not batched input
       SCREAMING_SNAKE_CASE_     						=      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.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )    ,    )

       # Test batched
       SCREAMING_SNAKE_CASE_     						=      image_processing(_lowerCAmelCase    ,    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.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )    ,    )



 def 				lowerCAmelCase_  (							self  :		str						):
       # Initialize image_processing
       SCREAMING_SNAKE_CASE_     						=      self.image_processing_class(**self.image_processor_dict						)
       # create random numpy tensors
       SCREAMING_SNAKE_CASE_     						=      prepare_image_inputs(self.image_processor_tester    ,    equal_resolution=_lowerCAmelCase    ,    numpify=_lowerCAmelCase						)
       for image in image_inputs:
             self.assertIsInstance(_lowerCAmelCase    ,    np.ndarray						)

       # Test not batched input
       SCREAMING_SNAKE_CASE_     						=      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.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )    ,    )

       # Test batched
       SCREAMING_SNAKE_CASE_     						=      image_processing(_lowerCAmelCase    ,    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.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )    ,    )




 def 				lowerCAmelCase_  (							self  :		Tuple						):
       # Initialize image_processing
       SCREAMING_SNAKE_CASE_     						=      self.image_processing_class(**self.image_processor_dict						)
       # create random PyTorch tensors
       SCREAMING_SNAKE_CASE_     						=      prepare_image_inputs(self.image_processor_tester    ,    equal_resolution=_lowerCAmelCase    ,    torchify=_lowerCAmelCase						)
       for image in image_inputs:
             self.assertIsInstance(_lowerCAmelCase    ,    torch.Tensor						)

       # Test not batched input
       SCREAMING_SNAKE_CASE_     						=      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.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )    ,    )

       # Test batched
       SCREAMING_SNAKE_CASE_     						=      image_processing(_lowerCAmelCase    ,    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.crop_size['height'],
               self.image_processor_tester.crop_size['width'],
           )    ,    )