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


lowerCamelCase__           =				5_03_58
lowerCamelCase__           =				5_03_62




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




					def 							UpperCamelCase_						( self :					Tuple						):


							"""simple docstring"""




							__snake_case		       =     "openai/whisper-small.en"
							__snake_case		       =     tempfile.mkdtemp()




					def 							UpperCamelCase_						( self :					Dict      ,**_lowerCAmelCase :					str						):


							"""simple docstring"""




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




					def 							UpperCamelCase_						( self :					Any      ,**_lowerCAmelCase :					Any						):


							"""simple docstring"""




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




					def 							UpperCamelCase_						( self :					int						):


							"""simple docstring"""




							shutil.rmtree(self.tmpdirname						)




					def 							UpperCamelCase_						( self :					Optional[int]						):


							"""simple docstring"""




							__snake_case		       =     self.get_tokenizer()
							__snake_case		       =     self.get_feature_extractor()

							__snake_case		       =     WhisperProcessor(tokenizer=_lowerCAmelCase      ,feature_extractor=_lowerCAmelCase						)

							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      ,_lowerCAmelCase						)

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




					def 							UpperCamelCase_						( self :					List[Any]						):


							"""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=_lowerCAmelCase      ,padding_value=1.0						)

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

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

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




					def 							UpperCamelCase_						( self :					List[Any]						):


							"""simple docstring"""




							__snake_case		       =     self.get_feature_extractor()
							__snake_case		       =     self.get_tokenizer()

							__snake_case		       =     WhisperProcessor(tokenizer=_lowerCAmelCase      ,feature_extractor=_lowerCAmelCase						)

							__snake_case		       =     floats_list((3, 1_000)						)

							__snake_case		       =     feature_extractor(_lowerCAmelCase      ,return_tensors="np"						)
							__snake_case		       =     processor(_lowerCAmelCase      ,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 							UpperCamelCase_						( self :					int						):


							"""simple docstring"""




							__snake_case		       =     self.get_feature_extractor()
							__snake_case		       =     self.get_tokenizer()

							__snake_case		       =     WhisperProcessor(tokenizer=_lowerCAmelCase      ,feature_extractor=_lowerCAmelCase						)

							__snake_case		       =     "This is a test string"

							__snake_case		       =     processor(text=_lowerCAmelCase						)

							__snake_case		       =     tokenizer(_lowerCAmelCase						)

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




					def 							UpperCamelCase_						( self :					List[str]						):


							"""simple docstring"""




							__snake_case		       =     self.get_feature_extractor()
							__snake_case		       =     self.get_tokenizer()

							__snake_case		       =     WhisperProcessor(tokenizer=_lowerCAmelCase      ,feature_extractor=_lowerCAmelCase						)

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

							__snake_case		       =     processor.batch_decode(_lowerCAmelCase						)
							__snake_case		       =     tokenizer.batch_decode(_lowerCAmelCase						)

							self.assertListEqual(_lowerCAmelCase      ,_lowerCAmelCase						)




					def 							UpperCamelCase_						( self :					Optional[Any]						):


							"""simple docstring"""




							__snake_case		       =     self.get_feature_extractor()
							__snake_case		       =     self.get_tokenizer()

							__snake_case		       =     WhisperProcessor(tokenizer=_lowerCAmelCase      ,feature_extractor=_lowerCAmelCase						)

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




					def 							UpperCamelCase_						( self :					List[Any]						):


							"""simple docstring"""




							__snake_case		       =     self.get_feature_extractor()
							__snake_case		       =     self.get_tokenizer()

							__snake_case		       =     WhisperProcessor(tokenizer=_lowerCAmelCase      ,feature_extractor=_lowerCAmelCase						)
							__snake_case		       =     processor.get_decoder_prompt_ids(task="transcribe"      ,no_timestamps=_lowerCAmelCase						)

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

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




					def 							UpperCamelCase_						( self :					str						):


							"""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(_lowerCAmelCase						)

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




					def 							UpperCamelCase_						( self :					Optional[int]						):


							"""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(_lowerCAmelCase						)

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







					def 							UpperCamelCase_						( self :					Optional[int]						):


							"""simple docstring"""




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

							def _test_prompt_error_raised_helper(_lowerCAmelCase :					Union[str, Any]      ,_lowerCAmelCase :					str						):
									with pytest.raises(_lowerCAmelCase						) as excinfo:
											processor.get_prompt_ids(_lowerCAmelCase						)
									__snake_case		       =     F"""Encountered text in the prompt corresponding to disallowed special token: {special_token}."""
									self.assertEqual(_lowerCAmelCase      ,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|>"						)






import posixpath
from pathlib import Path
from unittest.mock import patch

import pytest
from fsspec.implementations.local import AbstractFileSystem, LocalFileSystem, stringify_path
from fsspec.registry import _registry as _fsspec_registry




class 					UpperCamelCase	(     snake_case__      ):
					__UpperCamelCase						=   """mock"""




					def __init__( self :					List[Any]      ,*_lowerCAmelCase :					Optional[int]      ,_lowerCAmelCase :					Union[str, Any]      ,**_lowerCAmelCase :					Union[str, Any]						):


							"""simple docstring"""




							super().__init__()
							__snake_case		       =     LocalFileSystem(*_lowerCAmelCase      ,**_lowerCAmelCase						)
							__snake_case		       =     Path(_lowerCAmelCase						).resolve().as_posix() + "/"




					def 							UpperCamelCase_						( self :					List[Any]      ,_lowerCAmelCase :					Optional[Any]      ,*_lowerCAmelCase :					Union[str, Any]      ,**_lowerCAmelCase :					Dict						):


							"""simple docstring"""




							__snake_case		       =     posixpath.join(self.local_root_dir      ,self._strip_protocol(_lowerCAmelCase						)						)
							return self._fs.mkdir(_lowerCAmelCase      ,*_lowerCAmelCase      ,**_lowerCAmelCase						)




					def 							UpperCamelCase_						( self :					int      ,_lowerCAmelCase :					Tuple      ,*_lowerCAmelCase :					Optional[Any]      ,**_lowerCAmelCase :					Optional[Any]						):


							"""simple docstring"""




							__snake_case		       =     posixpath.join(self.local_root_dir      ,self._strip_protocol(_lowerCAmelCase						)						)
							return self._fs.makedirs(_lowerCAmelCase      ,*_lowerCAmelCase      ,**_lowerCAmelCase						)




					def 							UpperCamelCase_						( self :					Tuple      ,_lowerCAmelCase :					Any						):


							"""simple docstring"""




							__snake_case		       =     posixpath.join(self.local_root_dir      ,self._strip_protocol(_lowerCAmelCase						)						)
							return self._fs.rmdir(_lowerCAmelCase						)




					def 							UpperCamelCase_						( self :					List[Any]      ,_lowerCAmelCase :					List[Any]      ,_lowerCAmelCase :					str=True      ,*_lowerCAmelCase :					int      ,**_lowerCAmelCase :					List[str]						):


							"""simple docstring"""




							__snake_case		       =     posixpath.join(self.local_root_dir      ,self._strip_protocol(_lowerCAmelCase						)						)
							__snake_case		       =     self._fs.ls(_lowerCAmelCase      ,detail=_lowerCAmelCase      ,*_lowerCAmelCase      ,**_lowerCAmelCase						)
							if detail:
									return [{**info, "name": info["name"][len(self.local_root_dir						) :]} for info in out]
							else:
									return [name[len(self.local_root_dir						) :] for name in out]




					def 							UpperCamelCase_						( self :					List[str]      ,_lowerCAmelCase :					Any      ,*_lowerCAmelCase :					Any      ,**_lowerCAmelCase :					str						):


							"""simple docstring"""




							__snake_case		       =     posixpath.join(self.local_root_dir      ,self._strip_protocol(_lowerCAmelCase						)						)
							__snake_case		       =     dict(self._fs.info(_lowerCAmelCase      ,*_lowerCAmelCase      ,**_lowerCAmelCase						)						)
							__snake_case		       =     out["name"][len(self.local_root_dir						) :]
							return out




					def 							UpperCamelCase_						( self :					Tuple      ,_lowerCAmelCase :					Optional[int]      ,_lowerCAmelCase :					Optional[Any]      ,*_lowerCAmelCase :					int      ,**_lowerCAmelCase :					Optional[int]						):


							"""simple docstring"""




							__snake_case		       =     posixpath.join(self.local_root_dir      ,self._strip_protocol(_lowerCAmelCase						)						)
							__snake_case		       =     posixpath.join(self.local_root_dir      ,self._strip_protocol(_lowerCAmelCase						)						)
							return self._fs.cp_file(_lowerCAmelCase      ,_lowerCAmelCase      ,*_lowerCAmelCase      ,**_lowerCAmelCase						)




					def 							UpperCamelCase_						( self :					Union[str, Any]      ,_lowerCAmelCase :					int      ,*_lowerCAmelCase :					List[Any]      ,**_lowerCAmelCase :					int						):


							"""simple docstring"""




							__snake_case		       =     posixpath.join(self.local_root_dir      ,self._strip_protocol(_lowerCAmelCase						)						)
							return self._fs.rm_file(_lowerCAmelCase      ,*_lowerCAmelCase      ,**_lowerCAmelCase						)




					def 							UpperCamelCase_						( self :					Optional[Any]      ,_lowerCAmelCase :					str      ,*_lowerCAmelCase :					Optional[int]      ,**_lowerCAmelCase :					int						):


							"""simple docstring"""




							__snake_case		       =     posixpath.join(self.local_root_dir      ,self._strip_protocol(_lowerCAmelCase						)						)
							return self._fs.rm(_lowerCAmelCase      ,*_lowerCAmelCase      ,**_lowerCAmelCase						)




					def 							UpperCamelCase_						( self :					int      ,_lowerCAmelCase :					Optional[int]      ,*_lowerCAmelCase :					str      ,**_lowerCAmelCase :					Optional[Any]						):


							"""simple docstring"""




							__snake_case		       =     posixpath.join(self.local_root_dir      ,self._strip_protocol(_lowerCAmelCase						)						)
							return self._fs._open(_lowerCAmelCase      ,*_lowerCAmelCase      ,**_lowerCAmelCase						)




					def 							UpperCamelCase_						( self :					Union[str, Any]      ,_lowerCAmelCase :					Tuple						):


							"""simple docstring"""




							__snake_case		       =     posixpath.join(self.local_root_dir      ,self._strip_protocol(_lowerCAmelCase						)						)
							return self._fs.created(_lowerCAmelCase						)




					def 							UpperCamelCase_						( self :					Union[str, Any]      ,_lowerCAmelCase :					int						):


							"""simple docstring"""




							__snake_case		       =     posixpath.join(self.local_root_dir      ,self._strip_protocol(_lowerCAmelCase						)						)
							return self._fs.modified(_lowerCAmelCase						)







					@classmethod
					def 							UpperCamelCase_						( cls :					Dict      ,_lowerCAmelCase :					Dict						):


							"""simple docstring"""




							__snake_case		       =     stringify_path(_lowerCAmelCase						)
							if path.startswith("mock://"						):
									__snake_case		       =     path[7:]
							return path




class 					UpperCamelCase	(     snake_case__      ):
					__UpperCamelCase						=   """tmp"""
					__UpperCamelCase						=   None




					def __init__( self :					int      ,*_lowerCAmelCase :					Tuple      ,**_lowerCAmelCase :					Union[str, Any]						):


							"""simple docstring"""




							assert self.tmp_dir is not None, "TmpDirFileSystem.tmp_dir is not set"
							super().__init__(*_lowerCAmelCase      ,**_lowerCAmelCase      ,local_root_dir=self.tmp_dir      ,auto_mkdir=_lowerCAmelCase						)







					@classmethod
					def 							UpperCamelCase_						( cls :					int      ,_lowerCAmelCase :					str						):


							"""simple docstring"""




							__snake_case		       =     stringify_path(_lowerCAmelCase						)
							if path.startswith("tmp://"						):
									__snake_case		       =     path[6:]
							return path




@pytest.fixture
def 							_lowerCamelCase(				)  ->   str:
		__snake_case		       =     MockFileSystem
		__snake_case		       =     TmpDirFileSystem
		yield
		del _fsspec_registry["mock"]
		del _fsspec_registry["tmp"]



@pytest.fixture
def 							_lowerCamelCase(				__snake_case						,						__snake_case	)  ->   Dict:
		__snake_case		       =     tmp_path_factory.mktemp("mockfs"	)
		return MockFileSystem(local_root_dir=__snake_case						,						auto_mkdir=__snake_case	)





@pytest.fixture
def 							_lowerCamelCase(				__snake_case						,						__snake_case	)  ->   List[Any]:
		__snake_case		       =     tmp_path_factory.mktemp("tmpfs"	)
		with patch.object(__snake_case						,						"tmp_dir"						,						__snake_case	):
				yield TmpDirFileSystem()





