


import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
			from PIL import Image

			from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
			import torch

if is_tf_available():
			import tensorflow as tf

@require_vision
@require_torchvision
class 							a						(unittest.TestCase ):




							"""simple docstring"""


							def        __snake_case   (				self			:    Tuple							)      ->       Optional[Any]:
														__snake_case						:     Dict									=  tempfile.mkdtemp()
														__snake_case						:     Any									=  SamImageProcessor()
														__snake_case						:     Optional[int]									=  SamProcessor(lowerCamelCase							)
														processor.save_pretrained(self.tmpdirname							)

							def        __snake_case   (				self			:    Optional[Any]       ,      **lowerCamelCase			:    Optional[int]							)      ->       Optional[Any]:
														return AutoProcessor.from_pretrained(self.tmpdirname       ,      **lowerCamelCase							).image_processor

							def        __snake_case   (				self			:    Optional[Any]							)      ->       Dict:
														shutil.rmtree(self.tmpdirname							)

							def        __snake_case   (				self			:    int							)      ->       List[Any]:
														__snake_case						:     List[str]									=  [np.random.randint(255       ,      size=(3, 30, 400)       ,      dtype=np.uinta							)]

														__snake_case						:     int									=  [Image.fromarray(np.moveaxis(lowerCamelCase       ,      0       ,      -1							)							) for x in image_inputs]

														return image_inputs

							def        __snake_case   (				self			:    List[Any]							)      ->       Dict:
														__snake_case						:     int									=  SamProcessor(image_processor=self.get_image_processor()							)
														processor.save_pretrained(self.tmpdirname							)

														__snake_case						:     Dict									=  self.get_image_processor(do_normalize=lowerCamelCase       ,      padding_value=1.0							)

														__snake_case						:     Optional[Any]									=  SamProcessor.from_pretrained(self.tmpdirname       ,      do_normalize=lowerCamelCase       ,      padding_value=1.0							)

														self.assertEqual(processor.image_processor.to_json_string()       ,      image_processor_add_kwargs.to_json_string()							)
														self.assertIsInstance(processor.image_processor       ,      lowerCamelCase							)

							def        __snake_case   (				self			:    List[str]							)      ->       Tuple:
														__snake_case						:     int									=  self.get_image_processor()

														__snake_case						:     str									=  SamProcessor(image_processor=lowerCamelCase							)

														__snake_case						:     Optional[int]									=  self.prepare_image_inputs()

														__snake_case						:     List[str]									=  image_processor(lowerCamelCase       ,      return_tensors="np"							)
														__snake_case						:     Dict									=  processor(images=lowerCamelCase       ,      return_tensors="np"							)

														input_feat_extract.pop("original_sizes"							)  # pop original_sizes as it is popped in the processor
														input_feat_extract.pop("reshaped_input_sizes"							)  # pop original_sizes as it is popped in the processor

														for key in input_feat_extract.keys():
																					self.assertAlmostEqual(input_feat_extract[key].sum()       ,      input_processor[key].sum()       ,      delta=1E-2							)






							@require_torch
							def        __snake_case   (				self			:    Optional[Any]							)      ->       Dict:
														__snake_case						:     Tuple									=  self.get_image_processor()

														__snake_case						:     List[Any]									=  SamProcessor(image_processor=lowerCamelCase							)
														__snake_case						:     List[str]									=  [torch.ones((1, 3, 5, 5)							)]

														__snake_case						:     Tuple									=  [[1764, 2646]]

														__snake_case						:     Optional[int]									=  [[683, 1024]]
														__snake_case						:     int									=  processor.post_process_masks(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase							)
														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														__snake_case						:     Optional[Any]									=  processor.post_process_masks(
														    lowerCamelCase       ,      torch.tensor(lowerCamelCase							)       ,      torch.tensor(lowerCamelCase							)							)
														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														# should also work with np
														__snake_case						:     List[str]									=  [np.ones((1, 3, 5, 5)							)]
														__snake_case						:     Optional[int]									=  processor.post_process_masks(lowerCamelCase       ,      np.array(lowerCamelCase							)       ,      np.array(lowerCamelCase							)							)

														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														__snake_case						:     str									=  [[1, 0], [0, 1]]
														with self.assertRaises(lowerCamelCase							):
																					__snake_case						:     Optional[int]									=  processor.post_process_masks(lowerCamelCase       ,      np.array(lowerCamelCase							)       ,      np.array(lowerCamelCase							)							)

@require_vision
@require_tf
class 							a						(unittest.TestCase ):




							"""simple docstring"""


							def        __snake_case   (				self			:    List[Any]							)      ->       Union[str, Any]:
														__snake_case						:     int									=  tempfile.mkdtemp()
														__snake_case						:     str									=  SamImageProcessor()
														__snake_case						:     List[Any]									=  SamProcessor(lowerCamelCase							)
														processor.save_pretrained(self.tmpdirname							)

							def        __snake_case   (				self			:    str       ,      **lowerCamelCase			:    Any							)      ->       Tuple:
														return AutoProcessor.from_pretrained(self.tmpdirname       ,      **lowerCamelCase							).image_processor

							def        __snake_case   (				self			:    Optional[int]							)      ->       Any:
														shutil.rmtree(self.tmpdirname							)

							def        __snake_case   (				self			:    str							)      ->       List[Any]:
														__snake_case						:     int									=  [np.random.randint(255       ,      size=(3, 30, 400)       ,      dtype=np.uinta							)]

														__snake_case						:     Dict									=  [Image.fromarray(np.moveaxis(lowerCamelCase       ,      0       ,      -1							)							) for x in image_inputs]

														return image_inputs

							def        __snake_case   (				self			:    int							)      ->       List[str]:
														__snake_case						:     List[Any]									=  SamProcessor(image_processor=self.get_image_processor()							)
														processor.save_pretrained(self.tmpdirname							)

														__snake_case						:     Optional[Any]									=  self.get_image_processor(do_normalize=lowerCamelCase       ,      padding_value=1.0							)

														__snake_case						:     Tuple									=  SamProcessor.from_pretrained(self.tmpdirname       ,      do_normalize=lowerCamelCase       ,      padding_value=1.0							)

														self.assertEqual(processor.image_processor.to_json_string()       ,      image_processor_add_kwargs.to_json_string()							)
														self.assertIsInstance(processor.image_processor       ,      lowerCamelCase							)

							def        __snake_case   (				self			:    Union[str, Any]							)      ->       List[Any]:
														__snake_case						:     str									=  self.get_image_processor()

														__snake_case						:     Union[str, Any]									=  SamProcessor(image_processor=lowerCamelCase							)

														__snake_case						:     Dict									=  self.prepare_image_inputs()

														__snake_case						:     int									=  image_processor(lowerCamelCase       ,      return_tensors="np"							)
														__snake_case						:     List[str]									=  processor(images=lowerCamelCase       ,      return_tensors="np"							)

														input_feat_extract.pop("original_sizes"							)  # pop original_sizes as it is popped in the processor
														input_feat_extract.pop("reshaped_input_sizes"							)  # pop reshaped_input_sizes as it is popped in the processor

														for key in input_feat_extract.keys():
																					self.assertAlmostEqual(input_feat_extract[key].sum()       ,      input_processor[key].sum()       ,      delta=1E-2							)






							@require_tf
							def        __snake_case   (				self			:    Any							)      ->       Optional[int]:
														__snake_case						:     List[str]									=  self.get_image_processor()

														__snake_case						:     Dict									=  SamProcessor(image_processor=lowerCamelCase							)
														__snake_case						:     Union[str, Any]									=  [tf.ones((1, 3, 5, 5)							)]

														__snake_case						:     List[Any]									=  [[1764, 2646]]

														__snake_case						:     Dict									=  [[683, 1024]]
														__snake_case						:     List[str]									=  processor.post_process_masks(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      return_tensors="tf"							)
														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														__snake_case						:     Optional[Any]									=  processor.post_process_masks(
														    lowerCamelCase       ,      tf.convert_to_tensor(lowerCamelCase							)       ,      tf.convert_to_tensor(lowerCamelCase							)       ,      return_tensors="tf"       ,      )
														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														# should also work with np
														__snake_case						:     Union[str, Any]									=  [np.ones((1, 3, 5, 5)							)]
														__snake_case						:     List[str]									=  processor.post_process_masks(
														    lowerCamelCase       ,      np.array(lowerCamelCase							)       ,      np.array(lowerCamelCase							)       ,      return_tensors="tf"							)

														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														__snake_case						:     Tuple									=  [[1, 0], [0, 1]]
														with self.assertRaises(tf.errors.InvalidArgumentError							):
																					__snake_case						:     Dict									=  processor.post_process_masks(
																					    lowerCamelCase       ,      np.array(lowerCamelCase							)       ,      np.array(lowerCamelCase							)       ,      return_tensors="tf"							)

@require_vision
@require_torchvision
class 							a						(unittest.TestCase ):




							"""simple docstring"""


							def        __snake_case   (				self			:    List[str]							)      ->       str:
														__snake_case						:     Optional[int]									=  tempfile.mkdtemp()
														__snake_case						:     str									=  SamImageProcessor()
														__snake_case						:     List[Any]									=  SamProcessor(lowerCamelCase							)
														processor.save_pretrained(self.tmpdirname							)

							def        __snake_case   (				self			:    List[str]       ,      **lowerCamelCase			:    Any							)      ->       Dict:
														return AutoProcessor.from_pretrained(self.tmpdirname       ,      **lowerCamelCase							).image_processor

							def        __snake_case   (				self			:    Optional[int]							)      ->       List[Any]:
														shutil.rmtree(self.tmpdirname							)

							def        __snake_case   (				self			:    Optional[int]							)      ->       Optional[int]:
														__snake_case						:     str									=  [np.random.randint(255       ,      size=(3, 30, 400)       ,      dtype=np.uinta							)]

														__snake_case						:     List[Any]									=  [Image.fromarray(np.moveaxis(lowerCamelCase       ,      0       ,      -1							)							) for x in image_inputs]

														return image_inputs

							@is_pt_tf_cross_test
							def        __snake_case   (				self			:    Union[str, Any]							)      ->       List[str]:
														__snake_case						:     str									=  self.get_image_processor()

														__snake_case						:     str									=  SamProcessor(image_processor=lowerCamelCase							)
														__snake_case						:     List[Any]									=  np.random.randint(0       ,      2       ,      size=(1, 3, 5, 5)							).astype(np.floataa							)
														__snake_case						:     Dict									=  [tf.convert_to_tensor(lowerCamelCase							)]
														__snake_case						:     List[Any]									=  [torch.tensor(lowerCamelCase							)]

														__snake_case						:     Optional[Any]									=  [[1764, 2646]]

														__snake_case						:     Optional[int]									=  [[683, 1024]]
														__snake_case						:     Union[str, Any]									=  processor.post_process_masks(
														    lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      return_tensors="tf"							)
														__snake_case						:     Dict									=  processor.post_process_masks(
														    lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      return_tensors="pt"							)

														self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()							)							)






							@is_pt_tf_cross_test
							def        __snake_case   (				self			:    List[Any]							)      ->       List[str]:
														__snake_case						:     Any									=  self.get_image_processor()

														__snake_case						:     List[Any]									=  SamProcessor(image_processor=lowerCamelCase							)

														__snake_case						:     Dict									=  self.prepare_image_inputs()

														__snake_case						:     Any									=  image_processor(lowerCamelCase       ,      return_tensors="pt"							)["pixel_values"].numpy()
														__snake_case						:     Optional[Any]									=  processor(images=lowerCamelCase       ,      return_tensors="pt"							)["pixel_values"].numpy()

														__snake_case						:     Tuple									=  image_processor(lowerCamelCase       ,      return_tensors="tf"							)["pixel_values"].numpy()
														__snake_case						:     List[Any]									=  processor(images=lowerCamelCase       ,      return_tensors="tf"							)["pixel_values"].numpy()

														self.assertTrue(np.allclose(lowerCamelCase       ,      lowerCamelCase							)							)
														self.assertTrue(np.allclose(lowerCamelCase       ,      lowerCamelCase							)							)
														self.assertTrue(np.allclose(lowerCamelCase       ,      lowerCamelCase							)							)




'''simple docstring'''

import enum
import warnings

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
       import tensorflow as tf

       from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

if is_torch_available():
       from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

UpperCAmelCase					 =	logging.get_logger(__name__)


class 	lowerCAmelCase  (   enum.Enum ):
       lowerCAmelCase_												=			0
       lowerCAmelCase_												=			1






@add_end_docstrings(A )
class 	lowerCAmelCase  (   A ):
       lowerCAmelCase_												=			"generated"







       def __init__(				self      :			Tuple    ,   *__lowercase      :			Tuple    ,   **__lowercase      :			Optional[int]   ):







            """simple docstring"""







            super().__init__(*__lowercase    ,   **__lowercase   )

            self.check_model_type(
                TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
                if self.framework == 'tf'
                else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING   )







       def       snake_case (				self      :			Dict    ,   __lowercase      :			Any=None    ,   __lowercase      :			int=None    ,   __lowercase      :			List[str]=None    ,   __lowercase      :			int=None    ,   __lowercase      :			str=None    ,   __lowercase      :			List[Any]=None    ,   **__lowercase      :			Union[str, Any]    ,   ):







            """simple docstring"""







            __lowercase      		={}
            if truncation is not None:
                 __lowercase      		=truncation

            __lowercase      		=generate_kwargs

            __lowercase      		={}
            if return_tensors is not None and return_type is None:
                 __lowercase      		=ReturnType.TENSORS if return_tensors else ReturnType.TEXT
            if return_type is not None:
                 __lowercase      		=return_type

            if clean_up_tokenization_spaces is not None:
                 __lowercase      		=clean_up_tokenization_spaces

            if stop_sequence is not None:
                 __lowercase      		=self.tokenizer.encode(__lowercase    ,   add_special_tokens=__lowercase   )
                 if len(__lowercase   ) > 1:
                      warnings.warn(
                          'Stopping on a multiple token sequence is not yet supported on transformers. The first token of'
                          ' the stop sequence will be used as the stop sequence string in the interim.'   )
                 __lowercase      		=stop_sequence_ids[0]

            return preprocess_params, forward_params, postprocess_params







       def       snake_case (				self      :			List[Any]    ,   __lowercase      :			int    ,   __lowercase      :			int    ,   __lowercase      :			int   ):







            """simple docstring"""







            return True







       def       snake_case (				self      :			Optional[Any]    ,   *__lowercase      :			Optional[int]    ,   __lowercase      :			Union[str, Any]   ):







            """simple docstring"""







            __lowercase      		=self.model.config.prefix if self.model.config.prefix is not None else ''
            if isinstance(args[0]    ,   __lowercase   ):
                 if self.tokenizer.pad_token_id is None:
                      raise ValueError('Please make sure that the tokenizer has a pad_token_id when using a batch input'   )
                 __lowercase      		=([prefix + arg for arg in args[0]],)
                 __lowercase      		=True

            elif isinstance(args[0]    ,   __lowercase   ):
                 __lowercase      		=(prefix + args[0],)
                 __lowercase      		=False
            else:
                 raise ValueError(
                     f''' `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`'''   )
            __lowercase      		=self.tokenizer(*__lowercase    ,   padding=__lowercase    ,   truncation=__lowercase    ,   return_tensors=self.framework   )
            # This is produced by tokenizers but is an invalid generate kwargs
            if "token_type_ids" in inputs:
                 del inputs["token_type_ids"]
            return inputs







       def __call__(				self      :			Any    ,   *__lowercase      :			int    ,   **__lowercase      :			Optional[Any]   ):







            """simple docstring"""







            __lowercase      		=super().__call__(*__lowercase    ,   **__lowercase   )
            if (
                isinstance(args[0]    ,   __lowercase   )
                and all(isinstance(__lowercase    ,   __lowercase   ) for el in args[0]   )
                and all(len(__lowercase   ) == 1 for res in result   )
            ):
                 return [res[0] for res in result]
            return result







       def       snake_case (				self      :			List[str]    ,   __lowercase      :			Tuple    ,   __lowercase      :			List[Any]=TruncationStrategy.DO_NOT_TRUNCATE    ,   **__lowercase      :			Dict   ):







            """simple docstring"""







            __lowercase      		=self._parse_and_tokenize(__lowercase    ,   truncation=__lowercase    ,   **__lowercase   )
            return inputs







       def       snake_case (				self      :			Optional[int]    ,   __lowercase      :			Tuple    ,   **__lowercase      :			Tuple   ):







            """simple docstring"""







            if self.framework == "pt":
                 __lowercase		,  __lowercase      		=model_inputs['input_ids'].shape
            elif self.framework == "tf":
                 __lowercase		,  __lowercase      		=tf.shape(model_inputs['input_ids']   ).numpy()

            __lowercase      		=generate_kwargs.get('min_length'    ,   self.model.config.min_length   )
            __lowercase      		=generate_kwargs.get('max_length'    ,   self.model.config.max_length   )
            self.check_inputs(__lowercase    ,   generate_kwargs['min_length']    ,   generate_kwargs['max_length']   )
            __lowercase      		=self.model.generate(**__lowercase    ,   **__lowercase   )
            __lowercase      		=output_ids.shape[0]
            if self.framework == "pt":
                 __lowercase      		=output_ids.reshape(__lowercase    ,   out_b // in_b    ,   *output_ids.shape[1:]   )
            elif self.framework == "tf":
                 __lowercase      		=tf.reshape(__lowercase    ,   (in_b, out_b // in_b, *output_ids.shape[1:])   )
            return {"output_ids": output_ids}







       def       snake_case (				self      :			str    ,   __lowercase      :			Optional[Any]    ,   __lowercase      :			Optional[int]=ReturnType.TEXT    ,   __lowercase      :			Union[str, Any]=False   ):







            """simple docstring"""







            __lowercase      		=[]
            for output_ids in model_outputs["output_ids"][0]:
                 if return_type == ReturnType.TENSORS:
                      __lowercase      		={f'''{self.return_name}_token_ids''': output_ids}
                 elif return_type == ReturnType.TEXT:
                      __lowercase      		={
                          f'''{self.return_name}_text''': self.tokenizer.decode(
                              __lowercase    ,   skip_special_tokens=__lowercase    ,   clean_up_tokenization_spaces=__lowercase    ,   )
                      }
                 records.append(__lowercase   )
            return records






@add_end_docstrings(A )
class 	lowerCAmelCase  (   A ):
       lowerCAmelCase_												=			"summary"







       def __call__(				self      :			Dict    ,   *__lowercase      :			Dict    ,   **__lowercase      :			Dict   ):







            """simple docstring"""







            return super().__call__(*__lowercase    ,   **__lowercase   )







       def       snake_case (				self      :			str    ,   __lowercase      :			int    ,   __lowercase      :			int    ,   __lowercase      :			int   ):







            """simple docstring"""







            if max_length < min_length:
                 logger.warning(f'''Your min_length={min_length} must be inferior than your max_length={max_length}.'''   )

            if input_length < max_length:
                 logger.warning(
                     f'''Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is '''
                     'a summarization task, where outputs shorter than the input are typically wanted, you might '
                     f'''consider decreasing max_length manually, e.g. summarizer(\'...\', max_length={input_length//2})'''   )






@add_end_docstrings(A )
class 	lowerCAmelCase  (   A ):
       lowerCAmelCase_												=			"translation"







       def       snake_case (				self      :			int    ,   __lowercase      :			int    ,   __lowercase      :			int    ,   __lowercase      :			int   ):







            """simple docstring"""







            if input_length > 0.9 * max_length:
                 logger.warning(
                     f'''Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider '''
                     'increasing your max_length manually, e.g. translator(\'...\', max_length=400)'   )
            return True







       def       snake_case (				self      :			int    ,   *__lowercase      :			str    ,   __lowercase      :			List[str]=TruncationStrategy.DO_NOT_TRUNCATE    ,   __lowercase      :			List[Any]=None    ,   __lowercase      :			Optional[int]=None   ):







            """simple docstring"""







            if getattr(self.tokenizer    ,   '_build_translation_inputs'    ,   __lowercase   ):
                 return self.tokenizer._build_translation_inputs(
                     *__lowercase    ,   return_tensors=self.framework    ,   truncation=__lowercase    ,   src_lang=__lowercase    ,   tgt_lang=__lowercase   )
            else:
                 return super()._parse_and_tokenize(*__lowercase    ,   truncation=__lowercase   )







       def       snake_case (				self      :			Optional[int]    ,   __lowercase      :			List[str]=None    ,   __lowercase      :			Optional[Any]=None    ,   **__lowercase      :			int   ):







            """simple docstring"""







            __lowercase		,  __lowercase		,  __lowercase      		=super()._sanitize_parameters(**__lowercase   )
            if src_lang is not None:
                 __lowercase      		=src_lang
            if tgt_lang is not None:
                 __lowercase      		=tgt_lang
            if src_lang is None and tgt_lang is None:
                 # Backward compatibility, direct arguments use is preferred.
                 __lowercase      		=kwargs.get('task'    ,   self.task   )
                 __lowercase      		=task.split('_'   )
                 if task and len(__lowercase   ) == 4:
                      # translation, XX, to YY
                      __lowercase      		=items[1]
                      __lowercase      		=items[3]
            return preprocess_params, forward_params, postprocess_params







       def __call__(				self      :			Optional[Any]    ,   *__lowercase      :			Any    ,   **__lowercase      :			List[Any]   ):







            """simple docstring"""







            return super().__call__(*__lowercase    ,   **__lowercase   )


