


import unittest

from transformers import (
    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    PreTrainedTokenizer,
    is_vision_available,
)
from transformers.pipelines import ImageClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
 from PIL import Image
else:





 class     __a     :
  @staticmethod
  def 						SCREAMING_SNAKE_CASE__  (  *lowerCAmelCase__							,					**lowerCAmelCase__					)   ->							Dict:




         '''simple docstring'''
         pass





@is_pipeline_test
@require_torch_or_tf
@require_vision
class     __a     (       unittest.TestCase ):
 __lowercase					:				int  	=					MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
 __lowercase					:				str  	=					TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
 def 						SCREAMING_SNAKE_CASE__  (  self							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)   ->							Optional[Any]:




        '''simple docstring'''
        lowercase__:  Dict         =  ImageClassificationPipeline(model=lowerCAmelCase__							,					image_processor=lowerCAmelCase__							,					top_k=2					)
        lowercase__:  Dict         =  [
            Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'					),
            'http://images.cocodataset.org/val2017/000000039769.jpg',
        ]
        return image_classifier, examples
 def 						SCREAMING_SNAKE_CASE__  (  self							,					lowerCAmelCase__							,					lowerCAmelCase__					)   ->							int:




        '''simple docstring'''
        lowercase__:  List[Any]         =  image_classifier('./tests/fixtures/tests_samples/COCO/000000039769.png'					)

        self.assertEqual(
            lowerCAmelCase__							,					[
                {'score': ANY(lowerCAmelCase__					), 'label': ANY(lowerCAmelCase__					)},
                {'score': ANY(lowerCAmelCase__					), 'label': ANY(lowerCAmelCase__					)},
            ]							,					)

        import datasets

        lowercase__:  Optional[int]         =  datasets.load_dataset('hf-internal-testing/fixtures_image_utils'							,					'image'							,					split='test'					)

        # Accepts URL + PIL.Image + lists
        lowercase__:  Tuple         =  image_classifier(
            [
                Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'					),
                'http://images.cocodataset.org/val2017/000000039769.jpg',
                # RGBA
                dataset[0]['file'],
                # LA
                dataset[1]['file'],
                # L
                dataset[2]['file'],
            ]					)
        self.assertEqual(
            lowerCAmelCase__							,					[
                [
                    {'score': ANY(lowerCAmelCase__					), 'label': ANY(lowerCAmelCase__					)},
                    {'score': ANY(lowerCAmelCase__					), 'label': ANY(lowerCAmelCase__					)},
                ],
                [
                    {'score': ANY(lowerCAmelCase__					), 'label': ANY(lowerCAmelCase__					)},
                    {'score': ANY(lowerCAmelCase__					), 'label': ANY(lowerCAmelCase__					)},
                ],
                [
                    {'score': ANY(lowerCAmelCase__					), 'label': ANY(lowerCAmelCase__					)},
                    {'score': ANY(lowerCAmelCase__					), 'label': ANY(lowerCAmelCase__					)},
                ],
                [
                    {'score': ANY(lowerCAmelCase__					), 'label': ANY(lowerCAmelCase__					)},
                    {'score': ANY(lowerCAmelCase__					), 'label': ANY(lowerCAmelCase__					)},
                ],
                [
                    {'score': ANY(lowerCAmelCase__					), 'label': ANY(lowerCAmelCase__					)},
                    {'score': ANY(lowerCAmelCase__					), 'label': ANY(lowerCAmelCase__					)},
                ],
            ]							,					)
 @require_torch
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							int:




        '''simple docstring'''
        lowercase__:  int         =  'hf-internal-testing/tiny-random-vit'
        lowercase__:  Optional[Any]         =  pipeline('image-classification'							,					model=lowerCAmelCase__					)

        lowercase__:  Any         =  image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'					)
        self.assertEqual(
            nested_simplify(lowerCAmelCase__							,					decimals=4					)							,					[{'label': 'LABEL_1', 'score': 0.5_7_4}, {'label': 'LABEL_0', 'score': 0.4_2_6}]							,					)

        lowercase__:  Tuple         =  image_classifier(
            [
                'http://images.cocodataset.org/val2017/000000039769.jpg',
                'http://images.cocodataset.org/val2017/000000039769.jpg',
            ]							,					top_k=2							,					)
        self.assertEqual(
            nested_simplify(lowerCAmelCase__							,					decimals=4					)							,					[
                [{'label': 'LABEL_1', 'score': 0.5_7_4}, {'label': 'LABEL_0', 'score': 0.4_2_6}],
                [{'label': 'LABEL_1', 'score': 0.5_7_4}, {'label': 'LABEL_0', 'score': 0.4_2_6}],
            ]							,					)
 @require_tf
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[int]:




        '''simple docstring'''
        lowercase__:  Optional[Any]         =  'hf-internal-testing/tiny-random-vit'
        lowercase__:  Dict         =  pipeline('image-classification'							,					model=lowerCAmelCase__							,					framework='tf'					)

        lowercase__:  List[str]         =  image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'					)
        self.assertEqual(
            nested_simplify(lowerCAmelCase__							,					decimals=4					)							,					[{'label': 'LABEL_1', 'score': 0.5_7_4}, {'label': 'LABEL_0', 'score': 0.4_2_6}]							,					)

        lowercase__:  Dict         =  image_classifier(
            [
                'http://images.cocodataset.org/val2017/000000039769.jpg',
                'http://images.cocodataset.org/val2017/000000039769.jpg',
            ]							,					top_k=2							,					)
        self.assertEqual(
            nested_simplify(lowerCAmelCase__							,					decimals=4					)							,					[
                [{'label': 'LABEL_1', 'score': 0.5_7_4}, {'label': 'LABEL_0', 'score': 0.4_2_6}],
                [{'label': 'LABEL_1', 'score': 0.5_7_4}, {'label': 'LABEL_0', 'score': 0.4_2_6}],
            ]							,					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							str:




        '''simple docstring'''
        lowercase__:  Dict         =  PreTrainedTokenizer()

        # Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
        lowercase__:  str         =  pipeline(
            'image-classification'							,					model='hf-internal-testing/tiny-random-vit'							,					tokenizer=lowerCAmelCase__					)

        self.assertIs(image_classifier.tokenizer							,					lowerCAmelCase__					)
 @slow
 @require_torch
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Any:




        '''simple docstring'''
        # Perceiver is not tested by `run_pipeline_test` properly.
        # That is because the type of feature_extractor and model preprocessor need to be kept
        # in sync, which is not the case in the current design
        lowercase__:  Tuple         =  pipeline('image-classification'							,					model='deepmind/vision-perceiver-conv'					)
        lowercase__:  Dict         =  image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'					)
        self.assertEqual(
            nested_simplify(lowerCAmelCase__							,					decimals=4					)							,					[
                {'score': 0.4_3_8_5, 'label': 'tabby, tabby cat'},
                {'score': 0.3_2_1, 'label': 'tiger cat'},
                {'score': 0.0_5_0_2, 'label': 'Egyptian cat'},
                {'score': 0.0_1_3_7, 'label': 'crib, cot'},
                {'score': 0.0_0_7, 'label': 'radiator'},
            ]							,					)

        lowercase__:  str         =  pipeline('image-classification'							,					model='deepmind/vision-perceiver-fourier'					)
        lowercase__:  Union[str, Any]         =  image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'					)
        self.assertEqual(
            nested_simplify(lowerCAmelCase__							,					decimals=4					)							,					[
                {'score': 0.5_6_5_8, 'label': 'tabby, tabby cat'},
                {'score': 0.1_3_0_9, 'label': 'tiger cat'},
                {'score': 0.0_7_2_2, 'label': 'Egyptian cat'},
                {'score': 0.0_7_0_7, 'label': 'remote control, remote'},
                {'score': 0.0_0_8_2, 'label': 'computer keyboard, keypad'},
            ]							,					)

        lowercase__:  List[str]         =  pipeline('image-classification'							,					model='deepmind/vision-perceiver-learned'					)
        lowercase__:  Optional[int]         =  image_classifier('http://images.cocodataset.org/val2017/000000039769.jpg'					)
        self.assertEqual(
            nested_simplify(lowerCAmelCase__							,					decimals=4					)							,					[
                {'score': 0.3_0_2_2, 'label': 'tabby, tabby cat'},
                {'score': 0.2_3_6_2, 'label': 'Egyptian cat'},
                {'score': 0.1_8_5_6, 'label': 'tiger cat'},
                {'score': 0.0_3_2_4, 'label': 'remote control, remote'},
                {'score': 0.0_0_9_6, 'label': 'quilt, comforter, comfort, puff'},
            ]							,					)






import unittest

from cer import CER


__lowerCAmelCase									=				CER()





class     __a     (       unittest.TestCase ):
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[Any]:




        '''simple docstring'''
        lowercase__:  Optional[Any]         =  ['White House']
        lowercase__:  Optional[int]         =  ['white house']
        # S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
        lowercase__:  str         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.1_8_1_8_1_8_1_8_1_8					) < 1E-6					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[str]:




        '''simple docstring'''
        lowercase__:  Union[str, Any]         =  ['were wolf']
        lowercase__:  Union[str, Any]         =  ['werewolf']
        # S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
        lowercase__:  Tuple         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.1_1_1_1_1_1_1					) < 1E-6					)

        lowercase__:  Dict         =  ['werewolf']
        lowercase__:  Optional[Any]         =  ['weae     wolf']
        # S = 1, D = 1, I = 0, N = 8, CER = 0.25
        lowercase__:  Optional[int]         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.2_5					) < 1E-6					)

        # consecutive whitespaces case 1
        lowercase__:  Union[str, Any]         =  ['were wolf']
        lowercase__:  int         =  ['were               wolf']
        # S = 0, D = 0, I = 0, N = 9, CER = 0
        lowercase__:  int         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.0					) < 1E-6					)

        # consecutive whitespaces case 2
        lowercase__:  List[str]         =  ['were   wolf']
        lowercase__:  Tuple         =  ['were               wolf']
        # S = 0, D = 0, I = 0, N = 9, CER = 0
        lowercase__:  Dict         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.0					) < 1E-6					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[str]:




        '''simple docstring'''
        lowercase__:  Optional[int]         =  ['werewolf']
        lowercase__:  Dict         =  ['weaewolf']
        # S = 1, D = 0, I = 0, N = 8, CER = 0.125
        lowercase__:  int         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.1_2_5					) < 1E-6					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[Any]:




        '''simple docstring'''
        lowercase__:  int         =  ['werewolf']
        lowercase__:  Optional[int]         =  ['wereawolf']
        # S = 0, D = 1, I = 0, N = 8, CER = 0.125
        lowercase__:  Dict         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.1_2_5					) < 1E-6					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[Any]:




        '''simple docstring'''
        lowercase__:  Optional[Any]         =  ['werewolf']
        lowercase__:  List[str]         =  ['wereolf']
        # S = 0, D = 0, I = 1, N = 8, CER = 0.125
        lowercase__:  Optional[Any]         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.1_2_5					) < 1E-6					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[int]:




        '''simple docstring'''
        lowercase__:  List[str]         =  ['werewolf']
        lowercase__:  List[Any]         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertEqual(lowerCAmelCase__							,					0.0					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[Any]:




        '''simple docstring'''
        lowercase__:  int         =  ['werewolf', 'I am your father']
        lowercase__:  Union[str, Any]         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertEqual(lowerCAmelCase__							,					0.0					)

        lowercase__:  List[str]         =  ['werewolf', 'I am your father', 'doge']
        lowercase__:  Any         =  ['werxwolf', 'I       am your father', 'doge']
        # S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
        lowercase__:  int         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.0_3_5_7_1_4_2_8					) < 1E-6					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Optional[int]:




        '''simple docstring'''
        lowercase__:  List[Any]         =  ['My hovercraft', 'is full of eels']
        lowercase__:  Union[str, Any]         =  ['My hovercraft is full', ' of eels']
        # S = 0, D = 0, I = 2, N = 28, CER = 2 / 28
        # whitespace at the front of " of eels" will be strip during preporcessing
        # so need to insert 2 whitespaces
        lowercase__:  Optional[int]         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__							,					concatenate_texts=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.0_7_1_4_2_8					) < 1E-6					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Dict:




        '''simple docstring'''
        lowercase__:  Dict         =  ['我能吞下玻璃而不伤身体']
        lowercase__:  Tuple         =  [' 能吞虾玻璃而 不霜身体啦']
        # S = 3, D = 2, I = 0, N = 11, CER = 5 / 11
        lowercase__:  str         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.4_5_4_5_4_5_4_5_4_5					) < 1E-6					)

        lowercase__:  Any         =  ['我能吞下玻璃', '而不伤身体']
        lowercase__:  Union[str, Any]         =  ['我    能 吞 下 玻 璃', '而不伤身体']
        # S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
        lowercase__:  Optional[Any]         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertTrue(abs(char_error_rate - 0.4_5_4_5_4_5_4_5_4_5_4_5					) < 1E-6					)

        lowercase__:  int         =  ['我能吞下玻璃而不伤身体']
        lowercase__:  Any         =  cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)
        self.assertFalse(lowerCAmelCase__							,					0.0					)
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							List[str]:




        '''simple docstring'''
        lowercase__:  Dict         =  ['']
        lowercase__:  int         =  ['Hypothesis']
        with self.assertRaises(lowerCAmelCase__					):
               cer.compute(predictions=lowerCAmelCase__							,					references=lowerCAmelCase__					)


if __name__ == "__main__":
 unittest.main()


