





'''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   _lowercase						(							a				):
   _UpperCamelCase       		=   ["""image_processor"""]
   _UpperCamelCase       		=   """SamImageProcessor"""


   def __init__( self    ,   _UpperCAmelCase					):
       super().__init__(_UpperCAmelCase					)
       A       :       List[str]							=						self.image_processor
       A       :       Optional[int]							=						-10
       A       :       Optional[Any]							=						self.image_processor.size['''longest_edge''']


   def __call__( self    ,   _UpperCAmelCase=None    ,   _UpperCAmelCase=None    ,   _UpperCAmelCase=None    ,   _UpperCAmelCase=None    ,   _UpperCAmelCase = None    ,   **_UpperCAmelCase    ,   ):
       A       :       Any							=						self.image_processor(
           _UpperCAmelCase    ,   return_tensors=_UpperCAmelCase    ,   **_UpperCAmelCase    ,   )

       # pop arguments that are not used in the foward but used nevertheless
       A       :       Optional[int]							=						encoding_image_processor['''original_sizes''']

       if hasattr(_UpperCAmelCase    ,   '''numpy'''					):  # Checks if Torch or TF tensor
           A       :       Any							=						original_sizes.numpy()

       A       :       Dict							=						self._check_and_preprocess_points(
           input_points=_UpperCAmelCase    ,   input_labels=_UpperCAmelCase    ,   input_boxes=_UpperCAmelCase    ,   )

       A       :       Tuple							=						self._normalize_and_convert(
           _UpperCAmelCase    ,   _UpperCAmelCase    ,   input_points=_UpperCAmelCase    ,   input_labels=_UpperCAmelCase    ,   input_boxes=_UpperCAmelCase    ,   return_tensors=_UpperCAmelCase    ,   )

       return encoding_image_processor


   def 					snake_case				( self    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase=None    ,   _UpperCAmelCase=None    ,   _UpperCAmelCase=None    ,   _UpperCAmelCase="pt"    ,   ):
       if input_points is not None:
           if len(_UpperCAmelCase					) != len(_UpperCAmelCase					):
               A       :       List[Any]							=						[
                   self._normalize_coordinates(self.target_size    ,   _UpperCAmelCase    ,   original_sizes[0]					) for point in input_points
               ]
           else:
               A       :       Any							=						[
                   self._normalize_coordinates(self.target_size    ,   _UpperCAmelCase    ,   _UpperCAmelCase					)
                   for point, original_size in zip(_UpperCAmelCase    ,   _UpperCAmelCase					)
               ]
           # 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:
                   A       :       Union[str, Any]							=						self._pad_points_and_labels(_UpperCAmelCase    ,   _UpperCAmelCase					)

           A       :       Optional[int]							=						np.array(_UpperCAmelCase					)

       if input_labels is not None:
           A       :       int							=						np.array(_UpperCAmelCase					)

       if input_boxes is not None:
           if len(_UpperCAmelCase					) != len(_UpperCAmelCase					):
               A       :       Union[str, Any]							=						[
                   self._normalize_coordinates(self.target_size    ,   _UpperCAmelCase    ,   original_sizes[0]    ,   is_bounding_box=_UpperCAmelCase					)
                   for box in input_boxes
               ]
           else:
               A       :       int							=						[
                   self._normalize_coordinates(self.target_size    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   is_bounding_box=_UpperCAmelCase					)
                   for box, original_size in zip(_UpperCAmelCase    ,   _UpperCAmelCase					)
               ]
           A       :       Tuple							=						np.array(_UpperCAmelCase					)

       if input_boxes is not None:
           if return_tensors == "pt":
               A       :       Tuple							=						torch.from_numpy(_UpperCAmelCase					)
               # boxes batch size of 1 by default
               A       :       List[Any]							=						input_boxes.unsqueeze(1					) if len(input_boxes.shape					) != 3 else input_boxes
           elif return_tensors == "tf":
               A       :       Tuple							=						tf.convert_to_tensor(_UpperCAmelCase					)
               # boxes batch size of 1 by default
               A       :       str							=						tf.expand_dims(_UpperCAmelCase    ,   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":
               A       :       List[Any]							=						torch.from_numpy(_UpperCAmelCase					)
               # point batch size of 1 by default
               A       :       Any							=						input_points.unsqueeze(1					) if len(input_points.shape					) != 4 else input_points
           elif return_tensors == "tf":
               A       :       Any							=						tf.convert_to_tensor(_UpperCAmelCase					)
               # point batch size of 1 by default
               A       :       Optional[Any]							=						tf.expand_dims(_UpperCAmelCase    ,   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":
               A       :       str							=						torch.from_numpy(_UpperCAmelCase					)
               # point batch size of 1 by default
               A       :       int							=						input_labels.unsqueeze(1					) if len(input_labels.shape					) != 3 else input_labels
           elif return_tensors == "tf":
               A       :       Tuple							=						tf.convert_to_tensor(_UpperCAmelCase					)
               # point batch size of 1 by default
               A       :       Union[str, Any]							=						tf.expand_dims(_UpperCAmelCase    ,   1					) if len(input_labels.shape					) != 3 else input_labels
           encoding_image_processor.update({'''input_labels''': input_labels}					)

       return encoding_image_processor


   def 					snake_case				( self    ,   _UpperCAmelCase    ,   _UpperCAmelCase					):
       A       :       Any							=						max([point.shape[0] for point in input_points]					)
       A       :       List[str]							=						[]
       for i, point in enumerate(_UpperCAmelCase					):
           if point.shape[0] != expected_nb_points:
               A       :       Optional[int]							=						np.concatenate(
                   [point, np.zeros((expected_nb_points - point.shape[0], 2)					) + self.point_pad_value]    ,   axis=0					)
               A       :       Optional[int]							=						np.append(input_labels[i]    ,   [self.point_pad_value]					)
           processed_input_points.append(_UpperCAmelCase					)
       A       :       Optional[Any]							=						processed_input_points
       return input_points, input_labels


   def 					snake_case				( self    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase    ,   _UpperCAmelCase=False					):
       A       :       Union[str, Any]							=						original_size
       A       :       Union[str, Any]							=						self.image_processor._get_preprocess_shape(_UpperCAmelCase    ,   longest_edge=_UpperCAmelCase					)
       A       :       Union[str, Any]							=						deepcopy(_UpperCAmelCase					).astype(_UpperCAmelCase					)

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

       A       :       Dict							=						coords[..., 0] * (new_w / old_w)
       A       :       Tuple							=						coords[..., 1] * (new_h / old_h)

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

       return coords


   def 					snake_case				( self    ,   _UpperCAmelCase=None    ,   _UpperCAmelCase=None    ,   _UpperCAmelCase=None    ,   ):
       if input_points is not None:
           if hasattr(_UpperCAmelCase    ,   '''numpy'''					):  # Checks for TF or Torch tensor
               A       :       Any							=						input_points.numpy().tolist()

           if not isinstance(_UpperCAmelCase    ,   _UpperCAmelCase					) or not isinstance(input_points[0]    ,   _UpperCAmelCase					):
               raise ValueError('''Input points must be a list of list of floating points.'''					)
           A       :       List[str]							=						[np.array(_UpperCAmelCase					) for input_point in input_points]
       else:
           A       :       Union[str, Any]							=						None

       if input_labels is not None:
           if hasattr(_UpperCAmelCase    ,   '''numpy'''					):
               A       :       int							=						input_labels.numpy().tolist()

           if not isinstance(_UpperCAmelCase    ,   _UpperCAmelCase					) or not isinstance(input_labels[0]    ,   _UpperCAmelCase					):
               raise ValueError('''Input labels must be a list of list integers.'''					)
           A       :       Union[str, Any]							=						[np.array(_UpperCAmelCase					) for label in input_labels]
       else:
           A       :       Dict							=						None

       if input_boxes is not None:
           if hasattr(_UpperCAmelCase    ,   '''numpy'''					):
               A       :       Dict							=						input_boxes.numpy().tolist()

           if (
               not isinstance(_UpperCAmelCase    ,   _UpperCAmelCase					)
               or not isinstance(input_boxes[0]    ,   _UpperCAmelCase					)
               or not isinstance(input_boxes[0][0]    ,   _UpperCAmelCase					)
           ):
               raise ValueError('''Input boxes must be a list of list of list of floating points.'''					)
           A       :       Optional[int]							=						[np.array(_UpperCAmelCase					).astype(np.floataa					) for box in input_boxes]
       else:
           A       :       str							=						None

       return input_points, input_labels, input_boxes


   @property
   def 					snake_case				( self					):
       A       :       Optional[int]							=						self.image_processor.model_input_names
       return list(dict.fromkeys(_UpperCAmelCase					)					)


   def 					snake_case				( self    ,   *_UpperCAmelCase    ,   **_UpperCAmelCase					):
       return self.image_processor.post_process_masks(*_UpperCAmelCase    ,   **_UpperCAmelCase					)







'''simple docstring'''






import requests

snake_case_     =	"""YOUR API KEY"""
def 							_lowerCamelCase(  UpperCamelCase__    : str    ,       UpperCamelCase__    : str = giphy_api_key		)		->  list:
    A       :       Optional[Any]							=						'''+'''.join(query.split()		)
    A       :       List[Any]							=						F'''https://api.giphy.com/v1/gifs/search?q={formatted_query}&api_key={api_key}'''
    A       :       int							=						requests.get(UpperCamelCase__		).json()['''data''']
    return [gif["url"] for gif in gifs]


if __name__ == "__main__":
     print("""\n""".join(get_gifs("""space ship""")))
