






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


snake_case   :   Dict    		=		5_0_3_5_8
snake_case   :   Optional[Any]    		=		5_0_3_6_2



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







    """simple docstring"""







    def 							a__ (		self   :  Optional[Any]   ):





           '''simple docstring'''

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







    def 							a__ (		self   :  int							,						**UpperCamelCase_   :  Optional[int]   ):





           '''simple docstring'''

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







    def 							a__ (		self   :  str							,						**UpperCamelCase_   :  Tuple   ):





           '''simple docstring'''

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







    def 							a__ (		self   :  str   ):





           '''simple docstring'''

           shutil.rmtree(self.tmpdirname   )







    def 							a__ (		self   :  str   ):





           '''simple docstring'''

           __magic_name__										=						self.get_tokenizer()
           __magic_name__										=						self.get_feature_extractor()

           __magic_name__										=						WhisperProcessor(tokenizer=UpperCamelCase_							,						feature_extractor=UpperCamelCase_   )

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

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

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







    def 							a__ (		self   :  int   ):





           '''simple docstring'''

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

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

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

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

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







    def 							a__ (		self   :  List[Any]   ):





           '''simple docstring'''

           __magic_name__										=						self.get_feature_extractor()
           __magic_name__										=						self.get_tokenizer()

           __magic_name__										=						WhisperProcessor(tokenizer=UpperCamelCase_							,						feature_extractor=UpperCamelCase_   )

           __magic_name__										=						floats_list((3, 1_0_0_0)   )

           __magic_name__										=						feature_extractor(UpperCamelCase_							,						return_tensors='np'   )
           __magic_name__										=						processor(UpperCamelCase_							,						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   :  int   ):





           '''simple docstring'''

           __magic_name__										=						self.get_feature_extractor()
           __magic_name__										=						self.get_tokenizer()

           __magic_name__										=						WhisperProcessor(tokenizer=UpperCamelCase_							,						feature_extractor=UpperCamelCase_   )

           __magic_name__										=						'This is a test string'

           __magic_name__										=						processor(text=UpperCamelCase_   )

           __magic_name__										=						tokenizer(UpperCamelCase_   )

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







    def 							a__ (		self   :  Tuple   ):





           '''simple docstring'''

           __magic_name__										=						self.get_feature_extractor()
           __magic_name__										=						self.get_tokenizer()

           __magic_name__										=						WhisperProcessor(tokenizer=UpperCamelCase_							,						feature_extractor=UpperCamelCase_   )

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

           __magic_name__										=						processor.batch_decode(UpperCamelCase_   )
           __magic_name__										=						tokenizer.batch_decode(UpperCamelCase_   )

           self.assertListEqual(UpperCamelCase_							,						UpperCamelCase_   )







    def 							a__ (		self   :  Dict   ):





           '''simple docstring'''

           __magic_name__										=						self.get_feature_extractor()
           __magic_name__										=						self.get_tokenizer()

           __magic_name__										=						WhisperProcessor(tokenizer=UpperCamelCase_							,						feature_extractor=UpperCamelCase_   )

           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   :  Optional[Any]   ):





           '''simple docstring'''

           __magic_name__										=						self.get_feature_extractor()
           __magic_name__										=						self.get_tokenizer()

           __magic_name__										=						WhisperProcessor(tokenizer=UpperCamelCase_							,						feature_extractor=UpperCamelCase_   )
           __magic_name__										=						processor.get_decoder_prompt_ids(task='transcribe'							,						no_timestamps=UpperCamelCase_   )

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

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







    def 							a__ (		self   :  Union[str, Any]   ):





           '''simple docstring'''

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

           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(UpperCamelCase_							,						'<|startofprev|> Mr. Quilter'   )







    def 							a__ (		self   :  List[Any]   ):





           '''simple docstring'''

           __magic_name__										=						WhisperProcessor(tokenizer=self.get_tokenizer()							,						feature_extractor=self.get_feature_extractor()   )
           __magic_name__										=						processor.get_prompt_ids(''   )
           __magic_name__										=						processor.tokenizer.decode(UpperCamelCase_   )

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







    def 							a__ (		self   :  Union[str, Any]   ):





           '''simple docstring'''

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

           def _test_prompt_error_raised_helper(UpperCamelCase_   :  Dict							,						UpperCamelCase_   :  str   ):
                  with pytest.raises(UpperCamelCase_   ) as excinfo:
                         processor.get_prompt_ids(UpperCamelCase_   )
                  __magic_name__										=						f"""Encountered text in the prompt corresponding to disallowed special token: {special_token}."""
                  self.assertEqual(UpperCamelCase_							,						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 numpy as np


def        A	(							__snake_case:   Any   ,    __snake_case:   List[str]   ,    __snake_case:   Tuple     )						->		Dict:

       """simple docstring"""





       __magic_name__										=						n_draws * class_counts / class_counts.sum()
       # floored means we don't overshoot n_samples, but probably undershoot
       __magic_name__										=						np.floor(__snake_case     )
       # we add samples according to how much "left over" probability
       # they had, until we arrive at n_samples
       __magic_name__										=						int(n_draws - floored.sum()     )
       if need_to_add > 0:
              __magic_name__										=						continuous - floored
              __magic_name__										=						np.sort(np.unique(__snake_case     )     )[::-1]
              # add according to remainder, but break ties
              # randomly to avoid biases
              for value in values:
                     ((__magic_name__)							,     )										=						np.where(remainder == value     )
                     # if we need_to_add less than what's in inds
                     # we draw randomly from them.
                     # if we need to add more, we add them all and
                     # go to the next value
                     __magic_name__										=						min(len(__snake_case     )   ,    __snake_case     )
                     __magic_name__										=						rng.choice(__snake_case   ,    size=__snake_case   ,    replace=__snake_case     )
                     floored[inds] += 1
                     need_to_add -= add_now
                     if need_to_add == 0:
                            break
       return floored.astype(np.intaa     )






def        A	(							__snake_case:   Any   ,    __snake_case:   str   ,    __snake_case:   int   ,    __snake_case:   Optional[Any]   ,    __snake_case:   Any=1_0     )						->		Any:

       """simple docstring"""





       __magic_name__							,     __magic_name__										=						np.unique(__snake_case   ,    return_inverse=__snake_case     )
       __magic_name__										=						classes.shape[0]
       __magic_name__										=						np.bincount(__snake_case     )
       if np.min(__snake_case     ) < 2:
              raise ValueError('Minimum class count error'     )
       if n_train < n_classes:
              raise ValueError(
                  'The train_size = %d should be greater or ' 'equal to the number of classes = %d' % (n_train, n_classes)     )
       if n_test < n_classes:
              raise ValueError(
                  'The test_size = %d should be greater or ' 'equal to the number of classes = %d' % (n_test, n_classes)     )
       __magic_name__										=						np.split(np.argsort(__snake_case   ,    kind='mergesort'     )   ,    np.cumsum(__snake_case     )[:-1]     )
       for _ in range(__snake_case     ):
              __magic_name__										=						approximate_mode(__snake_case   ,    __snake_case   ,    __snake_case     )
              __magic_name__										=						class_counts - n_i
              __magic_name__										=						approximate_mode(__snake_case   ,    __snake_case   ,    __snake_case     )

              __magic_name__										=						[]
              __magic_name__										=						[]

              for i in range(__snake_case     ):
                     __magic_name__										=						rng.permutation(class_counts[i]     )
                     __magic_name__										=						class_indices[i].take(__snake_case   ,    mode='clip'     )
                     train.extend(perm_indices_class_i[: n_i[i]]     )
                     test.extend(perm_indices_class_i[n_i[i] : n_i[i] + t_i[i]]     )
              __magic_name__										=						rng.permutation(__snake_case     )
              __magic_name__										=						rng.permutation(__snake_case     )

              yield train, test