






'''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 				__SCREAMING_SNAKE_CASE							(				SCREAMING_SNAKE_CASE_  ):
      __a	       =["""image_processor"""]
      __a	       ="""SamImageProcessor"""




      def __init__(	self			, lowerCamelCase   )				->Optional[Any]:



         '''simple docstring'''
         super().__init__(lowerCamelCase   )
         __a					  =     self.image_processor
         __a					  =     -10
         __a					  =     self.image_processor.size['longest_edge']




      def __call__(	self			, lowerCamelCase=None			, lowerCamelCase=None			, lowerCamelCase=None			, lowerCamelCase=None			, lowerCamelCase = None			, **lowerCamelCase			, )				->List[Any]:



         '''simple docstring'''
         __a					  =     self.image_processor(
             lowerCamelCase			, return_tensors=lowerCamelCase			, **lowerCamelCase			, )

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

         if hasattr(lowerCamelCase			, 'numpy'   ):  # Checks if Torch or TF tensor
            __a					  =     original_sizes.numpy()

         __a					  =     self._check_and_preprocess_points(
             input_points=lowerCamelCase			, input_labels=lowerCamelCase			, input_boxes=lowerCamelCase			, )

         __a					  =     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			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase=None			, lowerCamelCase=None			, lowerCamelCase=None			, lowerCamelCase="pt"			, )				->str:



         '''simple docstring'''
         if input_points is not None:
            if len(lowerCamelCase   ) != len(lowerCamelCase   ):
               __a					  =     [
                   self._normalize_coordinates(self.target_size			, lowerCamelCase			, original_sizes[0]   ) for point in input_points
               ]
            else:
               __a					  =     [
                   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:
                  __a					  =     self._pad_points_and_labels(lowerCamelCase			, lowerCamelCase   )

            __a					  =     np.array(lowerCamelCase   )

         if input_labels is not None:
            __a					  =     np.array(lowerCamelCase   )

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

         if input_boxes is not None:
            if return_tensors == "pt":
               __a					  =     torch.from_numpy(lowerCamelCase   )
               # boxes batch size of 1 by default
               __a					  =     input_boxes.unsqueeze(1   ) if len(input_boxes.shape   ) != 3 else input_boxes
            elif return_tensors == "tf":
               __a					  =     tf.convert_to_tensor(lowerCamelCase   )
               # boxes batch size of 1 by default
               __a					  =     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":
               __a					  =     torch.from_numpy(lowerCamelCase   )
               # point batch size of 1 by default
               __a					  =     input_points.unsqueeze(1   ) if len(input_points.shape   ) != 4 else input_points
            elif return_tensors == "tf":
               __a					  =     tf.convert_to_tensor(lowerCamelCase   )
               # point batch size of 1 by default
               __a					  =     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":
               __a					  =     torch.from_numpy(lowerCamelCase   )
               # point batch size of 1 by default
               __a					  =     input_labels.unsqueeze(1   ) if len(input_labels.shape   ) != 3 else input_labels
            elif return_tensors == "tf":
               __a					  =     tf.convert_to_tensor(lowerCamelCase   )
               # point batch size of 1 by default
               __a					  =     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			, lowerCamelCase			, lowerCamelCase   )				->int:



         '''simple docstring'''
         __a					  =     max([point.shape[0] for point in input_points]   )
         __a					  =     []
         for i, point in enumerate(lowerCamelCase   ):
            if point.shape[0] != expected_nb_points:
               __a					  =     np.concatenate(
                   [point, np.zeros((expected_nb_points - point.shape[0], 2)   ) + self.point_pad_value]			, axis=0   )
               __a					  =     np.append(input_labels[i]			, [self.point_pad_value]   )
            processed_input_points.append(lowerCamelCase   )
         __a					  =     processed_input_points
         return input_points, input_labels




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase=False   )				->Any:



         '''simple docstring'''
         __a					  =     original_size
         __a					  =     self.image_processor._get_preprocess_shape(lowerCamelCase			, longest_edge=lowerCamelCase   )
         __a					  =     deepcopy(lowerCamelCase   ).astype(lowerCamelCase   )

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

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

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

         return coords




      def 			__UpperCamelCase				(	self			, lowerCamelCase=None			, lowerCamelCase=None			, lowerCamelCase=None			, )				->List[Any]:



         '''simple docstring'''
         if input_points is not None:
            if hasattr(lowerCamelCase			, 'numpy'   ):  # Checks for TF or Torch tensor
               __a					  =     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.'   )
            __a					  =     [np.array(lowerCamelCase   ) for input_point in input_points]
         else:
            __a					  =     None

         if input_labels is not None:
            if hasattr(lowerCamelCase			, 'numpy'   ):
               __a					  =     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.'   )
            __a					  =     [np.array(lowerCamelCase   ) for label in input_labels]
         else:
            __a					  =     None

         if input_boxes is not None:
            if hasattr(lowerCamelCase			, 'numpy'   ):
               __a					  =     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.'   )
            __a					  =     [np.array(lowerCamelCase   ).astype(np.floataa   ) for box in input_boxes]
         else:
            __a					  =     None

         return input_points, input_labels, input_boxes




      @property
      def 			__UpperCamelCase				(	self   )				->List[str]:



         '''simple docstring'''
         __a					  =     self.image_processor.model_input_names
         return list(dict.fromkeys(lowerCamelCase   )   )






      def 			__UpperCamelCase				(	self			, *lowerCamelCase			, **lowerCamelCase   )				->str:



         '''simple docstring'''
         return self.image_processor.post_process_masks(*lowerCamelCase			, **lowerCamelCase   )




"""simple docstring"""







import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


a_							=    logging.get_logger(__name__)


a_							=    """▁"""

a_							=    {"""vocab_file""": """spiece.model"""}

a_							=    {
    """vocab_file""": {
        """google/reformer-crime-and-punishment""": (
            """https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model"""
        )
    }
}

a_							=    {
    """google/reformer-crime-and-punishment""": 52_42_88,
}







class 					A_(SCREAMING_SNAKE_CASE_      ):







			"""simple docstring"""



			a_       :			Optional[int]												=			VOCAB_FILES_NAMES
			a_       :			int												=			PRETRAINED_VOCAB_FILES_MAP
			a_       :			str												=			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			a_       :			Optional[Any]												=			["""input_ids""", """attention_mask"""]
			def __init__(	self				,   A				,   A="</s>"				,   A="<unk>"				,   A=[]				,   A = None				,   **A				,   ):
						_lowerCamelCase					:					Any	       =    {} if sp_model_kwargs is None else sp_model_kwargs

						super().__init__(
						    eos_token=A				,   unk_token=A				,   additional_special_tokens=A				,   sp_model_kwargs=self.sp_model_kwargs				,   **A				,   )

						_lowerCamelCase					:					Dict	       =    vocab_file
						_lowerCamelCase					:					List[Any]	       =    spm.SentencePieceProcessor(**self.sp_model_kwargs							)
						self.sp_model.Load(A							)
			@property
			def    _lowerCAmelCase		(	self							):
						return self.sp_model.get_piece_size()
			def    _lowerCAmelCase		(	self							):
						_lowerCamelCase					:					int	       =    {self.convert_ids_to_tokens(A							): i for i in range(self.vocab_size							)}
						vocab.update(self.added_tokens_encoder							)
						return vocab
			def __getstate__(	self							):
						_lowerCamelCase					:					Optional[int]	       =    self.__dict__.copy()
						_lowerCamelCase					:					Union[str, Any]	       =    None
						return state
			def __setstate__(	self				,   A							):
						_lowerCamelCase					:					int	       =    d

						# for backward compatibility
						if not hasattr(self				,   'sp_model_kwargs'							):
									_lowerCamelCase					:					Union[str, Any]	       =    {}

						_lowerCamelCase					:					Dict	       =    spm.SentencePieceProcessor(**self.sp_model_kwargs							)
						self.sp_model.Load(self.vocab_file							)
			def    _lowerCAmelCase		(	self				,   A							):
						return self.sp_model.encode(A				,   out_type=A							)
			def    _lowerCAmelCase		(	self				,   A							):
						return self.sp_model.piece_to_id(A							)
			def    _lowerCAmelCase		(	self				,   A							):
						if index < self.sp_model.get_piece_size():
									_lowerCamelCase					:					Dict	       =    self.sp_model.IdToPiece(A							)
						return token
			def    _lowerCAmelCase		(	self				,   A							):
						_lowerCamelCase					:					Tuple	       =    []
						_lowerCamelCase					:					Optional[int]	       =    ''
						for token in tokens:
									# make sure that special tokens are not decoded using sentencepiece model
									if token in self.all_special_tokens:
												out_string += self.sp_model.decode(A							) + token
												_lowerCamelCase					:					Dict	       =    []
									else:
												current_sub_tokens.append(A							)
						out_string += self.sp_model.decode(A							)
						return out_string.strip()

			def    _lowerCAmelCase		(	self				,   A				,   A = None							):
						if not os.path.isdir(A							):
									logger.error(F"Vocabulary path ({save_directory}) should be a directory"							)
									return
						_lowerCamelCase					:					Any	       =    os.path.join(
						    A				,   (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']							)

						if os.path.abspath(self.vocab_file							) != os.path.abspath(A							) and os.path.isfile(self.vocab_file							):
									copyfile(self.vocab_file				,   A							)
						elif not os.path.isfile(self.vocab_file							):
									with open(A				,   'wb'							) as fi:
												_lowerCamelCase					:					List[str]	       =    self.sp_model.serialized_model_proto()
												fi.write(A							)

						return (out_vocab_file,)





