






"""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  (				UpperCamelCase_						):





	"""simple docstring"""




	_A       :				List[Any]    =				["""image_processor"""]
	_A       :				int    =				"""SamImageProcessor"""





	def __init__(self     ,     lowerCAmelCase_							):
		super().__init__(lowerCAmelCase_							)
		A_		:						Any	    =   self.image_processor
		A_		:						Optional[int]	    =   -10
		A_		:						Tuple	    =   self.image_processor.size['''longest_edge''']





	def __call__(self     ,     lowerCAmelCase_=None     ,     lowerCAmelCase_=None     ,     lowerCAmelCase_=None     ,     lowerCAmelCase_=None     ,     lowerCAmelCase_ = None     ,     **lowerCAmelCase_     ,     ):
		A_		:						Tuple	    =   self.image_processor(
		    lowerCAmelCase_     ,     return_tensors=lowerCAmelCase_     ,     **lowerCAmelCase_     ,     )

		# pop arguments that are not used in the foward but used nevertheless
		A_		:						List[str]	    =   encoding_image_processor['''original_sizes''']

		if hasattr(lowerCAmelCase_     ,     """numpy"""							):  # Checks if Torch or TF tensor
			A_		:						str	    =   original_sizes.numpy()

		A_		:						int	    =   self._check_and_preprocess_points(
		    input_points=lowerCAmelCase_     ,     input_labels=lowerCAmelCase_     ,     input_boxes=lowerCAmelCase_     ,     )

		A_		:						str	    =   self._normalize_and_convert(
		    lowerCAmelCase_     ,     lowerCAmelCase_     ,     input_points=lowerCAmelCase_     ,     input_labels=lowerCAmelCase_     ,     input_boxes=lowerCAmelCase_     ,     return_tensors=lowerCAmelCase_     ,     )

		return encoding_image_processor





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

			A_		:						str	    =   np.array(lowerCAmelCase_							)

		if input_labels is not None:
			A_		:						List[Any]	    =   np.array(lowerCAmelCase_							)

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

		if input_boxes is not None:
			if return_tensors == "pt":
				A_		:						Optional[int]	    =   torch.from_numpy(lowerCAmelCase_							)
				# 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_		:						int	    =   tf.convert_to_tensor(lowerCAmelCase_							)
				# boxes batch size of 1 by default
				A_		:						Union[str, Any]	    =   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_		:						int	    =   torch.from_numpy(lowerCAmelCase_							)
				# point batch size of 1 by default
				A_		:						Tuple	    =   input_points.unsqueeze(1							) if len(input_points.shape							) != 4 else input_points
			elif return_tensors == "tf":
				A_		:						Any	    =   tf.convert_to_tensor(lowerCAmelCase_							)
				# point batch size of 1 by default
				A_		:						Optional[Any]	    =   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_		:						str	    =   torch.from_numpy(lowerCAmelCase_							)
				# point batch size of 1 by default
				A_		:						Tuple	    =   input_labels.unsqueeze(1							) if len(input_labels.shape							) != 3 else input_labels
			elif return_tensors == "tf":
				A_		:						Optional[Any]	    =   tf.convert_to_tensor(lowerCAmelCase_							)
				# point batch size of 1 by default
				A_		:						List[Any]	    =   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    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_							):
		A_		:						Optional[int]	    =   max([point.shape[0] for point in input_points]							)
		A_		:						List[str]	    =   []
		for i, point in enumerate(lowerCAmelCase_							):
			if point.shape[0] != expected_nb_points:
				A_		:						str	    =   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(lowerCAmelCase_							)
		A_		:						int	    =   processed_input_points
		return input_points, input_labels





	def    lowerCamelCase(self     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_     ,     lowerCAmelCase_=False							):
		A_		:						str	    =   original_size
		A_		:						Dict	    =   self.image_processor._get_preprocess_shape(lowerCAmelCase_     ,     longest_edge=lowerCAmelCase_							)
		A_		:						Optional[Any]	    =   deepcopy(lowerCAmelCase_							).astype(lowerCAmelCase_							)

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

		A_		:						Tuple	    =   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    lowerCamelCase(self     ,     lowerCAmelCase_=None     ,     lowerCAmelCase_=None     ,     lowerCAmelCase_=None     ,     ):
		if input_points is not None:
			if hasattr(lowerCAmelCase_     ,     """numpy"""							):  # Checks for TF or Torch tensor
				A_		:						Union[str, Any]	    =   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_		:						Any	    =   [np.array(lowerCAmelCase_							) for input_point in input_points]
		else:
			A_		:						int	    =   None

		if input_labels is not None:
			if hasattr(lowerCAmelCase_     ,     """numpy"""							):
				A_		:						Optional[Any]	    =   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_		:						Optional[Any]	    =   [np.array(lowerCAmelCase_							) for label in input_labels]
		else:
			A_		:						Optional[int]	    =   None

		if input_boxes is not None:
			if hasattr(lowerCAmelCase_     ,     """numpy"""							):
				A_		:						List[str]	    =   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_		:						str	    =   [np.array(lowerCAmelCase_							).astype(np.floataa							) for box in input_boxes]
		else:
			A_		:						List[str]	    =   None

		return input_points, input_labels, input_boxes





	@property
	def    lowerCamelCase(self							):
		A_		:						Union[str, Any]	    =   self.image_processor.model_input_names
		return list(dict.fromkeys(lowerCAmelCase_							)							)





	def    lowerCamelCase(self     ,     *lowerCAmelCase_     ,     **lowerCAmelCase_							):
		return self.image_processor.post_process_masks(*lowerCAmelCase_     ,     **lowerCAmelCase_							)









"""simple docstring"""
import os
import unittest

from transformers import FunnelTokenizer, FunnelTokenizerFast
from transformers.models.funnel.tokenization_funnel import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin



@require_tokenizers
class      SCREAMING_SNAKE_CASE  (				_SCREAMING_SNAKE_CASE       ,       unittest.TestCase						):





	"""simple docstring"""




	_A       :				Dict    =				FunnelTokenizer
	_A       :				Union[str, Any]    =				FunnelTokenizerFast
	_A       :				List[str]    =				True
	_A       :				List[Any]    =				True





	def    lowerCamelCase(self							):
		super().setUp()

		A_		:						Tuple	    =   [
		    """<unk>""",
		    """<cls>""",
		    """<sep>""",
		    """want""",
		    """##want""",
		    """##ed""",
		    """wa""",
		    """un""",
		    """runn""",
		    """##ing""",
		    """,""",
		    """low""",
		    """lowest""",
		]
		A_		:						str	    =   os.path.join(self.tmpdirname     ,     VOCAB_FILES_NAMES["""vocab_file"""]							)
		with open(self.vocab_file     ,     """w"""     ,     encoding="""utf-8"""							) as vocab_writer:
			vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]							)							)





	def    lowerCamelCase(self     ,     **lowerCAmelCase_							):
		return FunnelTokenizer.from_pretrained(self.tmpdirname     ,     **lowerCAmelCase_							)





	def    lowerCamelCase(self     ,     **lowerCAmelCase_							):
		return FunnelTokenizerFast.from_pretrained(self.tmpdirname     ,     **lowerCAmelCase_							)





	def    lowerCamelCase(self     ,     lowerCAmelCase_							):
		A_		:						Union[str, Any]	    =   """UNwant\u00E9d,running"""
		A_		:						int	    =   """unwanted, running"""
		return input_text, output_text





	def    lowerCamelCase(self							):
		A_		:						Optional[Any]	    =   self.tokenizer_class(self.vocab_file							)

		A_		:						Any	    =   tokenizer.tokenize("""UNwant\u00E9d,running"""							)
		self.assertListEqual(lowerCAmelCase_     ,     ["""un""", """##want""", """##ed""", """,""", """runn""", """##ing"""]							)
		self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase_							)     ,     [7, 4, 5, 10, 8, 9]							)





	def    lowerCamelCase(self							):
		A_		:						List[Any]	    =   self.get_tokenizers(do_lower_case=lowerCAmelCase_							)
		for tokenizer in tokenizers:
			A_		:						Any	    =   tokenizer("""UNwant\u00E9d,running"""							)
			A_		:						Dict	    =   len(inputs["""input_ids"""]							) - 1
			self.assertListEqual(inputs["""token_type_ids"""]     ,     [2] + [0] * sentence_len							)

			A_		:						Tuple	    =   tokenizer("""UNwant\u00E9d,running"""     ,     """UNwant\u00E9d,running"""							)
			self.assertListEqual(inputs["""token_type_ids"""]     ,     [2] + [0] * sentence_len + [1] * sentence_len							)

