
import re
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin

class    lowerCAmelCase__     (	__lowercase			):
		a__       :						Any        =		["""image_processor""", """tokenizer"""]
		a__       :						Any        =		"""AutoImageProcessor"""
		a__       :						Union[str, Any]        =		"""AutoTokenizer"""






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

						__lowerCamelCase    					=	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__(SCREAMING_SNAKE_CASE__							,			SCREAMING_SNAKE_CASE__  )
						__lowerCamelCase    					=	self.image_processor
						__lowerCamelCase    					=	False






		def __call__(		self		:							Union[str, Any]							,			*SCREAMING_SNAKE_CASE__		:							Any							,			**SCREAMING_SNAKE_CASE__		:							Optional[Any]  )     ->							Any:
						# For backward compatibility
						if self._in_target_context_manager:
										return self.current_processor(*SCREAMING_SNAKE_CASE__							,			**SCREAMING_SNAKE_CASE__  )

						__lowerCamelCase    					=	kwargs.pop('''images'''							,			SCREAMING_SNAKE_CASE__  )
						__lowerCamelCase    					=	kwargs.pop('''text'''							,			SCREAMING_SNAKE_CASE__  )
						if len(SCREAMING_SNAKE_CASE__  ) > 0:
										__lowerCamelCase    					=	args[0]
										__lowerCamelCase    					=	args[1:]

						if images is None and text is None:
										raise ValueError('''You need to specify either an `images` or `text` input to process.'''  )

						if images is not None:
										__lowerCamelCase    					=	self.image_processor(SCREAMING_SNAKE_CASE__							,			*SCREAMING_SNAKE_CASE__							,			**SCREAMING_SNAKE_CASE__  )
						if text is not None:
										__lowerCamelCase    					=	self.tokenizer(SCREAMING_SNAKE_CASE__							,			**SCREAMING_SNAKE_CASE__  )

						if text is None:
										return inputs
						elif images is None:
										return encodings
						else:
										__lowerCamelCase    					=	encodings['''input_ids''']
										return inputs






		def  __A       (		self		:							Tuple							,			*SCREAMING_SNAKE_CASE__		:							Any							,			**SCREAMING_SNAKE_CASE__		:							Any  )     ->							List[Any]:
						return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE__							,			**SCREAMING_SNAKE_CASE__  )






		def  __A       (		self		:							Optional[int]							,			*SCREAMING_SNAKE_CASE__		:							Optional[int]							,			**SCREAMING_SNAKE_CASE__		:							int  )     ->							Optional[int]:
						return self.tokenizer.decode(*SCREAMING_SNAKE_CASE__							,			**SCREAMING_SNAKE_CASE__  )






		@contextmanager
		def  __A       (		self		:							List[str]  )     ->							Union[str, Any]:
						warnings.warn(
						    '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '''
						    '''labels by using the argument `text` of the regular `__call__` method (either in the same call as '''
						    '''your images inputs, or in a separate call.'''  )
						__lowerCamelCase    					=	True
						__lowerCamelCase    					=	self.tokenizer
						yield
						__lowerCamelCase    					=	self.image_processor
						__lowerCamelCase    					=	False






		def  __A       (		self		:							Any							,			SCREAMING_SNAKE_CASE__		:							List[Any]							,			SCREAMING_SNAKE_CASE__		:							List[Any]=False							,			SCREAMING_SNAKE_CASE__		:							List[Any]=None  )     ->							Tuple:
						if added_vocab is None:
										__lowerCamelCase    					=	self.tokenizer.get_added_vocab()

						__lowerCamelCase    					=	{}

						while tokens:
										__lowerCamelCase    					=	re.search(R'''<s_(.*?)>'''							,			SCREAMING_SNAKE_CASE__							,			re.IGNORECASE  )
										if start_token is None:
														break
										__lowerCamelCase    					=	start_token.group(1  )
										__lowerCamelCase    					=	re.search(Rf'''</s_{key}>'''							,			SCREAMING_SNAKE_CASE__							,			re.IGNORECASE  )
										__lowerCamelCase    					=	start_token.group()
										if end_token is None:
														__lowerCamelCase    					=	tokens.replace(SCREAMING_SNAKE_CASE__							,			''''''  )
										else:
														__lowerCamelCase    					=	end_token.group()
														__lowerCamelCase    					=	re.escape(SCREAMING_SNAKE_CASE__  )
														__lowerCamelCase    					=	re.escape(SCREAMING_SNAKE_CASE__  )
														__lowerCamelCase    					=	re.search(f'''{start_token_escaped}(.*?){end_token_escaped}'''							,			SCREAMING_SNAKE_CASE__							,			re.IGNORECASE  )
														if content is not None:
																		__lowerCamelCase    					=	content.group(1  ).strip()
																		if r"<s_" in content and r"</s_" in content:  # non-leaf node
																						__lowerCamelCase    					=	self.tokenajson(SCREAMING_SNAKE_CASE__							,			is_inner_value=SCREAMING_SNAKE_CASE__							,			added_vocab=SCREAMING_SNAKE_CASE__  )
																						if value:
																										if len(SCREAMING_SNAKE_CASE__  ) == 1:
																														__lowerCamelCase    					=	value[0]
																										__lowerCamelCase    					=	value
																		else:  # leaf nodes
																						__lowerCamelCase    					=	[]
																						for leaf in content.split(R'''<sep/>'''  ):
																										__lowerCamelCase    					=	leaf.strip()
																										if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>":
																														__lowerCamelCase    					=	leaf[1:-2]  # for categorical special tokens
																										output[key].append(SCREAMING_SNAKE_CASE__  )
																						if len(output[key]  ) == 1:
																										__lowerCamelCase    					=	output[key][0]

														__lowerCamelCase    					=	tokens[tokens.find(SCREAMING_SNAKE_CASE__  ) + len(SCREAMING_SNAKE_CASE__  ) :].strip()
														if tokens[:6] == r"<sep/>":  # non-leaf nodes
																		return [output] + self.tokenajson(tokens[6:]							,			is_inner_value=SCREAMING_SNAKE_CASE__							,			added_vocab=SCREAMING_SNAKE_CASE__  )

						if len(SCREAMING_SNAKE_CASE__  ):
										return [output] if is_inner_value else output
						else:
										return [] if is_inner_value else {"text_sequence": tokens}






		@property
		def  __A       (		self		:							Optional[Any]  )     ->							Dict:
						warnings.warn(
						    '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''							,			SCREAMING_SNAKE_CASE__							,			)
						return self.image_processor_class







		@property
		def  __A       (		self		:							Any  )     ->							Union[str, Any]:
						warnings.warn(
						    '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''							,			SCREAMING_SNAKE_CASE__							,			)
						return self.image_processor





from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
					if not (is_transformers_available() and is_torch_available() and is_transformers_version(">=", "4.25.0")):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline
else:
					from .pipeline_unclip import UnCLIPPipeline
					from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline
					from .text_proj import UnCLIPTextProjModel



