

from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType






class  UpperCAmelCase__	( A__ ):






  """simple docstring"""


  a     =      ["image_processor", "tokenizer"]
  a     =      "BlipImageProcessor"
  a     =      "AutoTokenizer"






  def __init__(							self		:	Optional[int]			,     __lowerCamelCase		:	Union[str, Any]			,     __lowerCamelCase		:	Union[str, Any]      )	->							str:
       SCREAMING_SNAKE_CASE__					=					False
       super().__init__(__lowerCamelCase			,     __lowerCamelCase      )
       SCREAMING_SNAKE_CASE__					=					self.image_processor






  def __call__(							self		:	Optional[Any]			,     __lowerCamelCase		:	ImageInput = None			,     __lowerCamelCase		:	Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None			,     __lowerCamelCase		:	bool = True			,     __lowerCamelCase		:	Union[bool, str, PaddingStrategy] = False			,     __lowerCamelCase		:	Union[bool, str, TruncationStrategy] = None			,     __lowerCamelCase		:	Optional[int] = None			,     __lowerCamelCase		:	int = 0			,     __lowerCamelCase		:	Optional[int] = None			,     __lowerCamelCase		:	Optional[bool] = None			,     __lowerCamelCase		:	bool = False			,     __lowerCamelCase		:	bool = False			,     __lowerCamelCase		:	bool = False			,     __lowerCamelCase		:	bool = False			,     __lowerCamelCase		:	bool = False			,     __lowerCamelCase		:	bool = True			,     __lowerCamelCase		:	Optional[Union[str, TensorType]] = None			,     **__lowerCamelCase		:	List[str]			,     )	->							BatchEncoding:
       if images is None and text is None:
            raise ValueError('''You have to specify either images or text.'''      )

       # Get only text
       if images is None:
            SCREAMING_SNAKE_CASE__					=					self.tokenizer
            SCREAMING_SNAKE_CASE__					=					self.tokenizer(
                text=__lowerCamelCase			,     add_special_tokens=__lowerCamelCase			,     padding=__lowerCamelCase			,     truncation=__lowerCamelCase			,     max_length=__lowerCamelCase			,     stride=__lowerCamelCase			,     pad_to_multiple_of=__lowerCamelCase			,     return_attention_mask=__lowerCamelCase			,     return_overflowing_tokens=__lowerCamelCase			,     return_special_tokens_mask=__lowerCamelCase			,     return_offsets_mapping=__lowerCamelCase			,     return_token_type_ids=__lowerCamelCase			,     return_length=__lowerCamelCase			,     verbose=__lowerCamelCase			,     return_tensors=__lowerCamelCase			,     **__lowerCamelCase			,     )
            return text_encoding

       # add pixel_values
       SCREAMING_SNAKE_CASE__					=					self.image_processor(__lowerCamelCase			,     return_tensors=__lowerCamelCase      )

       if text is not None:
            SCREAMING_SNAKE_CASE__					=					self.tokenizer(
                text=__lowerCamelCase			,     add_special_tokens=__lowerCamelCase			,     padding=__lowerCamelCase			,     truncation=__lowerCamelCase			,     max_length=__lowerCamelCase			,     stride=__lowerCamelCase			,     pad_to_multiple_of=__lowerCamelCase			,     return_attention_mask=__lowerCamelCase			,     return_overflowing_tokens=__lowerCamelCase			,     return_special_tokens_mask=__lowerCamelCase			,     return_offsets_mapping=__lowerCamelCase			,     return_token_type_ids=__lowerCamelCase			,     return_length=__lowerCamelCase			,     verbose=__lowerCamelCase			,     return_tensors=__lowerCamelCase			,     **__lowerCamelCase			,     )
       else:
            SCREAMING_SNAKE_CASE__					=					None

       if text_encoding is not None:
            encoding_image_processor.update(__lowerCamelCase      )

       return encoding_image_processor






  def    lowercase_	(							self		:	List[str]			,     *__lowerCamelCase		:	Optional[int]			,     **__lowerCamelCase		:	Tuple      )	->							List[Any]:
       return self.tokenizer.batch_decode(*__lowerCamelCase			,     **__lowerCamelCase      )






  def    lowercase_	(							self		:	Any			,     *__lowerCamelCase		:	Dict			,     **__lowerCamelCase		:	Optional[int]      )	->							List[str]:
       return self.tokenizer.decode(*__lowerCamelCase			,     **__lowerCamelCase      )







  @property
  # Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names
  def    lowercase_	(							self		:	Optional[Any]      )	->							Optional[int]:
       SCREAMING_SNAKE_CASE__					=					self.tokenizer.model_input_names
       SCREAMING_SNAKE_CASE__					=					self.image_processor.model_input_names
       return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names      )      )





from importlib import import_module

from .logging import get_logger


_SCREAMING_SNAKE_CASE  :						Optional[int]         =    get_logger(__name__)






class  UpperCAmelCase__	:






  """simple docstring"""


  def __init__(							self		:	Optional[int]			,     __lowerCamelCase		:	Optional[Any]			,     __lowerCamelCase		:	Any=None      )	->							int:
       SCREAMING_SNAKE_CASE__					=					attrs or []
       if module is not None:
            for key in module.__dict__:
                 if key in attrs or not key.startswith('''__'''      ):
                      setattr(self			,     __lowerCamelCase			,     getattr(__lowerCamelCase			,     __lowerCamelCase      )      )
       SCREAMING_SNAKE_CASE__					=					module._original_module if isinstance(__lowerCamelCase			,     _PatchedModuleObj      ) else module






class  UpperCAmelCase__	:






  """simple docstring"""


  a     =      []






  def __init__(							self		:	int			,     __lowerCamelCase		:	Union[str, Any]			,     __lowerCamelCase		:	str			,     __lowerCamelCase		:	Optional[int]			,     __lowerCamelCase		:	List[Any]=None      )	->							Any:
       SCREAMING_SNAKE_CASE__					=					obj
       SCREAMING_SNAKE_CASE__					=					target
       SCREAMING_SNAKE_CASE__					=					new
       SCREAMING_SNAKE_CASE__					=					target.split('''.'''      )[0]
       SCREAMING_SNAKE_CASE__					=					{}
       SCREAMING_SNAKE_CASE__					=					attrs or []






  def __enter__(							self		:	int      )	->							Tuple:
       *SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__					=					self.target.split('''.'''      )

       # Patch modules:
       # it's used to patch attributes of submodules like "os.path.join";
       # in this case we need to patch "os" and "os.path"

       for i in range(len(__lowerCamelCase      )      ):
            try:
                 SCREAMING_SNAKE_CASE__					=					import_module('''.'''.join(submodules[: i + 1]      )      )
            except ModuleNotFoundError:
                 continue
            # We iterate over all the globals in self.obj in case we find "os" or "os.path"
            for attr in self.obj.__dir__():
                 SCREAMING_SNAKE_CASE__					=					getattr(self.obj			,     __lowerCamelCase      )
                 # We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
                 # This allows to patch renamed modules like "from os import path as ospath".
                 if obj_attr is submodule or (
                     (isinstance(__lowerCamelCase			,     _PatchedModuleObj      ) and obj_attr._original_module is submodule)
                 ):
                      SCREAMING_SNAKE_CASE__					=					obj_attr
                      # patch at top level
                      setattr(self.obj			,     __lowerCamelCase			,     _PatchedModuleObj(__lowerCamelCase			,     attrs=self.attrs      )      )
                      SCREAMING_SNAKE_CASE__					=					getattr(self.obj			,     __lowerCamelCase      )
                      # construct lower levels patches
                      for key in submodules[i + 1 :]:
                           setattr(__lowerCamelCase			,     __lowerCamelCase			,     _PatchedModuleObj(getattr(__lowerCamelCase			,     __lowerCamelCase			,     __lowerCamelCase      )			,     attrs=self.attrs      )      )
                           SCREAMING_SNAKE_CASE__					=					getattr(__lowerCamelCase			,     __lowerCamelCase      )
                      # finally set the target attribute
                      setattr(__lowerCamelCase			,     __lowerCamelCase			,     self.new      )

        # Patch attribute itself:
        # it's used for builtins like "open",
        # and also to patch "os.path.join" we may also need to patch "join"
        # itself if it was imported as "from os.path import join".

       if submodules:  # if it's an attribute of a submodule like "os.path.join"
            try:
                 SCREAMING_SNAKE_CASE__					=					getattr(import_module('''.'''.join(__lowerCamelCase      )      )			,     __lowerCamelCase      )
            except (AttributeError, ModuleNotFoundError):
                 return
            # We iterate over all the globals in self.obj in case we find "os.path.join"
            for attr in self.obj.__dir__():
                 # We don't check for the name of the global, but rather if its value *is* "os.path.join".
                 # This allows to patch renamed attributes like "from os.path import join as pjoin".
                 if getattr(self.obj			,     __lowerCamelCase      ) is attr_value:
                      SCREAMING_SNAKE_CASE__					=					getattr(self.obj			,     __lowerCamelCase      )
                      setattr(self.obj			,     __lowerCamelCase			,     self.new      )
       elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
            SCREAMING_SNAKE_CASE__					=					globals()['''__builtins__'''][target_attr]
            setattr(self.obj			,     __lowerCamelCase			,     self.new      )
       else:
            raise RuntimeError(f'''Tried to patch attribute {target_attr} instead of a submodule.'''      )






  def __exit__(							self		:	Optional[Any]			,     *__lowerCamelCase		:	Tuple      )	->							List[str]:
       for attr in list(self.original      ):
            setattr(self.obj			,     __lowerCamelCase			,     self.original.pop(__lowerCamelCase      )      )






  def    lowercase_	(							self		:	List[Any]      )	->							Optional[Any]:
       self.__enter__()
       self._active_patches.append(self      )







  def    lowercase_	(							self		:	int      )	->							int:
       try:
            self._active_patches.remove(self      )
       except ValueError:
            # If the patch hasn't been started this will fail
            return None

       return self.__exit__()


