





'''simple docstring'''



import enum
import warnings

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
							import tensorflow as tf

							from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

if is_torch_available():
							from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

lowerCAmelCase_     :       Dict								=						logging.get_logger(__name__)
class 	lowerCamelCase_							(					enum.Enum			):
						_lowerCAmelCase     :   Tuple	     =				0
						_lowerCAmelCase     :   str	     =				1


@add_end_docstrings(snake_case_			)
class 	lowerCamelCase_							(					snake_case_			):
						_lowerCAmelCase     :   Optional[int]	     =				'generated'







						def __init__(  self   :      Any				,    *lowerCAmelCase__   :      Dict				,    **lowerCAmelCase__   :      Tuple				):

													"""simple docstring"""
													super().__init__(*lowercase_				,    **lowercase_				)

													self.check_model_type(
													    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
													    if self.framework == '''tf'''
													    else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING				)







						def 					__lowercase    (  self   :      Optional[int]				,    lowerCAmelCase__   :      Union[str, Any]=None				,    lowerCAmelCase__   :      Optional[Any]=None				,    lowerCAmelCase__   :      Optional[int]=None				,    lowerCAmelCase__   :      Optional[Any]=None				,    lowerCAmelCase__   :      Any=None				,    lowerCAmelCase__   :      Union[str, Any]=None				,    **lowerCAmelCase__   :      Optional[Any]				,    ):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Dict     					=			{}
													if truncation is not None:
																				SCREAMING_SNAKE_CASE					:      List[str]     					=			truncation

													SCREAMING_SNAKE_CASE					:      List[Any]     					=			generate_kwargs

													SCREAMING_SNAKE_CASE					:      Any     					=			{}
													if return_tensors is not None and return_type is None:
																				SCREAMING_SNAKE_CASE					:      Optional[int]     					=			ReturnType.TENSORS if return_tensors else ReturnType.TEXT
													if return_type is not None:
																				SCREAMING_SNAKE_CASE					:      Optional[int]     					=			return_type

													if clean_up_tokenization_spaces is not None:
																				SCREAMING_SNAKE_CASE					:      Dict     					=			clean_up_tokenization_spaces

													if stop_sequence is not None:
																				SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			self.tokenizer.encode(lowercase_				,    add_special_tokens=lowercase_				)
																				if len(lowercase_				) > 1:
																											warnings.warn(
																											    '''Stopping on a multiple token sequence is not yet supported on transformers. The first token of'''
																											    ''' the stop sequence will be used as the stop sequence string in the interim.'''				)
																				SCREAMING_SNAKE_CASE					:      Optional[int]     					=			stop_sequence_ids[0]

													return preprocess_params, forward_params, postprocess_params







						def 					__lowercase    (  self   :      int				,    lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      int				):

													"""simple docstring"""
													return True







						def 					__lowercase    (  self   :      Dict				,    *lowerCAmelCase__   :      List[str]				,    lowerCAmelCase__   :      List[Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			self.model.config.prefix if self.model.config.prefix is not None else ''''''
													if isinstance(args[0]				,    lowercase_				):
																				if self.tokenizer.pad_token_id is None:
																											raise ValueError('''Please make sure that the tokenizer has a pad_token_id when using a batch input'''				)
																				SCREAMING_SNAKE_CASE					:      Any     					=			([prefix + arg for arg in args[0]],)
																				SCREAMING_SNAKE_CASE					:      List[str]     					=			True

													elif isinstance(args[0]				,    lowercase_				):
																				SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			(prefix + args[0],)
																				SCREAMING_SNAKE_CASE					:      List[str]     					=			False
													else:
																				raise ValueError(
																				    F""" `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`"""				)
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			self.tokenizer(*lowercase_				,    padding=lowercase_				,    truncation=lowercase_				,    return_tensors=self.framework				)
													# This is produced by tokenizers but is an invalid generate kwargs
													if "token_type_ids" in inputs:
																				del inputs["token_type_ids"]
													return inputs







						def __call__(  self   :      List[Any]				,    *lowerCAmelCase__   :      Any				,    **lowerCAmelCase__   :      int				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Tuple     					=			super().__call__(*lowercase_				,    **lowercase_				)
													if (
													    isinstance(args[0]				,    lowercase_				)
													    and all(isinstance(lowercase_				,    lowercase_				) for el in args[0]				)
													    and all(len(lowercase_				) == 1 for res in result				)
													):
																				return [res[0] for res in result]
													return result







						def 					__lowercase    (  self   :      Tuple				,    lowerCAmelCase__   :      Union[str, Any]				,    lowerCAmelCase__   :      str=TruncationStrategy.DO_NOT_TRUNCATE				,    **lowerCAmelCase__   :      Dict				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Dict     					=			self._parse_and_tokenize(lowercase_				,    truncation=lowercase_				,    **lowercase_				)
													return inputs







						def 					__lowercase    (  self   :      str				,    lowerCAmelCase__   :      str				,    **lowerCAmelCase__   :      str				):

													"""simple docstring"""







													if self.framework == "pt":
																				SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      Dict     					=			model_inputs['''input_ids'''].shape
													elif self.framework == "tf":
																				SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			tf.shape(model_inputs['''input_ids''']				).numpy()

													SCREAMING_SNAKE_CASE					:      Any     					=			generate_kwargs.get('''min_length'''				,    self.model.config.min_length				)
													SCREAMING_SNAKE_CASE					:      int     					=			generate_kwargs.get('''max_length'''				,    self.model.config.max_length				)
													self.check_inputs(lowercase_				,    generate_kwargs['''min_length''']				,    generate_kwargs['''max_length''']				)
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			self.model.generate(**lowercase_				,    **lowercase_				)
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			output_ids.shape[0]
													if self.framework == "pt":
																				SCREAMING_SNAKE_CASE					:      str     					=			output_ids.reshape(lowercase_				,    out_b // in_b				,    *output_ids.shape[1:]				)
													elif self.framework == "tf":
																				SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			tf.reshape(lowercase_				,    (in_b, out_b // in_b, *output_ids.shape[1:])				)
													return {"output_ids": output_ids}







						def 					__lowercase    (  self   :      Dict				,    lowerCAmelCase__   :      str				,    lowerCAmelCase__   :      int=ReturnType.TEXT				,    lowerCAmelCase__   :      int=False				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Any     					=			[]
													for output_ids in model_outputs["output_ids"][0]:
																				if return_type == ReturnType.TENSORS:
																											SCREAMING_SNAKE_CASE					:      Any     					=			{F"""{self.return_name}_token_ids""": output_ids}
																				elif return_type == ReturnType.TEXT:
																											SCREAMING_SNAKE_CASE					:      Dict     					=			{
																											    F"""{self.return_name}_text""": self.tokenizer.decode(
																											        lowercase_				,    skip_special_tokens=lowercase_				,    clean_up_tokenization_spaces=lowercase_				,    )
																											}
																				records.append(lowercase_				)
													return records


@add_end_docstrings(snake_case_			)
class 	lowerCamelCase_							(					snake_case_			):
						_lowerCAmelCase     :   Dict	     =				'summary'







						def __call__(  self   :      Optional[Any]				,    *lowerCAmelCase__   :      int				,    **lowerCAmelCase__   :      Dict				):

													"""simple docstring"""
													return super().__call__(*lowercase_				,    **lowercase_				)







						def 					__lowercase    (  self   :      List[str]				,    lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      int				):

													"""simple docstring"""







													if max_length < min_length:
																				logger.warning(F"""Your min_length={min_length} must be inferior than your max_length={max_length}."""				)

													if input_length < max_length:
																				logger.warning(
																				    F"""Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is """
																				    '''a summarization task, where outputs shorter than the input are typically wanted, you might '''
																				    F"""consider decreasing max_length manually, e.g. summarizer(\'...\', max_length={input_length//2})"""				)


@add_end_docstrings(snake_case_			)
class 	lowerCamelCase_							(					snake_case_			):
						_lowerCAmelCase     :   Optional[int]	     =				'translation'







						def 					__lowercase    (  self   :      Dict				,    lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      int				):

													"""simple docstring"""







													if input_length > 0.9 * max_length:
																				logger.warning(
																				    F"""Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider """
																				    '''increasing your max_length manually, e.g. translator(\'...\', max_length=400)'''				)
													return True







						def 					__lowercase    (  self   :      Tuple				,    *lowerCAmelCase__   :      Any				,    lowerCAmelCase__   :      List[Any]=TruncationStrategy.DO_NOT_TRUNCATE				,    lowerCAmelCase__   :      Any=None				,    lowerCAmelCase__   :      Optional[Any]=None				):

													"""simple docstring"""







													if getattr(self.tokenizer				,    '''_build_translation_inputs'''				,    lowercase_				):
																				return self.tokenizer._build_translation_inputs(
																				    *lowercase_				,    return_tensors=self.framework				,    truncation=lowercase_				,    src_lang=lowercase_				,    tgt_lang=lowercase_				)
													else:
																				return super()._parse_and_tokenize(*lowercase_				,    truncation=lowercase_				)







						def 					__lowercase    (  self   :      List[str]				,    lowerCAmelCase__   :      Dict=None				,    lowerCAmelCase__   :      str=None				,    **lowerCAmelCase__   :      List[Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      int     					=			super()._sanitize_parameters(**lowercase_				)
													if src_lang is not None:
																				SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			src_lang
													if tgt_lang is not None:
																				SCREAMING_SNAKE_CASE					:      Any     					=			tgt_lang
													if src_lang is None and tgt_lang is None:
																				# Backward compatibility, direct arguments use is preferred.
																				SCREAMING_SNAKE_CASE					:      int     					=			kwargs.get('''task'''				,    self.task				)
																				SCREAMING_SNAKE_CASE					:      int     					=			task.split('''_'''				)
																				if task and len(lowercase_				) == 4:
																											# translation, XX, to YY
																											SCREAMING_SNAKE_CASE					:      str     					=			items[1]
																											SCREAMING_SNAKE_CASE					:      Optional[int]     					=			items[3]
													return preprocess_params, forward_params, postprocess_params







						def __call__(  self   :      List[str]				,    *lowerCAmelCase__   :      List[str]				,    **lowerCAmelCase__   :      str				):

													"""simple docstring"""
													return super().__call__(*lowercase_				,    **lowercase_				)









'''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 	lowerCamelCase_							(					unittest.TestCase			):







						def 					__lowercase    (  self   :      Union[str, Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      int     					=			'''laion/clap-htsat-unfused'''
													SCREAMING_SNAKE_CASE					:      Tuple     					=			tempfile.mkdtemp()







						def 					__lowercase    (  self   :      Tuple				,    **lowerCAmelCase__   :      Dict				):

													"""simple docstring"""
													return RobertaTokenizer.from_pretrained(self.checkpoint				,    **lowerCAmelCase__				)







						def 					__lowercase    (  self   :      int				,    **lowerCAmelCase__   :      str				):

													"""simple docstring"""
													return ClapFeatureExtractor.from_pretrained(self.checkpoint				,    **lowerCAmelCase__				)







						def 					__lowercase    (  self   :      Union[str, Any]				):

													"""simple docstring"""
													shutil.rmtree(self.tmpdirname				)







						def 					__lowercase    (  self   :      Dict				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      List[str]     					=			self.get_tokenizer()
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			self.get_feature_extractor()

													SCREAMING_SNAKE_CASE					:      List[str]     					=			ClapProcessor(tokenizer=lowerCAmelCase__				,    feature_extractor=lowerCAmelCase__				)

													processor.save_pretrained(self.tmpdirname				)
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			ClapProcessor.from_pretrained(self.tmpdirname				)

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

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







						def 					__lowercase    (  self   :      Union[str, Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Dict     					=			ClapProcessor(tokenizer=self.get_tokenizer()				,    feature_extractor=self.get_feature_extractor()				)
													processor.save_pretrained(self.tmpdirname				)

													SCREAMING_SNAKE_CASE					:      List[Any]     					=			self.get_tokenizer(bos_token='''(BOS)'''				,    eos_token='''(EOS)'''				)
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			self.get_feature_extractor(do_normalize=lowerCAmelCase__				,    padding_value=1.0				)

													SCREAMING_SNAKE_CASE					:      str     					=			ClapProcessor.from_pretrained(
													    self.tmpdirname				,    bos_token='''(BOS)'''				,    eos_token='''(EOS)'''				,    do_normalize=lowerCAmelCase__				,    padding_value=1.0				)

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

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







						def 					__lowercase    (  self   :      Dict				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			self.get_feature_extractor()
													SCREAMING_SNAKE_CASE					:      Tuple     					=			self.get_tokenizer()

													SCREAMING_SNAKE_CASE					:      Tuple     					=			ClapProcessor(tokenizer=lowerCAmelCase__				,    feature_extractor=lowerCAmelCase__				)

													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			floats_list((3, 10_00)				)

													SCREAMING_SNAKE_CASE					:      Any     					=			feature_extractor(lowerCAmelCase__				,    return_tensors='''np'''				)
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			processor(audios=lowerCAmelCase__				,    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 					__lowercase    (  self   :      Any				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Tuple     					=			self.get_feature_extractor()
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			self.get_tokenizer()

													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			ClapProcessor(tokenizer=lowerCAmelCase__				,    feature_extractor=lowerCAmelCase__				)

													SCREAMING_SNAKE_CASE					:      int     					=			'''This is a test string'''

													SCREAMING_SNAKE_CASE					:      int     					=			processor(text=lowerCAmelCase__				)

													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			tokenizer(lowerCAmelCase__				)

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







						def 					__lowercase    (  self   :      Optional[Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      str     					=			self.get_feature_extractor()
													SCREAMING_SNAKE_CASE					:      List[str]     					=			self.get_tokenizer()

													SCREAMING_SNAKE_CASE					:      Any     					=			ClapProcessor(tokenizer=lowerCAmelCase__				,    feature_extractor=lowerCAmelCase__				)

													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

													SCREAMING_SNAKE_CASE					:      Tuple     					=			processor.batch_decode(lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			tokenizer.batch_decode(lowerCAmelCase__				)

													self.assertListEqual(lowerCAmelCase__				,    lowerCAmelCase__				)







						def 					__lowercase    (  self   :      Optional[Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			self.get_feature_extractor()
													SCREAMING_SNAKE_CASE					:      str     					=			self.get_tokenizer()

													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			ClapProcessor(tokenizer=lowerCAmelCase__				,    feature_extractor=lowerCAmelCase__				)

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




