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


SCREAMING_SNAKE_CASE    =					5_0_3_5_8
SCREAMING_SNAKE_CASE    =					5_0_3_6_2






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

 '''simple docstring'''
 def        UpperCamelCase     (				self		:     Any   ):



   '''simple docstring'''




   UpperCAmelCase__			:				List[str]												=       "openai/whisper-small.en"
   UpperCAmelCase__			:				Optional[Any]												=       tempfile.mkdtemp()
 def        UpperCamelCase     (				self		:     Optional[int] ,							**snake_case__		:     List[Any]   ):



   '''simple docstring'''




   return WhisperTokenizer.from_pretrained(self.checkpoint ,							**snake_case__   )
 def        UpperCamelCase     (				self		:     Tuple ,							**snake_case__		:     str   ):



   '''simple docstring'''




   return WhisperFeatureExtractor.from_pretrained(self.checkpoint ,							**snake_case__   )
 def        UpperCamelCase     (				self		:     str   ):



   '''simple docstring'''




   shutil.rmtree(self.tmpdirname   )
 def        UpperCamelCase     (				self		:     Optional[int]   ):



   '''simple docstring'''




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

   UpperCAmelCase__			:				Optional[Any]												=       WhisperProcessor(tokenizer=snake_case__ ,							feature_extractor=snake_case__   )

   processor.save_pretrained(self.tmpdirname   )
   UpperCAmelCase__			:				List[Any]												=       WhisperProcessor.from_pretrained(self.tmpdirname   )

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

   self.assertEqual(processor.feature_extractor.to_json_string() ,							feature_extractor.to_json_string()   )
   self.assertIsInstance(processor.feature_extractor ,							snake_case__   )
 def        UpperCamelCase     (				self		:     Optional[int]   ):



   '''simple docstring'''




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

   UpperCAmelCase__			:				List[str]												=       self.get_tokenizer(bos_token="(BOS)" ,							eos_token="(EOS)"   )
   UpperCAmelCase__			:				Union[str, Any]												=       self.get_feature_extractor(do_normalize=snake_case__ ,							padding_value=1.0   )

   UpperCAmelCase__			:				int												=       WhisperProcessor.from_pretrained(
       self.tmpdirname ,							bos_token="(BOS)" ,							eos_token="(EOS)" ,							do_normalize=snake_case__ ,							padding_value=1.0   )

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

   self.assertEqual(processor.feature_extractor.to_json_string() ,							feature_extractor_add_kwargs.to_json_string()   )
   self.assertIsInstance(processor.feature_extractor ,							snake_case__   )
 def        UpperCamelCase     (				self		:     Any   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Dict												=       self.get_feature_extractor()
   UpperCAmelCase__			:				str												=       self.get_tokenizer()

   UpperCAmelCase__			:				Optional[Any]												=       WhisperProcessor(tokenizer=snake_case__ ,							feature_extractor=snake_case__   )

   UpperCAmelCase__			:				Optional[Any]												=       floats_list((3, 10_00)   )

   UpperCAmelCase__			:				List[Any]												=       feature_extractor(snake_case__ ,							return_tensors="np"   )
   UpperCAmelCase__			:				Optional[int]												=       processor(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        UpperCamelCase     (				self		:     Optional[int]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				int												=       self.get_feature_extractor()
   UpperCAmelCase__			:				Dict												=       self.get_tokenizer()

   UpperCAmelCase__			:				List[str]												=       WhisperProcessor(tokenizer=snake_case__ ,							feature_extractor=snake_case__   )

   UpperCAmelCase__			:				List[Any]												=       "This is a test string"

   UpperCAmelCase__			:				List[Any]												=       processor(text=snake_case__   )

   UpperCAmelCase__			:				Optional[Any]												=       tokenizer(snake_case__   )

   for key in encoded_tok.keys():
     self.assertListEqual(encoded_tok[key] ,							encoded_processor[key]   )
 def        UpperCamelCase     (				self		:     Optional[Any]   ):



   '''simple docstring'''




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

   UpperCAmelCase__			:				Tuple												=       WhisperProcessor(tokenizer=snake_case__ ,							feature_extractor=snake_case__   )

   UpperCAmelCase__			:				List[str]												=       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

   UpperCAmelCase__			:				Union[str, Any]												=       processor.batch_decode(snake_case__   )
   UpperCAmelCase__			:				Optional[int]												=       tokenizer.batch_decode(snake_case__   )

   self.assertListEqual(snake_case__ ,							snake_case__   )
 def        UpperCamelCase     (				self		:     Optional[int]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Dict												=       self.get_feature_extractor()
   UpperCAmelCase__			:				int												=       self.get_tokenizer()

   UpperCAmelCase__			:				Dict												=       WhisperProcessor(tokenizer=snake_case__ ,							feature_extractor=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        UpperCamelCase     (				self		:     Tuple   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Any												=       self.get_feature_extractor()
   UpperCAmelCase__			:				int												=       self.get_tokenizer()

   UpperCAmelCase__			:				List[str]												=       WhisperProcessor(tokenizer=snake_case__ ,							feature_extractor=snake_case__   )
   UpperCAmelCase__			:				Dict												=       processor.get_decoder_prompt_ids(task="transcribe" ,							no_timestamps=snake_case__   )

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

   UpperCAmelCase__			:				List[Any]												=       [TRANSCRIBE, NOTIMESTAMPS]
   self.assertListEqual([ids[-1] for ids in forced_decoder_ids] ,							snake_case__   )
 def        UpperCamelCase     (				self		:     Tuple   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Tuple												=       WhisperProcessor(tokenizer=self.get_tokenizer() ,							feature_extractor=self.get_feature_extractor()   )
   UpperCAmelCase__			:				Union[str, Any]												=       processor.get_prompt_ids("Mr. Quilter"   )
   UpperCAmelCase__			:				Union[str, Any]												=       processor.tokenizer.decode(snake_case__   )

   self.assertListEqual(prompt_ids.tolist() ,							[5_03_60, 17_70, 13, 22_64, 3_46, 3_53]   )
   self.assertEqual(snake_case__ ,							"<|startofprev|> Mr. Quilter"   )
 def        UpperCamelCase     (				self		:     int   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Optional[int]												=       WhisperProcessor(tokenizer=self.get_tokenizer() ,							feature_extractor=self.get_feature_extractor()   )
   UpperCAmelCase__			:				Union[str, Any]												=       processor.get_prompt_ids(""   )
   UpperCAmelCase__			:				Any												=       processor.tokenizer.decode(snake_case__   )

   self.assertListEqual(prompt_ids.tolist() ,							[5_03_60, 2_20]   )
   self.assertEqual(snake_case__ ,							"<|startofprev|> "   )







 def        UpperCamelCase     (				self		:     Dict   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Dict												=       WhisperProcessor(tokenizer=self.get_tokenizer() ,							feature_extractor=self.get_feature_extractor()   )

   def _test_prompt_error_raised_helper(snake_case__		:     Union[str, Any] ,							snake_case__		:     List[str]   ):
     with pytest.raises(snake_case__   ) as excinfo:
       processor.get_prompt_ids(snake_case__   )
     UpperCAmelCase__			:				Dict												=       F"""Encountered text in the prompt corresponding to disallowed special token: {special_token}."""
     self.assertEqual(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'''





from typing import TYPE_CHECKING

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


SCREAMING_SNAKE_CASE    =					{
    """configuration_donut_swin""": ["""DONUT_SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DonutSwinConfig"""],
    """processing_donut""": ["""DonutProcessor"""],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE    =					[
          """DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """DonutSwinModel""",
          """DonutSwinPreTrainedModel""",
      ]

try:
      if not is_vision_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE    =					["""DonutFeatureExtractor"""]
      SCREAMING_SNAKE_CASE    =					["""DonutImageProcessor"""]


if TYPE_CHECKING:
      from .configuration_donut_swin import DONUT_SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, DonutSwinConfig
      from .processing_donut import DonutProcessor

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_donut_swin import (
                DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
                DonutSwinModel,
                DonutSwinPreTrainedModel,
            )

      try:
            if not is_vision_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .feature_extraction_donut import DonutFeatureExtractor
            from .image_processing_donut import DonutImageProcessor

else:
      import sys

      SCREAMING_SNAKE_CASE    =					_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



