




import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
       from PIL import Image

       from transformers import (
           AutoProcessor,
           BertTokenizerFast,
           BlipImageProcessor,
           GPTaTokenizer,
           InstructBlipProcessor,
           PreTrainedTokenizerFast,
       )




@require_vision
class 						__lowercase		(unittest.TestCase							):




      """simple docstring"""





      def      UpperCAmelCase				(				self     )		->    Optional[Any]:
         snake_case				:    int           =	tempfile.mkdtemp()

         snake_case				:    Dict           =	BlipImageProcessor()
         snake_case				:    Dict           =	GPTaTokenizer.from_pretrained("""hf-internal-testing/tiny-random-GPT2Model"""     )
         snake_case				:    int           =	BertTokenizerFast.from_pretrained("""hf-internal-testing/tiny-random-bert"""     )

         snake_case				:    Optional[Any]           =	InstructBlipProcessor(A    ,				A    ,				A     )

         processor.save_pretrained(self.tmpdirname     )

      def      UpperCAmelCase				(				self    ,				**A     )		->    List[Any]:
         return AutoProcessor.from_pretrained(self.tmpdirname    ,				**A     ).tokenizer

      def      UpperCAmelCase				(				self    ,				**A     )		->    Any:
         return AutoProcessor.from_pretrained(self.tmpdirname    ,				**A     ).image_processor

      def      UpperCAmelCase				(				self    ,				**A     )		->    Dict:
         return AutoProcessor.from_pretrained(self.tmpdirname    ,				**A     ).qformer_tokenizer

      def      UpperCAmelCase				(				self     )		->    List[Any]:
         shutil.rmtree(self.tmpdirname     )

      def      UpperCAmelCase				(				self     )		->    Union[str, Any]:
         snake_case				:    int           =	[np.random.randint(2_5_5    ,				size=(3, 3_0, 4_0_0)    ,				dtype=np.uinta     )]

         snake_case				:    List[str]           =	[Image.fromarray(np.moveaxis(A    ,				0    ,				-1     )     ) for x in image_inputs]

         return image_inputs

      def      UpperCAmelCase				(				self     )		->    int:
         snake_case				:    Optional[Any]           =	InstructBlipProcessor(
             tokenizer=self.get_tokenizer()    ,				image_processor=self.get_image_processor()    ,				qformer_tokenizer=self.get_qformer_tokenizer()    ,				)
         processor.save_pretrained(self.tmpdirname     )

         snake_case				:    str           =	self.get_tokenizer(bos_token="""(BOS)"""    ,				eos_token="""(EOS)"""     )
         snake_case				:    List[str]           =	self.get_image_processor(do_normalize=A    ,				padding_value=1.0     )

         snake_case				:    List[Any]           =	InstructBlipProcessor.from_pretrained(
             self.tmpdirname    ,				bos_token="""(BOS)"""    ,				eos_token="""(EOS)"""    ,				do_normalize=A    ,				padding_value=1.0     )

         self.assertEqual(processor.tokenizer.get_vocab()    ,				tokenizer_add_kwargs.get_vocab()     )
         self.assertIsInstance(processor.tokenizer    ,				A     )

         self.assertEqual(processor.image_processor.to_json_string()    ,				image_processor_add_kwargs.to_json_string()     )
         self.assertIsInstance(processor.image_processor    ,				A     )
         self.assertIsInstance(processor.qformer_tokenizer    ,				A     )

      def      UpperCAmelCase				(				self     )		->    List[Any]:
         snake_case				:    Optional[Any]           =	self.get_image_processor()
         snake_case				:    Tuple           =	self.get_tokenizer()
         snake_case				:    Optional[Any]           =	self.get_qformer_tokenizer()

         snake_case				:    int           =	InstructBlipProcessor(
             tokenizer=A    ,				image_processor=A    ,				qformer_tokenizer=A     )

         snake_case				:    List[str]           =	self.prepare_image_inputs()

         snake_case				:    Union[str, Any]           =	image_processor(A    ,				return_tensors="""np"""     )
         snake_case				:    int           =	processor(images=A    ,				return_tensors="""np"""     )

         for key in input_feat_extract.keys():
            self.assertAlmostEqual(input_feat_extract[key].sum()    ,				input_processor[key].sum()    ,				delta=1e-2     )

      def      UpperCAmelCase				(				self     )		->    Optional[int]:
         snake_case				:    Dict           =	self.get_image_processor()
         snake_case				:    List[Any]           =	self.get_tokenizer()
         snake_case				:    Optional[int]           =	self.get_qformer_tokenizer()

         snake_case				:    Optional[Any]           =	InstructBlipProcessor(
             tokenizer=A    ,				image_processor=A    ,				qformer_tokenizer=A     )

         snake_case				:    Optional[Any]           =	"""lower newer"""

         snake_case				:    List[Any]           =	processor(text=A     )

         snake_case				:    List[str]           =	tokenizer(A    ,				return_token_type_ids=A     )
         snake_case				:    int           =	qformer_tokenizer(A    ,				return_token_type_ids=A     )

         for key in encoded_tokens.keys():
            self.assertListEqual(encoded_tokens[key]    ,				encoded_processor[key]     )

         for key in encoded_tokens_qformer.keys():
            self.assertListEqual(encoded_tokens_qformer[key]    ,				encoded_processor["""qformer_""" + key]     )

      def      UpperCAmelCase				(				self     )		->    Dict:
         snake_case				:    Optional[int]           =	self.get_image_processor()
         snake_case				:    Union[str, Any]           =	self.get_tokenizer()
         snake_case				:    Optional[Any]           =	self.get_qformer_tokenizer()

         snake_case				:    Optional[Any]           =	InstructBlipProcessor(
             tokenizer=A    ,				image_processor=A    ,				qformer_tokenizer=A     )

         snake_case				:    str           =	"""lower newer"""
         snake_case				:    List[str]           =	self.prepare_image_inputs()

         snake_case				:    Any           =	processor(text=A    ,				images=A     )

         self.assertListEqual(
             list(inputs.keys()     )    ,				["""input_ids""", """attention_mask""", """qformer_input_ids""", """qformer_attention_mask""", """pixel_values"""]    ,				)

         # test if it raises when no input is passed
         with pytest.raises(A     ):
            processor()

      def      UpperCAmelCase				(				self     )		->    Dict:
         snake_case				:    List[str]           =	self.get_image_processor()
         snake_case				:    List[Any]           =	self.get_tokenizer()
         snake_case				:    Any           =	self.get_qformer_tokenizer()

         snake_case				:    str           =	InstructBlipProcessor(
             tokenizer=A    ,				image_processor=A    ,				qformer_tokenizer=A     )

         snake_case				:    List[str]           =	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

         snake_case				:    Union[str, Any]           =	processor.batch_decode(A     )
         snake_case				:    int           =	tokenizer.batch_decode(A     )

         self.assertListEqual(A    ,				A     )

      def      UpperCAmelCase				(				self     )		->    Tuple:
         snake_case				:    Union[str, Any]           =	self.get_image_processor()
         snake_case				:    List[Any]           =	self.get_tokenizer()
         snake_case				:    Tuple           =	self.get_qformer_tokenizer()

         snake_case				:    Optional[Any]           =	InstructBlipProcessor(
             tokenizer=A    ,				image_processor=A    ,				qformer_tokenizer=A     )

         snake_case				:    Union[str, Any]           =	"""lower newer"""
         snake_case				:    Union[str, Any]           =	self.prepare_image_inputs()

         snake_case				:    str           =	processor(text=A    ,				images=A     )

         self.assertListEqual(
             list(inputs.keys()     )    ,				["""input_ids""", """attention_mask""", """qformer_input_ids""", """qformer_attention_mask""", """pixel_values"""]    ,				)











from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand




def 						SCREAMING_SNAKE_CASE__ (   lowercase					)  ->       List[str]:
   return ConvertCommand(
       args.model_type						,args.tf_checkpoint						,args.pytorch_dump_output						,args.config						,args.finetuning_task_name					)


lowerCamelCase	:	Tuple				=   '\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n'




class 						__lowercase		(UpperCamelCase__							):




      """simple docstring"""





      @staticmethod
      def      UpperCAmelCase				(				A     )		->    List[str]:
         snake_case				:    Union[str, Any]           =	parser.add_parser(
             """convert"""    ,				help="""CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints."""    ,				)
         train_parser.add_argument("""--model_type"""    ,				type=A    ,				required=A    ,				help="""Model's type."""     )
         train_parser.add_argument(
             """--tf_checkpoint"""    ,				type=A    ,				required=A    ,				help="""TensorFlow checkpoint path or folder."""     )
         train_parser.add_argument(
             """--pytorch_dump_output"""    ,				type=A    ,				required=A    ,				help="""Path to the PyTorch saved model output."""     )
         train_parser.add_argument("""--config"""    ,				type=A    ,				default=""""""    ,				help="""Configuration file path or folder."""     )
         train_parser.add_argument(
             """--finetuning_task_name"""    ,				type=A    ,				default=A    ,				help="""Optional fine-tuning task name if the TF model was a finetuned model."""    ,				)
         train_parser.set_defaults(func=A     )

      def __init__(				self    ,				A    ,				A    ,				A    ,				A    ,				A    ,				*A    ,				)		->    List[Any]:
         snake_case				:    Any           =	logging.get_logger("""transformers-cli/converting"""     )

         self._logger.info(f"""Loading model {model_type}"""     )
         snake_case				:    int           =	model_type
         snake_case				:    Any           =	tf_checkpoint
         snake_case				:    int           =	pytorch_dump_output
         snake_case				:    List[str]           =	config
         snake_case				:    Tuple           =	finetuning_task_name

      def      UpperCAmelCase				(				self     )		->    int:
         if self._model_type == "albert":
            try:
               from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import (
                   convert_tf_checkpoint_to_pytorch,
               )
            except ImportError:
               raise ImportError(A     )

            convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output     )
         elif self._model_type == "bert":
            try:
               from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import (
                   convert_tf_checkpoint_to_pytorch,
               )
            except ImportError:
               raise ImportError(A     )

            convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output     )
         elif self._model_type == "funnel":
            try:
               from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import (
                   convert_tf_checkpoint_to_pytorch,
               )
            except ImportError:
               raise ImportError(A     )

            convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output     )
         elif self._model_type == "t5":
            try:
               from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
            except ImportError:
               raise ImportError(A     )

            convert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output     )
         elif self._model_type == "gpt":
            from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import (
                convert_openai_checkpoint_to_pytorch,
            )

            convert_openai_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output     )
         elif self._model_type == "transfo_xl":
            try:
               from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import (
                   convert_transfo_xl_checkpoint_to_pytorch,
               )
            except ImportError:
               raise ImportError(A     )

            if "ckpt" in self._tf_checkpoint.lower():
               snake_case				:    List[Any]           =	self._tf_checkpoint
               snake_case				:    Tuple           =	""""""
            else:
               snake_case				:    Tuple           =	self._tf_checkpoint
               snake_case				:    Tuple           =	""""""
            convert_transfo_xl_checkpoint_to_pytorch(
                A    ,				self._config    ,				self._pytorch_dump_output    ,				A     )
         elif self._model_type == "gpt2":
            try:
               from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import (
                   convert_gpta_checkpoint_to_pytorch,
               )
            except ImportError:
               raise ImportError(A     )

            convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output     )
         elif self._model_type == "xlnet":
            try:
               from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import (
                   convert_xlnet_checkpoint_to_pytorch,
               )
            except ImportError:
               raise ImportError(A     )

            convert_xlnet_checkpoint_to_pytorch(
                self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output    ,				self._finetuning_task_name     )
         elif self._model_type == "xlm":
            from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import (
                convert_xlm_checkpoint_to_pytorch,
            )

            convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._pytorch_dump_output     )
         elif self._model_type == "lxmert":
            from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import (
                convert_lxmert_checkpoint_to_pytorch,
            )

            convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._pytorch_dump_output     )
         elif self._model_type == "rembert":
            from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import (
                convert_rembert_tf_checkpoint_to_pytorch,
            )

            convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint    ,				self._config    ,				self._pytorch_dump_output     )
         else:
            raise ValueError(
                """--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]"""     )





