



"""simple docstring"""





import os
from typing import List, Optional, Union

from ...image_processing_utils import BatchFeature
from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType
from ..auto import AutoTokenizer



class  _lowerCAmelCase     (					SCREAMING_SNAKE_CASE__		):





		"""simple docstring"""






		_lowerCamelCase      	=							['''image_processor''', '''tokenizer''']
		_lowerCamelCase      	=							'''BlipImageProcessor'''
		_lowerCamelCase      	=							'''AutoTokenizer'''





		def __init__(       self				,     _lowercase				,     _lowercase				,     _lowercase )			->    Optional[int]:







								'''simple docstring'''

								super().__init__(_lowercase				,     _lowercase )

								# add QFormer tokenizer
								snake_case_ :			List[str]            =      qformer_tokenizer





		def __call__(       self				,     _lowercase = None				,     _lowercase = None				,     _lowercase = True				,     _lowercase = False				,     _lowercase = None				,     _lowercase = None				,     _lowercase = 0				,     _lowercase = None				,     _lowercase = None				,     _lowercase = False				,     _lowercase = False				,     _lowercase = False				,     _lowercase = False				,     _lowercase = False				,     _lowercase = True				,     _lowercase = None				,     **_lowercase				,     )			->    BatchFeature:







								'''simple docstring'''

								if images is None and text is None:
														raise ValueError("""You have to specify at least images or text.""" )

								snake_case_ :			Optional[Any]            =      BatchFeature()

								if text is not None:
														snake_case_ :			List[str]            =      self.tokenizer(
														    text=_lowercase				,     add_special_tokens=_lowercase				,     padding=_lowercase				,     truncation=_lowercase				,     max_length=_lowercase				,     stride=_lowercase				,     pad_to_multiple_of=_lowercase				,     return_attention_mask=_lowercase				,     return_overflowing_tokens=_lowercase				,     return_special_tokens_mask=_lowercase				,     return_offsets_mapping=_lowercase				,     return_token_type_ids=_lowercase				,     return_length=_lowercase				,     verbose=_lowercase				,     return_tensors=_lowercase				,     **_lowercase				,     )
														encoding.update(_lowercase )
														snake_case_ :			Union[str, Any]            =      self.qformer_tokenizer(
														    text=_lowercase				,     add_special_tokens=_lowercase				,     padding=_lowercase				,     truncation=_lowercase				,     max_length=_lowercase				,     stride=_lowercase				,     pad_to_multiple_of=_lowercase				,     return_attention_mask=_lowercase				,     return_overflowing_tokens=_lowercase				,     return_special_tokens_mask=_lowercase				,     return_offsets_mapping=_lowercase				,     return_token_type_ids=_lowercase				,     return_length=_lowercase				,     verbose=_lowercase				,     return_tensors=_lowercase				,     **_lowercase				,     )
														snake_case_ :			List[str]            =      qformer_text_encoding.pop("""input_ids""" )
														snake_case_ :			Union[str, Any]            =      qformer_text_encoding.pop("""attention_mask""" )

								if images is not None:
														snake_case_ :			Tuple            =      self.image_processor(_lowercase				,     return_tensors=_lowercase )
														encoding.update(_lowercase )

								return encoding





		def  UpperCAmelCase__				(       self				,     *_lowercase				,     **_lowercase )			->    List[Any]:







								'''simple docstring'''

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





		def  UpperCAmelCase__				(       self				,     *_lowercase				,     **_lowercase )			->    Dict:







								'''simple docstring'''

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





		@property
		# Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names
		def  UpperCAmelCase__				(       self )			->    List[Any]:







								'''simple docstring'''

								snake_case_ :			str            =      self.tokenizer.model_input_names
								snake_case_ :			List[Any]            =      self.image_processor.model_input_names
								return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names ) )





		def  UpperCAmelCase__				(       self				,     _lowercase				,     **_lowercase )			->    Optional[int]:







								'''simple docstring'''

								if os.path.isfile(_lowercase ):
														raise ValueError(f'Provided path ({save_directory}) should be a directory, not a file' )
								os.makedirs(_lowercase				,     exist_ok=_lowercase )
								snake_case_ :			int            =      os.path.join(_lowercase				,     """qformer_tokenizer""" )
								self.qformer_tokenizer.save_pretrained(_lowercase )
								return super().save_pretrained(_lowercase				,     **_lowercase )





		@classmethod
		def  UpperCAmelCase__				(       cls				,     _lowercase				,     **_lowercase )			->    int:







								'''simple docstring'''

								snake_case_ :			List[str]            =      AutoTokenizer.from_pretrained(_lowercase				,     subfolder="""qformer_tokenizer""" )
								snake_case_ :			Union[str, Any]            =      cls._get_arguments_from_pretrained(_lowercase				,     **_lowercase )
								args.append(_lowercase )
								return cls(*_lowercase )








"""simple docstring"""





from packaging import version

from .import_utils import is_accelerate_available


if is_accelerate_available():
							import accelerate



def 					lowerCAmelCase_      (   SCREAMING_SNAKE_CASE							:				str ):


							'''simple docstring'''







							if not is_accelerate_available():
														return method
							__lowerCamelCase   :   Dict       =version.parse(accelerate.__version__ ).base_version
							if version.parse(SCREAMING_SNAKE_CASE ) < version.parse('''0.17.0''' ):
														return method

							def wrapper(self							:				List[Any]     ,    *SCREAMING_SNAKE_CASE							:				Any     ,    **SCREAMING_SNAKE_CASE							:				List[str] ):
														if hasattr(self     ,    '''_hf_hook''' ) and hasattr(self._hf_hook     ,    '''pre_forward''' ):
																					self._hf_hook.pre_forward(self )
														return method(self     ,    *SCREAMING_SNAKE_CASE     ,    **SCREAMING_SNAKE_CASE )

							return wrapper




