





import warnings
from typing import List

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import is_flax_available, is_tf_available, is_torch_available





class UpperCAmelCase_				( _SCREAMING_SNAKE_CASE ):
  __lowerCAmelCase   :						Optional[int]							=   ["""image_processor""", """tokenizer"""]
  __lowerCAmelCase   :						Any							=   """OwlViTImageProcessor"""
  __lowerCAmelCase   :						str							=   ("""CLIPTokenizer""", """CLIPTokenizerFast""")


  def __init__(							self ,__snake_case=None ,__snake_case=None ,**__snake_case ):



       """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.''' ,__snake_case ,)
            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__(__snake_case ,__snake_case )


  def __call__(							self ,__snake_case=None ,__snake_case=None ,__snake_case=None ,__snake_case="max_length" ,__snake_case="np" ,**__snake_case ):



       """simple docstring"""





       if text is None and query_images is None and images is None:
            raise ValueError(
                '''You have to specify at least one text or query image or image. All three cannot be none.''' )

       if text is not None:
            if isinstance(__snake_case ,__snake_case ) or (isinstance(__snake_case ,__snake_case ) and not isinstance(text[0] ,__snake_case )):
                 A_          =       [self.tokenizer(__snake_case ,padding=__snake_case ,return_tensors=__snake_case ,**__snake_case )]

            elif isinstance(__snake_case ,__snake_case ) and isinstance(text[0] ,__snake_case ):
                 A_          =       []

                 # Maximum number of queries across batch
                 A_          =       max([len(__snake_case ) for t in text] )

                 # Pad all batch samples to max number of text queries
                 for t in text:
                      if len(__snake_case ) != max_num_queries:
                           A_          =       t + [''' '''] * (max_num_queries - len(__snake_case ))

                      A_          =       self.tokenizer(__snake_case ,padding=__snake_case ,return_tensors=__snake_case ,**__snake_case )
                      encodings.append(__snake_case )
            else:
                 raise TypeError('''Input text should be a string, a list of strings or a nested list of strings''' )

            if return_tensors == "np":
                 A_          =       np.concatenate([encoding['''input_ids'''] for encoding in encodings] ,axis=0 )
                 A_          =       np.concatenate([encoding['''attention_mask'''] for encoding in encodings] ,axis=0 )

            elif return_tensors == "jax" and is_flax_available():
                 import jax.numpy as jnp

                 A_          =       jnp.concatenate([encoding['''input_ids'''] for encoding in encodings] ,axis=0 )
                 A_          =       jnp.concatenate([encoding['''attention_mask'''] for encoding in encodings] ,axis=0 )

            elif return_tensors == "pt" and is_torch_available():
                 import torch

                 A_          =       torch.cat([encoding['''input_ids'''] for encoding in encodings] ,dim=0 )
                 A_          =       torch.cat([encoding['''attention_mask'''] for encoding in encodings] ,dim=0 )

            elif return_tensors == "tf" and is_tf_available():
                 import tensorflow as tf

                 A_          =       tf.stack([encoding['''input_ids'''] for encoding in encodings] ,axis=0 )
                 A_          =       tf.stack([encoding['''attention_mask'''] for encoding in encodings] ,axis=0 )

            else:
                 raise ValueError('''Target return tensor type could not be returned''' )

            A_          =       BatchEncoding()
            A_          =       input_ids
            A_          =       attention_mask

       if query_images is not None:
            A_          =       BatchEncoding()
            A_          =       self.image_processor(
                __snake_case ,return_tensors=__snake_case ,**__snake_case ).pixel_values
            A_          =       query_pixel_values

       if images is not None:
            A_          =       self.image_processor(__snake_case ,return_tensors=__snake_case ,**__snake_case )

       if text is not None and images is not None:
            A_          =       image_features.pixel_values
            return encoding
       elif query_images is not None and images is not None:
            A_          =       image_features.pixel_values
            return encoding
       elif text is not None or query_images is not None:
            return encoding
       else:
            return BatchEncoding(data=dict(**__snake_case ) ,tensor_type=__snake_case )


  def 						__UpperCAmelCase     (							self ,*__snake_case ,**__snake_case ):



       """simple docstring"""





       return self.image_processor.post_process(*__snake_case ,**__snake_case )


  def 						__UpperCAmelCase     (							self ,*__snake_case ,**__snake_case ):



       """simple docstring"""





       return self.image_processor.post_process_object_detection(*__snake_case ,**__snake_case )


  def 						__UpperCAmelCase     (							self ,*__snake_case ,**__snake_case ):



       """simple docstring"""





       return self.image_processor.post_process_image_guided_detection(*__snake_case ,**__snake_case )


  def 						__UpperCAmelCase     (							self ,*__snake_case ,**__snake_case ):



       """simple docstring"""





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


  def 						__UpperCAmelCase     (							self ,*__snake_case ,**__snake_case ):



       """simple docstring"""





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


  @property
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





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




  @property
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





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










def      UpperCAmelCase_    (    _UpperCAmelCase							:Any  ,						_UpperCAmelCase							:Dict				)	->				Union[str, Any]:
     '''simple docstring'''


     A_          =       [1]
     for i in range(2  ,						_UpperCAmelCase				):
          factorials.append(factorials[-1] * i				)
     assert 0 <= k < factorials[-1] * n, "k out of bounds"

     A_          =       []
     A_          =       list(range(_UpperCAmelCase				)				)

     # Find permutation
     while factorials:
          A_          =       factorials.pop()
          A_   ,    A_          =       divmod(_UpperCAmelCase  ,						_UpperCAmelCase				)
          permutation.append(elements[number]				)
          elements.remove(elements[number]				)
     permutation.append(elements[0]				)

     return permutation


if __name__ == "__main__":
      import doctest

      doctest.testmod()



