





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  lowerCAmelCase							(       _lowercase ):
  __lowerCamelCase		=      ['image_processor', 'tokenizer']
  __lowerCamelCase		=      'OwlViTImageProcessor'
  __lowerCamelCase		=      ('CLIPTokenizer', 'CLIPTokenizerFast')







  def __init__(      self  :Any   ,							_lowercase  :int=None   ,							_lowercase  :Optional[Any]=None   ,							**_lowercase  :List[str] ):






     '''simple docstring'''





     lowercase__			       =  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   ,							)
        lowercase__			       =  kwargs.pop("feature_extractor" )

     lowercase__			       =  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 )







  def __call__(      self  :List[str]   ,							_lowercase  :Dict=None   ,							_lowercase  :List[str]=None   ,							_lowercase  :Tuple=None   ,							_lowercase  :Optional[Any]="max_length"   ,							_lowercase  :List[Any]="np"   ,							**_lowercase  :Optional[int] ):






     '''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(__UpperCamelCase   ,							__UpperCamelCase ) or (isinstance(__UpperCamelCase   ,							__UpperCamelCase ) and not isinstance(text[0]   ,							__UpperCamelCase )):
           lowercase__			       =  [self.tokenizer(__UpperCamelCase   ,							padding=__UpperCamelCase   ,							return_tensors=__UpperCamelCase   ,							**__UpperCamelCase )]

        elif isinstance(__UpperCamelCase   ,							__UpperCamelCase ) and isinstance(text[0]   ,							__UpperCamelCase ):
           lowercase__			       =  []

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

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

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

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

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

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

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

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

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

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

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

        lowercase__			       =  BatchEncoding()
        lowercase__			       =  input_ids
        lowercase__			       =  attention_mask

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

     if images is not None:
        lowercase__			       =  self.image_processor(__UpperCamelCase   ,							return_tensors=__UpperCamelCase   ,							**__UpperCamelCase )

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







  def   UpperCAmelCase					(      self  :str   ,							*_lowercase  :str   ,							**_lowercase  :str ):






     '''simple docstring'''





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







  def   UpperCAmelCase					(      self  :str   ,							*_lowercase  :Optional[Any]   ,							**_lowercase  :Optional[Any] ):






     '''simple docstring'''





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







  def   UpperCAmelCase					(      self  :List[Any]   ,							*_lowercase  :List[str]   ,							**_lowercase  :Optional[Any] ):






     '''simple docstring'''





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







  def   UpperCAmelCase					(      self  :Tuple   ,							*_lowercase  :Optional[Any]   ,							**_lowercase  :Any ):






     '''simple docstring'''





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







  def   UpperCAmelCase					(      self  :Optional[int]   ,							*_lowercase  :Tuple   ,							**_lowercase  :List[Any] ):






     '''simple docstring'''





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







  @property
  def   UpperCAmelCase					(      self  :Optional[Any] ):






     '''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   UpperCAmelCase					(      self  :List[str] ):






     '''simple docstring'''





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













from abc import ABC, abstractmethod
from typing import Optional, Union

from .. import Dataset, DatasetDict, Features, IterableDataset, IterableDatasetDict, NamedSplit
from ..utils.typing import NestedDataStructureLike, PathLike





class  lowerCAmelCase							(       lowercase_ ):







  def __init__(      self  :str   ,							_lowercase  :Optional[NestedDataStructureLike[PathLike]] = None   ,							_lowercase  :Optional[NamedSplit] = None   ,							_lowercase  :Optional[Features] = None   ,							_lowercase  :str = None   ,							_lowercase  :bool = False   ,							_lowercase  :bool = False   ,							_lowercase  :Optional[int] = None   ,							**_lowercase  :Tuple   ,							):






     '''simple docstring'''





     lowercase__			       =  path_or_paths
     lowercase__			       =  split if split or isinstance(_lowercase   ,							_lowercase ) else "train"
     lowercase__			       =  features
     lowercase__			       =  cache_dir
     lowercase__			       =  keep_in_memory
     lowercase__			       =  streaming
     lowercase__			       =  num_proc
     lowercase__			       =  kwargs







  @abstractmethod
  def   UpperCAmelCase					(      self  :Any ):






     '''simple docstring'''





     pass





class  lowerCAmelCase							(       lowercase_ ):







  def __init__(      self  :List[Any]   ,							_lowercase  :Optional[Features] = None   ,							_lowercase  :str = None   ,							_lowercase  :bool = False   ,							_lowercase  :bool = False   ,							_lowercase  :Optional[int] = None   ,							**_lowercase  :Optional[int]   ,							):






     '''simple docstring'''





     lowercase__			       =  features
     lowercase__			       =  cache_dir
     lowercase__			       =  keep_in_memory
     lowercase__			       =  streaming
     lowercase__			       =  num_proc
     lowercase__			       =  kwargs







  @abstractmethod
  def   UpperCAmelCase					(      self  :int ):






     '''simple docstring'''





     pass






