"""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 			UpperCamelCase__(	UpperCamelCase_       ):
						lowerCAmelCase__     :  Dict  							=						'microsoft/speecht5_tts'
						lowerCAmelCase__     :  Optional[int]  							=						(
						    '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.'
						)
						lowerCAmelCase__     :  Union[str, Any]  							=						'text_reader'
						lowerCAmelCase__     :  Union[str, Any]  							=						SpeechTaProcessor
						lowerCAmelCase__     :  Union[str, Any]  							=						SpeechTaForTextToSpeech
						lowerCAmelCase__     :  str  							=						SpeechTaHifiGan

						lowerCAmelCase__     :  List[str]  							=						['text']
						lowerCAmelCase__     :  Optional[int]  							=						['audio']





						def  snake_case__		(				self   )    -> Dict:




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





						def  snake_case__		(				self						,__UpperCAmelCase						,__UpperCAmelCase=None   )    -> Tuple:
													A__		   =							self.pre_processor(text=lowercase_						,return_tensors='pt'						,truncation=lowercase_   )

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

																				A__		   =							load_dataset('Matthijs/cmu-arctic-xvectors'						,split='validation'   )
																				A__		   =							torch.tensor(embeddings_dataset[73_05]['xvector']   ).unsqueeze(0   )

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





						def  snake_case__		(				self						,__UpperCAmelCase   )    -> Any:




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





						def  snake_case__		(				self						,__UpperCAmelCase   )    -> Optional[int]:




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









import flax.linen as nn
import jax.numpy as jnp

from .attention_flax import FlaxTransformeraDModel
from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD




class A  (   nn.Module     ):
      UpperCamelCase__							:  int
      UpperCamelCase__							:  int
      UpperCamelCase__							:  float      =0.0
      UpperCamelCase__							:  int      =1
      UpperCamelCase__							:  int      =1
      UpperCamelCase__							:  bool      =True
      UpperCamelCase__							:  bool      =False
      UpperCamelCase__							:  bool      =False
      UpperCamelCase__							:  bool      =False
      UpperCamelCase__							:  jnp.dtype      =jnp.floataa






      def        lowerCamelCase       (					self						:     Any )					->						Any:
             """simple docstring"""




             _lowerCamelCase       :		str		=[]
             _lowerCamelCase       :		Dict		=[]

             for i in range(self.num_layers ):
                    _lowerCamelCase       :		Union[str, Any]		=self.in_channels if i == 0 else self.out_channels

                    _lowerCamelCase       :		Any		=FlaxResnetBlockaD(
                        in_channels=lowercase_			,   out_channels=self.out_channels			,   dropout_prob=self.dropout			,   dtype=self.dtype			,   )
                    resnets.append(lowercase_ )

                    _lowerCamelCase       :		Dict		=FlaxTransformeraDModel(
                        in_channels=self.out_channels			,   n_heads=self.num_attention_heads			,   d_head=self.out_channels // self.num_attention_heads			,   depth=1			,   use_linear_projection=self.use_linear_projection			,   only_cross_attention=self.only_cross_attention			,   use_memory_efficient_attention=self.use_memory_efficient_attention			,   dtype=self.dtype			,   )
                    attentions.append(lowercase_ )

             _lowerCamelCase       :		Optional[int]		=resnets
             _lowerCamelCase       :		Dict		=attentions

             if self.add_downsample:
                    _lowerCamelCase       :		Union[str, Any]		=FlaxDownsampleaD(self.out_channels			,   dtype=self.dtype )






      def __call__(					self						:     Any			,   lowercase_						:     Optional[Any]			,   lowercase_						:     List[Any]			,   lowercase_						:     Union[str, Any]			,   lowercase_						:     List[str]=True )					->						Union[str, Any]:
             """simple docstring"""




             _lowerCamelCase       :		Tuple		=()

             for resnet, attn in zip(self.resnets			,   self.attentions ):
                    _lowerCamelCase       :		Union[str, Any]		=resnet(lowercase_			,   lowercase_			,   deterministic=lowercase_ )
                    _lowerCamelCase       :		Union[str, Any]		=attn(lowercase_			,   lowercase_			,   deterministic=lowercase_ )
                    output_states += (hidden_states,)

             if self.add_downsample:
                    _lowerCamelCase       :		Optional[int]		=self.downsamplers_a(lowercase_ )
                    output_states += (hidden_states,)

             return hidden_states, output_states




class A  (   nn.Module     ):
      UpperCamelCase__							:  int
      UpperCamelCase__							:  int
      UpperCamelCase__							:  float      =0.0
      UpperCamelCase__							:  int      =1
      UpperCamelCase__							:  bool      =True
      UpperCamelCase__							:  jnp.dtype      =jnp.floataa






      def        lowerCamelCase       (					self						:     Optional[int] )					->						int:
             """simple docstring"""




             _lowerCamelCase       :		str		=[]

             for i in range(self.num_layers ):
                    _lowerCamelCase       :		Tuple		=self.in_channels if i == 0 else self.out_channels

                    _lowerCamelCase       :		Any		=FlaxResnetBlockaD(
                        in_channels=lowercase_			,   out_channels=self.out_channels			,   dropout_prob=self.dropout			,   dtype=self.dtype			,   )
                    resnets.append(lowercase_ )
             _lowerCamelCase       :		Union[str, Any]		=resnets

             if self.add_downsample:
                    _lowerCamelCase       :		List[str]		=FlaxDownsampleaD(self.out_channels			,   dtype=self.dtype )






      def __call__(					self						:     Any			,   lowercase_						:     Dict			,   lowercase_						:     Union[str, Any]			,   lowercase_						:     Optional[Any]=True )					->						Dict:
             """simple docstring"""




             _lowerCamelCase       :		Optional[int]		=()

             for resnet in self.resnets:
                    _lowerCamelCase       :		Tuple		=resnet(lowercase_			,   lowercase_			,   deterministic=lowercase_ )
                    output_states += (hidden_states,)

             if self.add_downsample:
                    _lowerCamelCase       :		Tuple		=self.downsamplers_a(lowercase_ )
                    output_states += (hidden_states,)

             return hidden_states, output_states




class A  (   nn.Module     ):
      UpperCamelCase__							:  int
      UpperCamelCase__							:  int
      UpperCamelCase__							:  int
      UpperCamelCase__							:  float      =0.0
      UpperCamelCase__							:  int      =1
      UpperCamelCase__							:  int      =1
      UpperCamelCase__							:  bool      =True
      UpperCamelCase__							:  bool      =False
      UpperCamelCase__							:  bool      =False
      UpperCamelCase__							:  bool      =False
      UpperCamelCase__							:  jnp.dtype      =jnp.floataa






      def        lowerCamelCase       (					self						:     Dict )					->						Tuple:
             """simple docstring"""




             _lowerCamelCase       :		str		=[]
             _lowerCamelCase       :		List[str]		=[]

             for i in range(self.num_layers ):
                    _lowerCamelCase       :		List[str]		=self.in_channels if (i == self.num_layers - 1) else self.out_channels
                    _lowerCamelCase       :		Tuple		=self.prev_output_channel if i == 0 else self.out_channels

                    _lowerCamelCase       :		List[str]		=FlaxResnetBlockaD(
                        in_channels=resnet_in_channels + res_skip_channels			,   out_channels=self.out_channels			,   dropout_prob=self.dropout			,   dtype=self.dtype			,   )
                    resnets.append(lowercase_ )

                    _lowerCamelCase       :		Tuple		=FlaxTransformeraDModel(
                        in_channels=self.out_channels			,   n_heads=self.num_attention_heads			,   d_head=self.out_channels // self.num_attention_heads			,   depth=1			,   use_linear_projection=self.use_linear_projection			,   only_cross_attention=self.only_cross_attention			,   use_memory_efficient_attention=self.use_memory_efficient_attention			,   dtype=self.dtype			,   )
                    attentions.append(lowercase_ )

             _lowerCamelCase       :		int		=resnets
             _lowerCamelCase       :		Dict		=attentions

             if self.add_upsample:
                    _lowerCamelCase       :		str		=FlaxUpsampleaD(self.out_channels			,   dtype=self.dtype )






      def __call__(					self						:     Optional[Any]			,   lowercase_						:     Any			,   lowercase_						:     Any			,   lowercase_						:     List[Any]			,   lowercase_						:     Dict			,   lowercase_						:     Union[str, Any]=True )					->						Optional[int]:
             """simple docstring"""




             for resnet, attn in zip(self.resnets			,   self.attentions ):
                    # pop res hidden states
                    _lowerCamelCase       :		Optional[int]		=res_hidden_states_tuple[-1]
                    _lowerCamelCase       :		Union[str, Any]		=res_hidden_states_tuple[:-1]
                    _lowerCamelCase       :		Any		=jnp.concatenate((hidden_states, res_hidden_states)			,   axis=-1 )

                    _lowerCamelCase       :		Optional[Any]		=resnet(lowercase_			,   lowercase_			,   deterministic=lowercase_ )
                    _lowerCamelCase       :		List[Any]		=attn(lowercase_			,   lowercase_			,   deterministic=lowercase_ )

             if self.add_upsample:
                    _lowerCamelCase       :		Optional[Any]		=self.upsamplers_a(lowercase_ )

             return hidden_states




class A  (   nn.Module     ):
      UpperCamelCase__							:  int
      UpperCamelCase__							:  int
      UpperCamelCase__							:  int
      UpperCamelCase__							:  float      =0.0
      UpperCamelCase__							:  int      =1
      UpperCamelCase__							:  bool      =True
      UpperCamelCase__							:  jnp.dtype      =jnp.floataa






      def        lowerCamelCase       (					self						:     List[Any] )					->						Dict:
             """simple docstring"""




             _lowerCamelCase       :		List[str]		=[]

             for i in range(self.num_layers ):
                    _lowerCamelCase       :		Tuple		=self.in_channels if (i == self.num_layers - 1) else self.out_channels
                    _lowerCamelCase       :		int		=self.prev_output_channel if i == 0 else self.out_channels

                    _lowerCamelCase       :		str		=FlaxResnetBlockaD(
                        in_channels=resnet_in_channels + res_skip_channels			,   out_channels=self.out_channels			,   dropout_prob=self.dropout			,   dtype=self.dtype			,   )
                    resnets.append(lowercase_ )

             _lowerCamelCase       :		str		=resnets

             if self.add_upsample:
                    _lowerCamelCase       :		List[str]		=FlaxUpsampleaD(self.out_channels			,   dtype=self.dtype )






      def __call__(					self						:     List[str]			,   lowercase_						:     str			,   lowercase_						:     Dict			,   lowercase_						:     Union[str, Any]			,   lowercase_						:     Any=True )					->						int:
             """simple docstring"""




             for resnet in self.resnets:
                    # pop res hidden states
                    _lowerCamelCase       :		List[str]		=res_hidden_states_tuple[-1]
                    _lowerCamelCase       :		str		=res_hidden_states_tuple[:-1]
                    _lowerCamelCase       :		List[str]		=jnp.concatenate((hidden_states, res_hidden_states)			,   axis=-1 )

                    _lowerCamelCase       :		Optional[Any]		=resnet(lowercase_			,   lowercase_			,   deterministic=lowercase_ )

             if self.add_upsample:
                    _lowerCamelCase       :		Union[str, Any]		=self.upsamplers_a(lowercase_ )

             return hidden_states




class A  (   nn.Module     ):
      UpperCamelCase__							:  int
      UpperCamelCase__							:  float      =0.0
      UpperCamelCase__							:  int      =1
      UpperCamelCase__							:  int      =1
      UpperCamelCase__							:  bool      =False
      UpperCamelCase__							:  bool      =False
      UpperCamelCase__							:  jnp.dtype      =jnp.floataa






      def        lowerCamelCase       (					self						:     int )					->						Tuple:
             """simple docstring"""




             _lowerCamelCase       :		Optional[Any]		=[
                 FlaxResnetBlockaD(
                     in_channels=self.in_channels			,   out_channels=self.in_channels			,   dropout_prob=self.dropout			,   dtype=self.dtype			,   )
             ]

             _lowerCamelCase       :		Any		=[]

             for _ in range(self.num_layers ):
                    _lowerCamelCase       :		Optional[int]		=FlaxTransformeraDModel(
                        in_channels=self.in_channels			,   n_heads=self.num_attention_heads			,   d_head=self.in_channels // self.num_attention_heads			,   depth=1			,   use_linear_projection=self.use_linear_projection			,   use_memory_efficient_attention=self.use_memory_efficient_attention			,   dtype=self.dtype			,   )
                    attentions.append(lowercase_ )

                    _lowerCamelCase       :		Tuple		=FlaxResnetBlockaD(
                        in_channels=self.in_channels			,   out_channels=self.in_channels			,   dropout_prob=self.dropout			,   dtype=self.dtype			,   )
                    resnets.append(lowercase_ )

             _lowerCamelCase       :		List[Any]		=resnets
             _lowerCamelCase       :		List[str]		=attentions






      def __call__(					self						:     Optional[int]			,   lowercase_						:     List[Any]			,   lowercase_						:     Dict			,   lowercase_						:     Tuple			,   lowercase_						:     List[str]=True )					->						int:
             """simple docstring"""




             _lowerCamelCase       :		Dict		=self.resnets[0](lowercase_			,   lowercase_ )
             for attn, resnet in zip(self.attentions			,   self.resnets[1:] ):
                    _lowerCamelCase       :		Tuple		=attn(lowercase_			,   lowercase_			,   deterministic=lowercase_ )
                    _lowerCamelCase       :		List[str]		=resnet(lowercase_			,   lowercase_			,   deterministic=lowercase_ )

             return hidden_states


