
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast
from ...utils import logging


if TYPE_CHECKING:
					from ...feature_extraction_utils import FeatureExtractionMixin
					from ...tokenization_utils_base import PreTrainedTokenizerBase
					from ...utils import TensorType

_UpperCAmelCase :			Optional[int]              =	logging.get_logger(__name__)

_UpperCAmelCase :			List[Any]              =	{
    "openai/whisper-base": "https://huggingface.co/openai/whisper-base/resolve/main/config.json",
}

# fmt: off
_UpperCAmelCase :			List[str]              =	[
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 357, 366, 438, 532, 685,
    705, 796, 930, 1_058, 1_220, 1_267, 1_279, 1_303, 1_343, 1_377,
    1_391, 1_635, 1_782, 1_875, 2_162, 2_361, 2_488, 3_467, 4_008, 4_211,
    4_600, 4_808, 5_299, 5_855, 6_329, 7_203, 9_609, 9_959, 10_563, 10_786,
    11_420, 11_709, 11_907, 13_163, 13_697, 13_700, 14_808, 15_306, 16_410, 16_791,
    17_992, 19_203, 19_510, 20_724, 22_305, 22_935, 27_007, 30_109, 30_420, 33_409,
    34_949, 40_283, 40_493, 40_549, 47_282, 49_146, 50_257, 50_359, 50_360, 50_361
]
_UpperCAmelCase :			Optional[Any]              =	[
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 359, 503, 522, 542, 873,
    893, 902, 918, 922, 931, 1_350, 1_853, 1_982, 2_460, 2_627,
    3_246, 3_253, 3_268, 3_536, 3_846, 3_961, 4_183, 4_667, 6_585, 6_647,
    7_273, 9_061, 9_383, 10_428, 10_929, 11_938, 12_033, 12_331, 12_562, 13_793,
    14_157, 14_635, 15_265, 15_618, 16_553, 16_604, 18_362, 18_956, 20_075, 21_675,
    22_520, 26_130, 26_161, 26_435, 28_279, 29_464, 31_650, 32_302, 32_470, 36_865,
    42_863, 47_425, 49_870, 50_254, 50_258, 50_360, 50_361, 50_362
]


class     lowercase (		lowercase__       ):
		__lowercase	:	Tuple     					=    """whisper"""
		__lowercase	:	int     					=    ["""past_key_values"""]
		__lowercase	:	List[Any]     					=    {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}



		def __init__(			self    ,				A_=51_865    ,				A_=80    ,				A_=6    ,				A_=4    ,				A_=6    ,				A_=4    ,				A_=1_536    ,				A_=1_536    ,				A_=0.0    ,				A_=0.0    ,				A_=50_257    ,				A_=True    ,				A_=True    ,				A_="gelu"    ,				A_=256    ,				A_=0.0    ,				A_=0.0    ,				A_=0.0    ,				A_=0.02    ,				A_=False    ,				A_=1_500    ,				A_=448    ,				A_=50_256    ,				A_=50_256    ,				A_=50_256    ,				A_=None    ,				A_=[220, 50_256]    ,				A_=False    ,				A_=256    ,				A_=False    ,				A_=0.05    ,				A_=10    ,				A_=2    ,				A_=0.0    ,				A_=10    ,				A_=0    ,				A_=7    ,				**A_    ,				)  -> List[Any]:
				"""simple docstring"""
				UpperCamelCase          =					vocab_size
				UpperCamelCase          =					num_mel_bins
				UpperCamelCase          =					d_model
				UpperCamelCase          =					encoder_layers
				UpperCamelCase          =					encoder_attention_heads
				UpperCamelCase          =					decoder_layers
				UpperCamelCase          =					decoder_attention_heads
				UpperCamelCase          =					decoder_ffn_dim
				UpperCamelCase          =					encoder_ffn_dim
				UpperCamelCase          =					dropout
				UpperCamelCase          =					attention_dropout
				UpperCamelCase          =					activation_dropout
				UpperCamelCase          =					activation_function
				UpperCamelCase          =					init_std
				UpperCamelCase          =					encoder_layerdrop
				UpperCamelCase          =					decoder_layerdrop
				UpperCamelCase          =					use_cache
				UpperCamelCase          =					encoder_layers
				UpperCamelCase          =					scale_embedding  # scale factor will be sqrt(d_model) if True
				UpperCamelCase          =					max_source_positions
				UpperCamelCase          =					max_target_positions

				# Audio Classification-specific parameters. Feel free to ignore for other classes.
				UpperCamelCase          =					classifier_proj_size
				UpperCamelCase          =					use_weighted_layer_sum

				# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
				UpperCamelCase          =					apply_spec_augment
				UpperCamelCase          =					mask_time_prob
				UpperCamelCase          =					mask_time_length
				UpperCamelCase          =					mask_time_min_masks
				UpperCamelCase          =					mask_feature_prob
				UpperCamelCase          =					mask_feature_length
				UpperCamelCase          =					mask_feature_min_masks

				UpperCamelCase          =					median_filter_width

				super().__init__(
				    pad_token_id=lowercase_    ,				bos_token_id=lowercase_    ,				eos_token_id=lowercase_    ,				is_encoder_decoder=lowercase_    ,				decoder_start_token_id=lowercase_    ,				suppress_tokens=lowercase_    ,				begin_suppress_tokens=lowercase_    ,				**lowercase_    ,				)






class     lowercase (		lowercase__       ):



		@property
		def __UpperCamelCase			(			self	)  -> Optional[Any]:
				"""simple docstring"""
				UpperCamelCase          =					OrderedDict(
				    [
				        ('input_features', {0: 'batch', 1: 'feature_size', 2: 'encoder_sequence'}),
				    ]	)
				if self.use_past:
						UpperCamelCase          =					{0: "batch"}
				else:
						UpperCamelCase          =					{0: "batch", 1: "decoder_sequence"}

				if self.use_past:
						self.fill_with_past_key_values_(lowercase_    ,				direction='inputs'	)

				return common_inputs



		def __UpperCamelCase			(			self    ,				A_    ,				A_ = -1    ,				A_ = -1    ,				A_ = False    ,				A_ = None    ,				A_ = 22_050    ,				A_ = 5.0    ,				A_ = 220    ,				)  -> Optional[int]:
				"""simple docstring"""
				UpperCamelCase          =					OrderedDict()
				UpperCamelCase          =					OnnxConfig.generate_dummy_inputs(
				    self    ,				preprocessor=preprocessor.feature_extractor    ,				batch_size=lowercase_    ,				framework=lowercase_    ,				sampling_rate=lowercase_    ,				time_duration=lowercase_    ,				frequency=lowercase_    ,				)
				UpperCamelCase          =					encoder_inputs["input_features"].shape[2]
				UpperCamelCase          =					encoder_sequence_length // 2 if self.use_past else seq_length

				UpperCamelCase          =					super().generate_dummy_inputs(
				    preprocessor.tokenizer    ,				lowercase_    ,				lowercase_    ,				lowercase_    ,				lowercase_	)

				UpperCamelCase          =					encoder_inputs.pop('input_features'	)
				UpperCamelCase          =					decoder_inputs.pop('decoder_input_ids'	)

				if "past_key_values" in decoder_inputs:
						UpperCamelCase          =					decoder_inputs.pop('past_key_values'	)

				return dummy_inputs



		@property
		def __UpperCamelCase			(			self	)  -> Dict:
				"""simple docstring"""
				return 1e-3


import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
					from PIL import Image

					from transformers import OwlViTImageProcessor, OwlViTProcessor


@require_vision
class     lowercase (		unittest.TestCase       ):



		def __UpperCamelCase			(			self	)  -> List[str]:
				"""simple docstring"""
				UpperCamelCase          =					tempfile.mkdtemp()

				# fmt: off
				UpperCamelCase          =					['', 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>']
				# fmt: on
				UpperCamelCase          =					dict(zip(A_    ,				range(len(A_	)	)	)	)
				UpperCamelCase          =					['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>', '']
				UpperCamelCase          =					{'unk_token': '<unk>'}

				UpperCamelCase          =					os.path.join(self.tmpdirname    ,				VOCAB_FILES_NAMES['vocab_file']	)
				UpperCamelCase          =					os.path.join(self.tmpdirname    ,				VOCAB_FILES_NAMES['merges_file']	)
				with open(self.vocab_file    ,				'w'    ,				encoding='utf-8'	) as fp:
						fp.write(json.dumps(A_	) + '\n'	)
				with open(self.merges_file    ,				'w'    ,				encoding='utf-8'	) as fp:
						fp.write('\n'.join(A_	)	)

				UpperCamelCase          =					{
				    'do_resize': True,
				    'size': 20,
				    'do_center_crop': True,
				    'crop_size': 18,
				    'do_normalize': True,
				    'image_mean': [0.4814_5466, 0.457_8275, 0.4082_1073],
				    'image_std': [0.2686_2954, 0.2613_0258, 0.2757_7711],
				}
				UpperCamelCase          =					os.path.join(self.tmpdirname    ,				A_	)
				with open(self.image_processor_file    ,				'w'    ,				encoding='utf-8'	) as fp:
						json.dump(A_    ,				A_	)



		def __UpperCamelCase			(			self    ,				**A_	)  -> Union[str, Any]:
				"""simple docstring"""
				return CLIPTokenizer.from_pretrained(self.tmpdirname    ,				pad_token='!'    ,				**A_	)



		def __UpperCamelCase			(			self    ,				**A_	)  -> Tuple:
				"""simple docstring"""
				return CLIPTokenizerFast.from_pretrained(self.tmpdirname    ,				pad_token='!'    ,				**A_	)



		def __UpperCamelCase			(			self    ,				**A_	)  -> Union[str, Any]:
				"""simple docstring"""
				return OwlViTImageProcessor.from_pretrained(self.tmpdirname    ,				**A_	)



		def __UpperCamelCase			(			self	)  -> List[str]:
				"""simple docstring"""
				shutil.rmtree(self.tmpdirname	)



		def __UpperCamelCase			(			self	)  -> List[Any]:
				"""simple docstring"""
				UpperCamelCase          =					[np.random.randint(255    ,				size=(3, 30, 400)    ,				dtype=np.uinta	)]

				UpperCamelCase          =					[Image.fromarray(np.moveaxis(A_    ,				0    ,				-1	)	) for x in image_inputs]

				return image_inputs



		def __UpperCamelCase			(			self	)  -> Optional[Any]:
				"""simple docstring"""
				UpperCamelCase          =					self.get_tokenizer()
				UpperCamelCase          =					self.get_rust_tokenizer()
				UpperCamelCase          =					self.get_image_processor()

				UpperCamelCase          =					OwlViTProcessor(tokenizer=A_    ,				image_processor=A_	)
				processor_slow.save_pretrained(self.tmpdirname	)
				UpperCamelCase          =					OwlViTProcessor.from_pretrained(self.tmpdirname    ,				use_fast=A_	)

				UpperCamelCase          =					OwlViTProcessor(tokenizer=A_    ,				image_processor=A_	)
				processor_fast.save_pretrained(self.tmpdirname	)
				UpperCamelCase          =					OwlViTProcessor.from_pretrained(self.tmpdirname	)

				self.assertEqual(processor_slow.tokenizer.get_vocab()    ,				tokenizer_slow.get_vocab()	)
				self.assertEqual(processor_fast.tokenizer.get_vocab()    ,				tokenizer_fast.get_vocab()	)
				self.assertEqual(tokenizer_slow.get_vocab()    ,				tokenizer_fast.get_vocab()	)
				self.assertIsInstance(processor_slow.tokenizer    ,				A_	)
				self.assertIsInstance(processor_fast.tokenizer    ,				A_	)

				self.assertEqual(processor_slow.image_processor.to_json_string()    ,				image_processor.to_json_string()	)
				self.assertEqual(processor_fast.image_processor.to_json_string()    ,				image_processor.to_json_string()	)
				self.assertIsInstance(processor_slow.image_processor    ,				A_	)
				self.assertIsInstance(processor_fast.image_processor    ,				A_	)



		def __UpperCamelCase			(			self	)  -> List[Any]:
				"""simple docstring"""
				UpperCamelCase          =					OwlViTProcessor(tokenizer=self.get_tokenizer()    ,				image_processor=self.get_image_processor()	)
				processor.save_pretrained(self.tmpdirname	)

				UpperCamelCase          =					self.get_tokenizer(bos_token='(BOS)'    ,				eos_token='(EOS)'	)
				UpperCamelCase          =					self.get_image_processor(do_normalize=A_	)

				UpperCamelCase          =					OwlViTProcessor.from_pretrained(
				    self.tmpdirname    ,				bos_token='(BOS)'    ,				eos_token='(EOS)'    ,				do_normalize=A_	)

				self.assertEqual(processor.tokenizer.get_vocab()    ,				tokenizer_add_kwargs.get_vocab()	)
				self.assertIsInstance(processor.tokenizer    ,				A_	)

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



		def __UpperCamelCase			(			self	)  -> Optional[Any]:
				"""simple docstring"""
				UpperCamelCase          =					self.get_image_processor()
				UpperCamelCase          =					self.get_tokenizer()

				UpperCamelCase          =					OwlViTProcessor(tokenizer=A_    ,				image_processor=A_	)

				UpperCamelCase          =					self.prepare_image_inputs()

				UpperCamelCase          =					image_processor(A_    ,				return_tensors='np'	)
				UpperCamelCase          =					processor(images=A_    ,				return_tensors='np'	)

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



		def __UpperCamelCase			(			self	)  -> str:
				"""simple docstring"""
				UpperCamelCase          =					self.get_image_processor()
				UpperCamelCase          =					self.get_tokenizer()

				UpperCamelCase          =					OwlViTProcessor(tokenizer=A_    ,				image_processor=A_	)

				UpperCamelCase          =					'lower newer'

				UpperCamelCase          =					processor(text=A_    ,				return_tensors='np'	)

				UpperCamelCase          =					tokenizer(A_    ,				return_tensors='np'	)

				for key in encoded_tok.keys():
						self.assertListEqual(encoded_tok[key][0].tolist()    ,				encoded_processor[key][0].tolist()	)



		def __UpperCamelCase			(			self	)  -> int:
				"""simple docstring"""
				UpperCamelCase          =					self.get_image_processor()
				UpperCamelCase          =					self.get_tokenizer()

				UpperCamelCase          =					OwlViTProcessor(tokenizer=A_    ,				image_processor=A_	)

				UpperCamelCase          =					'lower newer'
				UpperCamelCase          =					self.prepare_image_inputs()

				UpperCamelCase          =					processor(text=A_    ,				images=A_	)

				self.assertListEqual(list(inputs.keys()	)    ,				['input_ids', 'attention_mask', 'pixel_values']	)

				# test if it raises when no input is passed
				with pytest.raises(A_	):
						processor()



		def __UpperCamelCase			(			self	)  -> Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase          =					'google/owlvit-base-patch32'
				UpperCamelCase          =					OwlViTProcessor.from_pretrained(A_	)

				UpperCamelCase          =					['cat', 'nasa badge']
				UpperCamelCase          =					processor(text=A_	)

				UpperCamelCase          =					16
				self.assertListEqual(list(inputs.keys()	)    ,				['input_ids', 'attention_mask']	)
				self.assertEqual(inputs['input_ids'].shape    ,				(2, seq_length)	)

				# test if it raises when no input is passed
				with pytest.raises(A_	):
						processor()



		def __UpperCamelCase			(			self	)  -> Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase          =					'google/owlvit-base-patch32'
				UpperCamelCase          =					OwlViTProcessor.from_pretrained(A_	)

				UpperCamelCase          =					[['cat', 'nasa badge'], ['person']]
				UpperCamelCase          =					processor(text=A_	)

				UpperCamelCase          =					16
				UpperCamelCase          =					len(A_	)
				UpperCamelCase          =					max([len(A_	) for texts in input_texts]	)

				self.assertListEqual(list(inputs.keys()	)    ,				['input_ids', 'attention_mask']	)
				self.assertEqual(inputs['input_ids'].shape    ,				(batch_size * num_max_text_queries, seq_length)	)

				# test if it raises when no input is passed
				with pytest.raises(A_	):
						processor()



		def __UpperCamelCase			(			self	)  -> Dict:
				"""simple docstring"""
				UpperCamelCase          =					'google/owlvit-base-patch32'
				UpperCamelCase          =					OwlViTProcessor.from_pretrained(A_	)

				UpperCamelCase          =					['cat', 'nasa badge']
				UpperCamelCase          =					processor(text=A_	)

				UpperCamelCase          =					16
				UpperCamelCase          =					inputs['input_ids']
				UpperCamelCase          =					[
				    [49_406, 2_368, 49_407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
				    [49_406, 6_841, 11_301, 49_407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
				]

				self.assertListEqual(list(inputs.keys()	)    ,				['input_ids', 'attention_mask']	)
				self.assertEqual(inputs['input_ids'].shape    ,				(2, seq_length)	)
				self.assertListEqual(list(input_ids[0]	)    ,				predicted_ids[0]	)
				self.assertListEqual(list(input_ids[1]	)    ,				predicted_ids[1]	)



		def __UpperCamelCase			(			self	)  -> Optional[Any]:
				"""simple docstring"""
				UpperCamelCase          =					self.get_image_processor()
				UpperCamelCase          =					self.get_tokenizer()

				UpperCamelCase          =					OwlViTProcessor(tokenizer=A_    ,				image_processor=A_	)

				UpperCamelCase          =					self.prepare_image_inputs()
				UpperCamelCase          =					self.prepare_image_inputs()

				UpperCamelCase          =					processor(images=A_    ,				query_images=A_	)

				self.assertListEqual(list(inputs.keys()	)    ,				['query_pixel_values', 'pixel_values']	)

				# test if it raises when no input is passed
				with pytest.raises(A_	):
						processor()



		def __UpperCamelCase			(			self	)  -> int:
				"""simple docstring"""
				UpperCamelCase          =					self.get_image_processor()
				UpperCamelCase          =					self.get_tokenizer()

				UpperCamelCase          =					OwlViTProcessor(tokenizer=A_    ,				image_processor=A_	)

				UpperCamelCase          =					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

				UpperCamelCase          =					processor.batch_decode(A_	)
				UpperCamelCase          =					tokenizer.batch_decode(A_	)

				self.assertListEqual(A_    ,				A_	)
