






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				     =  50358
_a				     =  50362





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





	"""simple docstring"""






	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      'openai/whisper-small.en'
							_UpperCAmelCase     =      tempfile.mkdtemp()

	def 	UpperCamelCase   (							self	,   **UpperCAmelCase   ):
							"""simple docstring"""







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

	def 	UpperCamelCase   (							self	,   **UpperCAmelCase   ):
							"""simple docstring"""







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

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							shutil.rmtree(self.tmpdirname   )

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      self.get_tokenizer()
							_UpperCAmelCase     =      self.get_feature_extractor()

							_UpperCAmelCase     =      WhisperProcessor(tokenizer=UpperCAmelCase	,   feature_extractor=UpperCAmelCase   )

							processor.save_pretrained(self.tmpdirname   )
							_UpperCAmelCase     =      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 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      WhisperProcessor(tokenizer=self.get_tokenizer()	,   feature_extractor=self.get_feature_extractor()   )
							processor.save_pretrained(self.tmpdirname   )

							_UpperCAmelCase     =      self.get_tokenizer(bos_token='(BOS)'	,   eos_token='(EOS)'   )
							_UpperCAmelCase     =      self.get_feature_extractor(do_normalize=UpperCAmelCase	,   padding_value=1.0   )

							_UpperCAmelCase     =      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 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      self.get_feature_extractor()
							_UpperCAmelCase     =      self.get_tokenizer()

							_UpperCAmelCase     =      WhisperProcessor(tokenizer=UpperCAmelCase	,   feature_extractor=UpperCAmelCase   )

							_UpperCAmelCase     =      floats_list((3, 1000)   )

							_UpperCAmelCase     =      feature_extractor(UpperCAmelCase	,   return_tensors='np'   )
							_UpperCAmelCase     =      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 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      self.get_feature_extractor()
							_UpperCAmelCase     =      self.get_tokenizer()

							_UpperCAmelCase     =      WhisperProcessor(tokenizer=UpperCAmelCase	,   feature_extractor=UpperCAmelCase   )

							_UpperCAmelCase     =      'This is a test string'

							_UpperCAmelCase     =      processor(text=UpperCAmelCase   )

							_UpperCAmelCase     =      tokenizer(UpperCAmelCase   )

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

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      self.get_feature_extractor()
							_UpperCAmelCase     =      self.get_tokenizer()

							_UpperCAmelCase     =      WhisperProcessor(tokenizer=UpperCAmelCase	,   feature_extractor=UpperCAmelCase   )

							_UpperCAmelCase     =      [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

							_UpperCAmelCase     =      processor.batch_decode(UpperCAmelCase   )
							_UpperCAmelCase     =      tokenizer.batch_decode(UpperCAmelCase   )

							self.assertListEqual(UpperCAmelCase	,   UpperCAmelCase   )

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      self.get_feature_extractor()
							_UpperCAmelCase     =      self.get_tokenizer()

							_UpperCAmelCase     =      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 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      self.get_feature_extractor()
							_UpperCAmelCase     =      self.get_tokenizer()

							_UpperCAmelCase     =      WhisperProcessor(tokenizer=UpperCAmelCase	,   feature_extractor=UpperCAmelCase   )
							_UpperCAmelCase     =      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     =      [TRANSCRIBE, NOTIMESTAMPS]
							self.assertListEqual([ids[-1] for ids in forced_decoder_ids]	,   UpperCAmelCase   )

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      WhisperProcessor(tokenizer=self.get_tokenizer()	,   feature_extractor=self.get_feature_extractor()   )
							_UpperCAmelCase     =      processor.get_prompt_ids('Mr. Quilter'   )
							_UpperCAmelCase     =      processor.tokenizer.decode(UpperCAmelCase   )

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

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      WhisperProcessor(tokenizer=self.get_tokenizer()	,   feature_extractor=self.get_feature_extractor()   )
							_UpperCAmelCase     =      processor.get_prompt_ids(''   )
							_UpperCAmelCase     =      processor.tokenizer.decode(UpperCAmelCase   )

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

	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      WhisperProcessor(tokenizer=self.get_tokenizer()	,   feature_extractor=self.get_feature_extractor()   )

							def _test_prompt_error_raised_helper(UpperCAmelCase	,   UpperCAmelCase   ):
													with pytest.raises(UpperCAmelCase   ) as excinfo:
																			processor.get_prompt_ids(UpperCAmelCase   )
													_UpperCAmelCase     =      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 __future__ import annotations

_a				     =  [2, 1, 4, 5, 6, 0, 8, 9, 1, 2, 0, 6, 4, 2, 0, 6, 5, 3, 2, 7]





class __lowerCamelCase			:





	"""simple docstring"""






	def __init__(							self	,   UpperCAmelCase   ):
							"""simple docstring"""







							_UpperCAmelCase     =      -1
							_UpperCAmelCase     =      -1
							_UpperCAmelCase     =      [-1] * length
							_UpperCAmelCase     =      None
							_UpperCAmelCase     =      None

	def __repr__(							self   ):
							"""simple docstring"""







							return F"""Node(min_value={self.minn} max_value={self.maxx})"""



def __A   (				__lowerCAmelCase     )->  Node | None:





						"""simple docstring"""



						_UpperCAmelCase     =      Node(len(__lowerCAmelCase     )     )
						_UpperCAmelCase ,			_UpperCAmelCase     =      min(__lowerCAmelCase     ), max(__lowerCAmelCase     )
						# Leaf node case where the node contains only one unique value
						if root.minn == root.maxx:
												return root
						_UpperCAmelCase     =      (root.minn + root.maxx) // 2

						_UpperCAmelCase     =      []
						_UpperCAmelCase     =      []

						for index, num in enumerate(__lowerCAmelCase     ):
												if num <= pivot:
																		left_arr.append(__lowerCAmelCase     )
												else:
																		right_arr.append(__lowerCAmelCase     )
												_UpperCAmelCase     =      len(__lowerCAmelCase     )
						_UpperCAmelCase     =      build_tree(__lowerCAmelCase     )
						_UpperCAmelCase     =      build_tree(__lowerCAmelCase     )
						return root


def __A   (				__lowerCAmelCase			,		__lowerCAmelCase			,		__lowerCAmelCase     )->  int:





						"""simple docstring"""



						if index < 0 or node is None:
												return 0
						# Leaf node cases
						if node.minn == node.maxx:
												return index + 1 if node.minn == num else 0
						_UpperCAmelCase     =      (node.minn + node.maxx) // 2
						if num <= pivot:
												# go the left subtree and map index to the left subtree
												return rank_till_index(node.left			,		__lowerCAmelCase			,		node.map_left[index] - 1     )
						else:
												# go to the right subtree and map index to the right subtree
												return rank_till_index(node.right			,		__lowerCAmelCase			,		index - node.map_left[index]     )


def __A   (				__lowerCAmelCase			,		__lowerCAmelCase			,		__lowerCAmelCase			,		__lowerCAmelCase     )->  int:





						"""simple docstring"""



						if start > end:
												return 0
						_UpperCAmelCase     =      rank_till_index(__lowerCAmelCase			,		__lowerCAmelCase			,		__lowerCAmelCase     )
						_UpperCAmelCase     =      rank_till_index(__lowerCAmelCase			,		__lowerCAmelCase			,		start - 1     )
						return rank_till_end - rank_before_start


def __A   (				__lowerCAmelCase			,		__lowerCAmelCase			,		__lowerCAmelCase			,		__lowerCAmelCase     )->  int:





						"""simple docstring"""



						if index > (end - start) or start > end or node is None:
												return -1
						# Leaf node case
						if node.minn == node.maxx:
												return node.minn
						# Number of elements in the left subtree in interval [start, end]
						_UpperCAmelCase     =      node.map_left[end] - (
						    node.map_left[start - 1] if start else 0
						)
						if num_elements_in_left_tree > index:
												return quantile(
												    node.left			,		__lowerCAmelCase			,		(node.map_left[start - 1] if start else 0)			,		node.map_left[end] - 1			,		)
						else:
												return quantile(
												    node.right			,		index - num_elements_in_left_tree			,		start - (node.map_left[start - 1] if start else 0)			,		end - node.map_left[end]			,		)


def __A   (				__lowerCAmelCase			,		__lowerCAmelCase			,		__lowerCAmelCase			,		__lowerCAmelCase			,		__lowerCAmelCase     )->  int:





						"""simple docstring"""



						if (
						    start > end
						    or node is None
						    or start_num > end_num
						    or node.minn > end_num
						    or node.maxx < start_num
						):
												return 0
						if start_num <= node.minn and node.maxx <= end_num:
												return end - start + 1
						_UpperCAmelCase     =      range_counting(
						    node.left			,		(node.map_left[start - 1] if start else 0)			,		node.map_left[end] - 1			,		__lowerCAmelCase			,		__lowerCAmelCase			,		)
						_UpperCAmelCase     =      range_counting(
						    node.right			,		start - (node.map_left[start - 1] if start else 0)			,		end - node.map_left[end]			,		__lowerCAmelCase			,		__lowerCAmelCase			,		)
						return left + right


if __name__ == "__main__":
		import doctest

		doctest.testmod()
