




import re
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin


class 				UpperCamelCase_    ( UpperCAmelCase__     ):

    '''simple docstring'''






    UpperCAmelCase__							  =      ['''image_processor''', '''tokenizer''']
    UpperCAmelCase__							  =      '''AutoImageProcessor'''
    UpperCAmelCase__							  =      '''AutoTokenizer'''


    def __init__(			self      :	Union[str, Any]					, UpperCAmelCase__      :	Any=None					, UpperCAmelCase__      :	Union[str, Any]=None					, **UpperCAmelCase__      :	Optional[int])						->Dict:





           '''simple docstring'''

           A__       =    None
           if "feature_extractor" in kwargs:
                  warnings.warn(
                      '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
                      ''' instead.'''					, UpperCAmelCase__					, )
                  A__       =    kwargs.pop('''feature_extractor''')

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


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





           '''simple docstring'''

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

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

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


    def        SCREAMING_SNAKE_CASE							(			self      :	List[str]					, *UpperCAmelCase__      :	str					, **UpperCAmelCase__      :	Optional[Any])						->str:





           '''simple docstring'''

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


    def        SCREAMING_SNAKE_CASE							(			self      :	Optional[Any]					, *UpperCAmelCase__      :	Dict					, **UpperCAmelCase__      :	Any)						->int:





           '''simple docstring'''

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


    @contextmanager
    def        SCREAMING_SNAKE_CASE							(			self      :	Tuple)						->Dict:





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


    def        SCREAMING_SNAKE_CASE							(			self      :	Any					, UpperCAmelCase__      :	Tuple					, UpperCAmelCase__      :	Union[str, Any]=False					, UpperCAmelCase__      :	int=None)						->List[Any]:





           '''simple docstring'''

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

           A__       =    {}

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

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

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


    @property
    def        SCREAMING_SNAKE_CASE							(			self      :	Optional[int])						->Optional[int]:





           '''simple docstring'''

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





    @property
    def        SCREAMING_SNAKE_CASE							(			self      :	Dict)						->Any:





           '''simple docstring'''

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








import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowerCamelCase	:		Any							       =   logging.get_logger(__name__)


class 				UpperCamelCase_    ( UpperCAmelCase__     ):

    '''simple docstring'''






    UpperCAmelCase__							  =      '''encoder-decoder'''
    UpperCAmelCase__							  =      True


    def __init__(			self      :	List[str]					, **UpperCAmelCase__      :	Union[str, Any])						->List[Any]:





           '''simple docstring'''

           super().__init__(**UpperCAmelCase__)
           assert (
               "encoder" in kwargs and "decoder" in kwargs
           ), "Config has to be initialized with encoder and decoder config"
           A__       =    kwargs.pop('''encoder''')
           A__       =    encoder_config.pop('''model_type''')
           A__       =    kwargs.pop('''decoder''')
           A__       =    decoder_config.pop('''model_type''')

           from ..auto.configuration_auto import AutoConfig

           A__       =    AutoConfig.for_model(UpperCAmelCase__					, **UpperCAmelCase__)
           A__       =    AutoConfig.for_model(UpperCAmelCase__					, **UpperCAmelCase__)
           A__       =    True


    @classmethod
    def        SCREAMING_SNAKE_CASE							(			cls      :	Union[str, Any]					, UpperCAmelCase__      :	PretrainedConfig					, UpperCAmelCase__      :	PretrainedConfig					, **UpperCAmelCase__      :	Union[str, Any])						->PretrainedConfig:





           '''simple docstring'''

           logger.info('''Set `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config''')
           A__       =    True
           A__       =    True

           return cls(encoder=encoder_config.to_dict()					, decoder=decoder_config.to_dict()					, **UpperCAmelCase__)





    def        SCREAMING_SNAKE_CASE							(			self      :	str)						->Optional[Any]:





           '''simple docstring'''

           A__       =    copy.deepcopy(self.__dict__)
           A__       =    self.encoder.to_dict()
           A__       =    self.decoder.to_dict()
           A__       =    self.__class__.model_type
           return output


