





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


lowerCamelCase__      =      5_0358
lowerCamelCase__      =      5_0362



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







  '''simple docstring'''


  def       __SCREAMING_SNAKE_CASE (						self      :				Any		)						-> Any:
        _UpperCamelCase					:		List[str]  =				"openai/whisper-small.en"
        _UpperCamelCase					:		Tuple  =				tempfile.mkdtemp()

  def       __SCREAMING_SNAKE_CASE (						self      :				Dict    ,      **__a      :				Optional[int]		)						-> Tuple:
        return WhisperTokenizer.from_pretrained(self.checkpoint    ,      **UpperCAmelCase_		)

  def       __SCREAMING_SNAKE_CASE (						self      :				Optional[int]    ,      **__a      :				List[str]		)						-> Tuple:
        return WhisperFeatureExtractor.from_pretrained(self.checkpoint    ,      **UpperCAmelCase_		)

  def       __SCREAMING_SNAKE_CASE (						self      :				Tuple		)						-> Optional[int]:
        shutil.rmtree(self.tmpdirname		)

  def       __SCREAMING_SNAKE_CASE (						self      :				Optional[int]		)						-> List[Any]:
        _UpperCamelCase					:		List[Any]  =				self.get_tokenizer()
        _UpperCamelCase					:		str  =				self.get_feature_extractor()

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

        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    ,      UpperCAmelCase_		)

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

  def       __SCREAMING_SNAKE_CASE (						self      :				Union[str, Any]		)						-> int:
        _UpperCamelCase					:		List[str]  =				WhisperProcessor(tokenizer=self.get_tokenizer()    ,      feature_extractor=self.get_feature_extractor()		)
        processor.save_pretrained(self.tmpdirname		)

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

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

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

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

  def       __SCREAMING_SNAKE_CASE (						self      :				int		)						-> Any:
        _UpperCamelCase					:		Optional[int]  =				self.get_feature_extractor()
        _UpperCamelCase					:		Optional[int]  =				self.get_tokenizer()

        _UpperCamelCase					:		int  =				WhisperProcessor(tokenizer=UpperCAmelCase_    ,      feature_extractor=UpperCAmelCase_		)

        _UpperCamelCase					:		Union[str, Any]  =				floats_list((3, 1000)		)

        _UpperCamelCase					:		Dict  =				feature_extractor(UpperCAmelCase_    ,      return_tensors="np"		)
        _UpperCamelCase					:		int  =				processor(UpperCAmelCase_    ,      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      :				Dict		)						-> Optional[Any]:
        _UpperCamelCase					:		Optional[Any]  =				self.get_feature_extractor()
        _UpperCamelCase					:		List[Any]  =				self.get_tokenizer()

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

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

        _UpperCamelCase					:		List[Any]  =				processor(text=UpperCAmelCase_		)

        _UpperCamelCase					:		Optional[Any]  =				tokenizer(UpperCAmelCase_		)

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

  def       __SCREAMING_SNAKE_CASE (						self      :				int		)						-> List[Any]:
        _UpperCamelCase					:		Tuple  =				self.get_feature_extractor()
        _UpperCamelCase					:		Optional[int]  =				self.get_tokenizer()

        _UpperCamelCase					:		Optional[int]  =				WhisperProcessor(tokenizer=UpperCAmelCase_    ,      feature_extractor=UpperCAmelCase_		)

        _UpperCamelCase					:		Tuple  =				[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

        _UpperCamelCase					:		str  =				processor.batch_decode(UpperCAmelCase_		)
        _UpperCamelCase					:		int  =				tokenizer.batch_decode(UpperCAmelCase_		)

        self.assertListEqual(UpperCAmelCase_    ,      UpperCAmelCase_		)

  def       __SCREAMING_SNAKE_CASE (						self      :				List[Any]		)						-> Union[str, Any]:
        _UpperCamelCase					:		Union[str, Any]  =				self.get_feature_extractor()
        _UpperCamelCase					:		Optional[int]  =				self.get_tokenizer()

        _UpperCamelCase					:		Any  =				WhisperProcessor(tokenizer=UpperCAmelCase_    ,      feature_extractor=UpperCAmelCase_		)

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

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

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

        _UpperCamelCase					:		List[Any]  =				[TRANSCRIBE, NOTIMESTAMPS]
        self.assertListEqual([ids[-1] for ids in forced_decoder_ids]    ,      UpperCAmelCase_		)

  def       __SCREAMING_SNAKE_CASE (						self      :				List[Any]		)						-> Dict:
        _UpperCamelCase					:		str  =				WhisperProcessor(tokenizer=self.get_tokenizer()    ,      feature_extractor=self.get_feature_extractor()		)
        _UpperCamelCase					:		List[Any]  =				processor.get_prompt_ids("Mr. Quilter"		)
        _UpperCamelCase					:		Union[str, Any]  =				processor.tokenizer.decode(UpperCAmelCase_		)

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

  def       __SCREAMING_SNAKE_CASE (						self      :				List[Any]		)						-> Optional[int]:
        _UpperCamelCase					:		Tuple  =				WhisperProcessor(tokenizer=self.get_tokenizer()    ,      feature_extractor=self.get_feature_extractor()		)
        _UpperCamelCase					:		Dict  =				processor.get_prompt_ids(""		)
        _UpperCamelCase					:		Tuple  =				processor.tokenizer.decode(UpperCAmelCase_		)

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


  def       __SCREAMING_SNAKE_CASE (						self      :				Dict		)						-> str:
        _UpperCamelCase					:		Tuple  =				WhisperProcessor(tokenizer=self.get_tokenizer()    ,      feature_extractor=self.get_feature_extractor()		)

        def _test_prompt_error_raised_helper(__a      :				List[str]    ,      __a      :				Optional[Any]		):
              with pytest.raises(UpperCAmelCase_		) as excinfo:
                    processor.get_prompt_ids(UpperCAmelCase_		)
              _UpperCamelCase					:		Dict  =				F'''Encountered text in the prompt corresponding to disallowed special token: {special_token}.'''
              self.assertEqual(UpperCAmelCase_    ,      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 dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np
import PIL

from ...utils import BaseOutput, is_invisible_watermark_available, is_torch_available, is_transformers_available

@dataclass
class 					__A   (  a			):
				__A            = 42


if is_transformers_available() and is_torch_available() and is_invisible_watermark_available():
							from .pipeline_stable_diffusion_xl import StableDiffusionXLPipeline
							from .pipeline_stable_diffusion_xl_imgaimg import StableDiffusionXLImgaImgPipeline





