



# 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__(							lowerCamelCase	):

   '''simple docstring'''




   A    :  Tuple       				=    "microsoft/speecht5_tts"
   A    :  Dict       				=    (
       "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    :  str       				=    "text_reader"
   A    :  Dict       				=    SpeechTaProcessor
   A    :  Dict       				=    SpeechTaForTextToSpeech
   A    :  Union[str, Any]       				=    SpeechTaHifiGan

   A    :  Dict       				=    ["text"]
   A    :  int       				=    ["audio"]


   def 			UpperCAmelCase		(						self  :			str)	->				Tuple:




         """simple docstring"""

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


   def 			UpperCAmelCase		(						self  :			Any			,				lowerCAmelCase  :			Dict			,				lowerCAmelCase  :			Optional[int]=None)	->				int:




         """simple docstring"""

         lowercase__     =  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.')

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

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


   def 			UpperCAmelCase		(						self  :			Tuple			,				lowerCAmelCase  :			Union[str, Any])	->				Union[str, Any]:




         """simple docstring"""

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



   def 			UpperCAmelCase		(						self  :			Union[str, Any]			,				lowerCAmelCase  :			str)	->				Dict:




         """simple docstring"""

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





import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin



class       UpperCAmelCase__(							unittest.TestCase     ,							lowerCamelCase	):

   '''simple docstring'''




   def 			UpperCAmelCase		(						self  :			List[str])	->				Any:




         """simple docstring"""

         lowercase__     =  load_tool('text-classification')
         self.tool.setup()
         lowercase__     =  load_tool('text-classification'			,				remote=lowerCAmelCase)


   def 			UpperCAmelCase		(						self  :			Any)	->				Tuple:




         """simple docstring"""

         lowercase__     =  self.tool('That\'s quite cool'			,				['positive', 'negative'])
         self.assertEqual(lowerCAmelCase			,				'positive')


   def 			UpperCAmelCase		(						self  :			int)	->				Optional[int]:




         """simple docstring"""

         lowercase__     =  self.remote_tool('That\'s quite cool'			,				['positive', 'negative'])
         self.assertEqual(lowerCAmelCase			,				'positive')


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




         """simple docstring"""

         lowercase__     =  self.tool(text='That\'s quite cool'			,				labels=['positive', 'negative'])
         self.assertEqual(lowerCAmelCase			,				'positive')



   def 			UpperCAmelCase		(						self  :			Any)	->				Any:




         """simple docstring"""

         lowercase__     =  self.remote_tool(text='That\'s quite cool'			,				labels=['positive', 'negative'])
         self.assertEqual(lowerCAmelCase			,				'positive')
