



"""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


_lowerCAmelCase				:					Dict							=							5_03_58
_lowerCAmelCase				:					Any							=							5_03_62






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



      def 						snake_case_					(     self     :	Any						):
           _UpperCAmelCase			:	Optional[Any]								=    'openai/whisper-small.en'
           _UpperCAmelCase			:	int								=    tempfile.mkdtemp()



      def 						snake_case_					(     self     :	Dict     ,	**A     :	List[str]						):
           return WhisperTokenizer.from_pretrained(self.checkpoint     ,	**A						)



      def 						snake_case_					(     self     :	Any     ,	**A     :	Union[str, Any]						):
           return WhisperFeatureExtractor.from_pretrained(self.checkpoint     ,	**A						)



      def 						snake_case_					(     self     :	Union[str, Any]						):
           shutil.rmtree(self.tmpdirname						)



      def 						snake_case_					(     self     :	Optional[Any]						):
           _UpperCAmelCase			:	str								=    self.get_tokenizer()
           _UpperCAmelCase			:	Tuple								=    self.get_feature_extractor()

           _UpperCAmelCase			:	Optional[Any]								=    WhisperProcessor(tokenizer=A     ,	feature_extractor=A						)

           processor.save_pretrained(self.tmpdirname						)
           _UpperCAmelCase			:	str								=    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 						snake_case_					(     self     :	Tuple						):
           _UpperCAmelCase			:	Optional[Any]								=    WhisperProcessor(tokenizer=self.get_tokenizer()     ,	feature_extractor=self.get_feature_extractor()						)
           processor.save_pretrained(self.tmpdirname						)

           _UpperCAmelCase			:	int								=    self.get_tokenizer(bos_token="(BOS)"     ,	eos_token="(EOS)"						)
           _UpperCAmelCase			:	Dict								=    self.get_feature_extractor(do_normalize=A     ,	padding_value=1.0						)

           _UpperCAmelCase			:	str								=    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 						snake_case_					(     self     :	List[str]						):
           _UpperCAmelCase			:	Optional[Any]								=    self.get_feature_extractor()
           _UpperCAmelCase			:	Optional[Any]								=    self.get_tokenizer()

           _UpperCAmelCase			:	str								=    WhisperProcessor(tokenizer=A     ,	feature_extractor=A						)

           _UpperCAmelCase			:	List[str]								=    floats_list((3, 1_0_0_0)						)

           _UpperCAmelCase			:	Union[str, Any]								=    feature_extractor(A     ,	return_tensors="np"						)
           _UpperCAmelCase			:	Any								=    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 						snake_case_					(     self     :	Dict						):
           _UpperCAmelCase			:	Tuple								=    self.get_feature_extractor()
           _UpperCAmelCase			:	List[str]								=    self.get_tokenizer()

           _UpperCAmelCase			:	Optional[Any]								=    WhisperProcessor(tokenizer=A     ,	feature_extractor=A						)

           _UpperCAmelCase			:	Any								=    'This is a test string'

           _UpperCAmelCase			:	List[Any]								=    processor(text=A						)

           _UpperCAmelCase			:	str								=    tokenizer(A						)

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



      def 						snake_case_					(     self     :	str						):
           _UpperCAmelCase			:	Union[str, Any]								=    self.get_feature_extractor()
           _UpperCAmelCase			:	Any								=    self.get_tokenizer()

           _UpperCAmelCase			:	Dict								=    WhisperProcessor(tokenizer=A     ,	feature_extractor=A						)

           _UpperCAmelCase			:	Optional[Any]								=    [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

           _UpperCAmelCase			:	Union[str, Any]								=    processor.batch_decode(A						)
           _UpperCAmelCase			:	Union[str, Any]								=    tokenizer.batch_decode(A						)

           self.assertListEqual(A     ,	A						)



      def 						snake_case_					(     self     :	List[str]						):
           _UpperCAmelCase			:	List[str]								=    self.get_feature_extractor()
           _UpperCAmelCase			:	Optional[Any]								=    self.get_tokenizer()

           _UpperCAmelCase			:	int								=    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 						snake_case_					(     self     :	int						):
           _UpperCAmelCase			:	List[Any]								=    self.get_feature_extractor()
           _UpperCAmelCase			:	Optional[int]								=    self.get_tokenizer()

           _UpperCAmelCase			:	Union[str, Any]								=    WhisperProcessor(tokenizer=A     ,	feature_extractor=A						)
           _UpperCAmelCase			:	Tuple								=    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)						)

           _UpperCAmelCase			:	Tuple								=    [TRANSCRIBE, NOTIMESTAMPS]
           self.assertListEqual([ids[-1] for ids in forced_decoder_ids]     ,	A						)



      def 						snake_case_					(     self     :	List[Any]						):
           _UpperCAmelCase			:	Optional[Any]								=    WhisperProcessor(tokenizer=self.get_tokenizer()     ,	feature_extractor=self.get_feature_extractor()						)
           _UpperCAmelCase			:	Optional[int]								=    processor.get_prompt_ids("Mr. Quilter"						)
           _UpperCAmelCase			:	List[str]								=    processor.tokenizer.decode(A						)

           self.assertListEqual(prompt_ids.tolist()     ,	[5_0_3_6_0, 1_7_7_0, 1_3, 2_2_6_4, 3_4_6, 3_5_3]						)
           self.assertEqual(A     ,	"<|startofprev|> Mr. Quilter"						)



      def 						snake_case_					(     self     :	Any						):
           _UpperCAmelCase			:	str								=    WhisperProcessor(tokenizer=self.get_tokenizer()     ,	feature_extractor=self.get_feature_extractor()						)
           _UpperCAmelCase			:	List[str]								=    processor.get_prompt_ids(""						)
           _UpperCAmelCase			:	List[str]								=    processor.tokenizer.decode(A						)

           self.assertListEqual(prompt_ids.tolist()     ,	[5_0_3_6_0, 2_2_0]						)
           self.assertEqual(A     ,	"<|startofprev|> "						)



      def 						snake_case_					(     self     :	List[Any]						):
           _UpperCAmelCase			:	Union[str, Any]								=    WhisperProcessor(tokenizer=self.get_tokenizer()     ,	feature_extractor=self.get_feature_extractor()						)

           def _test_prompt_error_raised_helper(A     :	List[Any]     ,	A     :	int						):
                with pytest.raises(A						) as excinfo:
                     processor.get_prompt_ids(A						)
                _UpperCAmelCase			:	Dict								=    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 unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
       import torch

       from transformers import CamembertModel


@require_torch
@require_sentencepiece
@require_tokenizers
class        A_					(       unittest.TestCase    ):




      '''simple docstring'''


      @slow
      def      _snake_case	(				self:       Any				):
       __lowerCamelCase		:							Dict							=						CamembertModel.from_pretrained('camembert-base'				)
       model.to(a				)

       __lowerCamelCase		:							List[Any]							=						torch.tensor(
           [[5, 121, 11, 660, 16, 730, 2_5543, 110, 83, 6]]						,      device=a						,      dtype=torch.long						,      )  # J'aime le camembert !
       with torch.no_grad():
        __lowerCamelCase		:							Tuple							=						model(a				)['last_hidden_state']
       __lowerCamelCase		:							Union[str, Any]							=						torch.Size((1, 10, 768)				)
       self.assertEqual(output.shape						,      a				)
       # compare the actual values for a slice.
       __lowerCamelCase		:							Tuple							=						torch.tensor(
           [[[-0.0_2_5_4, 0.0_2_3_5, 0.1_0_2_7], [0.0_6_0_6, -0.1_8_1_1, -0.0_4_1_8], [-0.1_5_6_1, -0.1_1_2_7, 0.2_6_8_7]]]						,      device=a						,      dtype=torch.float						,      )
       # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
       # camembert.eval()
       # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()

       self.assertTrue(torch.allclose(output[:, :3, :3]						,      a						,      atol=1e-4				)				)

