
from __future__ import annotations

import unittest

from transformers import DistilBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers.models.distilbert.modeling_tf_distilbert import (
       TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
       TFDistilBertForMaskedLM,
       TFDistilBertForMultipleChoice,
       TFDistilBertForQuestionAnswering,
       TFDistilBertForSequenceClassification,
       TFDistilBertForTokenClassification,
       TFDistilBertModel,
   )






class   UpperCAmelCase		:



   def __init__(self		:					List[str]    ,						snake_case__		:					Tuple    ,						)		->	Dict:


          '''simple docstring'''


          snake_case       :   Dict							=      parent
          snake_case       :   Optional[Any]							=      13
          snake_case       :   Dict							=      7
          snake_case       :   Optional[Any]							=      True
          snake_case       :   Optional[Any]							=      True
          snake_case       :   Optional[Any]							=      False
          snake_case       :   int							=      True
          snake_case       :   int							=      99
          snake_case       :   List[Any]							=      32
          snake_case       :   str							=      2
          snake_case       :   Optional[Any]							=      4
          snake_case       :   int							=      37
          snake_case       :   Any							=      "gelu"
          snake_case       :   List[str]							=      0.1
          snake_case       :   Union[str, Any]							=      0.1
          snake_case       :   Any							=      5_12
          snake_case       :   List[Any]							=      16
          snake_case       :   Any							=      2
          snake_case       :   Optional[Any]							=      0.02
          snake_case       :   List[str]							=      3
          snake_case       :   Any							=      4
          snake_case       :   Dict							=      None



   def 				_SCREAMING_SNAKE_CASE   (self		:					int )		->	Tuple:


          '''simple docstring'''


          snake_case       :   Union[str, Any]							=      ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size )

          snake_case       :   Optional[Any]							=      None
          if self.use_input_mask:
                 snake_case       :   Union[str, Any]							=      random_attention_mask([self.batch_size, self.seq_length] )

          snake_case       :   Optional[Any]							=      None
          snake_case       :   Optional[int]							=      None
          snake_case       :   Tuple							=      None
          if self.use_labels:
                 snake_case       :   Any							=      ids_tensor([self.batch_size]    ,						self.type_sequence_label_size )
                 snake_case       :   List[Any]							=      ids_tensor([self.batch_size, self.seq_length]    ,						self.num_labels )
                 snake_case       :   Tuple							=      ids_tensor([self.batch_size]    ,						self.num_choices )

          snake_case       :   Optional[int]							=      DistilBertConfig(
              vocab_size=self.vocab_size    ,						dim=self.hidden_size    ,						n_layers=self.num_hidden_layers    ,						n_heads=self.num_attention_heads    ,						hidden_dim=self.intermediate_size    ,						hidden_act=self.hidden_act    ,						dropout=self.hidden_dropout_prob    ,						attention_dropout=self.attention_probs_dropout_prob    ,						max_position_embeddings=self.max_position_embeddings    ,						initializer_range=self.initializer_range    ,						)

          return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels



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


          '''simple docstring'''


          snake_case       :   List[str]							=      TFDistilBertModel(config=UpperCAmelCase__ )
          snake_case       :   Tuple							=      {"input_ids": input_ids, "attention_mask": input_mask}

          snake_case       :   Optional[int]							=      model(UpperCAmelCase__ )

          snake_case       :   int							=      [input_ids, input_mask]

          snake_case       :   Union[str, Any]							=      model(UpperCAmelCase__ )

          self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.hidden_size) )



   def 				_SCREAMING_SNAKE_CASE   (self		:					Tuple    ,						snake_case__		:					Optional[Any]    ,						snake_case__		:					Any    ,						snake_case__		:					Tuple    ,						snake_case__		:					Any    ,						snake_case__		:					Union[str, Any]    ,						snake_case__		:					List[str] )		->	Optional[int]:


          '''simple docstring'''


          snake_case       :   str							=      TFDistilBertForMaskedLM(config=UpperCAmelCase__ )
          snake_case       :   Optional[Any]							=      {"input_ids": input_ids, "attention_mask": input_mask}
          snake_case       :   Union[str, Any]							=      model(UpperCAmelCase__ )
          self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.vocab_size) )



   def 				_SCREAMING_SNAKE_CASE   (self		:					int    ,						snake_case__		:					Optional[Any]    ,						snake_case__		:					List[str]    ,						snake_case__		:					Dict    ,						snake_case__		:					Any    ,						snake_case__		:					Dict    ,						snake_case__		:					Any )		->	int:


          '''simple docstring'''


          snake_case       :   Optional[Any]							=      TFDistilBertForQuestionAnswering(config=UpperCAmelCase__ )
          snake_case       :   Tuple							=      {
              "input_ids": input_ids,
              "attention_mask": input_mask,
          }
          snake_case       :   Optional[int]							=      model(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 				_SCREAMING_SNAKE_CASE   (self		:					Any    ,						snake_case__		:					Optional[int]    ,						snake_case__		:					Any    ,						snake_case__		:					Optional[int]    ,						snake_case__		:					List[Any]    ,						snake_case__		:					List[str]    ,						snake_case__		:					Any )		->	Union[str, Any]:


          '''simple docstring'''


          snake_case       :   Union[str, Any]							=      self.num_labels
          snake_case       :   List[str]							=      TFDistilBertForSequenceClassification(UpperCAmelCase__ )
          snake_case       :   str							=      {"input_ids": input_ids, "attention_mask": input_mask}
          snake_case       :   Dict							=      model(UpperCAmelCase__ )
          self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_labels) )



   def 				_SCREAMING_SNAKE_CASE   (self		:					Optional[int]    ,						snake_case__		:					str    ,						snake_case__		:					Optional[int]    ,						snake_case__		:					Dict    ,						snake_case__		:					int    ,						snake_case__		:					List[Any]    ,						snake_case__		:					Tuple )		->	str:


          '''simple docstring'''


          snake_case       :   Dict							=      self.num_choices
          snake_case       :   Tuple							=      TFDistilBertForMultipleChoice(UpperCAmelCase__ )
          snake_case       :   Tuple							=      tf.tile(tf.expand_dims(UpperCAmelCase__    ,						1 )    ,						(1, self.num_choices, 1) )
          snake_case       :   Dict							=      tf.tile(tf.expand_dims(UpperCAmelCase__    ,						1 )    ,						(1, self.num_choices, 1) )
          snake_case       :   Tuple							=      {
              "input_ids": multiple_choice_inputs_ids,
              "attention_mask": multiple_choice_input_mask,
          }
          snake_case       :   Tuple							=      model(UpperCAmelCase__ )
          self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_choices) )



   def 				_SCREAMING_SNAKE_CASE   (self		:					str    ,						snake_case__		:					Union[str, Any]    ,						snake_case__		:					str    ,						snake_case__		:					Any    ,						snake_case__		:					Optional[int]    ,						snake_case__		:					int    ,						snake_case__		:					List[Any] )		->	int:


          '''simple docstring'''


          snake_case       :   Any							=      self.num_labels
          snake_case       :   Optional[Any]							=      TFDistilBertForTokenClassification(UpperCAmelCase__ )
          snake_case       :   Dict							=      {"input_ids": input_ids, "attention_mask": input_mask}
          snake_case       :   Dict							=      model(UpperCAmelCase__ )
          self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.num_labels) )



   def 				_SCREAMING_SNAKE_CASE   (self		:					Dict )		->	Dict:


          '''simple docstring'''


          snake_case       :   Optional[int]							=      self.prepare_config_and_inputs()
          ((snake_case)				,					(snake_case)				,					(snake_case)				,					(snake_case)				,					(snake_case)				,					(snake_case))       :   List[str]							=      config_and_inputs
          snake_case       :   str							=      {"input_ids": input_ids, "attention_mask": input_mask}
          return config, inputs_dict






@require_tf
class   UpperCAmelCase		(						A_       ,A_       ,unittest.TestCase ):
   A__					:		int						     =     (
       (
           TFDistilBertModel,
           TFDistilBertForMaskedLM,
           TFDistilBertForQuestionAnswering,
           TFDistilBertForSequenceClassification,
           TFDistilBertForTokenClassification,
           TFDistilBertForMultipleChoice,
       )
       if is_tf_available()
       else None
   )
   A__					:		Union[str, Any]						     =     (
       {
           "feature-extraction": TFDistilBertModel,
           "fill-mask": TFDistilBertForMaskedLM,
           "question-answering": TFDistilBertForQuestionAnswering,
           "text-classification": TFDistilBertForSequenceClassification,
           "token-classification": TFDistilBertForTokenClassification,
           "zero-shot": TFDistilBertForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   A__					:		str						     =     False
   A__					:		List[str]						     =     False



   def 				_SCREAMING_SNAKE_CASE   (self		:					Optional[int] )		->	str:


          '''simple docstring'''


          snake_case       :   Dict							=      TFDistilBertModelTester(self )
          snake_case       :   List[Any]							=      ConfigTester(self    ,						config_class=UpperCAmelCase__    ,						dim=37 )



   def 				_SCREAMING_SNAKE_CASE   (self		:					str )		->	Tuple:


          '''simple docstring'''


          self.config_tester.run_common_tests()



   def 				_SCREAMING_SNAKE_CASE   (self		:					Optional[int] )		->	Dict:


          '''simple docstring'''


          snake_case       :   Any							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_model(*UpperCAmelCase__ )



   def 				_SCREAMING_SNAKE_CASE   (self		:					Dict )		->	int:


          '''simple docstring'''


          snake_case       :   List[Any]							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_masked_lm(*UpperCAmelCase__ )



   def 				_SCREAMING_SNAKE_CASE   (self		:					List[Any] )		->	Tuple:


          '''simple docstring'''


          snake_case       :   Any							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_question_answering(*UpperCAmelCase__ )



   def 				_SCREAMING_SNAKE_CASE   (self		:					Dict )		->	List[str]:


          '''simple docstring'''


          snake_case       :   Optional[int]							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_sequence_classification(*UpperCAmelCase__ )



   def 				_SCREAMING_SNAKE_CASE   (self		:					List[str] )		->	int:


          '''simple docstring'''


          snake_case       :   int							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_multiple_choice(*UpperCAmelCase__ )



   def 				_SCREAMING_SNAKE_CASE   (self		:					List[str] )		->	Optional[Any]:


          '''simple docstring'''


          snake_case       :   List[Any]							=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_distilbert_for_token_classification(*UpperCAmelCase__ )



   @slow
   def 				_SCREAMING_SNAKE_CASE   (self		:					str )		->	List[Any]:


          '''simple docstring'''


          for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1] ):
                 snake_case       :   int							=      TFDistilBertModel.from_pretrained(UpperCAmelCase__ )
                 self.assertIsNotNone(UpperCAmelCase__ )






@require_tf
class   UpperCAmelCase		(						unittest.TestCase ):



   @slow
   def 				_SCREAMING_SNAKE_CASE   (self		:					Union[str, Any] )		->	Tuple:


          '''simple docstring'''


          snake_case       :   List[str]							=      TFDistilBertModel.from_pretrained("distilbert-base-uncased" )
          snake_case       :   Optional[Any]							=      tf.constant([[0, 1, 2, 3, 4, 5]] )
          snake_case       :   Optional[int]							=      model(UpperCAmelCase__ )[0]

          snake_case       :   Any							=      [1, 6, 7_68]
          self.assertEqual(output.shape    ,						UpperCAmelCase__ )

          snake_case       :   List[str]							=      tf.constant(
              [
                  [
                      [0.19261885, -0.13732955, 0.4119799],
                      [0.22150156, -0.07422661, 0.39037204],
                      [0.22756018, -0.0896414, 0.3701467],
                  ]
              ] )
          tf.debugging.assert_near(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 					_lowerCAmelCase  (	lowercase   ):







 """simple docstring"""







 __UpperCAmelCase	:					Tuple   		=	"openai/whisper-base"
 __UpperCAmelCase	:					Union[str, Any]   		=	(
     "This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the "
     "transcribed text."
 )
 __UpperCAmelCase	:					List[str]   		=	"transcriber"
 __UpperCAmelCase	:					Optional[Any]   		=	WhisperProcessor
 __UpperCAmelCase	:					str   		=	WhisperForConditionalGeneration

 __UpperCAmelCase	:					List[str]   		=	["audio"]
 __UpperCAmelCase	:					Tuple   		=	["text"]


 def 					_lowercase (			self							:			str,					UpperCAmelCase__							:			int	):
    return self.pre_processor(UpperCAmelCase__,					return_tensors="pt"	).input_features


 def 					_lowercase (			self							:			Union[str, Any],					UpperCAmelCase__							:			Optional[Any]	):
    return self.model.generate(inputs=UpperCAmelCase__	)



 def 					_lowercase (			self							:			Dict,					UpperCAmelCase__							:			Optional[int]	):
    return self.pre_processor.batch_decode(UpperCAmelCase__,					skip_special_tokens=UpperCAmelCase__	)[0]




