


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

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






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




             if self.post_processor is None:
                    _lowerCamelCase       :		Optional[int]		='microsoft/speecht5_hifigan'
             super().setup()






      def        lowerCamelCase       (					self						:     List[Any]			,   lowercase_						:     List[Any]			,   lowercase_						:     Union[str, Any]=None )					->						Tuple:
             """simple docstring"""




             _lowerCamelCase       :		Optional[Any]		=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.' )

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

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






      def        lowerCamelCase       (					self						:     Optional[Any]			,   lowercase_						:     Dict )					->						Any:
             """simple docstring"""




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






      def        lowerCamelCase       (					self						:     Union[str, Any]			,   lowercase_						:     Any )					->						Optional[int]:
             """simple docstring"""




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






import os
import sys
from contextlib import contextmanager


# Windows only
if os.name == "nt":
 import ctypes
 import msvcrt  # noqa




 class A  (   ctypes.Structure     ):
       # _fields is a specific attr expected by ctypes
       UpperCamelCase__							:  List[Any]      =[('size', ctypes.c_int), ('visible', ctypes.c_byte)]




def 	a_      (    ):
       '''simple docstring'''





       if os.name == "nt":
              _lowerCamelCase       :		Optional[Any]		=CursorInfo()
              _lowerCamelCase       :		Dict		=ctypes.windll.kernelaa.GetStdHandle(-11     )
              ctypes.windll.kernelaa.GetConsoleCursorInfo(SCREAMING_SNAKE_CASE__		,			ctypes.byref(SCREAMING_SNAKE_CASE__     )     )
              _lowerCamelCase       :		Any		=False
              ctypes.windll.kernelaa.SetConsoleCursorInfo(SCREAMING_SNAKE_CASE__		,			ctypes.byref(SCREAMING_SNAKE_CASE__     )     )
       elif os.name == "posix":
              sys.stdout.write('\033[?25l'     )
              sys.stdout.flush()




def 	a_      (    ):
       '''simple docstring'''





       if os.name == "nt":
              _lowerCamelCase       :		Any		=CursorInfo()
              _lowerCamelCase       :		Optional[Any]		=ctypes.windll.kernelaa.GetStdHandle(-11     )
              ctypes.windll.kernelaa.GetConsoleCursorInfo(SCREAMING_SNAKE_CASE__		,			ctypes.byref(SCREAMING_SNAKE_CASE__     )     )
              _lowerCamelCase       :		Union[str, Any]		=True
              ctypes.windll.kernelaa.SetConsoleCursorInfo(SCREAMING_SNAKE_CASE__		,			ctypes.byref(SCREAMING_SNAKE_CASE__     )     )
       elif os.name == "posix":
              sys.stdout.write('\033[?25h'     )
              sys.stdout.flush()




@contextmanager
def 	a_      (    ):
       '''simple docstring'''





       try:
              hide_cursor()
              yield
       finally:
              show_cursor()


