
"""simple docstring"""




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   _lowercase						(       lowerCAmelCase_				):


     '''simple docstring'''





     _A								=							['image_processor', 'tokenizer']
     _A								=							'OwlViTImageProcessor'
     _A								=							('CLIPTokenizer', 'CLIPTokenizerFast')



     def __init__(      self ,			__UpperCamelCase=None ,			__UpperCamelCase=None ,			**__UpperCamelCase  )->					int:
          UpperCAmelCase__							: Optional[int] 							=						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 ,			)
               UpperCAmelCase__							: int 							=						kwargs.pop("feature_extractor"  )

          UpperCAmelCase__							: Any 							=						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 ,			__UpperCamelCase=None ,			__UpperCamelCase=None ,			__UpperCamelCase=None ,			__UpperCamelCase="max_length" ,			__UpperCamelCase="np" ,			**__UpperCamelCase  )->					Union[str, Any]:







          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  )):
                    UpperCAmelCase__							: str 							=						[self.tokenizer(__UpperCamelCase ,			padding=__UpperCamelCase ,			return_tensors=__UpperCamelCase ,			**__UpperCamelCase  )]

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

                    # Maximum number of queries across batch
                    UpperCAmelCase__							: List[Any] 							=						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:
                              UpperCAmelCase__							: Tuple 							=						t + [" "] * (max_num_queries - len(__UpperCamelCase  ))

                         UpperCAmelCase__							: Optional[int] 							=						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":
                    UpperCAmelCase__							: str 							=						np.concatenate([encoding["input_ids"] for encoding in encodings] ,			axis=0  )
                    UpperCAmelCase__							: Tuple 							=						np.concatenate([encoding["attention_mask"] for encoding in encodings] ,			axis=0  )

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

                    UpperCAmelCase__							: List[str] 							=						jnp.concatenate([encoding["input_ids"] for encoding in encodings] ,			axis=0  )
                    UpperCAmelCase__							: Tuple 							=						jnp.concatenate([encoding["attention_mask"] for encoding in encodings] ,			axis=0  )

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

                    UpperCAmelCase__							: int 							=						torch.cat([encoding["input_ids"] for encoding in encodings] ,			dim=0  )
                    UpperCAmelCase__							: Optional[Any] 							=						torch.cat([encoding["attention_mask"] for encoding in encodings] ,			dim=0  )

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

                    UpperCAmelCase__							: Dict 							=						tf.stack([encoding["input_ids"] for encoding in encodings] ,			axis=0  )
                    UpperCAmelCase__							: Union[str, Any] 							=						tf.stack([encoding["attention_mask"] for encoding in encodings] ,			axis=0  )

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

               UpperCAmelCase__							: Union[str, Any] 							=						BatchEncoding()
               UpperCAmelCase__							: int 							=						input_ids
               UpperCAmelCase__							: Dict 							=						attention_mask

          if query_images is not None:
               UpperCAmelCase__							: Optional[Any] 							=						BatchEncoding()
               UpperCAmelCase__							: Tuple 							=						self.image_processor(
                   __UpperCamelCase ,			return_tensors=__UpperCamelCase ,			**__UpperCamelCase  ).pixel_values
               UpperCAmelCase__							: List[Any] 							=						query_pixel_values

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

          if text is not None and images is not None:
               UpperCAmelCase__							: str 							=						image_features.pixel_values
               return encoding
          elif query_images is not None and images is not None:
               UpperCAmelCase__							: Any 							=						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  lowerCAmelCase__       (      self ,			*__UpperCamelCase ,			**__UpperCamelCase  )->					List[str]:
          return self.image_processor.post_process(*__UpperCamelCase ,			**__UpperCamelCase  )



     def  lowerCAmelCase__       (      self ,			*__UpperCamelCase ,			**__UpperCamelCase  )->					Optional[Any]:
          return self.image_processor.post_process_object_detection(*__UpperCamelCase ,			**__UpperCamelCase  )



     def  lowerCAmelCase__       (      self ,			*__UpperCamelCase ,			**__UpperCamelCase  )->					int:
          return self.image_processor.post_process_image_guided_detection(*__UpperCamelCase ,			**__UpperCamelCase  )



     def  lowerCAmelCase__       (      self ,			*__UpperCamelCase ,			**__UpperCamelCase  )->					Union[str, Any]:
          return self.tokenizer.batch_decode(*__UpperCamelCase ,			**__UpperCamelCase  )



     def  lowerCAmelCase__       (      self ,			*__UpperCamelCase ,			**__UpperCamelCase  )->					Optional[Any]:
          return self.tokenizer.decode(*__UpperCamelCase ,			**__UpperCamelCase  )



     @property
     def  lowerCAmelCase__       (      self  )->					str:
          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  lowerCAmelCase__       (      self  )->					Tuple:
          warnings.warn(
              "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead." ,			__UpperCamelCase ,			)
          return self.image_processor





"""simple docstring"""




import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin







class   _lowercase						(       unittest.TestCase	,   lowerCAmelCase_				):


     '''simple docstring'''





     def  lowerCAmelCase__       (      self  )->					Dict:
          UpperCAmelCase__							: Optional[Any] 							=						load_tool("text-classification"  )
          self.tool.setup()
          UpperCAmelCase__							: List[str] 							=						load_tool("text-classification" ,			remote=__UpperCamelCase  )



     def  lowerCAmelCase__       (      self  )->					Union[str, Any]:
          UpperCAmelCase__							: Dict 							=						self.tool("That's quite cool" ,			["positive", "negative"]  )
          self.assertEqual(__UpperCamelCase ,			"positive"  )



     def  lowerCAmelCase__       (      self  )->					str:
          UpperCAmelCase__							: List[Any] 							=						self.remote_tool("That's quite cool" ,			["positive", "negative"]  )
          self.assertEqual(__UpperCamelCase ,			"positive"  )



     def  lowerCAmelCase__       (      self  )->					Optional[int]:
          UpperCAmelCase__							: Any 							=						self.tool(text="That's quite cool" ,			labels=["positive", "negative"]  )
          self.assertEqual(__UpperCamelCase ,			"positive"  )



     def  lowerCAmelCase__       (      self  )->					str:
          UpperCAmelCase__							: str 							=						self.remote_tool(text="That's quite cool" ,			labels=["positive", "negative"]  )
          self.assertEqual(__UpperCamelCase ,			"positive"  )



