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


__magic_name__    				=					50_358
__magic_name__    				=					50_362

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







 """simple docstring"""


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




    '''simple docstring'''






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


 def        _lowerCAmelCase				(   self		:      Dict					,   **_snake_case		:      Optional[Any]				)				->			Any:




    '''simple docstring'''






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


 def        _lowerCAmelCase				(   self		:      Union[str, Any]					,   **_snake_case		:      str				)				->			Dict:




    '''simple docstring'''






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


 def        _lowerCAmelCase				(   self		:      Dict				)				->			Optional[Any]:




    '''simple docstring'''






    shutil.rmtree(self.tmpdirname				)


 def        _lowerCAmelCase				(   self		:      Dict				)				->			Tuple:




    '''simple docstring'''






    a__     				=		self.get_tokenizer()
    a__     				=		self.get_feature_extractor()

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

    processor.save_pretrained(self.tmpdirname				)
    a__     				=		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        _lowerCAmelCase				(   self		:      Union[str, Any]				)				->			Union[str, Any]:




    '''simple docstring'''






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

    a__     				=		self.get_tokenizer(bos_token='(BOS)'					,   eos_token='(EOS)'				)
    a__     				=		self.get_feature_extractor(do_normalize=_snake_case					,   padding_value=1.0				)

    a__     				=		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        _lowerCAmelCase				(   self		:      Optional[Any]				)				->			List[str]:




    '''simple docstring'''






    a__     				=		self.get_feature_extractor()
    a__     				=		self.get_tokenizer()

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

    a__     				=		floats_list((3, 1000)				)

    a__     				=		feature_extractor(_snake_case					,   return_tensors='np'				)
    a__     				=		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        _lowerCAmelCase				(   self		:      Dict				)				->			Tuple:




    '''simple docstring'''






    a__     				=		self.get_feature_extractor()
    a__     				=		self.get_tokenizer()

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

    a__     				=		'This is a test string'

    a__     				=		processor(text=_snake_case				)

    a__     				=		tokenizer(_snake_case				)

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


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




    '''simple docstring'''






    a__     				=		self.get_feature_extractor()
    a__     				=		self.get_tokenizer()

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

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

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

    self.assertListEqual(_snake_case					,   _snake_case				)


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




    '''simple docstring'''






    a__     				=		self.get_feature_extractor()
    a__     				=		self.get_tokenizer()

    a__     				=		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        _lowerCAmelCase				(   self		:      List[str]				)				->			Union[str, Any]:




    '''simple docstring'''






    a__     				=		self.get_feature_extractor()
    a__     				=		self.get_tokenizer()

    a__     				=		WhisperProcessor(tokenizer=_snake_case					,   feature_extractor=_snake_case				)
    a__     				=		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)				)

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


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




    '''simple docstring'''






    a__     				=		WhisperProcessor(tokenizer=self.get_tokenizer()					,   feature_extractor=self.get_feature_extractor()				)
    a__     				=		processor.get_prompt_ids('Mr. Quilter'				)
    a__     				=		processor.tokenizer.decode(_snake_case				)

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


 def        _lowerCAmelCase				(   self		:      List[str]				)				->			int:




    '''simple docstring'''






    a__     				=		WhisperProcessor(tokenizer=self.get_tokenizer()					,   feature_extractor=self.get_feature_extractor()				)
    a__     				=		processor.get_prompt_ids(''				)
    a__     				=		processor.tokenizer.decode(_snake_case				)

    self.assertListEqual(prompt_ids.tolist()					,   [5_0360, 220]				)
    self.assertEqual(_snake_case					,   '<|startofprev|> '				)



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




    '''simple docstring'''






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

    def _test_prompt_error_raised_helper(_snake_case		:      Optional[Any]					,   _snake_case		:      Any				):
       with pytest.raises(_snake_case				) as excinfo:
          processor.get_prompt_ids(_snake_case				)
       a__     				=		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"""



import argparse

from transformers import CanineConfig, CanineModel, CanineTokenizer, load_tf_weights_in_canine
from transformers.utils import logging


logging.set_verbosity_info()





def   _lowerCamelCase ( UpperCAmelCase__,UpperCAmelCase__					)							->     Optional[int]:


   '''simple docstring'''


   a__     				=		CanineConfig()
   a__     				=		CanineModel(UpperCAmelCase__					)
   model.eval()

   print(f'''Building PyTorch model from configuration: {config}'''					)

   # Load weights from tf checkpoint
   load_tf_weights_in_canine(UpperCAmelCase__,UpperCAmelCase__,UpperCAmelCase__					)

   # Save pytorch-model (weights and configuration)
   print(f'''Save PyTorch model to {pytorch_dump_path}'''					)
   model.save_pretrained(UpperCAmelCase__					)

   # Save tokenizer files
   a__     				=		CanineTokenizer()
   print(f'''Save tokenizer files to {pytorch_dump_path}'''					)
   tokenizer.save_pretrained(UpperCAmelCase__					)


if __name__ == "__main__":
     __magic_name__    				=					argparse.ArgumentParser()
     # Required parameters
     parser.add_argument(
         "--tf_checkpoint_path",
         default=None,
         type=str,
         required=True,
         help="Path to the TensorFlow checkpoint. Should end with model.ckpt",
     )
     parser.add_argument(
         "--pytorch_dump_path",
         default=None,
         type=str,
         required=True,
         help="Path to a folder where the PyTorch model will be placed.",
     )
     __magic_name__    				=					parser.parse_args()
     convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.pytorch_dump_path)


