

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


_UpperCamelCase		    =						5_0_3_5_8
_UpperCamelCase		    =						5_0_3_6_2






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





       """simple docstring"""


       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Tuple    =							"openai/whisper-small.en"
        lowerCAmelCase__      :							Dict    =							tempfile.mkdtemp()






       def  SCREAMING_SNAKE_CASE_  (      self ,				**snake_case      ):





        """simple docstring"""

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






       def  SCREAMING_SNAKE_CASE_  (      self ,				**snake_case      ):





        """simple docstring"""

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






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        shutil.rmtree(self.tmpdirname      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Any    =							self.get_tokenizer()
        lowerCAmelCase__      :							Any    =							self.get_feature_extractor()

        lowerCAmelCase__      :							int    =							WhisperProcessor(tokenizer=__SCREAMING_SNAKE_CASE ,				feature_extractor=__SCREAMING_SNAKE_CASE      )

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

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

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






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Tuple    =							WhisperProcessor(tokenizer=self.get_tokenizer() ,				feature_extractor=self.get_feature_extractor()      )
        processor.save_pretrained(self.tmpdirname      )

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

        lowerCAmelCase__      :							Dict    =							WhisperProcessor.from_pretrained(
            self.tmpdirname ,				bos_token="(BOS)" ,				eos_token="(EOS)" ,				do_normalize=__SCREAMING_SNAKE_CASE ,				padding_value=1.0      )

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

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






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Tuple    =							self.get_feature_extractor()
        lowerCAmelCase__      :							List[Any]    =							self.get_tokenizer()

        lowerCAmelCase__      :							str    =							WhisperProcessor(tokenizer=__SCREAMING_SNAKE_CASE ,				feature_extractor=__SCREAMING_SNAKE_CASE      )

        lowerCAmelCase__      :							Union[str, Any]    =							floats_list((3, 1_000)      )

        lowerCAmelCase__      :							Dict    =							feature_extractor(__SCREAMING_SNAKE_CASE ,				return_tensors="np"      )
        lowerCAmelCase__      :							Optional[Any]    =							processor(__SCREAMING_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  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							List[str]    =							self.get_feature_extractor()
        lowerCAmelCase__      :							Optional[Any]    =							self.get_tokenizer()

        lowerCAmelCase__      :							Dict    =							WhisperProcessor(tokenizer=__SCREAMING_SNAKE_CASE ,				feature_extractor=__SCREAMING_SNAKE_CASE      )

        lowerCAmelCase__      :							Dict    =							"This is a test string"

        lowerCAmelCase__      :							Tuple    =							processor(text=__SCREAMING_SNAKE_CASE      )

        lowerCAmelCase__      :							Union[str, Any]    =							tokenizer(__SCREAMING_SNAKE_CASE      )

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






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Optional[int]    =							self.get_feature_extractor()
        lowerCAmelCase__      :							List[Any]    =							self.get_tokenizer()

        lowerCAmelCase__      :							int    =							WhisperProcessor(tokenizer=__SCREAMING_SNAKE_CASE ,				feature_extractor=__SCREAMING_SNAKE_CASE      )

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

        lowerCAmelCase__      :							Tuple    =							processor.batch_decode(__SCREAMING_SNAKE_CASE      )
        lowerCAmelCase__      :							Tuple    =							tokenizer.batch_decode(__SCREAMING_SNAKE_CASE      )

        self.assertListEqual(__SCREAMING_SNAKE_CASE ,				__SCREAMING_SNAKE_CASE      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Optional[Any]    =							self.get_feature_extractor()
        lowerCAmelCase__      :							Any    =							self.get_tokenizer()

        lowerCAmelCase__      :							Union[str, Any]    =							WhisperProcessor(tokenizer=__SCREAMING_SNAKE_CASE ,				feature_extractor=__SCREAMING_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  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Tuple    =							self.get_feature_extractor()
        lowerCAmelCase__      :							Any    =							self.get_tokenizer()

        lowerCAmelCase__      :							Dict    =							WhisperProcessor(tokenizer=__SCREAMING_SNAKE_CASE ,				feature_extractor=__SCREAMING_SNAKE_CASE      )
        lowerCAmelCase__      :							Union[str, Any]    =							processor.get_decoder_prompt_ids(task="transcribe" ,				no_timestamps=__SCREAMING_SNAKE_CASE      )

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

        lowerCAmelCase__      :							int    =							[TRANSCRIBE, NOTIMESTAMPS]
        self.assertListEqual([ids[-1] for ids in forced_decoder_ids] ,				__SCREAMING_SNAKE_CASE      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							List[Any]    =							WhisperProcessor(tokenizer=self.get_tokenizer() ,				feature_extractor=self.get_feature_extractor()      )
        lowerCAmelCase__      :							Union[str, Any]    =							processor.get_prompt_ids("Mr. Quilter"      )
        lowerCAmelCase__      :							Optional[Any]    =							processor.tokenizer.decode(__SCREAMING_SNAKE_CASE      )

        self.assertListEqual(prompt_ids.tolist() ,				[50_360, 1_770, 13, 2_264, 346, 353]      )
        self.assertEqual(__SCREAMING_SNAKE_CASE ,				"<|startofprev|> Mr. Quilter"      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Union[str, Any]    =							WhisperProcessor(tokenizer=self.get_tokenizer() ,				feature_extractor=self.get_feature_extractor()      )
        lowerCAmelCase__      :							Optional[Any]    =							processor.get_prompt_ids(""      )
        lowerCAmelCase__      :							List[Any]    =							processor.tokenizer.decode(__SCREAMING_SNAKE_CASE      )

        self.assertListEqual(prompt_ids.tolist() ,				[50_360, 220]      )
        self.assertEqual(__SCREAMING_SNAKE_CASE ,				"<|startofprev|> "      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Optional[Any]    =							WhisperProcessor(tokenizer=self.get_tokenizer() ,				feature_extractor=self.get_feature_extractor()      )

        def _test_prompt_error_raised_helper(snake_case ,				snake_case      ):
         with pytest.raises(__SCREAMING_SNAKE_CASE      ) as excinfo:
          processor.get_prompt_ids(__SCREAMING_SNAKE_CASE      )
         lowerCAmelCase__      :							Tuple    =							F"""Encountered text in the prompt corresponding to disallowed special token: {special_token}."""
         self.assertEqual(__SCREAMING_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"""

# A Bipartite Graph is a graph whose vertices can be divided into two independent sets,
# U and V such that every edge (u, v) either connects a vertex from U to V or a vertex
# from V to U. In other words, for every edge (u, v), either u belongs to U and v to V,
# or u belongs to V and v to U. We can also say that there is no edge that connects
# vertices of same set.
from queue import Queue



def 			SCREAMING_SNAKE_CASE       (   lowercase__      ) ->	List[str]:
 lowerCAmelCase__      :							Tuple    =							Queue()
 lowerCAmelCase__      :							Optional[int]    =							[False] * len(lowercase__      )
 lowerCAmelCase__      :							Dict    =							[-1] * len(lowercase__      )

 def bfs():
  while not queue.empty():
   lowerCAmelCase__      :							Optional[Any]    =							queue.get()
   lowerCAmelCase__      :							Union[str, Any]    =							True

   for neighbour in graph[u]:
    if neighbour == u:
     return False

    if color[neighbour] == -1:
     lowerCAmelCase__      :							Any    =							1 - color[u]
     queue.put(lowercase__      )

    elif color[neighbour] == color[u]:
     return False

  return True

 for i in range(len(lowercase__      )      ):
  if not visited[i]:
   queue.put(lowercase__      )
   lowerCAmelCase__      :							int    =							0
   if bfs() is False:
    return False

 return True


if __name__ == "__main__":
    # Adjacency List of graph
    print(check_bipartite({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}))






