

import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
					from PIL import Image
else:







					class 			lowercase    :




									@staticmethod
									def    a							( *snake_case				,       **snake_case	):
																pass







@is_pipeline_test
@require_torch
@require_vision
class 			lowercase    (	unittest.TestCase      ):
				__SCREAMING_SNAKE_CASE :			Optional[int]	      =      MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING




				def    a							( self				,       snake_case				,       snake_case				,       snake_case	):
											snake_case_          =   pipeline('visual-question-answering'				,       model='hf-internal-testing/tiny-vilt-random-vqa'	)
											snake_case_          =   [
											    {
											        '''image''': Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'	),
											        '''question''': '''How many cats are there?''',
											    },
											    {
											        '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
											        '''question''': '''How many cats are there?''',
											    },
											]
											return vqa_pipeline, examples




				def    a							( self				,       snake_case				,       snake_case	):
											snake_case_          =   vqa_pipeline(snake_case__				,       top_k=1	)
											self.assertEqual(
											    snake_case__				,       [
											        [{'score': ANY(snake_case__	), 'answer': ANY(snake_case__	)}],
											        [{'score': ANY(snake_case__	), 'answer': ANY(snake_case__	)}],
											    ]				,       )




				@require_torch
				def    a							( self	):
											snake_case_          =   pipeline('visual-question-answering'				,       model='hf-internal-testing/tiny-vilt-random-vqa'	)
											snake_case_          =   '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
											snake_case_          =   '''How many cats are there?'''

											snake_case_          =   vqa_pipeline(image=snake_case__				,       question='How many cats are there?'				,       top_k=2	)
											self.assertEqual(
											    snake_case__				,       [{'score': ANY(snake_case__	), 'answer': ANY(snake_case__	)}, {'score': ANY(snake_case__	), 'answer': ANY(snake_case__	)}]	)

											snake_case_          =   vqa_pipeline({'image': image, 'question': question}				,       top_k=2	)
											self.assertEqual(
											    snake_case__				,       [{'score': ANY(snake_case__	), 'answer': ANY(snake_case__	)}, {'score': ANY(snake_case__	), 'answer': ANY(snake_case__	)}]	)




				@slow
				@require_torch
				def    a							( self	):
											snake_case_          =   pipeline('visual-question-answering'				,       model='dandelin/vilt-b32-finetuned-vqa'	)
											snake_case_          =   '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
											snake_case_          =   '''How many cats are there?'''

											snake_case_          =   vqa_pipeline(image=snake_case__				,       question=snake_case__				,       top_k=2	)
											self.assertEqual(
											    nested_simplify(snake_case__				,       decimals=4	)				,       [{'score': 0.87_99, 'answer': '2'}, {'score': 0.2_96, 'answer': '1'}]	)

											snake_case_          =   vqa_pipeline({'image': image, 'question': question}				,       top_k=2	)
											self.assertEqual(
											    nested_simplify(snake_case__				,       decimals=4	)				,       [{'score': 0.87_99, 'answer': '2'}, {'score': 0.2_96, 'answer': '1'}]	)

											snake_case_          =   vqa_pipeline(
											    [{'image': image, 'question': question}, {'image': image, 'question': question}]				,       top_k=2	)
											self.assertEqual(
											    nested_simplify(snake_case__				,       decimals=4	)				,       [[{'score': 0.87_99, 'answer': '2'}, {'score': 0.2_96, 'answer': '1'}]] * 2				,       )




				@require_tf
				@unittest.skip('Visual question answering not implemented in TF'	)
				def    a							( self	):
											pass








import argparse

from transformers import BigBirdConfig, BigBirdForPreTraining, BigBirdForQuestionAnswering, load_tf_weights_in_big_bird
from transformers.utils import logging


logging.set_verbosity_info()





def  __lowerCamelCase     (			UpperCamelCase__       ,							UpperCamelCase__       ,							UpperCamelCase__       ,							UpperCamelCase__				):







							'''simple docstring'''






							snake_case_          =   BigBirdConfig.from_json_file(UpperCamelCase__				)
							print(F'''Building PyTorch model from configuration: {config}'''				)

							if is_trivia_qa:
														snake_case_          =   BigBirdForQuestionAnswering(UpperCamelCase__				)
							else:
														snake_case_          =   BigBirdForPreTraining(UpperCamelCase__				)

							# Load weights from tf checkpoint
							load_tf_weights_in_big_bird(UpperCamelCase__       ,							UpperCamelCase__       ,							is_trivia_qa=UpperCamelCase__				)

							# Save pytorch-model
							print(F'''Save PyTorch model to {pytorch_dump_path}'''				)
							model.save_pretrained(UpperCamelCase__				)


if __name__ == "__main__":
					_UpperCAmelCase		:       Dict						    =	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(
					    """--big_bird_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."""
					)
					parser.add_argument(
					    """--is_trivia_qa""", action="""store_true""", help="""Whether to convert a model with a trivia_qa head."""
					)
					_UpperCAmelCase		:       str						    =	parser.parse_args()
					convert_tf_checkpoint_to_pytorch(
					    args.tf_checkpoint_path, args.big_bird_config_file, args.pytorch_dump_path, args.is_trivia_qa
					)




