

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 			__lowerCAmelCase					:




    @staticmethod
    def   A__					(    *lowerCAmelCase							,       **lowerCAmelCase )  ->			List[str]:
          '''simple docstring'''






          pass





@is_pipeline_test
@require_vision
class 			__lowerCAmelCase					( unittest.TestCase							):
  _a						      =  MODEL_FOR_VISION_2_SEQ_MAPPING
  _a						      =  TF_MODEL_FOR_VISION_2_SEQ_MAPPING




  def   A__					(    self							,       lowerCAmelCase							,       lowerCAmelCase							,       lowerCAmelCase )  ->			List[str]:
        '''simple docstring'''






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




  def   A__					(    self							,       lowerCAmelCase							,       lowerCAmelCase )  ->			int:
        '''simple docstring'''






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




  @require_tf
  def   A__					(    self )  ->			Tuple:
        '''simple docstring'''






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

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

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

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




  @require_torch
  def   A__					(    self )  ->			str:
        '''simple docstring'''






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

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

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




  @require_torch
  def   A__					(    self )  ->			Optional[int]:
        '''simple docstring'''






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

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




  @slow
  @require_torch
  def   A__					(    self )  ->			Tuple:
        '''simple docstring'''






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

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

        _lowercase     	=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 )  ->			List[Any]:
        '''simple docstring'''






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

        _lowercase     	=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 )  ->			int:
        '''simple docstring'''






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

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




  @slow
  @require_torch
  def   A__					(    self )  ->			int:
        '''simple docstring'''






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

        _lowercase     	='a photography of'

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

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




  @slow
  @require_torch
  def   A__					(    self )  ->			Optional[int]:
        '''simple docstring'''






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

        _lowercase     	='a photo of a'

        _lowercase     	=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_ ):
              _lowercase     	=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 )  ->			Any:
        '''simple docstring'''






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

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

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

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




  @slow
  @require_tf
  def   A__					(    self )  ->			List[str]:
        '''simple docstring'''






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

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

        _lowercase     	=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 '}],
            ]							,       )





from __future__ import annotations

import random
import string





class 			__lowerCAmelCase					:




  def __init__(    self							,       lowerCAmelCase = None )  ->			None:
        '''simple docstring'''






        _lowercase     	=passcode or self.__passcode_creator()
        _lowercase     	=self.__make_key_list()
        _lowercase     	=self.__make_shift_key()




  def __str__(    self )  ->			str:
        '''simple docstring'''






        return "".join(self.__passcode )




  def   A__					(    self							,       lowerCAmelCase )  ->			list[int]:
        '''simple docstring'''






        for i in range(1							,       len(lowerCAmelCase )							,       2 ):
              iterlist[i] *= -1
        return iterlist




  def   A__					(    self )  ->			list[str]:
        '''simple docstring'''






        _lowercase     	=string.ascii_letters + string.digits
        _lowercase     	=[random.choice(lowerCAmelCase ) for _ in range(random.randint(10							,       20 ) )]
        return password




  def   A__					(    self )  ->			list[str]:
        '''simple docstring'''






        _lowercase     	=(
            string.ascii_letters + string.digits + string.punctuation + ' \t\n'
        )

        _lowercase     	=[]

        # creates points known as breakpoints to break the key_list_options at those
        # points and pivot each substring
        _lowercase     	=sorted(set(self.__passcode ) )
        _lowercase     	=[]

        # algorithm for creating a new shuffled list, keys_l, out of key_list_options
        for i in key_list_options:
              temp_list.extend(lowerCAmelCase )

              # checking breakpoints at which to pivot temporary sublist and add it into
              # keys_l
              if i in breakpoints or i == key_list_options[-1]:
                    keys_l.extend(temp_list[::-1] )
                    temp_list.clear()

        # returning a shuffled keys_l to prevent brute force guessing of shift key
        return keys_l




  def   A__					(    self )  ->			int:
        '''simple docstring'''






        _lowercase     	=sum(self.__neg_pos([ord(lowerCAmelCase ) for x in self.__passcode] ) )
        return num if num > 0 else len(self.__passcode )




  def   A__					(    self							,       lowerCAmelCase )  ->			str:
        '''simple docstring'''






        _lowercase     	=''

        # decoding shift like Caesar cipher algorithm implementing negative shift or
        # reverse shift or left shift
        for i in encoded_message:
              _lowercase     	=self.__key_list.index(lowerCAmelCase )
              decoded_message += self.__key_list[
                  (position - self.__shift_key) % -len(self.__key_list )
              ]

        return decoded_message




  def   A__					(    self							,       lowerCAmelCase )  ->			str:
        '''simple docstring'''






        _lowercase     	=''

        # encoding shift like Caesar cipher algorithm implementing positive shift or
        # forward shift or right shift
        for i in plaintext:
              _lowercase     	=self.__key_list.index(lowerCAmelCase )
              encoded_message += self.__key_list[
                  (position + self.__shift_key) % len(self.__key_list )
              ]

        return encoded_message
def 					a				(      A__     :       str = "Hello, this is a modified Caesar cipher"					)   ->				str:





      """simple docstring"""



      _lowercase     	=ShuffledShiftCipher()
      return cipa.decrypt(cipa.encrypt(A__					)					)


if __name__ == "__main__":
  import doctest

  doctest.testmod()


