






"""simple docstring"""



import sys
from collections.abc import Mapping

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter

class 			a	(  TensorFormatter[Mapping, np.ndarray, Mapping]      ):







    def __init__(     self				,						_lowerCamelCase=None				,						**_lowerCamelCase			):
      super().__init__(features=_lowerCamelCase			)
      lowercase				       =			np_array_kwargs







    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      if isinstance(_lowerCamelCase				,						_lowerCamelCase			):
        if column and all(
            isinstance(_lowerCamelCase				,						np.ndarray			) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column			):
          return np.stack(_lowerCamelCase			)
        else:
          # don't use np.array(column, dtype=object)
          # since it fails in certain cases
          # see https://stackoverflow.com/q/51005699
          lowercase				       =			np.empty(len(_lowerCamelCase			)				,						dtype=_lowerCamelCase			)
          lowercase				       =			column
          return out
      return column







    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      if isinstance(_lowerCamelCase				,						(str, bytes, type(_lowerCamelCase			))			):
        return value
      elif isinstance(_lowerCamelCase				,						(np.character, np.ndarray)			) and np.issubdtype(value.dtype				,						np.character			):
        return value
      elif isinstance(_lowerCamelCase				,						np.number			):
        return value

      lowercase				       =			{}

      if isinstance(_lowerCamelCase				,						np.ndarray			) and np.issubdtype(value.dtype				,						np.integer			):
        lowercase				       =			{'dtype': np.intaa}
      elif isinstance(_lowerCamelCase				,						np.ndarray			) and np.issubdtype(value.dtype				,						np.floating			):
        lowercase				       =			{'dtype': np.floataa}
      elif config.PIL_AVAILABLE and "PIL" in sys.modules:
        import PIL.Image

        if isinstance(_lowerCamelCase				,						PIL.Image.Image			):
          return np.asarray(_lowerCamelCase				,						**self.np_array_kwargs			)

      return np.asarray(_lowerCamelCase				,						**{**default_dtype, **self.np_array_kwargs}			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      # support for torch, tf, jax etc.
      if config.TORCH_AVAILABLE and "torch" in sys.modules:
        import torch

        if isinstance(_lowerCamelCase				,						torch.Tensor			):
          return self._tensorize(data_struct.detach().cpu().numpy()[()]			)
      if hasattr(_lowerCamelCase				,						'__array__'			) and not isinstance(_lowerCamelCase				,						(np.ndarray, np.character, np.number)			):
        lowercase				       =			data_struct.__array__()
      # support for nested types like struct of list of struct
      if isinstance(_lowerCamelCase				,						np.ndarray			):
        if data_struct.dtype == object:
          return self._consolidate([self.recursive_tensorize(_lowerCamelCase			) for substruct in data_struct]			)
      if isinstance(_lowerCamelCase				,						(list, tuple)			):
        return self._consolidate([self.recursive_tensorize(_lowerCamelCase			) for substruct in data_struct]			)
      return self._tensorize(_lowerCamelCase			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      return map_nested(self._recursive_tensorize				,						_lowerCamelCase				,						map_list=_lowerCamelCase			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      lowercase				       =			self.numpy_arrow_extractor().extract_row(_lowerCamelCase			)
      lowercase				       =			self.python_features_decoder.decode_row(_lowerCamelCase			)
      return self.recursive_tensorize(_lowerCamelCase			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      lowercase				       =			self.numpy_arrow_extractor().extract_column(_lowerCamelCase			)
      lowercase				       =			self.python_features_decoder.decode_column(_lowerCamelCase				,						pa_table.column_names[0]			)
      lowercase				       =			self.recursive_tensorize(_lowerCamelCase			)
      lowercase				       =			self._consolidate(_lowerCamelCase			)
      return column







    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      lowercase				       =			self.numpy_arrow_extractor().extract_batch(_lowerCamelCase			)
      lowercase				       =			self.python_features_decoder.decode_batch(_lowerCamelCase			)
      lowercase				       =			self.recursive_tensorize(_lowerCamelCase			)
      for column_name in batch:
        lowercase				       =			self._consolidate(batch[column_name]			)
      return batch








'''simple docstring'''





from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding






class     lowercase  ( UpperCAmelCase_):
      __lowercase					=	['''image_processor''', '''tokenizer''']
      __lowercase					=	'''EfficientNetImageProcessor'''
      __lowercase					=	('''BertTokenizer''', '''BertTokenizerFast''')






      def __init__(self			,	a__			,	a__       )							-> Tuple:
         super().__init__(a__			,	a__       )






      def __call__(self			,	a__=None			,	a__=None			,	a__="max_length"			,	a__=6_4			,	a__=None			,	**a__       )							-> Any:
         if text is None and images is None:
            raise ValueError('''You have to specify either text or images. Both cannot be none.'''       )

         if text is not None:
            SCREAMING_SNAKE_CASE__      :					List[Any]							    =			self.tokenizer(
                a__			,	padding=a__			,	max_length=a__			,	return_tensors=a__			,	**a__       )

         if images is not None:
            SCREAMING_SNAKE_CASE__      :					Any							    =			self.image_processor(a__			,	return_tensors=a__			,	**a__       )

         if text is not None and images is not None:
            SCREAMING_SNAKE_CASE__      :					str							    =			image_features.pixel_values
            return encoding
         elif text is not None:
            return encoding
         else:
            return BatchEncoding(data=dict(**a__       )			,	tensor_type=a__       )






      def  A__(self			,	*a__			,	**a__       )							-> str:
         return self.tokenizer.batch_decode(*a__			,	**a__       )






      def  A__(self			,	*a__			,	**a__       )							-> str:
         return self.tokenizer.decode(*a__			,	**a__       )






      @property
      def  A__(self       )							-> Optional[int]:
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			self.tokenizer.model_input_names
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			self.image_processor.model_input_names
         return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names       )       )

