import re
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin


class 			UpperCAmelCase__    (		snake_case					):



  """simple docstring"""






  lowerCAmelCase__		:	Optional[int]    		= ['image_processor', 'tokenizer']
  lowerCAmelCase__		:	List[str]    		= 'AutoImageProcessor'
  lowerCAmelCase__		:	Optional[Any]    		= 'AutoTokenizer'






  def __init__(				self:			Union[str, Any]				,				__lowerCAmelCase:			str=None				,				__lowerCAmelCase:			str=None				,				**__lowerCAmelCase:			List[str]   ) -> Dict:




    '''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."				,				__lowerCAmelCase				,				)
      __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__(__lowerCAmelCase				,				__lowerCAmelCase   )
    __UpperCAmelCase						    =       self.image_processor
    __UpperCAmelCase						    =       False






  def __call__(				self:			str				,				*__lowerCAmelCase:			Any				,				**__lowerCAmelCase:			Optional[Any]   ) -> Any:




    '''simple docstring'''




    if self._in_target_context_manager:
      return self.current_processor(*__lowerCAmelCase				,				**__lowerCAmelCase   )

    __UpperCAmelCase						    =       kwargs.pop("images"				,				__lowerCAmelCase   )
    __UpperCAmelCase						    =       kwargs.pop("text"				,				__lowerCAmelCase   )
    if len(__lowerCAmelCase   ) > 0:
      __UpperCAmelCase						    =       args[0]
      __UpperCAmelCase						    =       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:
      __UpperCAmelCase						    =       self.image_processor(__lowerCAmelCase				,				*__lowerCAmelCase				,				**__lowerCAmelCase   )
    if text is not None:
      __UpperCAmelCase						    =       self.tokenizer(__lowerCAmelCase				,				**__lowerCAmelCase   )

    if text is None:
      return inputs
    elif images is None:
      return encodings
    else:
      __UpperCAmelCase						    =       encodings["input_ids"]
      return inputs






  def 	_UpperCAmelCase				(				self:			List[Any]				,				*__lowerCAmelCase:			Optional[Any]				,				**__lowerCAmelCase:			Optional[Any]   ) -> Tuple:




    '''simple docstring'''




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






  def 	_UpperCAmelCase				(				self:			Optional[Any]				,				*__lowerCAmelCase:			Tuple				,				**__lowerCAmelCase:			List[Any]   ) -> str:




    '''simple docstring'''




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






  @contextmanager
  def 	_UpperCAmelCase				(				self:			Optional[Any]   ) -> Optional[Any]:




    '''simple docstring'''




    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."   )
    __UpperCAmelCase						    =       True
    __UpperCAmelCase						    =       self.tokenizer
    yield
    __UpperCAmelCase						    =       self.image_processor
    __UpperCAmelCase						    =       False






  def 	_UpperCAmelCase				(				self:			Optional[int]				,				__lowerCAmelCase:			List[str]				,				__lowerCAmelCase:			List[Any]=False				,				__lowerCAmelCase:			Tuple=None   ) -> Tuple:




    '''simple docstring'''




    if added_vocab is None:
      __UpperCAmelCase						    =       self.tokenizer.get_added_vocab()

    __UpperCAmelCase						    =       {}

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

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

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






  @property
  def 	_UpperCAmelCase				(				self:			Any   ) -> Union[str, Any]:




    '''simple docstring'''




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






  @property
  def 	_UpperCAmelCase				(				self:			Union[str, Any]   ) -> Any:




    '''simple docstring'''




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




import inspect
import unittest


class 			UpperCAmelCase__    (		unittest.TestCase					):



  """simple docstring"""






  def 	_UpperCAmelCase				(				self:			Union[str, Any]   ) -> Dict:




    '''simple docstring'''




    try:
      import diffusers  # noqa: F401
    except ImportError:
      assert False






  def 	_UpperCAmelCase				(				self:			Dict   ) -> Dict:




    '''simple docstring'''




    import diffusers
    from diffusers.dependency_versions_table import deps

    __UpperCAmelCase						    =       inspect.getmembers(__lowerCAmelCase				,				inspect.isclass   )

    for cls_name, cls_module in all_classes:
      if "dummy_" in cls_module.__module__:
        for backend in cls_module._backends:
          if backend == "k_diffusion":
            __UpperCAmelCase						    =       "k-diffusion"
          elif backend == "invisible_watermark":
            __UpperCAmelCase						    =       "invisible-watermark"
          assert backend in deps, F'''{backend} is not in the deps table!'''



