






'''simple docstring'''
import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import BeitImageProcessor


class  _snake_case (					unittest.TestCase       ):

					"""simple docstring"""





					def __init__(  self ,			UpperCAmelCase__ ,			UpperCAmelCase__=7 ,			UpperCAmelCase__=3 ,			UpperCAmelCase__=18 ,			UpperCAmelCase__=30 ,			UpperCAmelCase__=400 ,			UpperCAmelCase__=True ,			UpperCAmelCase__=None ,			UpperCAmelCase__=True ,			UpperCAmelCase__=None ,			UpperCAmelCase__=True ,			UpperCAmelCase__=[0.5, 0.5, 0.5] ,			UpperCAmelCase__=[0.5, 0.5, 0.5] ,			UpperCAmelCase__=False ,			)   ->						int:
										a_						= size if size is not None else {'height': 20, 'width': 20}
										a_						= crop_size if crop_size is not None else {'height': 18, 'width': 18}
										a_						= parent
										a_						= batch_size
										a_						= num_channels
										a_						= image_size
										a_						= min_resolution
										a_						= max_resolution
										a_						= do_resize
										a_						= size
										a_						= do_center_crop
										a_						= crop_size
										a_						= do_normalize
										a_						= image_mean
										a_						= image_std
										a_						= do_reduce_labels




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						List[str]:
										return {
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "do_center_crop": self.do_center_crop,
										    "crop_size": self.crop_size,
										    "do_normalize": self.do_normalize,
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_reduce_labels": self.do_reduce_labels,
										}



def 				a						( )					->					List[str]:






					"""simple docstring"""







					a_						= load_dataset('hf-internal-testing/fixtures_ade20k'       ,	split='test'			)

					a_						= Image.open(dataset[0]['file']			)
					a_						= Image.open(dataset[1]['file']			)

					return image, map

def 				a						( )					->					int:






					"""simple docstring"""







					a_						= load_dataset('hf-internal-testing/fixtures_ade20k'       ,	split='test'			)

					a_						= Image.open(ds[0]['file']			)
					a_						= Image.open(ds[1]['file']			)
					a_						= Image.open(ds[2]['file']			)
					a_						= Image.open(ds[3]['file']			)

					return [imagea, imagea], [mapa, mapa]





@require_torch
@require_vision
class  _snake_case (					snake_case  ,       unittest.TestCase       ):

					"""simple docstring"""





					_UpperCamelCase					=   BeitImageProcessor if is_vision_available() else None




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						List[Any]:
										a_						= BeitImageProcessingTester(self  )




					@property
					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						str:
										return self.image_processor_tester.prepare_image_processor_dict()




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						Tuple:
										a_						= self.image_processing_class(**self.image_processor_dict  )
										self.assertTrue(hasattr(UpperCAmelCase__ ,			'do_resize'  )  )
										self.assertTrue(hasattr(UpperCAmelCase__ ,			'size'  )  )
										self.assertTrue(hasattr(UpperCAmelCase__ ,			'do_center_crop'  )  )
										self.assertTrue(hasattr(UpperCAmelCase__ ,			'center_crop'  )  )
										self.assertTrue(hasattr(UpperCAmelCase__ ,			'do_normalize'  )  )
										self.assertTrue(hasattr(UpperCAmelCase__ ,			'image_mean'  )  )
										self.assertTrue(hasattr(UpperCAmelCase__ ,			'image_std'  )  )




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						str:
										a_						= self.image_processing_class.from_dict(self.image_processor_dict  )
										self.assertEqual(image_processor.size ,			{'height': 20, 'width': 20}  )
										self.assertEqual(image_processor.crop_size ,			{'height': 18, 'width': 18}  )
										self.assertEqual(image_processor.do_reduce_labels ,			UpperCAmelCase__  )

										a_						= self.image_processing_class.from_dict(
										    self.image_processor_dict ,			size=42 ,			crop_size=84 ,			reduce_labels=UpperCAmelCase__  )
										self.assertEqual(image_processor.size ,			{'height': 42, 'width': 42}  )
										self.assertEqual(image_processor.crop_size ,			{'height': 84, 'width': 84}  )
										self.assertEqual(image_processor.do_reduce_labels ,			UpperCAmelCase__  )




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						List[Any]:
										pass




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						Any:
										a_						= self.image_processing_class(**self.image_processor_dict  )
										# create random PIL images
										a_						= prepare_image_inputs(self.image_processor_tester ,			equal_resolution=UpperCAmelCase__  )
										for image in image_inputs:
															self.assertIsInstance(UpperCAmelCase__ ,			Image.Image  )

										# Test not batched input
										a_						= image_processing(image_inputs[0] ,			return_tensors='pt'  ).pixel_values
										self.assertEqual(
										    encoded_images.shape ,			(
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    ) ,			)

										# Test batched
										a_						= image_processing(UpperCAmelCase__ ,			return_tensors='pt'  ).pixel_values
										self.assertEqual(
										    encoded_images.shape ,			(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    ) ,			)




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						Optional[int]:
										a_						= self.image_processing_class(**self.image_processor_dict  )
										# create random numpy tensors
										a_						= prepare_image_inputs(self.image_processor_tester ,			equal_resolution=UpperCAmelCase__ ,			numpify=UpperCAmelCase__  )
										for image in image_inputs:
															self.assertIsInstance(UpperCAmelCase__ ,			np.ndarray  )

										# Test not batched input
										a_						= image_processing(image_inputs[0] ,			return_tensors='pt'  ).pixel_values
										self.assertEqual(
										    encoded_images.shape ,			(
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    ) ,			)

										# Test batched
										a_						= image_processing(UpperCAmelCase__ ,			return_tensors='pt'  ).pixel_values
										self.assertEqual(
										    encoded_images.shape ,			(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    ) ,			)




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						Optional[Any]:
										a_						= self.image_processing_class(**self.image_processor_dict  )
										# create random PyTorch tensors
										a_						= prepare_image_inputs(self.image_processor_tester ,			equal_resolution=UpperCAmelCase__ ,			torchify=UpperCAmelCase__  )
										for image in image_inputs:
															self.assertIsInstance(UpperCAmelCase__ ,			torch.Tensor  )

										# Test not batched input
										a_						= image_processing(image_inputs[0] ,			return_tensors='pt'  ).pixel_values
										self.assertEqual(
										    encoded_images.shape ,			(
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    ) ,			)

										# Test batched
										a_						= image_processing(UpperCAmelCase__ ,			return_tensors='pt'  ).pixel_values
										self.assertEqual(
										    encoded_images.shape ,			(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    ) ,			)




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						Dict:
										a_						= self.image_processing_class(**self.image_processor_dict  )
										# create random PyTorch tensors
										a_						= prepare_image_inputs(self.image_processor_tester ,			equal_resolution=UpperCAmelCase__ ,			torchify=UpperCAmelCase__  )
										a_						= []
										for image in image_inputs:
															self.assertIsInstance(UpperCAmelCase__ ,			torch.Tensor  )
															maps.append(torch.zeros(image.shape[-2:]  ).long()  )

										# Test not batched input
										a_						= image_processing(image_inputs[0] ,			maps[0] ,			return_tensors='pt'  )
										self.assertEqual(
										    encoding['pixel_values'].shape ,			(
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    ) ,			)
										self.assertEqual(
										    encoding['labels'].shape ,			(
										        1,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    ) ,			)
										self.assertEqual(encoding['labels'].dtype ,			torch.long  )
										self.assertTrue(encoding['labels'].min().item() >= 0  )
										self.assertTrue(encoding['labels'].max().item() <= 255  )

										# Test batched
										a_						= image_processing(UpperCAmelCase__ ,			UpperCAmelCase__ ,			return_tensors='pt'  )
										self.assertEqual(
										    encoding['pixel_values'].shape ,			(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    ) ,			)
										self.assertEqual(
										    encoding['labels'].shape ,			(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    ) ,			)
										self.assertEqual(encoding['labels'].dtype ,			torch.long  )
										self.assertTrue(encoding['labels'].min().item() >= 0  )
										self.assertTrue(encoding['labels'].max().item() <= 255  )

										# Test not batched input (PIL images)
										a_				,       a_						= prepare_semantic_single_inputs()

										a_						= image_processing(UpperCAmelCase__ ,			UpperCAmelCase__ ,			return_tensors='pt'  )
										self.assertEqual(
										    encoding['pixel_values'].shape ,			(
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    ) ,			)
										self.assertEqual(
										    encoding['labels'].shape ,			(
										        1,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    ) ,			)
										self.assertEqual(encoding['labels'].dtype ,			torch.long  )
										self.assertTrue(encoding['labels'].min().item() >= 0  )
										self.assertTrue(encoding['labels'].max().item() <= 255  )

										# Test batched input (PIL images)
										a_				,       a_						= prepare_semantic_batch_inputs()

										a_						= image_processing(UpperCAmelCase__ ,			UpperCAmelCase__ ,			return_tensors='pt'  )
										self.assertEqual(
										    encoding['pixel_values'].shape ,			(
										        2,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    ) ,			)
										self.assertEqual(
										    encoding['labels'].shape ,			(
										        2,
										        self.image_processor_tester.crop_size['height'],
										        self.image_processor_tester.crop_size['width'],
										    ) ,			)
										self.assertEqual(encoding['labels'].dtype ,			torch.long  )
										self.assertTrue(encoding['labels'].min().item() >= 0  )
										self.assertTrue(encoding['labels'].max().item() <= 255  )




					def 			__SCREAMING_SNAKE_CASE			(  self  )   ->						Tuple:
										a_						= self.image_processing_class(**self.image_processor_dict  )

										# ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150
										a_				,       a_						= prepare_semantic_single_inputs()
										a_						= image_processing(UpperCAmelCase__ ,			UpperCAmelCase__ ,			return_tensors='pt'  )
										self.assertTrue(encoding['labels'].min().item() >= 0  )
										self.assertTrue(encoding['labels'].max().item() <= 150  )

										a_						= True
										a_						= image_processing(UpperCAmelCase__ ,			UpperCAmelCase__ ,			return_tensors='pt'  )
										self.assertTrue(encoding['labels'].min().item() >= 0  )
										self.assertTrue(encoding['labels'].max().item() <= 255  )







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   :			Dict          = logging.get_logger(__name__)

class 						__lowerCAmelCase      (      enum.Enum					):
							UpperCamelCase__   	=    0
							UpperCamelCase__   	=    1







@add_end_docstrings(__magic_name__					)
class 						__lowerCAmelCase      (      __magic_name__					):
							UpperCamelCase__   	=    '''generated'''
							def __init__(		self		:Any						, *__magic_name__		:Tuple						, **__magic_name__		:Tuple							):







								'''simple docstring'''







								super().__init__(*__magic_name__						, **__magic_name__							)

								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  lowerCamelCase__				(		self		:Optional[int]						, __magic_name__		:Any=None						, __magic_name__		:Optional[Any]=None						, __magic_name__		:Any=None						, __magic_name__		:List[str]=None						, __magic_name__		:Tuple=None						, __magic_name__		:str=None						, **__magic_name__		:List[Any]						, ):







								'''simple docstring'''







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

								a									=				generate_kwargs

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

								if clean_up_tokenization_spaces is not None:
									a									=				clean_up_tokenization_spaces

								if stop_sequence is not None:
									a									=				self.tokenizer.encode(__magic_name__						, add_special_tokens=__magic_name__							)
									if len(__magic_name__							) > 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."""							)
									a									=				stop_sequence_ids[0]

								return preprocess_params, forward_params, postprocess_params
							def  lowerCamelCase__				(		self		:Optional[int]						, __magic_name__		:int						, __magic_name__		:int						, __magic_name__		:int							):







								'''simple docstring'''







								return True
							def  lowerCamelCase__				(		self		:Dict						, *__magic_name__		:Optional[int]						, __magic_name__		:List[str]							):







								'''simple docstring'''







								a									=				self.model.config.prefix if self.model.config.prefix is not None else """"""
								if isinstance(args[0]						, __magic_name__							):
									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"""							)
									a									=				([prefix + arg for arg in args[0]],)
									a									=				True

								elif isinstance(args[0]						, __magic_name__							):
									a									=				(prefix + args[0],)
									a									=				False
								else:
									raise ValueError(
									    F' `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`'							)
								a									=				self.tokenizer(*__magic_name__						, padding=__magic_name__						, truncation=__magic_name__						, 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		:Tuple						, *__magic_name__		:Any						, **__magic_name__		:str							):







								'''simple docstring'''







								a									=				super().__call__(*__magic_name__						, **__magic_name__							)
								if (
								    isinstance(args[0]						, __magic_name__							)
								    and all(isinstance(__magic_name__						, __magic_name__							) for el in args[0]							)
								    and all(len(__magic_name__							) == 1 for res in result							)
								):
									return [res[0] for res in result]
								return result
							def  lowerCamelCase__				(		self		:Dict						, __magic_name__		:Optional[Any]						, __magic_name__		:List[str]=TruncationStrategy.DO_NOT_TRUNCATE						, **__magic_name__		:Any							):







								'''simple docstring'''







								a									=				self._parse_and_tokenize(__magic_name__						, truncation=__magic_name__						, **__magic_name__							)
								return inputs
							def  lowerCamelCase__				(		self		:Any						, __magic_name__		:int						, **__magic_name__		:int							):







								'''simple docstring'''







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

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





							def  lowerCamelCase__				(		self		:Optional[int]						, __magic_name__		:Dict						, __magic_name__		:Any=ReturnType.TEXT						, __magic_name__		:int=False							):







								'''simple docstring'''







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







@add_end_docstrings(__magic_name__					)
class 						__lowerCAmelCase      (      __magic_name__					):
							UpperCamelCase__   	=    '''summary'''
							def __call__(		self		:Any						, *__magic_name__		:List[str]						, **__magic_name__		:Optional[int]							):







								'''simple docstring'''







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





							def  lowerCamelCase__				(		self		:Any						, __magic_name__		:int						, __magic_name__		:int						, __magic_name__		: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(__magic_name__					)
class 						__lowerCAmelCase      (      __magic_name__					):
							UpperCamelCase__   	=    '''translation'''
							def  lowerCamelCase__				(		self		:List[Any]						, __magic_name__		:int						, __magic_name__		:int						, __magic_name__		: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  lowerCamelCase__				(		self		:str						, *__magic_name__		:Union[str, Any]						, __magic_name__		:Any=TruncationStrategy.DO_NOT_TRUNCATE						, __magic_name__		:Optional[Any]=None						, __magic_name__		:List[str]=None							):







								'''simple docstring'''







								if getattr(self.tokenizer						, """_build_translation_inputs"""						, __magic_name__							):
									return self.tokenizer._build_translation_inputs(
									    *__magic_name__						, return_tensors=self.framework						, truncation=__magic_name__						, src_lang=__magic_name__						, tgt_lang=__magic_name__							)
								else:
									return super()._parse_and_tokenize(*__magic_name__						, truncation=__magic_name__							)
							def  lowerCamelCase__				(		self		:int						, __magic_name__		:List[str]=None						, __magic_name__		:Union[str, Any]=None						, **__magic_name__		:Optional[int]							):







								'''simple docstring'''







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





							def __call__(		self		:Optional[Any]						, *__magic_name__		:Any						, **__magic_name__		:str							):







								'''simple docstring'''







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

