
"""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_			      =					50_358
SCREAMING_SNAKE_CASE_			      =					50_362
@require_torch
@require_torchaudio
@require_sentencepiece
class      lowerCAmelCase_						(							unittest.TestCase				):



							'''simple docstring'''





							def  A__		(				self							)   ->       List[Any]:
													__lowerCAmelCase        =       """openai/whisper-small.en"""
													__lowerCAmelCase        =       tempfile.mkdtemp()




							def  A__		(				self  ,							**snake_case_							)   ->       List[str]:
													return WhisperTokenizer.from_pretrained(self.checkpoint  ,							**snake_case_							)




							def  A__		(				self  ,							**snake_case_							)   ->       List[Any]:
													return WhisperFeatureExtractor.from_pretrained(self.checkpoint  ,							**snake_case_							)




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




							def  A__		(				self							)   ->       List[str]:
													__lowerCAmelCase        =       self.get_tokenizer()
													__lowerCAmelCase        =       self.get_feature_extractor()

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

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

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

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




							def  A__		(				self							)   ->       str:
													__lowerCAmelCase        =       WhisperProcessor(tokenizer=self.get_tokenizer()  ,							feature_extractor=self.get_feature_extractor()							)
													processor.save_pretrained(self.tmpdirname							)

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

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

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

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




							def  A__		(				self							)   ->       Any:
													__lowerCAmelCase        =       self.get_feature_extractor()
													__lowerCAmelCase        =       self.get_tokenizer()

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

													__lowerCAmelCase        =       floats_list((3, 1_000)							)

													__lowerCAmelCase        =       feature_extractor(snake_case_  ,							return_tensors="""np"""							)
													__lowerCAmelCase        =       processor(snake_case_  ,							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  A__		(				self							)   ->       Optional[Any]:
													__lowerCAmelCase        =       self.get_feature_extractor()
													__lowerCAmelCase        =       self.get_tokenizer()

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

													__lowerCAmelCase        =       """This is a test string"""

													__lowerCAmelCase        =       processor(text=snake_case_							)

													__lowerCAmelCase        =       tokenizer(snake_case_							)

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




							def  A__		(				self							)   ->       Dict:
													__lowerCAmelCase        =       self.get_feature_extractor()
													__lowerCAmelCase        =       self.get_tokenizer()

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

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

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

													self.assertListEqual(snake_case_  ,							snake_case_							)




							def  A__		(				self							)   ->       str:
													__lowerCAmelCase        =       self.get_feature_extractor()
													__lowerCAmelCase        =       self.get_tokenizer()

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

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




							def  A__		(				self							)   ->       List[Any]:
													__lowerCAmelCase        =       self.get_feature_extractor()
													__lowerCAmelCase        =       self.get_tokenizer()

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

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

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




							def  A__		(				self							)   ->       Optional[int]:
													__lowerCAmelCase        =       WhisperProcessor(tokenizer=self.get_tokenizer()  ,							feature_extractor=self.get_feature_extractor()							)
													__lowerCAmelCase        =       processor.get_prompt_ids("""Mr. Quilter"""							)
													__lowerCAmelCase        =       processor.tokenizer.decode(snake_case_							)

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




							def  A__		(				self							)   ->       Any:
													__lowerCAmelCase        =       WhisperProcessor(tokenizer=self.get_tokenizer()  ,							feature_extractor=self.get_feature_extractor()							)
													__lowerCAmelCase        =       processor.get_prompt_ids(""""""							)
													__lowerCAmelCase        =       processor.tokenizer.decode(snake_case_							)

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




							def  A__		(				self							)   ->       Any:
													__lowerCAmelCase        =       WhisperProcessor(tokenizer=self.get_tokenizer()  ,							feature_extractor=self.get_feature_extractor()							)

													def _test_prompt_error_raised_helper(snake_case_  ,							snake_case_							):
																			with pytest.raises(snake_case_							) as excinfo:
																									processor.get_prompt_ids(snake_case_							)
																			__lowerCAmelCase        =       f"""Encountered text in the prompt corresponding to disallowed special token: {special_token}."""
																			self.assertEqual(snake_case_  ,							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"""
def 				lowercase      (_lowerCAmelCase		):
						# must be
						__lowerCAmelCase        =       [list(_lowerCAmelCase		) for row in matrix]
						if matrix and isinstance(_lowerCAmelCase				,  _lowerCAmelCase		):
												if isinstance(matrix[0]				,  _lowerCAmelCase		):
																		__lowerCAmelCase        =       0
																		for row in matrix:
																								if prev_len == 0:
																														__lowerCAmelCase        =       len(_lowerCAmelCase		)
																														__lowerCAmelCase        =       True
																								else:
																														__lowerCAmelCase        =       prev_len == len(_lowerCAmelCase		)
												else:
																		__lowerCAmelCase        =       True
						else:
												__lowerCAmelCase        =       False

						return result

def 				lowercase      (_lowerCAmelCase		):
						if check_matrix(_lowerCAmelCase		) and len(_lowerCAmelCase		) > 0:
												__lowerCAmelCase        =       [list(_lowerCAmelCase		) for row in a]
												__lowerCAmelCase        =       len(_lowerCAmelCase		)
												if isinstance(a[0]				,  _lowerCAmelCase		):
																		__lowerCAmelCase        =       len(a[0]		)
												else:
																		for dat in a:
																								print(_lowerCAmelCase		)
																		return

												# horizotal printing increasing
												for i in range(0				,  _lowerCAmelCase		):
																		print(a[0][i]		)
												# vertical printing down
												for i in range(1				,  _lowerCAmelCase		):
																		print(a[i][mat_col - 1]		)
												# horizotal printing decreasing
												if mat_row > 1:
																		for i in range(mat_col - 2				,  -1				,  -1		):
																								print(a[mat_row - 1][i]		)
        # vertical printing up
												for i in range(mat_row - 2				,  0				,  -1		):
																		print(a[i][0]		)
												__lowerCAmelCase        =       [row[1 : mat_col - 1] for row in a[1 : mat_row - 1]]
												if len(_lowerCAmelCase		) > 0:
																		spiral_print_clockwise(_lowerCAmelCase		)
												else:
																		return
						else:
												print("""Not a valid matrix"""		)
												return

def 				lowercase      (_lowerCAmelCase		):
						if matrix:
												return list(matrix.pop(0		)		) + spiral_traversal(list(zip(*_lowerCAmelCase		)		)[::-1]		)
						else:
												return []


# driver code
if __name__ == "__main__":
			import doctest

			doctest.testmod()

			SCREAMING_SNAKE_CASE_			      =					[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
			spiral_print_clockwise(a)
