






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


if is_vision_available():
       from PIL import Image
else:


       class    snake_case						:


          '''simple docstring'''

          @staticmethod
          def 							_SCREAMING_SNAKE_CASE							(      *_lowerCamelCase					:	str,			**_lowerCamelCase					:	int    ):

            '''simple docstring'''
            pass


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


   '''simple docstring'''

   A_		:      Optional[int]							=					MODEL_FOR_VISION_2_SEQ_MAPPING
   A_		:      Optional[int]							=					TF_MODEL_FOR_VISION_2_SEQ_MAPPING


   def 							_SCREAMING_SNAKE_CASE							(      self					:	Optional[int],			_lowerCamelCase					:	int,			_lowerCamelCase					:	Optional[Any],			_lowerCamelCase					:	Union[str, Any]    ):

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


   def 							_SCREAMING_SNAKE_CASE							(      self					:	List[str],			_lowerCamelCase					:	Optional[Any],			_lowerCamelCase					:	Optional[int]    ):

     '''simple docstring'''
     __A		     =					pipe(_lowerCamelCase    )
     self.assertEqual(
         _lowerCamelCase,			[
             [{'''generated_text''': ANY(_lowerCamelCase    )}],
             [{'''generated_text''': ANY(_lowerCamelCase    )}],
         ],			)


   @require_tf
   def 							_SCREAMING_SNAKE_CASE							(      self					:	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(_lowerCamelCase    )
     self.assertEqual(
         _lowerCamelCase,			[
             {
                 '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
             },
         ],			)

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

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


   @require_torch
   def 							_SCREAMING_SNAKE_CASE							(      self					:	str    ):

     '''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(_lowerCamelCase    )
     self.assertEqual(
         _lowerCamelCase,			[
             {
                 '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
             },
         ],			)

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


   @require_torch
   def 							_SCREAMING_SNAKE_CASE							(      self					:	Dict    ):

     '''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(_lowerCamelCase,			prompt=_lowerCamelCase    )
     self.assertTrue(outputs[0]['''generated_text'''].startswith(_lowerCamelCase    )    )


   @slow
   @require_torch
   def 							_SCREAMING_SNAKE_CASE							(      self					:	Dict    ):

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

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

     __A		     =					pipe([image, image]    )
     self.assertEqual(
         _lowerCamelCase,			[
             [{'''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 							_SCREAMING_SNAKE_CASE							(      self					:	Optional[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(_lowerCamelCase,			stream=_lowerCamelCase    ).raw    )

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


   @slow
   @require_torch
   def 							_SCREAMING_SNAKE_CASE							(      self					:	Tuple    ):

     '''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(_lowerCamelCase,			stream=_lowerCamelCase    ).raw    )

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


   @slow
   @require_torch
   def 							_SCREAMING_SNAKE_CASE							(      self					:	Any    ):

     '''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(_lowerCamelCase,			stream=_lowerCamelCase    ).raw    )

     __A		     =					'''a photography of'''

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

     with self.assertRaises(_lowerCamelCase    ):
       __A		     =					pipe([image, image],			prompt=[prompt, prompt]    )


   @slow
   @require_torch
   def 							_SCREAMING_SNAKE_CASE							(      self					:	Optional[int]    ):

     '''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(_lowerCamelCase,			stream=_lowerCamelCase    ).raw    )

     __A		     =					'''a photo of a'''

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

     with self.assertRaises(_lowerCamelCase    ):
       __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 							_SCREAMING_SNAKE_CASE							(      self					:	Optional[Any]    ):

     '''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(_lowerCamelCase,			stream=_lowerCamelCase    ).raw    )

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

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

     with self.assertRaises(_lowerCamelCase    ):
       __A		     =					pipe([image, image],			prompt=[prompt, prompt]    )






   @slow
   @require_tf
   def 							_SCREAMING_SNAKE_CASE							(      self					:	List[Any]    ):

     '''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(_lowerCamelCase    )
     self.assertEqual(_lowerCamelCase,			[{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}]    )

     __A		     =					pipe([image, image]    )
     self.assertEqual(
         _lowerCamelCase,			[
             [{'''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 '''}],
         ],			)












"""simple docstring"""


import argparse

import torch

from transformers import RoFormerConfig, RoFormerForMaskedLM, load_tf_weights_in_roformer
from transformers.utils import logging


logging.set_verbosity_info()



def    lowerCAmelCase							(       __UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase					):




  """simple docstring"""







  __A		     =					RoFormerConfig.from_json_file(__UpperCamelCase					)
  print(f'Building PyTorch model from configuration: {config}'					)
  __A		     =					RoFormerForMaskedLM(__UpperCamelCase					)

  # Load weights from tf checkpoint
  load_tf_weights_in_roformer(__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase					)

  # Save pytorch-model
  print(f'Save PyTorch model to {pytorch_dump_path}'					)
  torch.save(model.state_dict()						,			__UpperCamelCase						,			_use_new_zipfile_serialization=__UpperCamelCase					)


if __name__ == "__main__":
       lowercase_								=	argparse.ArgumentParser()
       # Required parameters
       parser.add_argument(
           '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.'
       )
       parser.add_argument(
           '--bert_config_file',
           default=None,
           type=str,
           required=True,
           help=(
               'The config json file corresponding to the pre-trained BERT model. \n'
               'This specifies the model architecture.'
           ),
       )
       parser.add_argument(
           '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
       )
       lowercase_								=	parser.parse_args()
       convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)




