





import warnings
from typing import List

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import is_flax_available, is_tf_available, is_torch_available







class   _UpperCamelCase  (       A_  ):







					'''simple docstring'''

					lowerCamelCase     :     Dict			    =       ['image_processor', 'tokenizer']
					lowerCamelCase     :     int			    =       'OwlViTImageProcessor'
					lowerCamelCase     :     List[str]			    =       ('CLIPTokenizer', 'CLIPTokenizerFast')





					def __init__(						self    :  Union[str, Any] ,					__lowercase    :  str=None ,					__lowercase    :  Any=None ,					**__lowercase    :  Optional[int]		):






						'''simple docstring'''







						UpperCAmelCase_								=     None
						if "feature_extractor" in kwargs:
							warnings.warn(
							    """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
							    """ instead.""" ,					__lowercase ,					)
							UpperCAmelCase_								=     kwargs.pop("""feature_extractor"""		)

						UpperCAmelCase_								=     image_processor if image_processor is not None else feature_extractor
						if image_processor is None:
							raise ValueError("""You need to specify an `image_processor`."""		)
						if tokenizer is None:
							raise ValueError("""You need to specify a `tokenizer`."""		)

						super().__init__(__lowercase ,					__lowercase		)





					def __call__(						self    :  List[Any] ,					__lowercase    :  Union[str, Any]=None ,					__lowercase    :  List[Any]=None ,					__lowercase    :  Dict=None ,					__lowercase    :  Any="max_length" ,					__lowercase    :  Any="np" ,					**__lowercase    :  Dict		):






						'''simple docstring'''







						if text is None and query_images is None and images is None:
							raise ValueError(
							    """You have to specify at least one text or query image or image. All three cannot be none."""		)

						if text is not None:
							if isinstance(__lowercase ,					__lowercase		) or (isinstance(__lowercase ,					__lowercase		) and not isinstance(text[0] ,					__lowercase		)):
								UpperCAmelCase_								=     [self.tokenizer(__lowercase ,					padding=__lowercase ,					return_tensors=__lowercase ,					**__lowercase		)]

							elif isinstance(__lowercase ,					__lowercase		) and isinstance(text[0] ,					__lowercase		):
								UpperCAmelCase_								=     []

								# Maximum number of queries across batch
								UpperCAmelCase_								=     max([len(__lowercase		) for t in text]		)

								# Pad all batch samples to max number of text queries
								for t in text:
									if len(__lowercase		) != max_num_queries:
										UpperCAmelCase_								=     t + [""" """] * (max_num_queries - len(__lowercase		))

									UpperCAmelCase_								=     self.tokenizer(__lowercase ,					padding=__lowercase ,					return_tensors=__lowercase ,					**__lowercase		)
									encodings.append(__lowercase		)
							else:
								raise TypeError("""Input text should be a string, a list of strings or a nested list of strings"""		)

							if return_tensors == "np":
								UpperCAmelCase_								=     np.concatenate([encoding["""input_ids"""] for encoding in encodings] ,					axis=0		)
								UpperCAmelCase_								=     np.concatenate([encoding["""attention_mask"""] for encoding in encodings] ,					axis=0		)

							elif return_tensors == "jax" and is_flax_available():
								import jax.numpy as jnp

								UpperCAmelCase_								=     jnp.concatenate([encoding["""input_ids"""] for encoding in encodings] ,					axis=0		)
								UpperCAmelCase_								=     jnp.concatenate([encoding["""attention_mask"""] for encoding in encodings] ,					axis=0		)

							elif return_tensors == "pt" and is_torch_available():
								import torch

								UpperCAmelCase_								=     torch.cat([encoding["""input_ids"""] for encoding in encodings] ,					dim=0		)
								UpperCAmelCase_								=     torch.cat([encoding["""attention_mask"""] for encoding in encodings] ,					dim=0		)

							elif return_tensors == "tf" and is_tf_available():
								import tensorflow as tf

								UpperCAmelCase_								=     tf.stack([encoding["""input_ids"""] for encoding in encodings] ,					axis=0		)
								UpperCAmelCase_								=     tf.stack([encoding["""attention_mask"""] for encoding in encodings] ,					axis=0		)

							else:
								raise ValueError("""Target return tensor type could not be returned"""		)

							UpperCAmelCase_								=     BatchEncoding()
							UpperCAmelCase_								=     input_ids
							UpperCAmelCase_								=     attention_mask

						if query_images is not None:
							UpperCAmelCase_								=     BatchEncoding()
							UpperCAmelCase_								=     self.image_processor(
							    __lowercase ,					return_tensors=__lowercase ,					**__lowercase		).pixel_values
							UpperCAmelCase_								=     query_pixel_values

						if images is not None:
							UpperCAmelCase_								=     self.image_processor(__lowercase ,					return_tensors=__lowercase ,					**__lowercase		)

						if text is not None and images is not None:
							UpperCAmelCase_								=     image_features.pixel_values
							return encoding
						elif query_images is not None and images is not None:
							UpperCAmelCase_								=     image_features.pixel_values
							return encoding
						elif text is not None or query_images is not None:
							return encoding
						else:
							return BatchEncoding(data=dict(**__lowercase		) ,					tensor_type=__lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  str ,					*__lowercase    :  Tuple ,					**__lowercase    :  Union[str, Any]		):






						'''simple docstring'''







						return self.image_processor.post_process(*__lowercase ,					**__lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  List[str] ,					*__lowercase    :  Optional[int] ,					**__lowercase    :  List[str]		):






						'''simple docstring'''







						return self.image_processor.post_process_object_detection(*__lowercase ,					**__lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  int ,					*__lowercase    :  Dict ,					**__lowercase    :  int		):






						'''simple docstring'''







						return self.image_processor.post_process_image_guided_detection(*__lowercase ,					**__lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  Dict ,					*__lowercase    :  Tuple ,					**__lowercase    :  Tuple		):






						'''simple docstring'''







						return self.tokenizer.batch_decode(*__lowercase ,					**__lowercase		)





					def        SCREAMING_SNAKE_CASE							(						self    :  Optional[Any] ,					*__lowercase    :  Tuple ,					**__lowercase    :  Any		):






						'''simple docstring'''







						return self.tokenizer.decode(*__lowercase ,					**__lowercase		)





					@property
					def        SCREAMING_SNAKE_CASE							(						self    :  Tuple		):






						'''simple docstring'''







						warnings.warn(
						    """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.""" ,					__lowercase ,					)
						return self.image_processor_class





					@property
					def        SCREAMING_SNAKE_CASE							(						self    :  int		):






						'''simple docstring'''







						warnings.warn(
						    """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.""" ,					__lowercase ,					)
						return self.image_processor









from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand




def    A_(			A   ):
	return ConvertCommand(
	    args.model_type				,      args.tf_checkpoint				,      args.pytorch_dump_output				,      args.config				,      args.finetuning_task_name   )


UpperCamelCase__   : Any					  = """
transformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires
TensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.
"""







class   _UpperCamelCase  (       A_  ):







					'''simple docstring'''

					@staticmethod
					def        SCREAMING_SNAKE_CASE							(						__lowercase    :  ArgumentParser		):






						'''simple docstring'''







						UpperCAmelCase_								=     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=__lowercase ,					required=__lowercase ,					help="""Model's type."""		)
						train_parser.add_argument(
						    """--tf_checkpoint""" ,					type=__lowercase ,					required=__lowercase ,					help="""TensorFlow checkpoint path or folder."""		)
						train_parser.add_argument(
						    """--pytorch_dump_output""" ,					type=__lowercase ,					required=__lowercase ,					help="""Path to the PyTorch saved model output."""		)
						train_parser.add_argument("""--config""" ,					type=__lowercase ,					default="""""" ,					help="""Configuration file path or folder."""		)
						train_parser.add_argument(
						    """--finetuning_task_name""" ,					type=__lowercase ,					default=__lowercase ,					help="""Optional fine-tuning task name if the TF model was a finetuned model.""" ,					)
						train_parser.set_defaults(func=__lowercase		)





					def __init__(						self    :  Optional[int] ,					__lowercase    :  str ,					__lowercase    :  str ,					__lowercase    :  str ,					__lowercase    :  str ,					__lowercase    :  str ,					*__lowercase    :  Tuple ,					):






						'''simple docstring'''







						UpperCAmelCase_								=     logging.get_logger("""transformers-cli/converting"""		)

						self._logger.info(F"""Loading model {model_type}"""		)
						UpperCAmelCase_								=     model_type
						UpperCAmelCase_								=     tf_checkpoint
						UpperCAmelCase_								=     pytorch_dump_output
						UpperCAmelCase_								=     config
						UpperCAmelCase_								=     finetuning_task_name





					def        SCREAMING_SNAKE_CASE							(						self    :  List[Any]		):






						'''simple docstring'''







						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(__lowercase		)

							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(__lowercase		)

							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(__lowercase		)

							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(__lowercase		)

							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(__lowercase		)

							if "ckpt" in self._tf_checkpoint.lower():
								UpperCAmelCase_								=     self._tf_checkpoint
								UpperCAmelCase_								=     """"""
							else:
								UpperCAmelCase_								=     self._tf_checkpoint
								UpperCAmelCase_								=     """"""
							convert_transfo_xl_checkpoint_to_pytorch(
							    __lowercase ,					self._config ,					self._pytorch_dump_output ,					__lowercase		)
						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(__lowercase		)

							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(__lowercase		)

							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]"""		)


