

'''simple docstring'''



from copy import deepcopy
from typing import Optional, Union

import numpy as np

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


if is_torch_available():
  import torch

if is_tf_available():
  import tensorflow as tf







class 						lowerCAmelCase__   (      lowerCAmelCase__					):



 """simple docstring"""



 lowerCAmelCase__  					=      ['''image_processor''']
 lowerCAmelCase__  					=      '''SamImageProcessor'''
 def __init__(		self					:       int					,							__SCREAMING_SNAKE_CASE					:       Optional[int]			)  -> Any:


      """simple docstring"""




      super().__init__(__lowerCAmelCase			)
      __SCREAMING_SNAKE_CASE     =      self.image_processor
      __SCREAMING_SNAKE_CASE     =      -10
      __SCREAMING_SNAKE_CASE     =      self.image_processor.size["""longest_edge"""]
 def __call__(		self					:       int					,							__SCREAMING_SNAKE_CASE					:       Tuple=None					,							__SCREAMING_SNAKE_CASE					:       List[Any]=None					,							__SCREAMING_SNAKE_CASE					:       Optional[Any]=None					,							__SCREAMING_SNAKE_CASE					:       Tuple=None					,							__SCREAMING_SNAKE_CASE					:       List[str] = None					,							**__SCREAMING_SNAKE_CASE					:       Dict					,							)  -> str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.image_processor(
          __lowerCAmelCase					,							return_tensors=__lowerCAmelCase					,							**__lowerCAmelCase					,							)

      # pop arguments that are not used in the foward but used nevertheless
      __SCREAMING_SNAKE_CASE     =      encoding_image_processor["""original_sizes"""]

      if hasattr(__lowerCAmelCase					,							"""numpy"""			):  # Checks if Torch or TF tensor
           __SCREAMING_SNAKE_CASE     =      original_sizes.numpy()

      __SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE     =      self._check_and_preprocess_points(
          input_points=__lowerCAmelCase					,							input_labels=__lowerCAmelCase					,							input_boxes=__lowerCAmelCase					,							)

      __SCREAMING_SNAKE_CASE     =      self._normalize_and_convert(
          __lowerCAmelCase					,							__lowerCAmelCase					,							input_points=__lowerCAmelCase					,							input_labels=__lowerCAmelCase					,							input_boxes=__lowerCAmelCase					,							return_tensors=__lowerCAmelCase					,							)

      return encoding_image_processor
 def       UpperCAmelCase__		(		self					:       int					,							__SCREAMING_SNAKE_CASE					:       int					,							__SCREAMING_SNAKE_CASE					:       Any					,							__SCREAMING_SNAKE_CASE					:       Optional[int]=None					,							__SCREAMING_SNAKE_CASE					:       Tuple=None					,							__SCREAMING_SNAKE_CASE					:       List[str]=None					,							__SCREAMING_SNAKE_CASE					:       Optional[Any]="pt"					,							)  -> Any:


      """simple docstring"""




      if input_points is not None:
           if len(__lowerCAmelCase			) != len(__lowerCAmelCase			):
                __SCREAMING_SNAKE_CASE     =      [
                    self._normalize_coordinates(self.target_size					,							__lowerCAmelCase					,							original_sizes[0]			) for point in input_points
                ]
           else:
                __SCREAMING_SNAKE_CASE     =      [
                    self._normalize_coordinates(self.target_size					,							__lowerCAmelCase					,							__lowerCAmelCase			)
                    for point, original_size in zip(__lowerCAmelCase					,							__lowerCAmelCase			)
                ]
           # check that all arrays have the same shape
           if not all(point.shape == input_points[0].shape for point in input_points			):
                if input_labels is not None:
                     __SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE     =      self._pad_points_and_labels(__lowerCAmelCase					,							__lowerCAmelCase			)

           __SCREAMING_SNAKE_CASE     =      np.array(__lowerCAmelCase			)

      if input_labels is not None:
           __SCREAMING_SNAKE_CASE     =      np.array(__lowerCAmelCase			)

      if input_boxes is not None:
           if len(__lowerCAmelCase			) != len(__lowerCAmelCase			):
                __SCREAMING_SNAKE_CASE     =      [
                    self._normalize_coordinates(self.target_size					,							__lowerCAmelCase					,							original_sizes[0]					,							is_bounding_box=__lowerCAmelCase			)
                    for box in input_boxes
                ]
           else:
                __SCREAMING_SNAKE_CASE     =      [
                    self._normalize_coordinates(self.target_size					,							__lowerCAmelCase					,							__lowerCAmelCase					,							is_bounding_box=__lowerCAmelCase			)
                    for box, original_size in zip(__lowerCAmelCase					,							__lowerCAmelCase			)
                ]
           __SCREAMING_SNAKE_CASE     =      np.array(__lowerCAmelCase			)

      if input_boxes is not None:
           if return_tensors == "pt":
                __SCREAMING_SNAKE_CASE     =      torch.from_numpy(__lowerCAmelCase			)
                # boxes batch size of 1 by default
                __SCREAMING_SNAKE_CASE     =      input_boxes.unsqueeze(1			) if len(input_boxes.shape			) != 3 else input_boxes
           elif return_tensors == "tf":
                __SCREAMING_SNAKE_CASE     =      tf.convert_to_tensor(__lowerCAmelCase			)
                # boxes batch size of 1 by default
                __SCREAMING_SNAKE_CASE     =      tf.expand_dims(__lowerCAmelCase					,							1			) if len(input_boxes.shape			) != 3 else input_boxes
           encoding_image_processor.update({"""input_boxes""": input_boxes}			)
      if input_points is not None:
           if return_tensors == "pt":
                __SCREAMING_SNAKE_CASE     =      torch.from_numpy(__lowerCAmelCase			)
                # point batch size of 1 by default
                __SCREAMING_SNAKE_CASE     =      input_points.unsqueeze(1			) if len(input_points.shape			) != 4 else input_points
           elif return_tensors == "tf":
                __SCREAMING_SNAKE_CASE     =      tf.convert_to_tensor(__lowerCAmelCase			)
                # point batch size of 1 by default
                __SCREAMING_SNAKE_CASE     =      tf.expand_dims(__lowerCAmelCase					,							1			) if len(input_points.shape			) != 4 else input_points
           encoding_image_processor.update({"""input_points""": input_points}			)
      if input_labels is not None:
           if return_tensors == "pt":
                __SCREAMING_SNAKE_CASE     =      torch.from_numpy(__lowerCAmelCase			)
                # point batch size of 1 by default
                __SCREAMING_SNAKE_CASE     =      input_labels.unsqueeze(1			) if len(input_labels.shape			) != 3 else input_labels
           elif return_tensors == "tf":
                __SCREAMING_SNAKE_CASE     =      tf.convert_to_tensor(__lowerCAmelCase			)
                # point batch size of 1 by default
                __SCREAMING_SNAKE_CASE     =      tf.expand_dims(__lowerCAmelCase					,							1			) if len(input_labels.shape			) != 3 else input_labels
           encoding_image_processor.update({"""input_labels""": input_labels}			)

      return encoding_image_processor
 def       UpperCAmelCase__		(		self					:       List[Any]					,							__SCREAMING_SNAKE_CASE					:       Any					,							__SCREAMING_SNAKE_CASE					:       str			)  -> Tuple:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      max([point.shape[0] for point in input_points]			)
      __SCREAMING_SNAKE_CASE     =      []
      for i, point in enumerate(__lowerCAmelCase			):
           if point.shape[0] != expected_nb_points:
                __SCREAMING_SNAKE_CASE     =      np.concatenate(
                    [point, np.zeros((expected_nb_points - point.shape[0], 2)			) + self.point_pad_value]					,							axis=0			)
                __SCREAMING_SNAKE_CASE     =      np.append(input_labels[i]					,							[self.point_pad_value]			)
           processed_input_points.append(__lowerCAmelCase			)
      __SCREAMING_SNAKE_CASE     =      processed_input_points
      return input_points, input_labels
 def       UpperCAmelCase__		(		self					:       Union[str, Any]					,							__SCREAMING_SNAKE_CASE					:       str					,							__SCREAMING_SNAKE_CASE					:       Tuple					,							__SCREAMING_SNAKE_CASE					:       int					,							__SCREAMING_SNAKE_CASE					:       Optional[Any]=False			)  -> str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE     =      original_size
      __SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE     =      self.image_processor._get_preprocess_shape(__lowerCAmelCase					,							longest_edge=__lowerCAmelCase			)
      __SCREAMING_SNAKE_CASE     =      deepcopy(__lowerCAmelCase			).astype(__lowerCAmelCase			)

      if is_bounding_box:
           __SCREAMING_SNAKE_CASE     =      coords.reshape(-1					,							2					,							2			)

      __SCREAMING_SNAKE_CASE     =      coords[..., 0] * (new_w / old_w)
      __SCREAMING_SNAKE_CASE     =      coords[..., 1] * (new_h / old_h)

      if is_bounding_box:
           __SCREAMING_SNAKE_CASE     =      coords.reshape(-1					,							4			)

      return coords
 def       UpperCAmelCase__		(		self					:       List[Any]					,							__SCREAMING_SNAKE_CASE					:       List[str]=None					,							__SCREAMING_SNAKE_CASE					:       str=None					,							__SCREAMING_SNAKE_CASE					:       Any=None					,							)  -> Dict:


      """simple docstring"""




      if input_points is not None:
           if hasattr(__lowerCAmelCase					,							"""numpy"""			):  # Checks for TF or Torch tensor
                __SCREAMING_SNAKE_CASE     =      input_points.numpy().tolist()

           if not isinstance(__lowerCAmelCase					,							__lowerCAmelCase			) or not isinstance(input_points[0]					,							__lowerCAmelCase			):
                raise ValueError("""Input points must be a list of list of floating points."""			)
           __SCREAMING_SNAKE_CASE     =      [np.array(__lowerCAmelCase			) for input_point in input_points]
      else:
           __SCREAMING_SNAKE_CASE     =      None

      if input_labels is not None:
           if hasattr(__lowerCAmelCase					,							"""numpy"""			):
                __SCREAMING_SNAKE_CASE     =      input_labels.numpy().tolist()

           if not isinstance(__lowerCAmelCase					,							__lowerCAmelCase			) or not isinstance(input_labels[0]					,							__lowerCAmelCase			):
                raise ValueError("""Input labels must be a list of list integers."""			)
           __SCREAMING_SNAKE_CASE     =      [np.array(__lowerCAmelCase			) for label in input_labels]
      else:
           __SCREAMING_SNAKE_CASE     =      None

      if input_boxes is not None:
           if hasattr(__lowerCAmelCase					,							"""numpy"""			):
                __SCREAMING_SNAKE_CASE     =      input_boxes.numpy().tolist()

           if (
               not isinstance(__lowerCAmelCase					,							__lowerCAmelCase			)
               or not isinstance(input_boxes[0]					,							__lowerCAmelCase			)
               or not isinstance(input_boxes[0][0]					,							__lowerCAmelCase			)
           ):
                raise ValueError("""Input boxes must be a list of list of list of floating points."""			)
           __SCREAMING_SNAKE_CASE     =      [np.array(__lowerCAmelCase			).astype(np.floataa			) for box in input_boxes]
      else:
           __SCREAMING_SNAKE_CASE     =      None

      return input_points, input_labels, input_boxes
 @property
 def       UpperCAmelCase__		(		self					:       int			)  -> Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.image_processor.model_input_names
      return list(dict.fromkeys(__lowerCAmelCase			)			)


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


      """simple docstring"""




      return self.image_processor.post_process_masks(*__lowerCAmelCase					,							**__lowerCAmelCase			)







'''simple docstring'''




from typing import List, Optional

import numpy as np

from ...processing_utils import ProcessorMixin
from ...utils import to_numpy






class 				a__(					lowerCAmelCase__	):


			'''simple docstring'''






			UpperCAmelCase_			:     str      =       '''EncodecFeatureExtractor'''
			UpperCAmelCase_			:     Dict      =       ('''T5Tokenizer''', '''T5TokenizerFast''')



			def __init__(   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase):




					"""simple docstring"""


					super().__init__(__lowerCAmelCase       ,    __lowerCAmelCase)
					lowerCAmelCase   					=  self.feature_extractor
					lowerCAmelCase   					=  False



			def 							a_       (   self       ,    __lowerCAmelCase=None       ,    __lowerCAmelCase=None       ,    __lowerCAmelCase=True):




					"""simple docstring"""


					return self.tokenizer.get_decoder_prompt_ids(task=__lowerCAmelCase       ,    language=__lowerCAmelCase       ,    no_timestamps=__lowerCAmelCase)



			def __call__(   self       ,    *__lowerCAmelCase       ,    **__lowerCAmelCase):




					"""simple docstring"""


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

					lowerCAmelCase   					=  kwargs.pop("""audio"""       ,    __lowerCAmelCase)
					lowerCAmelCase   					=  kwargs.pop("""sampling_rate"""       ,    __lowerCAmelCase)
					lowerCAmelCase   					=  kwargs.pop("""text"""       ,    __lowerCAmelCase)
					if len(__lowerCAmelCase) > 0:
							lowerCAmelCase   					=  args[0]
							lowerCAmelCase   					=  args[1:]

					if audio is None and text is None:
							raise ValueError("""You need to specify either an `audio` or `text` input to process.""")

					if text is not None:
							lowerCAmelCase   					=  self.tokenizer(__lowerCAmelCase       ,    **__lowerCAmelCase)

					if audio is not None:
							lowerCAmelCase   					=  self.feature_extractor(__lowerCAmelCase       ,    *__lowerCAmelCase       ,    sampling_rate=__lowerCAmelCase       ,    **__lowerCAmelCase)

					if audio is None:
							return inputs

					elif text is None:
							return audio_inputs

					else:
							lowerCAmelCase   					=  audio_inputs["""input_values"""]
							if "padding_mask" in audio_inputs:
									lowerCAmelCase   					=  audio_inputs["""padding_mask"""]
							return inputs



			def 							a_       (   self       ,    *__lowerCAmelCase       ,    **__lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase   					=  kwargs.pop("""audio"""       ,    __lowerCAmelCase)
					lowerCAmelCase   					=  kwargs.pop("""padding_mask"""       ,    __lowerCAmelCase)

					if len(__lowerCAmelCase) > 0:
							lowerCAmelCase   					=  args[0]
							lowerCAmelCase   					=  args[1:]

					if audio_values is not None:
							return self._decode_audio(__lowerCAmelCase       ,    padding_mask=__lowerCAmelCase)
					else:
							return self.tokenizer.batch_decode(*__lowerCAmelCase       ,    **__lowerCAmelCase)



			def 							a_       (   self       ,    *__lowerCAmelCase       ,    **__lowerCAmelCase):




					"""simple docstring"""


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







			def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase = None):




					"""simple docstring"""


					lowerCAmelCase   					=  to_numpy(__lowerCAmelCase)
					lowerCAmelCase					,     lowerCAmelCase					,     lowerCAmelCase   					=  audio_values.shape

					if padding_mask is None:
							return list(__lowerCAmelCase)

					lowerCAmelCase   					=  to_numpy(__lowerCAmelCase)

					# match the sequence length of the padding mask to the generated audio arrays by padding with the **non-padding**
					# token (so that the generated audio values are **not** treated as padded tokens)
					lowerCAmelCase   					=  seq_len - padding_mask.shape[-1]
					lowerCAmelCase   					=  1 - self.feature_extractor.padding_value
					lowerCAmelCase   					=  np.pad(__lowerCAmelCase       ,    ((0, 0), (0, difference))       ,    """constant"""       ,    constant_values=__lowerCAmelCase)

					lowerCAmelCase   					=  audio_values.tolist()
					for i in range(__lowerCAmelCase):
							lowerCAmelCase   					=  np.asarray(audio_values[i])[
							    padding_mask[i][None, :] != self.feature_extractor.padding_value
							]
							lowerCAmelCase   					=  sliced_audio.reshape(__lowerCAmelCase       ,    -1)

					return audio_values




