


# 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__  (				lowercase__			):
      snake_case__				:     int        =						'''microsoft/speecht5_tts'''
      snake_case__				:     List[Any]        =						(
          '''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__				:     Optional[Any]        =						'''text_reader'''
      snake_case__				:     int        =						SpeechTaProcessor
      snake_case__				:     Any        =						SpeechTaForTextToSpeech
      snake_case__				:     List[str]        =						SpeechTaHifiGan

      snake_case__				:     int        =						['''text''']
      snake_case__				:     Dict        =						['''audio''']
      def        SCREAMING_SNAKE_CASE							(       self				:       Union[str, Any]     )		->			Tuple:
             if self.post_processor is None:
                    a_					:		List[Any]							=  'microsoft/speecht5_hifigan'
             super().setup()
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     ,							SCREAMING_SNAKE_CASE__				:       List[str]     ,							SCREAMING_SNAKE_CASE__				:       Optional[int]=None     )		->			str:
             a_					:		Union[str, Any]							=  self.pre_processor(text=SCREAMING_SNAKE_CASE__     ,							return_tensors='pt'     ,							truncation=SCREAMING_SNAKE_CASE__     )

             if speaker_embeddings is None:
                    if not is_datasets_available():
                           raise ImportError('Datasets needs to be installed if not passing speaker embeddings.'     )

                    a_					:		str							=  load_dataset('Matthijs/cmu-arctic-xvectors'     ,							split='validation'     )
                    a_					:		List[str]							=  torch.tensor(embeddings_dataset[7_3_0_5]['xvector']     ).unsqueeze(0     )

             return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings}
      def        SCREAMING_SNAKE_CASE							(       self				:       List[Any]     ,							SCREAMING_SNAKE_CASE__				:       int     )		->			Any:
             with torch.no_grad():
                    return self.model.generate_speech(**SCREAMING_SNAKE_CASE__     )

      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       List[str]     )		->			List[str]:
             with torch.no_grad():
                    return self.post_processor(SCREAMING_SNAKE_CASE__     ).cpu().detach()






"""simple docstring"""






import os
import sys
from contextlib import contextmanager


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





							class      lowerCamelCase__   (     ctypes.Structure				):







												"""simple docstring"""


												__a	       =							[("""size""", ctypes.c_int), ("""visible""", ctypes.c_byte)]
def      lowerCamelCase       (     )		->       Optional[int]:

							'''simple docstring'''



							if os.name == "nt":
														__UpperCAmelCase   :						Dict      							=					CursorInfo()
														__UpperCAmelCase   :						Any      							=					ctypes.windll.kernelaa.GetStdHandle(-1_1							)
														ctypes.windll.kernelaa.GetConsoleCursorInfo(_UpperCamelCase       ,			ctypes.byref(_UpperCamelCase							)							)
														__UpperCAmelCase   :						Tuple      							=					False
														ctypes.windll.kernelaa.SetConsoleCursorInfo(_UpperCamelCase       ,			ctypes.byref(_UpperCamelCase							)							)
							elif os.name == "posix":
														sys.stdout.write("""\033[?25l"""							)
														sys.stdout.flush()
def      lowerCamelCase       (     )		->       Optional[int]:

							'''simple docstring'''



							if os.name == "nt":
														__UpperCAmelCase   :						str      							=					CursorInfo()
														__UpperCAmelCase   :						int      							=					ctypes.windll.kernelaa.GetStdHandle(-1_1							)
														ctypes.windll.kernelaa.GetConsoleCursorInfo(_UpperCamelCase       ,			ctypes.byref(_UpperCamelCase							)							)
														__UpperCAmelCase   :						Union[str, Any]      							=					True
														ctypes.windll.kernelaa.SetConsoleCursorInfo(_UpperCamelCase       ,			ctypes.byref(_UpperCamelCase							)							)
							elif os.name == "posix":
														sys.stdout.write("""\033[?25h"""							)
														sys.stdout.flush()
@contextmanager
def      lowerCamelCase       (     )		->       str:

							'''simple docstring'''



							try:
														hide_cursor()
														yield
							finally:
														show_cursor()
