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


_a									=				50_358
_a									=				50_362


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




  '''simple docstring'''







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   List[Any]     	=       "openai/whisper-small.en"
     UpperCAmelCase_   :   List[Any]     	=       tempfile.mkdtemp()







  def 			UpperCamelCase__   (						self							,  **lowercase_    ):

     """simple docstring"""
     return WhisperTokenizer.from_pretrained(self.checkpoint							,  **lowercase_    )







  def 			UpperCamelCase__   (						self							,  **lowercase_    ):

     """simple docstring"""
     return WhisperFeatureExtractor.from_pretrained(self.checkpoint							,  **lowercase_    )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     shutil.rmtree(self.tmpdirname    )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   str     	=       self.get_tokenizer()
     UpperCAmelCase_   :   Tuple     	=       self.get_feature_extractor()

     UpperCAmelCase_   :   List[str]     	=       WhisperProcessor(tokenizer=lowercase_							,  feature_extractor=lowercase_    )

     processor.save_pretrained(self.tmpdirname    )
     UpperCAmelCase_   :   int     	=       WhisperProcessor.from_pretrained(self.tmpdirname    )

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

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







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Union[str, Any]     	=       WhisperProcessor(tokenizer=self.get_tokenizer()							,  feature_extractor=self.get_feature_extractor()    )
     processor.save_pretrained(self.tmpdirname    )

     UpperCAmelCase_   :   Optional[Any]     	=       self.get_tokenizer(bos_token="(BOS)"							,  eos_token="(EOS)"    )
     UpperCAmelCase_   :   Optional[int]     	=       self.get_feature_extractor(do_normalize=lowercase_							,  padding_value=1.0    )

     UpperCAmelCase_   :   List[str]     	=       WhisperProcessor.from_pretrained(
         self.tmpdirname							,  bos_token="(BOS)"							,  eos_token="(EOS)"							,  do_normalize=lowercase_							,  padding_value=1.0    )

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

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







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   str     	=       self.get_feature_extractor()
     UpperCAmelCase_   :   Any     	=       self.get_tokenizer()

     UpperCAmelCase_   :   Dict     	=       WhisperProcessor(tokenizer=lowercase_							,  feature_extractor=lowercase_    )

     UpperCAmelCase_   :   Dict     	=       floats_list((3, 1000)    )

     UpperCAmelCase_   :   str     	=       feature_extractor(lowercase_							,  return_tensors="np"    )
     UpperCAmelCase_   :   Optional[int]     	=       processor(lowercase_							,  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    ):

     """simple docstring"""
     UpperCAmelCase_   :   Dict     	=       self.get_feature_extractor()
     UpperCAmelCase_   :   List[Any]     	=       self.get_tokenizer()

     UpperCAmelCase_   :   List[Any]     	=       WhisperProcessor(tokenizer=lowercase_							,  feature_extractor=lowercase_    )

     UpperCAmelCase_   :   List[str]     	=       "This is a test string"

     UpperCAmelCase_   :   List[str]     	=       processor(text=lowercase_    )

     UpperCAmelCase_   :   Any     	=       tokenizer(lowercase_    )

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







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Any     	=       self.get_feature_extractor()
     UpperCAmelCase_   :   List[Any]     	=       self.get_tokenizer()

     UpperCAmelCase_   :   List[str]     	=       WhisperProcessor(tokenizer=lowercase_							,  feature_extractor=lowercase_    )

     UpperCAmelCase_   :   int     	=       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

     UpperCAmelCase_   :   List[str]     	=       processor.batch_decode(lowercase_    )
     UpperCAmelCase_   :   Optional[int]     	=       tokenizer.batch_decode(lowercase_    )

     self.assertListEqual(lowercase_							,  lowercase_    )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Optional[int]     	=       self.get_feature_extractor()
     UpperCAmelCase_   :   Optional[Any]     	=       self.get_tokenizer()

     UpperCAmelCase_   :   Union[str, Any]     	=       WhisperProcessor(tokenizer=lowercase_							,  feature_extractor=lowercase_    )

     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    ):

     """simple docstring"""
     UpperCAmelCase_   :   Union[str, Any]     	=       self.get_feature_extractor()
     UpperCAmelCase_   :   List[Any]     	=       self.get_tokenizer()

     UpperCAmelCase_   :   Tuple     	=       WhisperProcessor(tokenizer=lowercase_							,  feature_extractor=lowercase_    )
     UpperCAmelCase_   :   Union[str, Any]     	=       processor.get_decoder_prompt_ids(task="transcribe"							,  no_timestamps=lowercase_    )

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

     UpperCAmelCase_   :   Optional[int]     	=       [TRANSCRIBE, NOTIMESTAMPS]
     self.assertListEqual([ids[-1] for ids in forced_decoder_ids]							,  lowercase_    )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Optional[int]     	=       WhisperProcessor(tokenizer=self.get_tokenizer()							,  feature_extractor=self.get_feature_extractor()    )
     UpperCAmelCase_   :   Tuple     	=       processor.get_prompt_ids("Mr. Quilter"    )
     UpperCAmelCase_   :   Optional[Any]     	=       processor.tokenizer.decode(lowercase_    )

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







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Optional[Any]     	=       WhisperProcessor(tokenizer=self.get_tokenizer()							,  feature_extractor=self.get_feature_extractor()    )
     UpperCAmelCase_   :   Union[str, Any]     	=       processor.get_prompt_ids(""    )
     UpperCAmelCase_   :   List[str]     	=       processor.tokenizer.decode(lowercase_    )

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







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Dict     	=       WhisperProcessor(tokenizer=self.get_tokenizer()							,  feature_extractor=self.get_feature_extractor()    )

     def _test_prompt_error_raised_helper(lowercase_							,  lowercase_    ):
        with pytest.raises(lowercase_    ) as excinfo:
           processor.get_prompt_ids(lowercase_    )
        UpperCAmelCase_   :   Optional[int]     	=       F"""Encountered text in the prompt corresponding to disallowed special token: {special_token}."""
        self.assertEqual(lowercase_							,  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"""





class A_      :




  '''simple docstring'''







  def __init__(						self							,  lowercase_    ):

     """simple docstring"""
     UpperCAmelCase_   :   str     	=       data
     UpperCAmelCase_   :   str     	=       None
     UpperCAmelCase_   :   List[Any]     	=       None







  def __str__(						self    ):

     """simple docstring"""
     return F"""{self.data}"""




class A_      :




  '''simple docstring'''







  def __init__(						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Tuple     	=       None
     UpperCAmelCase_   :   List[str]     	=       None







  def __iter__(						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Optional[int]     	=       self.head
     while node:
        yield node.data
        UpperCAmelCase_   :   Optional[Any]     	=       node.next







  def __str__(						self    ):

     """simple docstring"""
     return "->".join([str(lowercase_    ) for item in self]    )







  def __len__(						self    ):

     """simple docstring"""
     return sum(1 for _ in self    )







  def 			UpperCamelCase__   (						self							,  lowercase_    ):

     """simple docstring"""
     self.insert_at_nth(0							,  lowercase_    )







  def 			UpperCamelCase__   (						self							,  lowercase_    ):

     """simple docstring"""
     self.insert_at_nth(len(self    )							,  lowercase_    )







  def 			UpperCamelCase__   (						self							,  lowercase_							,  lowercase_    ):

     """simple docstring"""
     UpperCAmelCase_   :   int     	=       len(self    )

     if not 0 <= index <= length:
        raise IndexError("list index out of range"    )
     UpperCAmelCase_   :   Tuple     	=       Node(lowercase_    )
     if self.head is None:
        UpperCAmelCase_   :   List[str]     	=       new_node
     elif index == 0:
        UpperCAmelCase_   :   int     	=       new_node
        UpperCAmelCase_   :   Optional[Any]     	=       self.head
        UpperCAmelCase_   :   Optional[int]     	=       new_node
     elif index == length:
        UpperCAmelCase_   :   Union[str, Any]     	=       new_node
        UpperCAmelCase_   :   Optional[Any]     	=       self.tail
        UpperCAmelCase_   :   Tuple     	=       new_node
     else:
        UpperCAmelCase_   :   Dict     	=       self.head
        for _ in range(0							,  lowercase_    ):
           UpperCAmelCase_   :   str     	=       temp.next
        UpperCAmelCase_   :   int     	=       new_node
        UpperCAmelCase_   :   Tuple     	=       temp.previous
        UpperCAmelCase_   :   Union[str, Any]     	=       temp
        UpperCAmelCase_   :   int     	=       new_node







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return self.delete_at_nth(0    )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return self.delete_at_nth(len(self    ) - 1    )







  def 			UpperCamelCase__   (						self							,  lowercase_    ):

     """simple docstring"""
     UpperCAmelCase_   :   int     	=       len(self    )

     if not 0 <= index <= length - 1:
        raise IndexError("list index out of range"    )
     UpperCAmelCase_   :   Optional[int]     	=       self.head  # default first node
     if length == 1:
        UpperCAmelCase_   :   Union[str, Any]     	=       None
     elif index == 0:
        UpperCAmelCase_   :   List[Any]     	=       self.head.next
        UpperCAmelCase_   :   str     	=       None
     elif index == length - 1:
        UpperCAmelCase_   :   str     	=       self.tail
        UpperCAmelCase_   :   Optional[Any]     	=       self.tail.previous
        UpperCAmelCase_   :   Any     	=       None
     else:
        UpperCAmelCase_   :   List[Any]     	=       self.head
        for _ in range(0							,  lowercase_    ):
           UpperCAmelCase_   :   str     	=       temp.next
        UpperCAmelCase_   :   Dict     	=       temp
        UpperCAmelCase_   :   Optional[Any]     	=       temp.previous
        UpperCAmelCase_   :   int     	=       temp.next
     return delete_node.data







  def 			UpperCamelCase__   (						self							,  lowercase_    ):

     """simple docstring"""
     UpperCAmelCase_   :   Tuple     	=       self.head

     while current.data != data:  # Find the position to delete
        if current.next:
           UpperCAmelCase_   :   List[str]     	=       current.next
        else:  # We have reached the end an no value matches
           raise ValueError("No data matching given value"    )

     if current == self.head:
        self.delete_head()

     elif current == self.tail:
        self.delete_tail()

     else:  # Before: 1 <--> 2(current) <--> 3
        UpperCAmelCase_   :   str     	=       current.next  # 1 --> 3
        UpperCAmelCase_   :   Optional[Any]     	=       current.previous  # 1 <--> 3
     return data







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return len(self    ) == 0







def       __a	(						):
   UpperCAmelCase_   :   Dict     	=       DoublyLinkedList()
   assert linked_list.is_empty() is True
   assert str(__lowerCamelCase    ) == ""

   try:
      linked_list.delete_head()
      raise AssertionError  # This should not happen.
   except IndexError:
      assert True  # This should happen.

   try:
      linked_list.delete_tail()
      raise AssertionError  # This should not happen.
   except IndexError:
      assert True  # This should happen.

   for i in range(10    ):
      assert len(__lowerCamelCase    ) == i
      linked_list.insert_at_nth(__lowerCamelCase,						i + 1    )
   assert str(__lowerCamelCase    ) == "->".join(str(__lowerCamelCase    ) for i in range(1,						11    )    )

   linked_list.insert_at_head(0    )
   linked_list.insert_at_tail(11    )
   assert str(__lowerCamelCase    ) == "->".join(str(__lowerCamelCase    ) for i in range(0,						12    )    )

   assert linked_list.delete_head() == 0
   assert linked_list.delete_at_nth(9    ) == 10
   assert linked_list.delete_tail() == 11
   assert len(__lowerCamelCase    ) == 9
   assert str(__lowerCamelCase    ) == "->".join(str(__lowerCamelCase    ) for i in range(1,						10    )    )


if __name__ == "__main__":
    from doctest import testmod

    testmod()


