



"""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 	__snake_case	(     _lowercase):
  snake_case__    :       List[Any]        =						"microsoft/speecht5_tts"
  snake_case__    :       Dict        =						(
      "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."
  )
  snake_case__    :       Dict        =						"text_reader"
  snake_case__    :       Optional[Any]        =						SpeechTaProcessor
  snake_case__    :       List[str]        =						SpeechTaForTextToSpeech
  snake_case__    :       Union[str, Any]        =						SpeechTaHifiGan

  snake_case__    :       Dict        =						["text"]
  snake_case__    :       Any        =						["audio"]




  def 						SCREAMING_SNAKE_CASE    (			self					:						Dict		):






         """simple docstring"""






         if self.post_processor is None:
                _lowerCamelCase						:    Dict         =			'''microsoft/speecht5_hifigan'''
         super().setup()




  def 						SCREAMING_SNAKE_CASE    (			self					:						int						, __lowerCAmelCase					:						Tuple						, __lowerCAmelCase					:						str=None		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[int]         =			self.pre_processor(text=__lowerCAmelCase						, return_tensors='''pt'''						, truncation=__lowerCAmelCase		)

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

                _lowerCamelCase						:    int         =			load_dataset('''Matthijs/cmu-arctic-xvectors'''						, split='''validation'''		)
                _lowerCamelCase						:    int         =			torch.tensor(embeddings_dataset[7_3_0_5]['''xvector''']		).unsqueeze(0		)

         return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings}




  def 						SCREAMING_SNAKE_CASE    (			self					:						int						, __lowerCAmelCase					:						List[str]		):






         """simple docstring"""






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




  def 						SCREAMING_SNAKE_CASE    (			self					:						List[Any]						, __lowerCAmelCase					:						List[Any]		):






         """simple docstring"""






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







"""simple docstring"""






from torch import nn






class 	__snake_case	(     nn.Module):




  def __init__(			self					:						Optional[Any]						, __lowerCAmelCase					:						Dict						, __lowerCAmelCase					:						str		):






         """simple docstring"""






         super().__init__()
         _lowerCamelCase						:    List[str]         =			class_size
         _lowerCamelCase						:    List[str]         =			embed_size
         # self.mlp1 = nn.Linear(embed_size, embed_size)
         # self.mlp2 = (nn.Linear(embed_size, class_size))
         _lowerCamelCase						:    Optional[int]         =			nn.Linear(__lowerCAmelCase						, __lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE    (			self					:						List[Any]						, __lowerCAmelCase					:						Tuple		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[Any]         =			self.mlp(__lowerCAmelCase		)
         return logits


