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






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


_snake_case				=			{
    "configuration_oneformer": ["ONEFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "OneFormerConfig"],
    "processing_oneformer": ["OneFormerProcessor"],
}

try:
      if not is_vision_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      _snake_case				=			["OneFormerImageProcessor"]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      _snake_case				=			[
          "ONEFORMER_PRETRAINED_MODEL_ARCHIVE_LIST",
          "OneFormerForUniversalSegmentation",
          "OneFormerModel",
          "OneFormerPreTrainedModel",
      ]

if TYPE_CHECKING:
      from .configuration_oneformer import ONEFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, OneFormerConfig
      from .processing_oneformer import OneFormerProcessor

      try:
            if not is_vision_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .image_processing_oneformer import OneFormerImageProcessor
      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_oneformer import (
                ONEFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
                OneFormerForUniversalSegmentation,
                OneFormerModel,
                OneFormerPreTrainedModel,
            )


else:
      import sys

      _snake_case				=			_LazyModule(__name__, globals()["__file__"], _import_structure)



