
"""simple docstring"""




from __future__ import annotations

import unittest

from transformers import 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 numpy
  import tensorflow as tf

  from transformers import (
      TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
      TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
      TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
      BertConfig,
      DPRConfig,
      TFDPRContextEncoder,
      TFDPRQuestionEncoder,
      TFDPRReader,
  )


class       __magic_name__							:
       def __init__(  self			,					A_			,					A_=13			,					A_=7			,					A_=True			,					A_=True			,					A_=True			,					A_=True			,					A_=99			,					A_=32			,					A_=2			,					A_=4			,					A_=37			,					A_="gelu"			,					A_=0.1			,					A_=0.1			,					A_=512			,					A_=16			,					A_=2			,					A_=0.02			,					A_=3			,					A_=4			,					A_=None			,					A_=0			,					)  ->						Any:





         """simple docstring"""




         _lowercase:      List[Any]   			=  parent
         _lowercase:      Any   			=  batch_size
         _lowercase:      str   			=  seq_length
         _lowercase:      Any   			=  is_training
         _lowercase:      Any   			=  use_input_mask
         _lowercase:      str   			=  use_token_type_ids
         _lowercase:      Union[str, Any]   			=  use_labels
         _lowercase:      Optional[int]   			=  vocab_size
         _lowercase:      int   			=  hidden_size
         _lowercase:      int   			=  num_hidden_layers
         _lowercase:      Union[str, Any]   			=  num_attention_heads
         _lowercase:      Union[str, Any]   			=  intermediate_size
         _lowercase:      Any   			=  hidden_act
         _lowercase:      int   			=  hidden_dropout_prob
         _lowercase:      Dict   			=  attention_probs_dropout_prob
         _lowercase:      Tuple   			=  max_position_embeddings
         _lowercase:      Tuple   			=  type_vocab_size
         _lowercase:      str   			=  type_sequence_label_size
         _lowercase:      Optional[Any]   			=  initializer_range
         _lowercase:      List[str]   			=  num_labels
         _lowercase:      Union[str, Any]   			=  num_choices
         _lowercase:      Tuple   			=  scope
         _lowercase:      Optional[int]   			=  projection_dim
       def 				lowercase_				(  self							)  ->						Dict:





         """simple docstring"""




         _lowercase:      Optional[int]   			=  ids_tensor([self.batch_size, self.seq_length]			,					self.vocab_size							)

         _lowercase:      Optional[int]   			=  None
         if self.use_input_mask:
           # follow test_modeling_tf_ctrl.py
           _lowercase:      Tuple   			=  random_attention_mask([self.batch_size, self.seq_length]							)

         _lowercase:      Optional[Any]   			=  None
         if self.use_token_type_ids:
           _lowercase:      List[str]   			=  ids_tensor([self.batch_size, self.seq_length]			,					self.type_vocab_size							)

         _lowercase:      Optional[Any]   			=  None
         _lowercase:      Optional[Any]   			=  None
         _lowercase:      Union[str, Any]   			=  None
         if self.use_labels:
           _lowercase:      Any   			=  ids_tensor([self.batch_size]			,					self.type_sequence_label_size							)
           _lowercase:      Tuple   			=  ids_tensor([self.batch_size, self.seq_length]			,					self.num_labels							)
           _lowercase:      str   			=  ids_tensor([self.batch_size]			,					self.num_choices							)

         _lowercase:      Tuple   			=  BertConfig(
             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			,					type_vocab_size=self.type_vocab_size			,					is_decoder=A_			,					initializer_range=self.initializer_range			,					)
         _lowercase:      List[Any]   			=  DPRConfig(projection_dim=self.projection_dim			,					**config.to_dict()							)

         return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
       def 				lowercase_				(  self			,					A_			,					A_			,					A_			,					A_			,					A_			,					A_			,					A_							)  ->						str:





         """simple docstring"""




         _lowercase:      Optional[Any]   			=  TFDPRContextEncoder(config=A_							)
         _lowercase:      Union[str, Any]   			=  model(A_			,					attention_mask=A_			,					token_type_ids=A_							)
         _lowercase:      int   			=  model(A_			,					token_type_ids=A_							)
         _lowercase:      Dict   			=  model(A_							)
         self.parent.assertEqual(result.pooler_output.shape			,					(self.batch_size, self.projection_dim or self.hidden_size)							)
       def 				lowercase_				(  self			,					A_			,					A_			,					A_			,					A_			,					A_			,					A_			,					A_							)  ->						str:





         """simple docstring"""




         _lowercase:      List[str]   			=  TFDPRQuestionEncoder(config=A_							)
         _lowercase:      Any   			=  model(A_			,					attention_mask=A_			,					token_type_ids=A_							)
         _lowercase:      Any   			=  model(A_			,					token_type_ids=A_							)
         _lowercase:      List[Any]   			=  model(A_							)
         self.parent.assertEqual(result.pooler_output.shape			,					(self.batch_size, self.projection_dim or self.hidden_size)							)
       def 				lowercase_				(  self			,					A_			,					A_			,					A_			,					A_			,					A_			,					A_			,					A_							)  ->						int:





         """simple docstring"""




         _lowercase:      Union[str, Any]   			=  TFDPRReader(config=A_							)
         _lowercase:      Tuple   			=  model(A_			,					attention_mask=A_							)

         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)							)
         self.parent.assertEqual(result.relevance_logits.shape			,					(self.batch_size,)							)



       def 				lowercase_				(  self							)  ->						str:





         """simple docstring"""




         _lowercase:      List[str]   			=  self.prepare_config_and_inputs()
         (
             (
             _lowercase
         )			,					(
             _lowercase
         )			,					(
             _lowercase
         )			,					(
             _lowercase
         )			,					(
             _lowercase
         )			,					(
             _lowercase
         )			,					(
             _lowercase
         )			,					
         ):      Union[str, Any]   			=  config_and_inputs
         _lowercase:      Any   			=  {'''input_ids''': input_ids}
         return config, inputs_dict


@require_tf
class       __magic_name__							(							SCREAMING_SNAKE_CASE__ ,       SCREAMING_SNAKE_CASE__ ,       unittest.TestCase			):
       UpperCamelCase_ =		(
           (
               TFDPRContextEncoder,
               TFDPRQuestionEncoder,
               TFDPRReader,
           )
           if is_tf_available()
           else ()
       )
       UpperCamelCase_ =		{'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

       UpperCamelCase_ =		False
       UpperCamelCase_ =		False
       UpperCamelCase_ =		False
       UpperCamelCase_ =		False
       UpperCamelCase_ =		False
       def 				lowercase_				(  self							)  ->						Union[str, Any]:





         """simple docstring"""




         _lowercase:      Optional[int]   			=  TFDPRModelTester(self							)
         _lowercase:      Union[str, Any]   			=  ConfigTester(self			,					config_class=A_			,					hidden_size=37							)
       def 				lowercase_				(  self							)  ->						Dict:





         """simple docstring"""




         self.config_tester.run_common_tests()
       def 				lowercase_				(  self							)  ->						Dict:





         """simple docstring"""




         _lowercase:      Tuple   			=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_dpr_context_encoder(*A_							)
       def 				lowercase_				(  self							)  ->						Any:





         """simple docstring"""




         _lowercase:      Tuple   			=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_dpr_question_encoder(*A_							)
       def 				lowercase_				(  self							)  ->						List[Any]:





         """simple docstring"""




         _lowercase:      Dict   			=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_dpr_reader(*A_							)



       @slow
       def 				lowercase_				(  self							)  ->						Tuple:





         """simple docstring"""




         for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowercase:      List[Any]   			=  TFDPRContextEncoder.from_pretrained(A_							)
           self.assertIsNotNone(A_							)

         for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowercase:      List[str]   			=  TFDPRContextEncoder.from_pretrained(A_							)
           self.assertIsNotNone(A_							)

         for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowercase:      Dict   			=  TFDPRQuestionEncoder.from_pretrained(A_							)
           self.assertIsNotNone(A_							)

         for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowercase:      Optional[int]   			=  TFDPRReader.from_pretrained(A_							)
           self.assertIsNotNone(A_							)


@require_tf
class       __magic_name__							(							unittest.TestCase			):
       @slow
       def 				lowercase_				(  self							)  ->						List[Any]:





         """simple docstring"""




         _lowercase:      str   			=  TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''							)

         _lowercase:      List[str]   			=  tf.constant(
             [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]							)  # [CLS] hello, is my dog cute? [SEP]
         _lowercase:      Optional[Any]   			=  model(A_							)[0]  # embedding shape = (1, 768)
         # compare the actual values for a slice.
         _lowercase:      Optional[int]   			=  tf.constant(
             [
                 [
                     0.03_23_62_53,
                     0.12_75_33_35,
                     0.16_81_85_09,
                     0.00_27_97_86,
                     0.3_89_69_33,
                     0.24_26_49_45,
                     0.2_17_89_71,
                     -0.02_33_52_27,
                     -0.08_48_19_59,
                     -0.14_32_41_17,
                 ]
             ]							)
         self.assertTrue(numpy.allclose(output[:, :10].numpy()			,					expected_slice.numpy()			,					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.
import torch

from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor
from ..utils import is_datasets_available
from .base import PipelineTool


if is_datasets_available():
  from datasets import load_dataset


class       __magic_name__							(							SCREAMING_SNAKE_CASE__			):
       UpperCamelCase_ =		'''microsoft/speecht5_tts'''
       UpperCamelCase_ =		(
           '''This is a tool that reads an English text out loud. It takes an input named `text` which should contain the '''
           '''text to read (in English) and returns a waveform object containing the sound.'''
       )
       UpperCamelCase_ =		'''text_reader'''
       UpperCamelCase_ =		SpeechTaProcessor
       UpperCamelCase_ =		SpeechTaForTextToSpeech
       UpperCamelCase_ =		SpeechTaHifiGan

       UpperCamelCase_ =		['''text''']
       UpperCamelCase_ =		['''audio''']
       def 				lowercase_				(  self							)  ->						Union[str, Any]:





         """simple docstring"""




         if self.post_processor is None:
           _lowercase:      Tuple   			=  '''microsoft/speecht5_hifigan'''
         super().setup()
       def 				lowercase_				(  self			,					A_			,					A_=None							)  ->						Optional[int]:





         """simple docstring"""




         _lowercase:      Dict   			=  self.pre_processor(text=A_			,					return_tensors='''pt'''			,					truncation=A_							)

         if speaker_embeddings is None:
           if not is_datasets_available():
             raise ImportError('''Datasets needs to be installed if not passing speaker embeddings.'''							)

           _lowercase:      Union[str, Any]   			=  load_dataset('''Matthijs/cmu-arctic-xvectors'''			,					split='''validation'''							)
           _lowercase:      Optional[int]   			=  torch.tensor(embeddings_dataset[7305]['''xvector''']							).unsqueeze(0							)

         return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings}
       def 				lowercase_				(  self			,					A_							)  ->						Optional[int]:





         """simple docstring"""




         with torch.no_grad():
           return self.model.generate_speech(**A_							)



       def 				lowercase_				(  self			,					A_							)  ->						Dict:





         """simple docstring"""




         with torch.no_grad():
           return self.post_processor(A_							).cpu().detach()
