



from ...processing_utils import ProcessorMixin





class    lowerCamelCase__  (						UpperCAmelCase_):
     """simple docstring"""



     _A       =			'SpeechT5FeatureExtractor'
     _A       =			'SpeechT5Tokenizer'







     def __init__(self				,			__a				,			__a    ):






           '''simple docstring'''



           super().__init__(__a				,			__a    )







     def __call__(self				,			*__a				,			**__a    ):






           '''simple docstring'''



           lowerCamelCase								=		kwargs.pop("audio"				,			__a    )
           lowerCamelCase								=		kwargs.pop("text"				,			__a    )
           lowerCamelCase								=		kwargs.pop("text_target"				,			__a    )
           lowerCamelCase								=		kwargs.pop("audio_target"				,			__a    )
           lowerCamelCase								=		kwargs.pop("sampling_rate"				,			__a    )

           if audio is not None and text is not None:
                 raise ValueError(
                     "Cannot process both `audio` and `text` inputs. Did you mean `audio_target` or `text_target`?"    )
           if audio_target is not None and text_target is not None:
                 raise ValueError(
                     "Cannot process both `audio_target` and `text_target` inputs. Did you mean `audio` or `text`?"    )
           if audio is None and audio_target is None and text is None and text_target is None:
                 raise ValueError(
                     "You need to specify either an `audio`, `audio_target`, `text`, or `text_target` input to process."    )

           if audio is not None:
                 lowerCamelCase								=		self.feature_extractor(__a				,			*__a				,			sampling_rate=__a				,			**__a    )
           elif text is not None:
                 lowerCamelCase								=		self.tokenizer(__a				,			**__a    )
           else:
                 lowerCamelCase								=		None

           if audio_target is not None:
                 lowerCamelCase								=		self.feature_extractor(audio_target=__a				,			*__a				,			sampling_rate=__a				,			**__a    )
                 lowerCamelCase								=		targets["input_values"]
           elif text_target is not None:
                 lowerCamelCase								=		self.tokenizer(__a				,			**__a    )
                 lowerCamelCase								=		targets["input_ids"]
           else:
                 lowerCamelCase								=		None

           if inputs is None:
                 return targets

           if targets is not None:
                 lowerCamelCase								=		labels

                 lowerCamelCase								=		targets.get("attention_mask"    )
                 if decoder_attention_mask is not None:
                       lowerCamelCase								=		decoder_attention_mask

           return inputs







     def 						_a      (self				,			*__a				,			**__a    ):






           '''simple docstring'''



           lowerCamelCase								=		kwargs.pop("input_values"				,			__a    )
           lowerCamelCase								=		kwargs.pop("input_ids"				,			__a    )
           lowerCamelCase								=		kwargs.pop("labels"				,			__a    )

           if input_values is not None and input_ids is not None:
                 raise ValueError("Cannot process both `input_values` and `input_ids` inputs."    )
           if input_values is None and input_ids is None and labels is None:
                 raise ValueError(
                     "You need to specify either an `input_values`, `input_ids`, or `labels` input to be padded."    )

           if input_values is not None:
                 lowerCamelCase								=		self.feature_extractor.pad(__a				,			*__a				,			**__a    )
           elif input_ids is not None:
                 lowerCamelCase								=		self.tokenizer.pad(__a				,			**__a    )
           else:
                 lowerCamelCase								=		None

           if labels is not None:
                 if "input_ids" in labels or (isinstance(__a				,			__a    ) and "input_ids" in labels[0]):
                       lowerCamelCase								=		self.tokenizer.pad(__a				,			**__a    )
                       lowerCamelCase								=		targets["input_ids"]
                 else:
                       lowerCamelCase								=		self.feature_extractor.feature_size
                       lowerCamelCase								=		self.feature_extractor.num_mel_bins
                       lowerCamelCase								=		self.feature_extractor.pad(__a				,			*__a				,			**__a    )
                       lowerCamelCase								=		feature_size_hack
                       lowerCamelCase								=		targets["input_values"]
           else:
                 lowerCamelCase								=		None

           if inputs is None:
                 return targets

           if targets is not None:
                 lowerCamelCase								=		labels

                 lowerCamelCase								=		targets.get("attention_mask"    )
                 if decoder_attention_mask is not None:
                       lowerCamelCase								=		decoder_attention_mask

           return inputs







     def 						_a      (self				,			*__a				,			**__a    ):






           '''simple docstring'''



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







     def 						_a      (self				,			*__a				,			**__a    ):






           '''simple docstring'''



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




from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch





@require_torch
@require_tf
class    lowerCamelCase__  (						UpperCAmelCase_):
     """simple docstring"""



     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		SMALL_MODEL_IDENTIFIER
           lowerCamelCase								=		"pt"
           lowerCamelCase								=		"tf"







     def 						_a      (self				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		AutoModel.from_pretrained(self.test_model    )
           model_pt.save_pretrained(__a    )







     def 						_a      (self				,			__a    ):






           '''simple docstring'''



           lowerCamelCase								=		TFAutoModel.from_pretrained(self.test_model				,			from_pt=__a    )
           model_tf.save_pretrained(__a    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		"mock_framework"

           # Framework provided - return whatever the user provides
           lowerCamelCase								=		FeaturesManager.determine_framework(self.test_model				,			__a    )
           self.assertEqual(__a				,			__a    )

           # Local checkpoint and framework provided - return provided framework
           # PyTorch checkpoint
           with TemporaryDirectory() as local_pt_ckpt:
                 self._setup_pt_ckpt(__a    )
                 lowerCamelCase								=		FeaturesManager.determine_framework(__a				,			__a    )
                 self.assertEqual(__a				,			__a    )

           # TensorFlow checkpoint
           with TemporaryDirectory() as local_tf_ckpt:
                 self._setup_tf_ckpt(__a    )
                 lowerCamelCase								=		FeaturesManager.determine_framework(__a				,			__a    )
                 self.assertEqual(__a				,			__a    )







     def 						_a      (self    ):






           '''simple docstring'''



           with TemporaryDirectory() as local_pt_ckpt:
                 self._setup_pt_ckpt(__a    )
                 lowerCamelCase								=		FeaturesManager.determine_framework(__a    )
                 self.assertEqual(__a				,			self.framework_pt    )

           # TensorFlow checkpoint
           with TemporaryDirectory() as local_tf_ckpt:
                 self._setup_tf_ckpt(__a    )
                 lowerCamelCase								=		FeaturesManager.determine_framework(__a    )
                 self.assertEqual(__a				,			self.framework_tf    )

           # Invalid local checkpoint
           with TemporaryDirectory() as local_invalid_ckpt:
                 with self.assertRaises(__a    ):
                       lowerCamelCase								=		FeaturesManager.determine_framework(__a    )







     def 						_a      (self    ):






           '''simple docstring'''



           lowerCamelCase								=		MagicMock(return_value=__a    )
           with patch("transformers.onnx.features.is_tf_available"				,			__a    ):
                 lowerCamelCase								=		FeaturesManager.determine_framework(self.test_model    )
                 self.assertEqual(__a				,			self.framework_pt    )

           # PyTorch not in environment -> use TensorFlow
           lowerCamelCase								=		MagicMock(return_value=__a    )
           with patch("transformers.onnx.features.is_torch_available"				,			__a    ):
                 lowerCamelCase								=		FeaturesManager.determine_framework(self.test_model    )
                 self.assertEqual(__a				,			self.framework_tf    )

           # Both in environment -> use PyTorch
           lowerCamelCase								=		MagicMock(return_value=__a    )
           lowerCamelCase								=		MagicMock(return_value=__a    )
           with patch("transformers.onnx.features.is_tf_available"				,			__a    ), patch(
               "transformers.onnx.features.is_torch_available"				,			__a    ):
                 lowerCamelCase								=		FeaturesManager.determine_framework(self.test_model    )
                 self.assertEqual(__a				,			self.framework_pt    )

           # Both not in environment -> raise error
           lowerCamelCase								=		MagicMock(return_value=__a    )
           lowerCamelCase								=		MagicMock(return_value=__a    )
           with patch("transformers.onnx.features.is_tf_available"				,			__a    ), patch(
               "transformers.onnx.features.is_torch_available"				,			__a    ):
                 with self.assertRaises(__a    ):
                       lowerCamelCase								=		FeaturesManager.determine_framework(self.test_model    )