





"""simple docstring"""






import unittest

from transformers import MPNetConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    MPNetForMaskedLM,
		    MPNetForMultipleChoice,
		    MPNetForQuestionAnswering,
		    MPNetForSequenceClassification,
		    MPNetForTokenClassification,
		    MPNetModel,
		)




class 			__lowercase     :







					'''simple docstring'''




					def __init__(  self		,     _UpperCAmelCase		,     _UpperCAmelCase=13		,     _UpperCAmelCase=7		,     _UpperCAmelCase=True		,     _UpperCAmelCase=True		,     _UpperCAmelCase=False		,     _UpperCAmelCase=True		,     _UpperCAmelCase=99		,     _UpperCAmelCase=64		,     _UpperCAmelCase=5		,     _UpperCAmelCase=4		,     _UpperCAmelCase=64		,     _UpperCAmelCase="gelu"		,     _UpperCAmelCase=0.1		,     _UpperCAmelCase=0.1		,     _UpperCAmelCase=512		,     _UpperCAmelCase=16		,     _UpperCAmelCase=2		,     _UpperCAmelCase=0.0_2		,     _UpperCAmelCase=3		,     _UpperCAmelCase=4		,     _UpperCAmelCase=None		,     ):
									__a	:  Union[str, Any]         =					parent
									__a	:  List[str]         =					batch_size
									__a	:  List[str]         =					seq_length
									__a	:  Union[str, Any]         =					is_training
									__a	:  Optional[int]         =					use_input_mask
									__a	:  Union[str, Any]         =					use_token_type_ids
									__a	:  List[Any]         =					use_labels
									__a	:  str         =					vocab_size
									__a	:  Dict         =					hidden_size
									__a	:  List[str]         =					num_hidden_layers
									__a	:  List[str]         =					num_attention_heads
									__a	:  Tuple         =					intermediate_size
									__a	:  str         =					hidden_act
									__a	:  List[Any]         =					hidden_dropout_prob
									__a	:  Union[str, Any]         =					attention_probs_dropout_prob
									__a	:  Dict         =					max_position_embeddings
									__a	:  Dict         =					type_vocab_size
									__a	:  Dict         =					type_sequence_label_size
									__a	:  List[str]         =					initializer_range
									__a	:  int         =					num_labels
									__a	:  Optional[int]         =					num_choices
									__a	:  Optional[int]         =					scope







					def 	_lowerCamelCase						(  self       ):
									return MPNetConfig.from_pretrained('''microsoft/mpnet-base'''       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[Any]         =					ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size       )

									__a	:  Dict         =					None
									if self.use_input_mask:
													__a	:  str         =					random_attention_mask([self.batch_size, self.seq_length]       )

									__a	:  Any         =					None
									__a	:  Any         =					None
									__a	:  Any         =					None
									if self.use_labels:
													__a	:  List[str]         =					ids_tensor([self.batch_size]		,     self.type_sequence_label_size       )
													__a	:  Dict         =					ids_tensor([self.batch_size, self.seq_length]		,     self.num_labels       )
													__a	:  Tuple         =					ids_tensor([self.batch_size]		,     self.num_choices       )

									__a	:  Dict         =					self.get_config()
									return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels







					def 	_lowerCamelCase						(  self       ):
									return MPNetConfig(
									    vocab_size=self.vocab_size		,     hidden_size=self.hidden_size		,     num_hidden_layers=self.num_hidden_layers		,     num_attention_heads=self.num_attention_heads		,     intermediate_size=self.intermediate_size		,     hidden_act=self.hidden_act		,     hidden_dropout_prob=self.hidden_dropout_prob		,     attention_probs_dropout_prob=self.attention_probs_dropout_prob		,     max_position_embeddings=self.max_position_embeddings		,     initializer_range=self.initializer_range		,     )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  List[Any]         =					MPNetModel(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  int         =					model(_UpperCAmelCase		,     _UpperCAmelCase       )
									__a	:  Any         =					model(_UpperCAmelCase       )
									self.parent.assertEqual(result.last_hidden_state.shape		,     (self.batch_size, self.seq_length, self.hidden_size)       )
									self.parent.assertEqual(result.pooler_output.shape		,     (self.batch_size, self.hidden_size)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Union[str, Any]         =					MPNetForQuestionAnswering(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  Tuple         =					model(
									    _UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     start_positions=_UpperCAmelCase		,     end_positions=_UpperCAmelCase		,     )
									self.parent.assertEqual(result.start_logits.shape		,     (self.batch_size, self.seq_length)       )
									self.parent.assertEqual(result.end_logits.shape		,     (self.batch_size, self.seq_length)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  List[Any]         =					self.num_labels
									__a	:  Optional[int]         =					MPNetForSequenceClassification(_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  List[str]         =					model(_UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     labels=_UpperCAmelCase       )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Tuple         =					self.num_choices
									__a	:  Union[str, Any]         =					MPNetForMultipleChoice(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  Dict         =					input_ids.unsqueeze(1       ).expand(-1		,     self.num_choices		,     -1       ).contiguous()
									__a	:  int         =					input_mask.unsqueeze(1       ).expand(-1		,     self.num_choices		,     -1       ).contiguous()
									__a	:  Optional[Any]         =					model(
									    _UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     labels=_UpperCAmelCase		,     )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_choices)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Tuple         =					self.num_labels
									__a	:  str         =					MPNetForTokenClassification(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  Optional[Any]         =					model(_UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     labels=_UpperCAmelCase       )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels)       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[Any]         =					self.prepare_config_and_inputs()
									((__a)    ,  (__a)    ,  (__a)    ,  (__a)    ,  (__a)    ,  (__a))	:  Dict         =					config_and_inputs
									__a	:  List[Any]         =					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
									return config, inputs_dict






@require_torch
class 			__lowercase     (     _UpperCamelCase				, _UpperCamelCase				, unittest.TestCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					(
					    (
					        MPNetForMaskedLM,
					        MPNetForMultipleChoice,
					        MPNetForQuestionAnswering,
					        MPNetForSequenceClassification,
					        MPNetForTokenClassification,
					        MPNetModel,
					    )
					    if is_torch_available()
					    else ()
					)
					__lowerCAmelCase					=					(
					    {
					        '''feature-extraction''': MPNetModel,
					        '''fill-mask''': MPNetForMaskedLM,
					        '''question-answering''': MPNetForQuestionAnswering,
					        '''text-classification''': MPNetForSequenceClassification,
					        '''token-classification''': MPNetForTokenClassification,
					        '''zero-shot''': MPNetForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					__lowerCAmelCase					=					False
					__lowerCAmelCase					=					True







					def 	_lowerCamelCase						(  self       ):
									__a	:  str         =					MPNetModelTester(self       )
									__a	:  List[str]         =					ConfigTester(self		,     config_class=_UpperCAmelCase		,     hidden_size=37       )







					def 	_lowerCamelCase						(  self       ):
									self.config_tester.run_common_tests()







					def 	_lowerCamelCase						(  self       ):
									__a	:  Any         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_model(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[str]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_sequence_classification(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[Any]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_multiple_choice(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Union[str, Any]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_token_classification(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Dict         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_question_answering(*_UpperCAmelCase       )






@require_torch
class 			__lowercase     (     unittest.TestCase							):







					'''simple docstring'''




					@slow
					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[int]         =					MPNetModel.from_pretrained('''microsoft/mpnet-base'''       )
									__a	:  List[Any]         =					torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]       )
									__a	:  List[Any]         =					model(_UpperCAmelCase       )[0]
									__a	:  str         =					torch.Size((1, 11, 768)       )
									self.assertEqual(output.shape		,     _UpperCAmelCase       )
									__a	:  Tuple         =					torch.tensor(
									    [[[-0.0_5_5_0, 0.1_9_4_3, -0.0_7_4_0], [-0.0_5_6_2, 0.2_2_1_1, -0.0_5_7_9], [-0.0_4_3_7, 0.3_3_3_7, -0.0_6_4_1]]]       )
									# compare the actual values for a slice.
									self.assertTrue(torch.allclose(output[:, :3, :3]		,     _UpperCAmelCase		,     atol=1e-4       )       )






"""simple docstring"""






# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor
from .base import PipelineTool




class 			__lowercase     (     _UpperCamelCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					'''openai/whisper-base'''
					__lowerCAmelCase					=					(
					    '''This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the '''
					    '''transcribed text.'''
					)
					__lowerCAmelCase					=					'''transcriber'''
					__lowerCAmelCase					=					WhisperProcessor
					__lowerCAmelCase					=					WhisperForConditionalGeneration

					__lowerCAmelCase					=					['''audio''']
					__lowerCAmelCase					=					['''text''']







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase       ):
									return self.pre_processor(_UpperCAmelCase		,     return_tensors='''pt'''       ).input_features







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase       ):
									return self.model.generate(inputs=_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase       ):
									return self.pre_processor.batch_decode(_UpperCAmelCase		,     skip_special_tokens=_UpperCAmelCase       )[0]