


import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin

class     snake_case      (							_UpperCamelCase						):
			'''simple docstring'''

			UpperCAmelCase							:							Tuple							=			"""MCTCTFeatureExtractor"""
			UpperCAmelCase							:							str							=			"""AutoTokenizer"""

			def __init__(						self  :  Optional[int]				,				lowerCAmelCase_  :  Dict				,				lowerCAmelCase_  :  Union[str, Any]       )       ->       Dict:




								"""simple docstring"""






								super().__init__(UpperCamelCase__				,				UpperCamelCase__       )
								SCREAMING_SNAKE_CASE_						   =							self.feature_extractor
								SCREAMING_SNAKE_CASE_						   =							False

			def __call__(						self  :  Any				,				*lowerCAmelCase_  :  Any				,				**lowerCAmelCase_  :  Optional[int]       )       ->       List[Any]:




								"""simple docstring"""






								if self._in_target_context_manager:
													return self.current_processor(*UpperCamelCase__				,				**UpperCamelCase__       )

								if "raw_speech" in kwargs:
													warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'''       )
													SCREAMING_SNAKE_CASE_						   =							kwargs.pop('''raw_speech'''       )
								else:
													SCREAMING_SNAKE_CASE_						   =							kwargs.pop('''audio'''				,				UpperCamelCase__       )
								SCREAMING_SNAKE_CASE_						   =							kwargs.pop('''sampling_rate'''				,				UpperCamelCase__       )
								SCREAMING_SNAKE_CASE_						   =							kwargs.pop('''text'''				,				UpperCamelCase__       )
								if len(UpperCamelCase__       ) > 0:
													SCREAMING_SNAKE_CASE_						   =							args[0]
													SCREAMING_SNAKE_CASE_						   =							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:
													SCREAMING_SNAKE_CASE_						   =							self.feature_extractor(UpperCamelCase__				,				*UpperCamelCase__				,				sampling_rate=UpperCamelCase__				,				**UpperCamelCase__       )
								if text is not None:
													SCREAMING_SNAKE_CASE_						   =							self.tokenizer(UpperCamelCase__				,				**UpperCamelCase__       )

								if text is None:
													return inputs
								elif audio is None:
													return encodings
								else:
													SCREAMING_SNAKE_CASE_						   =							encodings['''input_ids''']
													return inputs

			def _lowercase			(						self  :  str				,				*lowerCAmelCase_  :  Any				,				**lowerCAmelCase_  :  Optional[int]       )       ->       List[str]:




								"""simple docstring"""






								return self.tokenizer.batch_decode(*UpperCamelCase__				,				**UpperCamelCase__       )

			def _lowercase			(						self  :  Any				,				*lowerCAmelCase_  :  Union[str, Any]				,				**lowerCAmelCase_  :  Tuple       )       ->       Union[str, Any]:




								"""simple docstring"""






								if self._in_target_context_manager:
													return self.current_processor.pad(*UpperCamelCase__				,				**UpperCamelCase__       )

								SCREAMING_SNAKE_CASE_						   =							kwargs.pop('''input_features'''				,				UpperCamelCase__       )
								SCREAMING_SNAKE_CASE_						   =							kwargs.pop('''labels'''				,				UpperCamelCase__       )
								if len(UpperCamelCase__       ) > 0:
													SCREAMING_SNAKE_CASE_						   =							args[0]
													SCREAMING_SNAKE_CASE_						   =							args[1:]

								if input_features is not None:
													SCREAMING_SNAKE_CASE_						   =							self.feature_extractor.pad(UpperCamelCase__				,				*UpperCamelCase__				,				**UpperCamelCase__       )
								if labels is not None:
													SCREAMING_SNAKE_CASE_						   =							self.tokenizer.pad(UpperCamelCase__				,				**UpperCamelCase__       )

								if labels is None:
													return input_features
								elif input_features is None:
													return labels
								else:
													SCREAMING_SNAKE_CASE_						   =							labels['''input_ids''']
													return input_features

			def _lowercase			(						self  :  List[Any]				,				*lowerCAmelCase_  :  int				,				**lowerCAmelCase_  :  Tuple       )       ->       List[Any]:




								"""simple docstring"""






								return self.tokenizer.decode(*UpperCamelCase__				,				**UpperCamelCase__       )




			@contextmanager
			def _lowercase			(						self  :  List[str]       )       ->       Optional[Any]:




								"""simple docstring"""






								warnings.warn(
								    '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '''
								    '''labels by using the argument `text` of the regular `__call__` method (either in the same call as '''
								    '''your audio inputs, or in a separate call.'''       )
								SCREAMING_SNAKE_CASE_						   =							True
								SCREAMING_SNAKE_CASE_						   =							self.tokenizer
								yield
								SCREAMING_SNAKE_CASE_						   =							self.feature_extractor
								SCREAMING_SNAKE_CASE_						   =							False



'''simple docstring'''






import unittest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TextaTextGenerationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch
from transformers.utils import is_torch_available

from .test_pipelines_common import ANY


if is_torch_available():
   import torch







@is_pipeline_test
class lowercase_       (		unittest.TestCase			):







   """simple docstring"""

   __lowerCAmelCase         =   MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
   __lowerCAmelCase         =   TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
   def 	__UpperCAmelCase   (      self      :			Any, UpperCamelCase__      :			str, UpperCamelCase__      :			Optional[int], UpperCamelCase__      :			Any     )  ->			List[str]:
        _A    		=  TextaTextGenerationPipeline(model=UpperCamelCase__, tokenizer=UpperCamelCase__     )
        return generator, ["Something to write", "Something else"]
   def 	__UpperCAmelCase   (      self      :			Any, UpperCamelCase__      :			Tuple, UpperCamelCase__      :			Optional[Any]     )  ->			List[str]:
        _A    		=  generator('Something there'     )
        self.assertEqual(UpperCamelCase__, [{'generated_text': ANY(UpperCamelCase__     )}]     )
        # These are encoder decoder, they don't just append to incoming string
        self.assertFalse(outputs[0]['generated_text'].startswith('Something there'     )     )

        _A    		=  generator(['This is great !', 'Something else'], num_return_sequences=2, do_sample=UpperCamelCase__     )
        self.assertEqual(
            UpperCamelCase__, [
                [{'generated_text': ANY(UpperCamelCase__     )}, {'generated_text': ANY(UpperCamelCase__     )}],
                [{'generated_text': ANY(UpperCamelCase__     )}, {'generated_text': ANY(UpperCamelCase__     )}],
            ], )

        _A    		=  generator(
            ['This is great !', 'Something else'], num_return_sequences=2, batch_size=2, do_sample=UpperCamelCase__     )
        self.assertEqual(
            UpperCamelCase__, [
                [{'generated_text': ANY(UpperCamelCase__     )}, {'generated_text': ANY(UpperCamelCase__     )}],
                [{'generated_text': ANY(UpperCamelCase__     )}, {'generated_text': ANY(UpperCamelCase__     )}],
            ], )

        with self.assertRaises(UpperCamelCase__     ):
             generator(4     )
   @require_torch
   def 	__UpperCAmelCase   (      self      :			Union[str, Any]     )  ->			Optional[Any]:
        _A    		=  pipeline('text2text-generation', model='patrickvonplaten/t5-tiny-random', framework='pt'     )
        # do_sample=False necessary for reproducibility
        _A    		=  generator('Something there', do_sample=UpperCamelCase__     )
        self.assertEqual(UpperCamelCase__, [{'generated_text': ''}]     )

        _A    		=  3
        _A    		=  generator(
            'Something there', num_return_sequences=UpperCamelCase__, num_beams=UpperCamelCase__, )
        _A    		=  [
            {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide Beide'},
            {'generated_text': 'Beide Beide Beide Beide Beide Beide Beide Beide'},
            {'generated_text': ''},
        ]
        self.assertEqual(UpperCamelCase__, UpperCamelCase__     )

        _A    		=  generator('This is a test', do_sample=UpperCamelCase__, num_return_sequences=2, return_tensors=UpperCamelCase__     )
        self.assertEqual(
            UpperCamelCase__, [
                {'generated_token_ids': ANY(torch.Tensor     )},
                {'generated_token_ids': ANY(torch.Tensor     )},
            ], )
        _A    		=  generator.model.config.eos_token_id
        _A    		=  '<pad>'
        _A    		=  generator(
            ['This is a test', 'This is a second test'], do_sample=UpperCamelCase__, num_return_sequences=2, batch_size=2, return_tensors=UpperCamelCase__, )
        self.assertEqual(
            UpperCamelCase__, [
                [
                    {'generated_token_ids': ANY(torch.Tensor     )},
                    {'generated_token_ids': ANY(torch.Tensor     )},
                ],
                [
                    {'generated_token_ids': ANY(torch.Tensor     )},
                    {'generated_token_ids': ANY(torch.Tensor     )},
                ],
            ], )

   @require_tf
   def 	__UpperCAmelCase   (      self      :			Optional[Any]     )  ->			Optional[int]:
        _A    		=  pipeline('text2text-generation', model='patrickvonplaten/t5-tiny-random', framework='tf'     )
        # do_sample=False necessary for reproducibility
        _A    		=  generator('Something there', do_sample=UpperCamelCase__     )
        self.assertEqual(UpperCamelCase__, [{'generated_text': ''}]     )



