

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


__A     						=      5_03_58
__A     						=      5_03_62



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



      def 		UpperCamelCase_						(     self :							int						)       ->	Tuple:
        _snake_case		=				'''openai/whisper-small.en'''
        _snake_case		=				tempfile.mkdtemp()



      def 		UpperCamelCase_						(     self :							Optional[Any]     ,   **A__ :							str						)       ->	Optional[Any]:
        return WhisperTokenizer.from_pretrained(self.checkpoint     ,   **A__						)



      def 		UpperCamelCase_						(     self :							Any     ,   **A__ :							Optional[int]						)       ->	Dict:
        return WhisperFeatureExtractor.from_pretrained(self.checkpoint     ,   **A__						)



      def 		UpperCamelCase_						(     self :							Union[str, Any]						)       ->	str:
        shutil.rmtree(self.tmpdirname						)



      def 		UpperCamelCase_						(     self :							Tuple						)       ->	Optional[Any]:
        _snake_case		=				self.get_tokenizer()
        _snake_case		=				self.get_feature_extractor()

        _snake_case		=				WhisperProcessor(tokenizer=A__     ,   feature_extractor=A__						)

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

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



      def 		UpperCamelCase_						(     self :							List[str]						)       ->	str:
        _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=A__     ,   padding_value=1.0						)

        _snake_case		=				WhisperProcessor.from_pretrained(
            self.tmpdirname     ,   bos_token='''(BOS)'''     ,   eos_token='''(EOS)'''     ,   do_normalize=A__     ,   padding_value=1.0						)

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

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



      def 		UpperCamelCase_						(     self :							List[Any]						)       ->	Any:
        _snake_case		=				self.get_feature_extractor()
        _snake_case		=				self.get_tokenizer()

        _snake_case		=				WhisperProcessor(tokenizer=A__     ,   feature_extractor=A__						)

        _snake_case		=				floats_list((3, 1000)						)

        _snake_case		=				feature_extractor(A__     ,   return_tensors='''np'''						)
        _snake_case		=				processor(A__     ,   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						)       ->	Any:
        _snake_case		=				self.get_feature_extractor()
        _snake_case		=				self.get_tokenizer()

        _snake_case		=				WhisperProcessor(tokenizer=A__     ,   feature_extractor=A__						)

        _snake_case		=				'''This is a test string'''

        _snake_case		=				processor(text=A__						)

        _snake_case		=				tokenizer(A__						)

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



      def 		UpperCamelCase_						(     self :							List[str]						)       ->	Optional[int]:
        _snake_case		=				self.get_feature_extractor()
        _snake_case		=				self.get_tokenizer()

        _snake_case		=				WhisperProcessor(tokenizer=A__     ,   feature_extractor=A__						)

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

        _snake_case		=				processor.batch_decode(A__						)
        _snake_case		=				tokenizer.batch_decode(A__						)

        self.assertListEqual(A__     ,   A__						)



      def 		UpperCamelCase_						(     self :							Optional[Any]						)       ->	Any:
        _snake_case		=				self.get_feature_extractor()
        _snake_case		=				self.get_tokenizer()

        _snake_case		=				WhisperProcessor(tokenizer=A__     ,   feature_extractor=A__						)

        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 :							Optional[Any]						)       ->	Union[str, Any]:
        _snake_case		=				self.get_feature_extractor()
        _snake_case		=				self.get_tokenizer()

        _snake_case		=				WhisperProcessor(tokenizer=A__     ,   feature_extractor=A__						)
        _snake_case		=				processor.get_decoder_prompt_ids(task='''transcribe'''     ,   no_timestamps=A__						)

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

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



      def 		UpperCamelCase_						(     self :							Any						)       ->	int:
        _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(A__						)

        self.assertListEqual(prompt_ids.tolist()     ,   [50360, 1770, 13, 2264, 346, 353]						)
        self.assertEqual(A__     ,   '''<|startofprev|> Mr. Quilter'''						)



      def 		UpperCamelCase_						(     self :							Tuple						)       ->	List[str]:
        _snake_case		=				WhisperProcessor(tokenizer=self.get_tokenizer()     ,   feature_extractor=self.get_feature_extractor()						)
        _snake_case		=				processor.get_prompt_ids(''''''						)
        _snake_case		=				processor.tokenizer.decode(A__						)

        self.assertListEqual(prompt_ids.tolist()     ,   [50360, 220]						)
        self.assertEqual(A__     ,   '''<|startofprev|> '''						)







      def 		UpperCamelCase_						(     self :							Optional[Any]						)       ->	Tuple:
        _snake_case		=				WhisperProcessor(tokenizer=self.get_tokenizer()     ,   feature_extractor=self.get_feature_extractor()						)

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

from ...utils import logging
from .image_processing_detr import DetrImageProcessor


__A     						=      logging.get_logger(__name__)



class     lowercase_       (    __lowercase	):



      def __init__(     self :							List[str]     ,   *A__ :							Tuple     ,   **A__ :							int						)       ->	None:
        warnings.warn(
            '''The class DetrFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'''
            ''' Please use DetrImageProcessor instead.'''     ,   A__     ,   )
        super().__init__(*A__     ,   **A__						)

