
'''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_				(	snake_case__					):
 UpperCAmelCase_		    =						"""microsoft/speecht5_tts"""
 UpperCAmelCase_		    =						(
     """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."""
 )
 UpperCAmelCase_		    =						"""text_reader"""
 UpperCAmelCase_		    =						SpeechTaProcessor
 UpperCAmelCase_		    =						SpeechTaForTextToSpeech
 UpperCAmelCase_		    =						SpeechTaHifiGan

 UpperCAmelCase_		    =						["""text"""]
 UpperCAmelCase_		    =						["""audio"""]





 def        snake_case__		(    self):
     if self.post_processor is None:
         snake_case_ :    Dict     			=						"microsoft/speecht5_hifigan"
     super().setup()





 def        snake_case__		(    self ,       lowercase_ ,       lowercase_=None):
     snake_case_ :    Tuple     			=						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.")

         snake_case_ :    Optional[Any]     			=						load_dataset("Matthijs/cmu-arctic-xvectors" ,       split="validation")
         snake_case_ :    List[Any]     			=						torch.tensor(embeddings_dataset[73_05]["xvector"]).unsqueeze(0)

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





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





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






'''simple docstring'''
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding






class    UpperCAmelCase_				(	snake_case__					):
 UpperCAmelCase_		    =						"""ClapFeatureExtractor"""
 UpperCAmelCase_		    =						("""RobertaTokenizer""", """RobertaTokenizerFast""")





 def __init__(    self ,       lowercase_ ,       lowercase_):
     super().__init__(lowercase_ ,       lowercase_)





 def __call__(    self ,       lowercase_=None ,       lowercase_=None ,       lowercase_=None ,       **lowercase_):
     snake_case_ :    Any     			=						kwargs.pop("sampling_rate" ,       lowercase_)

     if text is None and audios is None:
         raise ValueError("You have to specify either text or audios. Both cannot be none.")

     if text is not None:
         snake_case_ :    Optional[int]     			=						self.tokenizer(lowercase_ ,       return_tensors=lowercase_ ,       **lowercase_)

     if audios is not None:
         snake_case_ :    Any     			=						self.feature_extractor(
             lowercase_ ,       sampling_rate=lowercase_ ,       return_tensors=lowercase_ ,       **lowercase_)

     if text is not None and audios is not None:
         snake_case_ :    Dict     			=						audio_features.input_features
         return encoding
     elif text is not None:
         return encoding
     else:
         return BatchEncoding(data=dict(**lowercase_) ,       tensor_type=lowercase_)





 def        snake_case__		(    self ,       *lowercase_ ,       **lowercase_):
     return self.tokenizer.batch_decode(*lowercase_ ,       **lowercase_)





 def        snake_case__		(    self ,       *lowercase_ ,       **lowercase_):
     return self.tokenizer.decode(*lowercase_ ,       **lowercase_)





 @property
 def        snake_case__		(    self):
     snake_case_ :    Union[str, Any]     			=						self.tokenizer.model_input_names
     snake_case_ :    int     			=						self.feature_extractor.model_input_names
     return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names))




