


"""simple docstring"""
import shutil
import tempfile
import unittest

import pytest

from transformers import WhisperTokenizer, is_speech_available
from transformers.testing_utils import require_sentencepiece, require_torch, require_torchaudio

from .test_feature_extraction_whisper import floats_list


if is_speech_available():
				from transformers import WhisperFeatureExtractor, WhisperProcessor


__SCREAMING_SNAKE_CASE				:     Tuple		=     50_358
__SCREAMING_SNAKE_CASE				:     Any		=     50_362





@require_torch
@require_torchaudio
@require_sentencepiece
class   __A  (unittest.TestCase):





							'''simple docstring'''

							def 		lowerCAmelCase							(					self				:   Optional[int]							)       ->Any:



														"""simple docstring"""



														snake_case_     							=		'''openai/whisper-small.en'''
														snake_case_     							=		tempfile.mkdtemp()



							def 		lowerCAmelCase							(					self				:   Any		,							**UpperCAmelCase_				:   Optional[Any]							)       ->int:



														"""simple docstring"""



														return WhisperTokenizer.from_pretrained(self.checkpoint		,							**UpperCamelCase_							)



							def 		lowerCAmelCase							(					self				:   Dict		,							**UpperCAmelCase_				:   int							)       ->Optional[Any]:



														"""simple docstring"""



														return WhisperFeatureExtractor.from_pretrained(self.checkpoint		,							**UpperCamelCase_							)



							def 		lowerCAmelCase							(					self				:   List[Any]							)       ->List[str]:



														"""simple docstring"""



														shutil.rmtree(self.tmpdirname							)



							def 		lowerCAmelCase							(					self				:   List[Any]							)       ->int:



														"""simple docstring"""



														snake_case_     							=		self.get_tokenizer()
														snake_case_     							=		self.get_feature_extractor()

														snake_case_     							=		WhisperProcessor(tokenizer=UpperCamelCase_		,							feature_extractor=UpperCamelCase_							)

														processor.save_pretrained(self.tmpdirname							)
														snake_case_     							=		WhisperProcessor.from_pretrained(self.tmpdirname							)

														self.assertEqual(processor.tokenizer.get_vocab()		,							tokenizer.get_vocab()							)
														self.assertIsInstance(processor.tokenizer		,							UpperCamelCase_							)

														self.assertEqual(processor.feature_extractor.to_json_string()		,							feature_extractor.to_json_string()							)
														self.assertIsInstance(processor.feature_extractor		,							UpperCamelCase_							)



							def 		lowerCAmelCase							(					self				:   Any							)       ->str:



														"""simple docstring"""



														snake_case_     							=		WhisperProcessor(tokenizer=self.get_tokenizer()		,							feature_extractor=self.get_feature_extractor()							)
														processor.save_pretrained(self.tmpdirname							)

														snake_case_     							=		self.get_tokenizer(bos_token="""(BOS)"""		,							eos_token="""(EOS)"""							)
														snake_case_     							=		self.get_feature_extractor(do_normalize=UpperCamelCase_		,							padding_value=1.0							)

														snake_case_     							=		WhisperProcessor.from_pretrained(
														    self.tmpdirname		,							bos_token="""(BOS)"""		,							eos_token="""(EOS)"""		,							do_normalize=UpperCamelCase_		,							padding_value=1.0							)

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

														self.assertEqual(processor.feature_extractor.to_json_string()		,							feature_extractor_add_kwargs.to_json_string()							)
														self.assertIsInstance(processor.feature_extractor		,							UpperCamelCase_							)



							def 		lowerCAmelCase							(					self				:   str							)       ->Optional[int]:



														"""simple docstring"""



														snake_case_     							=		self.get_feature_extractor()
														snake_case_     							=		self.get_tokenizer()

														snake_case_     							=		WhisperProcessor(tokenizer=UpperCamelCase_		,							feature_extractor=UpperCamelCase_							)

														snake_case_     							=		floats_list((3, 1_000)							)

														snake_case_     							=		feature_extractor(UpperCamelCase_		,							return_tensors="""np"""							)
														snake_case_     							=		processor(UpperCamelCase_		,							return_tensors="""np"""							)

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



							def 		lowerCAmelCase							(					self				:   Any							)       ->str:



														"""simple docstring"""



														snake_case_     							=		self.get_feature_extractor()
														snake_case_     							=		self.get_tokenizer()

														snake_case_     							=		WhisperProcessor(tokenizer=UpperCamelCase_		,							feature_extractor=UpperCamelCase_							)

														snake_case_     							=		'''This is a test string'''

														snake_case_     							=		processor(text=UpperCamelCase_							)

														snake_case_     							=		tokenizer(UpperCamelCase_							)

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



							def 		lowerCAmelCase							(					self				:   Optional[Any]							)       ->Tuple:



														"""simple docstring"""



														snake_case_     							=		self.get_feature_extractor()
														snake_case_     							=		self.get_tokenizer()

														snake_case_     							=		WhisperProcessor(tokenizer=UpperCamelCase_		,							feature_extractor=UpperCamelCase_							)

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

														snake_case_     							=		processor.batch_decode(UpperCamelCase_							)
														snake_case_     							=		tokenizer.batch_decode(UpperCamelCase_							)

														self.assertListEqual(UpperCamelCase_		,							UpperCamelCase_							)



							def 		lowerCAmelCase							(					self				:   Union[str, Any]							)       ->List[str]:



														"""simple docstring"""



														snake_case_     							=		self.get_feature_extractor()
														snake_case_     							=		self.get_tokenizer()

														snake_case_     							=		WhisperProcessor(tokenizer=UpperCamelCase_		,							feature_extractor=UpperCamelCase_							)

														self.assertListEqual(
														    processor.model_input_names		,							feature_extractor.model_input_names		,							msg="""`processor` and `feature_extractor` model input names do not match"""		,							)



							def 		lowerCAmelCase							(					self				:   List[Any]							)       ->Any:



														"""simple docstring"""



														snake_case_     							=		self.get_feature_extractor()
														snake_case_     							=		self.get_tokenizer()

														snake_case_     							=		WhisperProcessor(tokenizer=UpperCamelCase_		,							feature_extractor=UpperCamelCase_							)
														snake_case_     							=		processor.get_decoder_prompt_ids(task="""transcribe"""		,							no_timestamps=UpperCamelCase_							)

														self.assertIsInstance(UpperCamelCase_		,							UpperCamelCase_							)
														for ids in forced_decoder_ids:
																					self.assertIsInstance(UpperCamelCase_		,							(list, tuple)							)

														snake_case_     							=		[TRANSCRIBE, NOTIMESTAMPS]
														self.assertListEqual([ids[-1] for ids in forced_decoder_ids]		,							UpperCamelCase_							)



							def 		lowerCAmelCase							(					self				:   str							)       ->Union[str, Any]:



														"""simple docstring"""



														snake_case_     							=		WhisperProcessor(tokenizer=self.get_tokenizer()		,							feature_extractor=self.get_feature_extractor()							)
														snake_case_     							=		processor.get_prompt_ids("""Mr. Quilter"""							)
														snake_case_     							=		processor.tokenizer.decode(UpperCamelCase_							)

														self.assertListEqual(prompt_ids.tolist()		,							[50_360, 1_770, 13, 2_264, 346, 353]							)
														self.assertEqual(UpperCamelCase_		,							"""<|startofprev|> Mr. Quilter"""							)



							def 		lowerCAmelCase							(					self				:   List[str]							)       ->List[Any]:



														"""simple docstring"""



														snake_case_     							=		WhisperProcessor(tokenizer=self.get_tokenizer()		,							feature_extractor=self.get_feature_extractor()							)
														snake_case_     							=		processor.get_prompt_ids(""""""							)
														snake_case_     							=		processor.tokenizer.decode(UpperCamelCase_							)

														self.assertListEqual(prompt_ids.tolist()		,							[50_360, 220]							)
														self.assertEqual(UpperCamelCase_		,							"""<|startofprev|> """							)



							def 		lowerCAmelCase							(					self				:   str							)       ->Dict:



														"""simple docstring"""



														snake_case_     							=		WhisperProcessor(tokenizer=self.get_tokenizer()		,							feature_extractor=self.get_feature_extractor()							)

														def _test_prompt_error_raised_helper(UpperCAmelCase_				:   List[str]		,							UpperCAmelCase_				:   Optional[Any]							):
																					with pytest.raises(UpperCamelCase_							) as excinfo:
																												processor.get_prompt_ids(UpperCamelCase_							)
																					snake_case_     							=		F"""Encountered text in the prompt corresponding to disallowed special token: {special_token}."""
																					self.assertEqual(UpperCamelCase_		,							str(excinfo.value							)							)

														_test_prompt_error_raised_helper("""<|startofprev|> test"""		,							"""<|startofprev|>"""							)
														_test_prompt_error_raised_helper("""test <|notimestamps|>"""		,							"""<|notimestamps|>"""							)
														_test_prompt_error_raised_helper("""test <|zh|> test <|transcribe|>"""		,							"""<|zh|>"""							)





'''simple docstring'''






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__lowercase	=    {'''configuration_detr''': ['''DETR_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DetrConfig''', '''DetrOnnxConfig''']}

try:
							if not is_vision_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__lowercase	=    ['''DetrFeatureExtractor''']
							__lowercase	=    ['''DetrImageProcessor''']

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__lowercase	=    [
							    '''DETR_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''DetrForObjectDetection''',
							    '''DetrForSegmentation''',
							    '''DetrModel''',
							    '''DetrPreTrainedModel''',
							]


if TYPE_CHECKING:
							from .configuration_detr import DETR_PRETRAINED_CONFIG_ARCHIVE_MAP, DetrConfig, DetrOnnxConfig

							try:
														if not is_vision_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .feature_extraction_detr import DetrFeatureExtractor
														from .image_processing_detr import DetrImageProcessor

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_detr import (
														    DETR_PRETRAINED_MODEL_ARCHIVE_LIST,
														    DetrForObjectDetection,
														    DetrForSegmentation,
														    DetrModel,
														    DetrPreTrainedModel,
														)

else:
							import sys

							__lowercase	=    _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)






