





'''simple docstring'''







# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

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


if TYPE_CHECKING:
					import torch
class 				__SCREAMING_SNAKE_CASE  (TensorFormatter[Mapping, 'torch.Tensor', Mapping]      ):






				"""simple docstring"""







				def __init__(  self     :					Any					,				__a     :					Tuple=None					,				**__a     :					Tuple					):
								super().__init__(features=__a					)
								_a								= torch_tensor_kwargs
								import torch  # noqa import torch at initialization




				def   UpperCamelCase__   (  self     :					Optional[Any]					,				__a     :					Any					):
								import torch

								if isinstance(__a					,				__a					) and column:
												if all(
												    isinstance(__a					,				torch.Tensor					) and x.shape == column[0].shape and x.dtype == column[0].dtype
												    for x in column					):
																return torch.stack(__a					)
								return column




				def   UpperCamelCase__   (  self     :					Optional[int]					,				__a     :					Tuple					):
								import torch

								if isinstance(__a					,				(str, bytes, type(__a					))					):
												return value
								elif isinstance(__a					,				(np.character, np.ndarray)					) and np.issubdtype(value.dtype					,				np.character					):
												return value.tolist()

								_a								= {}

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

												if isinstance(__a					,				PIL.Image.Image					):
																_a								= np.asarray(__a					)
								return torch.tensor(__a					,				**{**default_dtype, **self.torch_tensor_kwargs}					)




				def   UpperCamelCase__   (  self     :					Optional[int]					,				__a     :					List[Any]					):
								import torch

								# support for torch, tf, jax etc.
								if hasattr(__a					,				"__array__"					) and not isinstance(__a					,				torch.Tensor					):
												_a								= data_struct.__array__()
								# support for nested types like struct of list of struct
								if isinstance(__a					,				np.ndarray					):
												if data_struct.dtype == object:  # torch tensors cannot be instantied from an array of objects
																return self._consolidate([self.recursive_tensorize(__a					) for substruct in data_struct]					)
								elif isinstance(__a					,				(list, tuple)					):
												return self._consolidate([self.recursive_tensorize(__a					) for substruct in data_struct]					)
								return self._tensorize(__a					)




				def   UpperCamelCase__   (  self     :					int					,				__a     :					dict					):
								return map_nested(self._recursive_tensorize					,				__a					,				map_list=__a					)




				def   UpperCamelCase__   (  self     :					Any					,				__a     :					pa.Table					):
								_a								= self.numpy_arrow_extractor().extract_row(__a					)
								_a								= self.python_features_decoder.decode_row(__a					)
								return self.recursive_tensorize(__a					)




				def   UpperCamelCase__   (  self     :					List[Any]					,				__a     :					pa.Table					):
								_a								= self.numpy_arrow_extractor().extract_column(__a					)
								_a								= self.python_features_decoder.decode_column(__a					,				pa_table.column_names[0]					)
								_a								= self.recursive_tensorize(__a					)
								_a								= self._consolidate(__a					)
								return column




				def   UpperCamelCase__   (  self     :					Optional[Any]					,				__a     :					pa.Table					):
								_a								= self.numpy_arrow_extractor().extract_batch(__a					)
								_a								= self.python_features_decoder.decode_batch(__a					)
								_a								= self.recursive_tensorize(__a					)
								for column_name in batch:
												_a								= self._consolidate(batch[column_name]					)
								return batch



import warnings

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

class 				UpperCAmelCase_		(			__lowercase   ):
							"""simple docstring"""







							UpperCAmelCase__       :						Union[str, Any]	    =    ["image_processor", "tokenizer"]
							UpperCAmelCase__       :						Dict	    =    "ChineseCLIPImageProcessor"
							UpperCAmelCase__       :						List[str]	    =    ("BertTokenizer", "BertTokenizerFast")


							def __init__(			self       ,							_a=None       ,							_a=None       ,							**_a       )    ->    Any:
												_a	:    Optional[Any]         =		None
												if "feature_extractor" in kwargs:
																	warnings.warn(
																	    '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
																	    ''' instead.'''       ,							_a       ,							)
																	_a	:    Tuple         =		kwargs.pop('''feature_extractor'''       )

												_a	:    Tuple         =		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__(_a       ,							_a       )
												_a	:    List[str]         =		self.image_processor


							def __call__(			self       ,							_a=None       ,							_a=None       ,							_a=None       ,							**_a       )    ->    int:

												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:
																	_a	:    List[str]         =		self.tokenizer(_a       ,							return_tensors=_a       ,							**_a       )

												if images is not None:
																	_a	:    Optional[Any]         =		self.image_processor(_a       ,							return_tensors=_a       ,							**_a       )

												if text is not None and images is not None:
																	_a	:    Union[str, Any]         =		image_features.pixel_values
																	return encoding
												elif text is not None:
																	return encoding
												else:
																	return BatchEncoding(data=dict(**_a       )       ,							tensor_type=_a       )


							def 				__lowercase				(			self       ,							*_a       ,							**_a       )    ->    Union[str, Any]:
												return self.tokenizer.batch_decode(*_a       ,							**_a       )


							def 				__lowercase				(			self       ,							*_a       ,							**_a       )    ->    Any:
												return self.tokenizer.decode(*_a       ,							**_a       )


							@property
							def 				__lowercase				(			self       )    ->    Optional[Any]:
												_a	:    Any         =		self.tokenizer.model_input_names
												_a	:    Dict         =		self.image_processor.model_input_names
												return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names       )       )




							@property
							def 				__lowercase				(			self       )    ->    Dict:
												warnings.warn(
												    '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''       ,							_a       ,							)
												return self.image_processor_class



