

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:
     lowercase_									=    False


if is_vision_available():
     from PIL import Image
else:


     class 					A		:







         """simple docstring"""





         @staticmethod
         def 		snake_case__	( *lowercase_					:			Union[str, Any],**lowercase_					:			Any						)->       Any:





             '''simple docstring'''







             pass


@is_pipeline_test
@require_vision
class 					A		(				unittest.TestCase					):







    """simple docstring"""





    lowerCamelCase						      =				MODEL_FOR_VISION_2_SEQ_MAPPING
    lowerCamelCase						      =				TF_MODEL_FOR_VISION_2_SEQ_MAPPING


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





        '''simple docstring'''







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


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





        '''simple docstring'''







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


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





        '''simple docstring'''







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

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

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

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


    @require_torch
    def 		snake_case__	( self					:			Any						)->       Any:





        '''simple docstring'''







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

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

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


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





        '''simple docstring'''







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

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


    @slow
    @require_torch
    def 		snake_case__	( self					:			Dict						)->       Any:





        '''simple docstring'''







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

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

        A__        =  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 		snake_case__	( self					:			Optional[Any]						)->       List[Any]:





        '''simple docstring'''







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

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


    @slow
    @require_torch
    def 		snake_case__	( self					:			Tuple						)->       List[str]:





        '''simple docstring'''







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

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


    @slow
    @require_torch
    def 		snake_case__	( self					:			Optional[Any]						)->       Optional[int]:





        '''simple docstring'''







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

        A__        =  'a photography of'

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

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


    @slow
    @require_torch
    def 		snake_case__	( self					:			List[str]						)->       str:





        '''simple docstring'''







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

        A__        =  'a photo of a'

        A__        =  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_						):
            A__        =  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 		snake_case__	( self					:			int						)->       Optional[int]:





        '''simple docstring'''







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

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

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

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


    @slow
    @require_tf
    def 		snake_case__	( self					:			Optional[int]						)->       Tuple:





        '''simple docstring'''







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

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

        A__        =  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 timeit


class 					A		:







    """simple docstring"""





    def __init__( self					:			Optional[int],lowercase_					:			Union[str, Any]						)->       List[str]:





        '''simple docstring'''







        if isinstance(lowercase_,lowercase_						):  # Initializes a matrix identical to the one provided.
            A__        =  arg
            A__        =  len(lowercase_						)
        else:  # Initializes a square matrix of the given size and set values to zero.
            A__        =  arg
            A__        =  [[0 for _ in range(self.n						)] for _ in range(self.n						)]


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





        '''simple docstring'''







        A__        =  Matrix(self.n						)
        for i in range(self.n						):
            for j in range(self.n						):
                for k in range(self.n						):
                    matrix.t[i][j] += self.t[i][k] * b.t[k][j]
        return matrix




def 		_snake_case(       SCREAMING_SNAKE_CASE__     :      List[Any]       ,     SCREAMING_SNAKE_CASE__     :      Optional[Any] )				-> Union[str, Any]:





    '''simple docstring'''






    A__        =  Matrix([[1, 0], [0, 1]] )
    while b > 0:
        if b & 1:
            matrix *= a
        a *= a
        b >>= 1
    return matrix
def 		_snake_case(       SCREAMING_SNAKE_CASE__     :      Optional[int]       ,     SCREAMING_SNAKE_CASE__     :      str       ,     SCREAMING_SNAKE_CASE__     :      Any )				-> List[str]:





    '''simple docstring'''






    if n == 1:
        return fa
    elif n == 2:
        return fa
    A__        =  Matrix([[1, 1], [1, 0]] )
    A__        =  modular_exponentiation(SCREAMING_SNAKE_CASE__       ,     n - 2 )
    return fa * matrix.t[0][0] + fa * matrix.t[0][1]
def 		_snake_case(       SCREAMING_SNAKE_CASE__     :      Dict       ,     SCREAMING_SNAKE_CASE__     :      Tuple       ,     SCREAMING_SNAKE_CASE__     :      Tuple )				-> Dict:





    '''simple docstring'''






    if n == 1:
        return fa
    elif n == 2:
        return fa

    A__        =  fa
    A__        =  fa
    n -= 2

    while n > 0:
        A__ ,		A__        =  fn_a + fn_a, fn_a
        n -= 1

    return fn_a
def 		_snake_case(       )				-> Optional[Any]:





    '''simple docstring'''






    A__        =  '\nfrom random import randint\nfrom __main__ import fibonacci_with_matrix_exponentiation\n'
    A__        =  'fibonacci_with_matrix_exponentiation(randint(1,70000), 1, 1)'
    A__        =  timeit.timeit(setup=SCREAMING_SNAKE_CASE__       ,     stmt=SCREAMING_SNAKE_CASE__       ,     number=100 )
    print('With matrix exponentiation the average execution time is '       ,     exec_time / 100 )
    return exec_time
def 		_snake_case(       )				-> Optional[int]:





    '''simple docstring'''






    A__        =  '\nfrom random import randint\nfrom __main__ import simple_fibonacci\n'
    A__        =  'simple_fibonacci(randint(1,70000), 1, 1)'
    A__        =  timeit.timeit(setup=SCREAMING_SNAKE_CASE__       ,     stmt=SCREAMING_SNAKE_CASE__       ,     number=100 )
    print(
        'Without matrix exponentiation the average execution time is '       ,     exec_time / 100 )
    return exec_time
def 		_snake_case(       )				-> Union[str, Any]:





    '''simple docstring'''






    matrix_exponentiation_time()
    simple_fibonacci_time()


if __name__ == "__main__":
     main()

