


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


lowercase_           =	5_0_3_5_8
lowercase_           =	5_0_3_6_2


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




      '''simple docstring'''


      def      _snake_case	(				self:       Any				):
       __lowerCamelCase		:							Optional[Any]							=						'openai/whisper-small.en'
       __lowerCamelCase		:							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]				):
       __lowerCamelCase		:							str							=						self.get_tokenizer()
       __lowerCamelCase		:							Tuple							=						self.get_feature_extractor()

       __lowerCamelCase		:							Optional[Any]							=						WhisperProcessor(tokenizer=a						,      feature_extractor=a				)

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

       __lowerCamelCase		:							int							=						self.get_tokenizer(bos_token='(BOS)'						,      eos_token='(EOS)'				)
       __lowerCamelCase		:							Dict							=						self.get_feature_extractor(do_normalize=a						,      padding_value=1.0				)

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

       __lowerCamelCase		:							str							=						WhisperProcessor(tokenizer=a						,      feature_extractor=a				)

       __lowerCamelCase		:							List[str]							=						floats_list((3, 1000)				)

       __lowerCamelCase		:							Union[str, Any]							=						feature_extractor(a						,      return_tensors='np'				)
       __lowerCamelCase		:							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				):
       __lowerCamelCase		:							Tuple							=						self.get_feature_extractor()
       __lowerCamelCase		:							List[str]							=						self.get_tokenizer()

       __lowerCamelCase		:							Optional[Any]							=						WhisperProcessor(tokenizer=a						,      feature_extractor=a				)

       __lowerCamelCase		:							Any							=						'This is a test string'

       __lowerCamelCase		:							List[Any]							=						processor(text=a				)

       __lowerCamelCase		:							str							=						tokenizer(a				)

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




      def      _snake_case	(				self:       str				):
       __lowerCamelCase		:							Union[str, Any]							=						self.get_feature_extractor()
       __lowerCamelCase		:							Any							=						self.get_tokenizer()

       __lowerCamelCase		:							Dict							=						WhisperProcessor(tokenizer=a						,      feature_extractor=a				)

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

       __lowerCamelCase		:							Union[str, Any]							=						processor.batch_decode(a				)
       __lowerCamelCase		:							Union[str, Any]							=						tokenizer.batch_decode(a				)

       self.assertListEqual(a						,      a				)




      def      _snake_case	(				self:       List[str]				):
       __lowerCamelCase		:							List[str]							=						self.get_feature_extractor()
       __lowerCamelCase		:							Optional[Any]							=						self.get_tokenizer()

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

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

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




      def      _snake_case	(				self:       List[Any]				):
       __lowerCamelCase		:							Optional[Any]							=						WhisperProcessor(tokenizer=self.get_tokenizer()						,      feature_extractor=self.get_feature_extractor()				)
       __lowerCamelCase		:							Optional[int]							=						processor.get_prompt_ids('Mr. Quilter'				)
       __lowerCamelCase		:							List[str]							=						processor.tokenizer.decode(a				)

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




      def      _snake_case	(				self:       Any				):
       __lowerCamelCase		:							str							=						WhisperProcessor(tokenizer=self.get_tokenizer()						,      feature_extractor=self.get_feature_extractor()				)
       __lowerCamelCase		:							List[str]							=						processor.get_prompt_ids(''				)
       __lowerCamelCase		:							List[str]							=						processor.tokenizer.decode(a				)

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




      def      _snake_case	(				self:       List[Any]				):
       __lowerCamelCase		:							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				)
        __lowerCamelCase		:							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 doctest

import projectq
from projectq.ops import H, Measure
def       UpperCamelCase__     (  SCREAMING_SNAKE_CASE__						):
 __lowerCamelCase		:							int							=						quantum_engine.allocate_qubit()
 H | qubit
 Measure | qubit
 return int(SCREAMING_SNAKE_CASE__						)


if __name__ == "__main__":
       doctest.testmod()

       # initialises a new quantum backend
       lowercase_           =	projectq.MainEngine()

       # Generate a list of 10 random numbers
       lowercase_           =	[get_random_number(quantum_engine) for _ in range(1_0)]

       # Flushes the quantum engine from memory
       quantum_engine.flush()

       print('Random numbers', random_numbers_list)

