





'''simple docstring'''






import copy
import os
from typing import TYPE_CHECKING, List, Union


if TYPE_CHECKING:
					pass

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase       		=      logging.get_logger(__name__)

lowerCAmelCase       		=      {
    """kakaobrain/align-base""": """https://huggingface.co/kakaobrain/align-base/resolve/main/config.json""",
}
class 		lowerCamelCase  (      snake_case_       ):
	snake_case_      =   """align_text_model"""



	def __init__(   self		,      a_=30_522		,      a_=768		,      a_=12		,      a_=12		,      a_=3_072		,      a_="gelu"		,      a_=0.1		,      a_=0.1		,      a_=512		,      a_=2		,      a_=0.02		,      a_=1e-1_2		,      a_=0		,      a_="absolute"		,      a_=True		,      **a_		,      ):
			super().__init__(**a_	)

			lowerCAmelCase     :    Dict						=   vocab_size
			lowerCAmelCase     :    str						=   hidden_size
			lowerCAmelCase     :    List[str]						=   num_hidden_layers
			lowerCAmelCase     :    Optional[Any]						=   num_attention_heads
			lowerCAmelCase     :    List[str]						=   hidden_act
			lowerCAmelCase     :    str						=   intermediate_size
			lowerCAmelCase     :    Union[str, Any]						=   hidden_dropout_prob
			lowerCAmelCase     :    Any						=   attention_probs_dropout_prob
			lowerCAmelCase     :    Union[str, Any]						=   max_position_embeddings
			lowerCAmelCase     :    Tuple						=   type_vocab_size
			lowerCAmelCase     :    Any						=   initializer_range
			lowerCAmelCase     :    List[Any]						=   layer_norm_eps
			lowerCAmelCase     :    Any						=   position_embedding_type
			lowerCAmelCase     :    Optional[Any]						=   use_cache
			lowerCAmelCase     :    List[str]						=   pad_token_id





	@classmethod
	def   _lowerCamelCase    (   cls		,      a_		,      **a_	):
			cls._set_token_in_kwargs(a_	)

			lowerCAmelCase	,		lowerCAmelCase     :    str						=   cls.get_config_dict(a_		,      **a_	)

			# get the text config dict if we are loading from AlignConfig
			if config_dict.get("model_type"	) == "align":
					lowerCAmelCase     :    int						=   config_dict["text_config"]

			if "model_type" in config_dict and hasattr(cls		,      "model_type"	) and config_dict["model_type"] != cls.model_type:
					logger.warning(
					    F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
					    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''	)

			return cls.from_dict(a_		,      **a_	)






class 		lowerCamelCase  (      snake_case_       ):
	snake_case_      =   """align_vision_model"""



	def __init__(   self		,      a_ = 3		,      a_ = 600		,      a_ = 2.0		,      a_ = 3.1		,      a_ = 8		,      a_ = [3, 3, 5, 3, 5, 5, 3]		,      a_ = [32, 16, 24, 40, 80, 112, 192]		,      a_ = [16, 24, 40, 80, 112, 192, 320]		,      a_ = []		,      a_ = [1, 2, 2, 2, 1, 2, 1]		,      a_ = [1, 2, 2, 3, 3, 4, 1]		,      a_ = [1, 6, 6, 6, 6, 6, 6]		,      a_ = 0.25		,      a_ = "swish"		,      a_ = 2_560		,      a_ = "mean"		,      a_ = 0.02		,      a_ = 0.001		,      a_ = 0.99		,      a_ = 0.2		,      **a_		,      ):
			super().__init__(**a_	)

			lowerCAmelCase     :    Dict						=   num_channels
			lowerCAmelCase     :    Union[str, Any]						=   image_size
			lowerCAmelCase     :    Optional[int]						=   width_coefficient
			lowerCAmelCase     :    str						=   depth_coefficient
			lowerCAmelCase     :    str						=   depth_divisor
			lowerCAmelCase     :    Union[str, Any]						=   kernel_sizes
			lowerCAmelCase     :    int						=   in_channels
			lowerCAmelCase     :    Optional[int]						=   out_channels
			lowerCAmelCase     :    str						=   depthwise_padding
			lowerCAmelCase     :    Dict						=   strides
			lowerCAmelCase     :    Optional[int]						=   num_block_repeats
			lowerCAmelCase     :    Optional[int]						=   expand_ratios
			lowerCAmelCase     :    Any						=   squeeze_expansion_ratio
			lowerCAmelCase     :    Dict						=   hidden_act
			lowerCAmelCase     :    str						=   hidden_dim
			lowerCAmelCase     :    List[str]						=   pooling_type
			lowerCAmelCase     :    Dict						=   initializer_range
			lowerCAmelCase     :    int						=   batch_norm_eps
			lowerCAmelCase     :    Any						=   batch_norm_momentum
			lowerCAmelCase     :    str						=   drop_connect_rate
			lowerCAmelCase     :    List[Any]						=   sum(a_	) * 4





	@classmethod
	def   _lowerCamelCase    (   cls		,      a_		,      **a_	):
			cls._set_token_in_kwargs(a_	)

			lowerCAmelCase	,		lowerCAmelCase     :    Any						=   cls.get_config_dict(a_		,      **a_	)

			# get the vision config dict if we are loading from AlignConfig
			if config_dict.get("model_type"	) == "align":
					lowerCAmelCase     :    List[Any]						=   config_dict["vision_config"]

			if "model_type" in config_dict and hasattr(cls		,      "model_type"	) and config_dict["model_type"] != cls.model_type:
					logger.warning(
					    F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
					    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''	)

			return cls.from_dict(a_		,      **a_	)






class 		lowerCamelCase  (      snake_case_       ):
	snake_case_      =   """align"""
	snake_case_      =   True



	def __init__(   self		,      a_=None		,      a_=None		,      a_=640		,      a_=1.0		,      a_=0.02		,      **a_		,      ):
			super().__init__(**a_	)

			if text_config is None:
					lowerCAmelCase     :    Any						=   {}
					logger.info("text_config is None. Initializing the AlignTextConfig with default values."	)

			if vision_config is None:
					lowerCAmelCase     :    Optional[int]						=   {}
					logger.info("vision_config is None. Initializing the AlignVisionConfig with default values."	)

			lowerCAmelCase     :    List[Any]						=   AlignTextConfig(**a_	)
			lowerCAmelCase     :    Optional[int]						=   AlignVisionConfig(**a_	)

			lowerCAmelCase     :    List[str]						=   projection_dim
			lowerCAmelCase     :    List[Any]						=   temperature_init_value
			lowerCAmelCase     :    Optional[int]						=   initializer_range



	@classmethod
	def   _lowerCamelCase    (   cls		,      a_		,      a_		,      **a_	):
			return cls(text_config=text_config.to_dict()		,      vision_config=vision_config.to_dict()		,      **a_	)





	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Optional[Any]						=   copy.deepcopy(self.__dict__	)
			lowerCAmelCase     :    Optional[int]						=   self.text_config.to_dict()
			lowerCAmelCase     :    List[Any]						=   self.vision_config.to_dict()
			lowerCAmelCase     :    Dict						=   self.__class__.model_type
			return output









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



