






'''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 							__SCREAMING_SNAKE_CASE       (							nn.Module     ):
      __a  					=42
      __a  					=42
      __a  					=0.0
      __a  					=1
      __a  					=1
      __a  					=True
      __a  					=False
      __a  					=False
      __a  					=False
      __a  					=jnp.floataa




      def  __UpperCamelCase							(  self      )							->Any:



         '''simple docstring'''
         __a				     =    []
         __a				     =    []

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

            __a				     =    FlaxResnetBlockaD(
                in_channels=UpperCAmelCase__ ,   out_channels=self.out_channels ,   dropout_prob=self.dropout ,   dtype=self.dtype ,   )
            resnets.append(UpperCAmelCase__      )

            __a				     =    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(UpperCAmelCase__      )

         __a				     =    resnets
         __a				     =    attentions

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






      def __call__(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase=True      )							->Union[str, Any]:



         '''simple docstring'''
         __a				     =    ()

         for resnet, attn in zip(self.resnets ,   self.attentions      ):
            __a				     =    resnet(UpperCAmelCase__ ,   UpperCAmelCase__ ,   deterministic=UpperCAmelCase__      )
            __a				     =    attn(UpperCAmelCase__ ,   UpperCAmelCase__ ,   deterministic=UpperCAmelCase__      )
            output_states += (hidden_states,)

         if self.add_downsample:
            __a				     =    self.downsamplers_a(UpperCAmelCase__      )
            output_states += (hidden_states,)

         return hidden_states, output_states







class 							__SCREAMING_SNAKE_CASE       (							nn.Module     ):
      __a  					=42
      __a  					=42
      __a  					=0.0
      __a  					=1
      __a  					=True
      __a  					=jnp.floataa




      def  __UpperCamelCase							(  self      )							->Any:



         '''simple docstring'''
         __a				     =    []

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

            __a				     =    FlaxResnetBlockaD(
                in_channels=UpperCAmelCase__ ,   out_channels=self.out_channels ,   dropout_prob=self.dropout ,   dtype=self.dtype ,   )
            resnets.append(UpperCAmelCase__      )
         __a				     =    resnets

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






      def __call__(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase=True      )							->Dict:



         '''simple docstring'''
         __a				     =    ()

         for resnet in self.resnets:
            __a				     =    resnet(UpperCAmelCase__ ,   UpperCAmelCase__ ,   deterministic=UpperCAmelCase__      )
            output_states += (hidden_states,)

         if self.add_downsample:
            __a				     =    self.downsamplers_a(UpperCAmelCase__      )
            output_states += (hidden_states,)

         return hidden_states, output_states







class 							__SCREAMING_SNAKE_CASE       (							nn.Module     ):
      __a  					=42
      __a  					=42
      __a  					=42
      __a  					=0.0
      __a  					=1
      __a  					=1
      __a  					=True
      __a  					=False
      __a  					=False
      __a  					=False
      __a  					=jnp.floataa




      def  __UpperCamelCase							(  self      )							->Any:



         '''simple docstring'''
         __a				     =    []
         __a				     =    []

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

            __a				     =    FlaxResnetBlockaD(
                in_channels=resnet_in_channels + res_skip_channels ,   out_channels=self.out_channels ,   dropout_prob=self.dropout ,   dtype=self.dtype ,   )
            resnets.append(UpperCAmelCase__      )

            __a				     =    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(UpperCAmelCase__      )

         __a				     =    resnets
         __a				     =    attentions

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






      def __call__(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase=True      )							->Optional[Any]:



         '''simple docstring'''
         for resnet, attn in zip(self.resnets ,   self.attentions      ):
            # pop res hidden states
            __a				     =    res_hidden_states_tuple[-1]
            __a				     =    res_hidden_states_tuple[:-1]
            __a				     =    jnp.concatenate((hidden_states, res_hidden_states) ,   axis=-1      )

            __a				     =    resnet(UpperCAmelCase__ ,   UpperCAmelCase__ ,   deterministic=UpperCAmelCase__      )
            __a				     =    attn(UpperCAmelCase__ ,   UpperCAmelCase__ ,   deterministic=UpperCAmelCase__      )

         if self.add_upsample:
            __a				     =    self.upsamplers_a(UpperCAmelCase__      )

         return hidden_states







class 							__SCREAMING_SNAKE_CASE       (							nn.Module     ):
      __a  					=42
      __a  					=42
      __a  					=42
      __a  					=0.0
      __a  					=1
      __a  					=True
      __a  					=jnp.floataa




      def  __UpperCamelCase							(  self      )							->Any:



         '''simple docstring'''
         __a				     =    []

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

            __a				     =    FlaxResnetBlockaD(
                in_channels=resnet_in_channels + res_skip_channels ,   out_channels=self.out_channels ,   dropout_prob=self.dropout ,   dtype=self.dtype ,   )
            resnets.append(UpperCAmelCase__      )

         __a				     =    resnets

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






      def __call__(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase=True      )							->Optional[int]:



         '''simple docstring'''
         for resnet in self.resnets:
            # pop res hidden states
            __a				     =    res_hidden_states_tuple[-1]
            __a				     =    res_hidden_states_tuple[:-1]
            __a				     =    jnp.concatenate((hidden_states, res_hidden_states) ,   axis=-1      )

            __a				     =    resnet(UpperCAmelCase__ ,   UpperCAmelCase__ ,   deterministic=UpperCAmelCase__      )

         if self.add_upsample:
            __a				     =    self.upsamplers_a(UpperCAmelCase__      )

         return hidden_states







class 							__SCREAMING_SNAKE_CASE       (							nn.Module     ):
      __a  					=42
      __a  					=0.0
      __a  					=1
      __a  					=1
      __a  					=False
      __a  					=False
      __a  					=jnp.floataa




      def  __UpperCamelCase							(  self      )							->Union[str, Any]:



         '''simple docstring'''
         __a				     =    [
             FlaxResnetBlockaD(
                 in_channels=self.in_channels ,   out_channels=self.in_channels ,   dropout_prob=self.dropout ,   dtype=self.dtype ,   )
         ]

         __a				     =    []

         for _ in range(self.num_layers      ):
            __a				     =    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(UpperCAmelCase__      )

            __a				     =    FlaxResnetBlockaD(
                in_channels=self.in_channels ,   out_channels=self.in_channels ,   dropout_prob=self.dropout ,   dtype=self.dtype ,   )
            resnets.append(UpperCAmelCase__      )

         __a				     =    resnets
         __a				     =    attentions






      def __call__(  self ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase ,   lowerCamelCase=True      )							->Optional[Any]:



         '''simple docstring'''
         __a				     =    self.resnets[0](UpperCAmelCase__ ,   UpperCAmelCase__      )
         for attn, resnet in zip(self.attentions ,   self.resnets[1:]      ):
            __a				     =    attn(UpperCAmelCase__ ,   UpperCAmelCase__ ,   deterministic=UpperCAmelCase__      )
            __a				     =    resnet(UpperCAmelCase__ ,   UpperCAmelCase__ ,   deterministic=UpperCAmelCase__      )

         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 							__SCREAMING_SNAKE_CASE       (							_lowerCAmelCase     ):
      __a  					="microsoft/speecht5_tts"
      __a  					=(
          "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."
      )
      __a  					="text_reader"
      __a  					=SpeechTaProcessor
      __a  					=SpeechTaForTextToSpeech
      __a  					=SpeechTaHifiGan

      __a  					=["text"]
      __a  					=["audio"]




      def  __UpperCamelCase							(  self      )							->str:



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




      def  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase=None      )							->str:



         '''simple docstring'''
         __a				     =    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.'      )

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

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




      def  __UpperCamelCase							(  self ,   lowerCamelCase      )							->List[str]:



         '''simple docstring'''
         with torch.no_grad():
            return self.model.generate_speech(**lowerCamelCase      )






      def  __UpperCamelCase							(  self ,   lowerCamelCase      )							->List[str]:



         '''simple docstring'''
         with torch.no_grad():
            return self.post_processor(lowerCamelCase      ).cpu().detach()