


"""simple docstring"""







import shutil
import tempfile
import unittest

from transformers import ClapFeatureExtractor, ClapProcessor, RobertaTokenizer, RobertaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torchaudio

from .test_feature_extraction_clap import floats_list






@require_torchaudio
@require_sentencepiece
class 				__A    (	unittest.TestCase      ):






 """simple docstring"""




 def 				SCREAMING_SNAKE_CASE							(		self						) ->					int:
     a					='''laion/clap-htsat-unfused'''
     a					=tempfile.mkdtemp()


 def 				SCREAMING_SNAKE_CASE							(		self			,   **__A						) ->					int:
     return RobertaTokenizer.from_pretrained(self.checkpoint			,   **__A						)


 def 				SCREAMING_SNAKE_CASE							(		self			,   **__A						) ->					List[Any]:
     return ClapFeatureExtractor.from_pretrained(self.checkpoint			,   **__A						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					int:
     shutil.rmtree(self.tmpdirname						)


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					List[str]:
     a					=self.get_tokenizer()
     a					=self.get_feature_extractor()

     a					=ClapProcessor(tokenizer=__A			,   feature_extractor=__A						)

     processor.save_pretrained(self.tmpdirname						)
     a					=ClapProcessor.from_pretrained(self.tmpdirname						)

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

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


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					List[str]:
     a					=ClapProcessor(tokenizer=self.get_tokenizer()			,   feature_extractor=self.get_feature_extractor()						)
     processor.save_pretrained(self.tmpdirname						)

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

     a					=ClapProcessor.from_pretrained(
         self.tmpdirname			,   bos_token='''(BOS)'''			,   eos_token='''(EOS)'''			,   do_normalize=__A			,   padding_value=1.0						)

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

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


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					List[Any]:
     a					=self.get_feature_extractor()
     a					=self.get_tokenizer()

     a					=ClapProcessor(tokenizer=__A			,   feature_extractor=__A						)

     a					=floats_list((3, 1000)						)

     a					=feature_extractor(__A			,   return_tensors='''np'''						)
     a					=processor(audios=__A			,   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						) ->					Any:
     a					=self.get_feature_extractor()
     a					=self.get_tokenizer()

     a					=ClapProcessor(tokenizer=__A			,   feature_extractor=__A						)

     a					='''This is a test string'''

     a					=processor(text=__A						)

     a					=tokenizer(__A						)

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


 def 				SCREAMING_SNAKE_CASE							(		self						) ->					List[Any]:
     a					=self.get_feature_extractor()
     a					=self.get_tokenizer()

     a					=ClapProcessor(tokenizer=__A			,   feature_extractor=__A						)

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

     a					=processor.batch_decode(__A						)
     a					=tokenizer.batch_decode(__A						)

     self.assertListEqual(__A			,   __A						)





 def 				SCREAMING_SNAKE_CASE							(		self						) ->					List[str]:
     a					=self.get_feature_extractor()
     a					=self.get_tokenizer()

     a					=ClapProcessor(tokenizer=__A			,   feature_extractor=__A						)

     self.assertListEqual(
         processor.model_input_names[2:]			,   feature_extractor.model_input_names			,   msg='''`processor` and `feature_extractor` model input names do not match'''			,   )



"""simple docstring"""







from ...processing_utils import ProcessorMixin






class 				__A    (	_SCREAMING_SNAKE_CASE      ):






 """simple docstring"""




 __lowerCAmelCase			 =  "WhisperFeatureExtractor"
 __lowerCAmelCase			 =  "WhisperTokenizer"


 def __init__(		self			,   __A			,   __A						) ->					Dict:
     super().__init__(__A			,   __A						)
     a					=self.feature_extractor
     a					=False


 def 				SCREAMING_SNAKE_CASE							(		self			,   __A=None			,   __A=None			,   __A=True						) ->					int:
     return self.tokenizer.get_decoder_prompt_ids(task=__A			,   language=__A			,   no_timestamps=__A						)


 def __call__(		self			,   *__A			,   **__A						) ->					Tuple:
     # For backward compatibility
     if self._in_target_context_manager:
         return self.current_processor(*__A			,   **__A						)

     a					=kwargs.pop('''audio'''			,   __A						)
     a					=kwargs.pop('''sampling_rate'''			,   __A						)
     a					=kwargs.pop('''text'''			,   __A						)
     if len(__A						) > 0:
         a					=args[0]
         a					=args[1:]

     if audio is None and text is None:
         raise ValueError('''You need to specify either an `audio` or `text` input to process.'''						)

     if audio is not None:
         a					=self.feature_extractor(__A			,   *__A			,   sampling_rate=__A			,   **__A						)
     if text is not None:
         a					=self.tokenizer(__A			,   **__A						)

     if text is None:
         return inputs

     elif audio is None:
         return encodings
     else:
         a					=encodings['''input_ids''']
         return inputs


 def 				SCREAMING_SNAKE_CASE							(		self			,   *__A			,   **__A						) ->					Optional[Any]:
     return self.tokenizer.batch_decode(*__A			,   **__A						)


 def 				SCREAMING_SNAKE_CASE							(		self			,   *__A			,   **__A						) ->					Union[str, Any]:
     return self.tokenizer.decode(*__A			,   **__A						)





 def 				SCREAMING_SNAKE_CASE							(		self			,   __A			,   __A="np"						) ->					Optional[Any]:
     return self.tokenizer.get_prompt_ids(__A			,   return_tensors=__A						)