





'''simple docstring'''






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

from .attention_flax import FlaxTransformeraDModel
from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD
class 		lowerCamelCase  (      nn.Module       ):
	snake_case_      =   42
	snake_case_      =   42
	snake_case_      =   0.0
	snake_case_      =   1
	snake_case_      =   1
	snake_case_      =   True
	snake_case_      =   False
	snake_case_      =   False
	snake_case_      =   False
	snake_case_      =   jnp.floataa



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Union[str, Any]						=   []
			lowerCAmelCase     :    List[Any]						=   []

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

					lowerCAmelCase     :    Dict						=   FlaxResnetBlockaD(
					    in_channels=a_		,      out_channels=self.out_channels		,      dropout_prob=self.dropout		,      dtype=self.dtype		,      )
					resnets.append(a_	)

					lowerCAmelCase     :    str						=   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(a_	)

			lowerCAmelCase     :    Optional[int]						=   resnets
			lowerCAmelCase     :    Any						=   attentions

			if self.add_downsample:
					lowerCAmelCase     :    str						=   FlaxDownsampleaD(self.out_channels		,      dtype=self.dtype	)





	def __call__(   self		,      a_		,      a_		,      a_		,      a_=True	):
			lowerCAmelCase     :    Any						=   ()

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

			if self.add_downsample:
					lowerCAmelCase     :    int						=   self.downsamplers_a(a_	)
					output_states += (hidden_states,)

			return hidden_states, output_states






class 		lowerCamelCase  (      nn.Module       ):
	snake_case_      =   42
	snake_case_      =   42
	snake_case_      =   0.0
	snake_case_      =   1
	snake_case_      =   True
	snake_case_      =   jnp.floataa



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Tuple						=   []

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

					lowerCAmelCase     :    int						=   FlaxResnetBlockaD(
					    in_channels=a_		,      out_channels=self.out_channels		,      dropout_prob=self.dropout		,      dtype=self.dtype		,      )
					resnets.append(a_	)
			lowerCAmelCase     :    Optional[Any]						=   resnets

			if self.add_downsample:
					lowerCAmelCase     :    Optional[Any]						=   FlaxDownsampleaD(self.out_channels		,      dtype=self.dtype	)





	def __call__(   self		,      a_		,      a_		,      a_=True	):
			lowerCAmelCase     :    List[str]						=   ()

			for resnet in self.resnets:
					lowerCAmelCase     :    Dict						=   resnet(a_		,      a_		,      deterministic=a_	)
					output_states += (hidden_states,)

			if self.add_downsample:
					lowerCAmelCase     :    Tuple						=   self.downsamplers_a(a_	)
					output_states += (hidden_states,)

			return hidden_states, output_states






class 		lowerCamelCase  (      nn.Module       ):
	snake_case_      =   42
	snake_case_      =   42
	snake_case_      =   42
	snake_case_      =   0.0
	snake_case_      =   1
	snake_case_      =   1
	snake_case_      =   True
	snake_case_      =   False
	snake_case_      =   False
	snake_case_      =   False
	snake_case_      =   jnp.floataa



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Dict						=   []
			lowerCAmelCase     :    Union[str, Any]						=   []

			for i in range(self.num_layers	):
					lowerCAmelCase     :    Optional[int]						=   self.in_channels if (i == self.num_layers - 1) else self.out_channels
					lowerCAmelCase     :    Union[str, Any]						=   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(a_	)

					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(a_	)

			lowerCAmelCase     :    List[str]						=   resnets
			lowerCAmelCase     :    Optional[int]						=   attentions

			if self.add_upsample:
					lowerCAmelCase     :    List[Any]						=   FlaxUpsampleaD(self.out_channels		,      dtype=self.dtype	)





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

					lowerCAmelCase     :    Tuple						=   resnet(a_		,      a_		,      deterministic=a_	)
					lowerCAmelCase     :    List[Any]						=   attn(a_		,      a_		,      deterministic=a_	)

			if self.add_upsample:
					lowerCAmelCase     :    Union[str, Any]						=   self.upsamplers_a(a_	)

			return hidden_states






class 		lowerCamelCase  (      nn.Module       ):
	snake_case_      =   42
	snake_case_      =   42
	snake_case_      =   42
	snake_case_      =   0.0
	snake_case_      =   1
	snake_case_      =   True
	snake_case_      =   jnp.floataa



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    int						=   []

			for i in range(self.num_layers	):
					lowerCAmelCase     :    str						=   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     :    Optional[Any]						=   FlaxResnetBlockaD(
					    in_channels=resnet_in_channels + res_skip_channels		,      out_channels=self.out_channels		,      dropout_prob=self.dropout		,      dtype=self.dtype		,      )
					resnets.append(a_	)

			lowerCAmelCase     :    List[str]						=   resnets

			if self.add_upsample:
					lowerCAmelCase     :    List[Any]						=   FlaxUpsampleaD(self.out_channels		,      dtype=self.dtype	)





	def __call__(   self		,      a_		,      a_		,      a_		,      a_=True	):
			for resnet in self.resnets:
					# pop res hidden states
					lowerCAmelCase     :    Tuple						=   res_hidden_states_tuple[-1]
					lowerCAmelCase     :    List[str]						=   res_hidden_states_tuple[:-1]
					lowerCAmelCase     :    Optional[int]						=   jnp.concatenate((hidden_states, res_hidden_states)		,      axis=-1	)

					lowerCAmelCase     :    List[str]						=   resnet(a_		,      a_		,      deterministic=a_	)

			if self.add_upsample:
					lowerCAmelCase     :    int						=   self.upsamplers_a(a_	)

			return hidden_states






class 		lowerCamelCase  (      nn.Module       ):
	snake_case_      =   42
	snake_case_      =   0.0
	snake_case_      =   1
	snake_case_      =   1
	snake_case_      =   False
	snake_case_      =   False
	snake_case_      =   jnp.floataa



	def   _lowerCamelCase    (   self	):
			# there is always at least one resnet
			lowerCAmelCase     :    Optional[Any]						=   [
			    FlaxResnetBlockaD(
			        in_channels=self.in_channels		,      out_channels=self.in_channels		,      dropout_prob=self.dropout		,      dtype=self.dtype		,      )
			]

			lowerCAmelCase     :    int						=   []

			for _ in range(self.num_layers	):
					lowerCAmelCase     :    List[str]						=   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(a_	)

					lowerCAmelCase     :    List[str]						=   FlaxResnetBlockaD(
					    in_channels=self.in_channels		,      out_channels=self.in_channels		,      dropout_prob=self.dropout		,      dtype=self.dtype		,      )
					resnets.append(a_	)

			lowerCAmelCase     :    Optional[Any]						=   resnets
			lowerCAmelCase     :    str						=   attentions





	def __call__(   self		,      a_		,      a_		,      a_		,      a_=True	):
			lowerCAmelCase     :    List[Any]						=   self.resnets[0](a_		,      a_	)
			for attn, resnet in zip(self.attentions		,      self.resnets[1:]	):
					lowerCAmelCase     :    Tuple						=   attn(a_		,      a_		,      deterministic=a_	)
					lowerCAmelCase     :    str						=   resnet(a_		,      a_		,      deterministic=a_	)

			return hidden_states










'''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 		lowerCamelCase  (      _A       ):
	snake_case_      =   "microsoft/speecht5_tts"
	snake_case_      =   (
	    "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_      =   "text_reader"
	snake_case_      =   SpeechTaProcessor
	snake_case_      =   SpeechTaForTextToSpeech
	snake_case_      =   SpeechTaHifiGan

	snake_case_      =   ["text"]
	snake_case_      =   ["audio"]



	def   _lowerCamelCase    (   self	):
			if self.post_processor is None:
					lowerCAmelCase     :    str						=   "microsoft/speecht5_hifigan"
			super().setup()



	def   _lowerCamelCase    (   self		,      a_		,      a_=None	):
			lowerCAmelCase     :    str						=   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."	)

					lowerCAmelCase     :    Tuple						=   load_dataset("Matthijs/cmu-arctic-xvectors"		,      split="validation"	)
					lowerCAmelCase     :    Union[str, Any]						=   torch.tensor(embeddings_dataset[7_305]["xvector"]	).unsqueeze(0	)

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



	def   _lowerCamelCase    (   self		,      a_	):
			with torch.no_grad():
					return self.model.generate_speech(**a_	)





	def   _lowerCamelCase    (   self		,      a_	):
			with torch.no_grad():
					return self.post_processor(a_	).cpu().detach()



