



"""simple docstring"""






import unittest

import requests

from transformers import MODEL_FOR_VISION_2_SEQ_MAPPING, TF_MODEL_FOR_VISION_2_SEQ_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    is_torch_available,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_torch_available():
    from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
    lowerCamelCase				:     List[str]								=  False


if is_vision_available():
    from PIL import Image
else:




    class    __snake_case:




        @staticmethod
        def   A   (   *A_    ,							**A_			):





          '''simple docstring'''



          pass




@is_pipeline_test
@require_vision
class    __snake_case(					unittest.TestCase       ):
    _A       = MODEL_FOR_VISION_2_SEQ_MAPPING
    _A       = TF_MODEL_FOR_VISION_2_SEQ_MAPPING




    def   A   (   self    ,							A_    ,							A_    ,							A_			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			pipeline('''image-to-text'''    ,							model=lowercase_    ,							tokenizer=lowercase_    ,							image_processor=lowercase_			)
      _SCREAMING_SNAKE_CASE        =			[
          Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			),
          '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
      ]
      return pipe, examples




    def   A   (   self    ,							A_    ,							A_			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			pipe(lowercase_			)
      self.assertEqual(
          lowercase_    ,							[
              [{'''generated_text''': ANY(lowercase_			)}],
              [{'''generated_text''': ANY(lowercase_			)}],
          ]    ,							)




    @require_tf
    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			pipeline('''image-to-text'''    ,							model='''hf-internal-testing/tiny-random-vit-gpt2'''    ,							framework='''tf'''			)
      _SCREAMING_SNAKE_CASE        =			'''./tests/fixtures/tests_samples/COCO/000000039769.png'''

      _SCREAMING_SNAKE_CASE        =			pipe(lowercase_			)
      self.assertEqual(
          lowercase_    ,							[
              {
                  '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
              },
          ]    ,							)

      _SCREAMING_SNAKE_CASE        =			pipe([image, image]			)
      self.assertEqual(
          lowercase_    ,							[
              [
                  {
                      '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                  }
              ],
              [
                  {
                      '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                  }
              ],
          ]    ,							)

      _SCREAMING_SNAKE_CASE        =			pipe(lowercase_    ,							max_new_tokens=1			)
      self.assertEqual(
          lowercase_    ,							[{'''generated_text''': '''growth'''}]    ,							)




    @require_torch
    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			pipeline('''image-to-text'''    ,							model='''hf-internal-testing/tiny-random-vit-gpt2'''			)
      _SCREAMING_SNAKE_CASE        =			'''./tests/fixtures/tests_samples/COCO/000000039769.png'''

      _SCREAMING_SNAKE_CASE        =			pipe(lowercase_			)
      self.assertEqual(
          lowercase_    ,							[
              {
                  '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
              },
          ]    ,							)

      _SCREAMING_SNAKE_CASE        =			pipe([image, image]			)
      self.assertEqual(
          lowercase_    ,							[
              [
                  {
                      '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                  }
              ],
              [
                  {
                      '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                  }
              ],
          ]    ,							)




    @require_torch
    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			pipeline('''image-to-text'''    ,							model='''hf-internal-testing/tiny-random-BlipForConditionalGeneration'''			)
      _SCREAMING_SNAKE_CASE        =			'''./tests/fixtures/tests_samples/COCO/000000039769.png'''
      _SCREAMING_SNAKE_CASE        =			'''a photo of'''

      _SCREAMING_SNAKE_CASE        =			pipe(lowercase_    ,							prompt=lowercase_			)
      self.assertTrue(outputs[0]['''generated_text'''].startswith(lowercase_			)			)




    @slow
    @require_torch
    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			pipeline('''image-to-text'''    ,							model='''ydshieh/vit-gpt2-coco-en'''			)
      _SCREAMING_SNAKE_CASE        =			'''./tests/fixtures/tests_samples/COCO/000000039769.png'''

      _SCREAMING_SNAKE_CASE        =			pipe(lowercase_			)
      self.assertEqual(lowercase_    ,							[{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}]			)

      _SCREAMING_SNAKE_CASE        =			pipe([image, image]			)
      self.assertEqual(
          lowercase_    ,							[
              [{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}],
              [{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}],
          ]    ,							)




    @slow
    @require_torch
    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			pipeline('''image-to-text'''    ,							model='''Salesforce/blip-image-captioning-base'''			)
      _SCREAMING_SNAKE_CASE        =			'''https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'''
      _SCREAMING_SNAKE_CASE        =			Image.open(requests.get(lowercase_    ,							stream=lowercase_			).raw			)

      _SCREAMING_SNAKE_CASE        =			pipe(lowercase_			)
      self.assertEqual(lowercase_    ,							[{'''generated_text''': '''a pink pokemon pokemon with a blue shirt and a blue shirt'''}]			)




    @slow
    @require_torch
    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			pipeline('''image-to-text'''    ,							model='''microsoft/git-base-coco'''			)
      _SCREAMING_SNAKE_CASE        =			'''https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'''
      _SCREAMING_SNAKE_CASE        =			Image.open(requests.get(lowercase_    ,							stream=lowercase_			).raw			)

      _SCREAMING_SNAKE_CASE        =			pipe(lowercase_			)
      self.assertEqual(lowercase_    ,							[{'''generated_text''': '''a cartoon of a purple character.'''}]			)




    @slow
    @require_torch
    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			pipeline('''image-to-text'''    ,							model='''Salesforce/blip-image-captioning-base'''			)
      _SCREAMING_SNAKE_CASE        =			'''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'''
      _SCREAMING_SNAKE_CASE        =			Image.open(requests.get(lowercase_    ,							stream=lowercase_			).raw			)

      _SCREAMING_SNAKE_CASE        =			'''a photography of'''

      _SCREAMING_SNAKE_CASE        =			pipe(lowercase_    ,							prompt=lowercase_			)
      self.assertEqual(lowercase_    ,							[{'''generated_text''': '''a photography of a volcano'''}]			)

      with self.assertRaises(lowercase_			):
        _SCREAMING_SNAKE_CASE        =			pipe([image, image]    ,							prompt=[prompt, prompt]			)




    @slow
    @require_torch
    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			pipeline('''image-to-text'''    ,							model='''microsoft/git-base-coco'''			)
      _SCREAMING_SNAKE_CASE        =			'''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'''
      _SCREAMING_SNAKE_CASE        =			Image.open(requests.get(lowercase_    ,							stream=lowercase_			).raw			)

      _SCREAMING_SNAKE_CASE        =			'''a photo of a'''

      _SCREAMING_SNAKE_CASE        =			pipe(lowercase_    ,							prompt=lowercase_			)
      self.assertEqual(lowercase_    ,							[{'''generated_text''': '''a photo of a tent with a tent and a tent in the background.'''}]			)

      with self.assertRaises(lowercase_			):
        _SCREAMING_SNAKE_CASE        =			pipe([image, image]    ,							prompt=[prompt, prompt]			)




    @unittest.skipIf(
        not is_torch_greater_or_equal_than_1_11    ,							reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.'''			)
    @slow
    @require_torch
    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			pipeline('''image-to-text'''    ,							model='''google/pix2struct-ai2d-base'''			)
      _SCREAMING_SNAKE_CASE        =			'''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'''
      _SCREAMING_SNAKE_CASE        =			Image.open(requests.get(lowercase_    ,							stream=lowercase_			).raw			)

      _SCREAMING_SNAKE_CASE        =			'''What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud'''

      _SCREAMING_SNAKE_CASE        =			pipe(lowercase_    ,							prompt=lowercase_			)
      self.assertEqual(lowercase_    ,							[{'''generated_text''': '''ash cloud'''}]			)

      with self.assertRaises(lowercase_			):
        _SCREAMING_SNAKE_CASE        =			pipe([image, image]    ,							prompt=[prompt, prompt]			)




    @slow
    @require_tf
    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			pipeline('''image-to-text'''    ,							model='''ydshieh/vit-gpt2-coco-en'''    ,							framework='''tf'''			)
      _SCREAMING_SNAKE_CASE        =			'''./tests/fixtures/tests_samples/COCO/000000039769.png'''

      _SCREAMING_SNAKE_CASE        =			pipe(lowercase_			)
      self.assertEqual(lowercase_    ,							[{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}]			)

      _SCREAMING_SNAKE_CASE        =			pipe([image, image]			)
      self.assertEqual(
          lowercase_    ,							[
              [{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}],
              [{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}],
          ]    ,							)



import unittest

from cer import CER


lowercase_									=    CER()


class 					A		(				unittest.TestCase					):







    """simple docstring"""





    def 		snake_case__	( self					:			str						)->       Dict:





        '''simple docstring'''







        A__        =  ['White House']
        A__        =  ['white house']
        # S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
        A__        =  cer.compute(predictions=lowercase_,references=lowercase_						)
        self.assertTrue(abs(char_error_rate - 0.1_818_181_818						) < 1E-6						)


    def 		snake_case__	( self					:			str						)->       Dict:





        '''simple docstring'''







        A__        =  ['were wolf']
        A__        =  ['werewolf']
        # S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
        A__        =  cer.compute(predictions=lowercase_,references=lowercase_						)
        self.assertTrue(abs(char_error_rate - 0.1_111_111						) < 1E-6						)

        A__        =  ['werewolf']
        A__        =  ['weae     wolf']
        # S = 1, D = 1, I = 0, N = 8, CER = 0.25
        A__        =  cer.compute(predictions=lowercase_,references=lowercase_						)
        self.assertTrue(abs(char_error_rate - 0.25						) < 1E-6						)

        # consecutive whitespaces case 1
        A__        =  ['were wolf']
        A__        =  ['were               wolf']
        # S = 0, D = 0, I = 0, N = 9, CER = 0
        A__        =  cer.compute(predictions=lowercase_,references=lowercase_						)
        self.assertTrue(abs(char_error_rate - 0.0						) < 1E-6						)

        # consecutive whitespaces case 2
        A__        =  ['were   wolf']
        A__        =  ['were               wolf']
        # S = 0, D = 0, I = 0, N = 9, CER = 0
        A__        =  cer.compute(predictions=lowercase_,references=lowercase_						)
        self.assertTrue(abs(char_error_rate - 0.0						) < 1E-6						)


    def 		snake_case__	( self					:			int						)->       str:





        '''simple docstring'''







        A__        =  ['werewolf']
        A__        =  ['weaewolf']
        # S = 1, D = 0, I = 0, N = 8, CER = 0.125
        A__        =  cer.compute(predictions=lowercase_,references=lowercase_						)
        self.assertTrue(abs(char_error_rate - 0.125						) < 1E-6						)


    def 		snake_case__	( self					:			Optional[int]						)->       Optional[int]:





        '''simple docstring'''







        A__        =  ['werewolf']
        A__        =  ['wereawolf']
        # S = 0, D = 1, I = 0, N = 8, CER = 0.125
        A__        =  cer.compute(predictions=lowercase_,references=lowercase_						)
        self.assertTrue(abs(char_error_rate - 0.125						) < 1E-6						)


    def 		snake_case__	( self					:			List[Any]						)->       Optional[Any]:





        '''simple docstring'''







        A__        =  ['werewolf']
        A__        =  ['wereolf']
        # S = 0, D = 0, I = 1, N = 8, CER = 0.125
        A__        =  cer.compute(predictions=lowercase_,references=lowercase_						)
        self.assertTrue(abs(char_error_rate - 0.125						) < 1E-6						)


    def 		snake_case__	( self					:			str						)->       Tuple:





        '''simple docstring'''







        A__        =  ['werewolf']
        A__        =  cer.compute(predictions=lowercase_,references=lowercase_						)
        self.assertEqual(lowercase_,0.0						)


    def 		snake_case__	( self					:			Optional[int]						)->       Union[str, Any]:





        '''simple docstring'''







        A__        =  ['werewolf', 'I am your father']
        A__        =  cer.compute(predictions=lowercase_,references=lowercase_						)
        self.assertEqual(lowercase_,0.0						)

        A__        =  ['werewolf', 'I am your father', 'doge']
        A__        =  ['werxwolf', 'I       am your father', 'doge']
        # S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
        A__        =  cer.compute(predictions=lowercase_,references=lowercase_						)
        self.assertTrue(abs(char_error_rate - 0.03_571_428						) < 1E-6						)


    def 		snake_case__	( self					:			List[str]						)->       Union[str, Any]:





        '''simple docstring'''







        A__        =  ['My hovercraft', 'is full of eels']
        A__        =  ['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
        A__        =  cer.compute(predictions=lowercase_,references=lowercase_,concatenate_texts=lowercase_						)
        self.assertTrue(abs(char_error_rate - 0.071_428						) < 1E-6						)


    def 		snake_case__	( self					:			Dict						)->       List[str]:





        '''simple docstring'''







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

        A__        =  ['我能吞下玻璃', '而不伤身体']
        A__        =  ['我    能 吞 下 玻 璃', '而不伤身体']
        # S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
        A__        =  cer.compute(predictions=lowercase_,references=lowercase_						)
        self.assertTrue(abs(char_error_rate - 0.454_545_454_545						) < 1E-6						)

        A__        =  ['我能吞下玻璃而不伤身体']
        A__        =  cer.compute(predictions=lowercase_,references=lowercase_						)
        self.assertFalse(lowercase_,0.0						)


    def 		snake_case__	( self					:			Any						)->       List[str]:





        '''simple docstring'''







        A__        =  ['']
        A__        =  ['Hypothesis']
        with self.assertRaises(lowercase_						):
            cer.compute(predictions=lowercase_,references=lowercase_						)


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

