



# 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  __magic_name__   (							A__						):
		lowercase      :       Optional[int]          ='''microsoft/speecht5_tts'''
		lowercase      :       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.'''
		)
		lowercase      :       List[str]          ='''text_reader'''
		lowercase      :       str          =SpeechTaProcessor
		lowercase      :       Optional[int]          =SpeechTaForTextToSpeech
		lowercase      :       List[str]          =SpeechTaHifiGan

		lowercase      :       Any          =['''text''']
		lowercase      :       int          =['''audio''']






		def SCREAMING_SNAKE_CASE_			(						self      :     int      )				->					Any:
								'''simple docstring'''







								if self.post_processor is None:
														UpperCAmelCase         =     "microsoft/speecht5_hifigan"
								super().setup()






		def SCREAMING_SNAKE_CASE_			(						self      :     str				,		UpperCamelCase__      :     int				,		UpperCamelCase__      :     List[Any]=None      )				->					Optional[Any]:
								'''simple docstring'''







								UpperCAmelCase         =     self.pre_processor(text=UpperCamelCase__				,		return_tensors="pt"				,		truncation=UpperCamelCase__      )

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

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

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






		def SCREAMING_SNAKE_CASE_			(						self      :     List[str]				,		UpperCamelCase__      :     Any      )				->					List[Any]:
								'''simple docstring'''







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







		def SCREAMING_SNAKE_CASE_			(						self      :     List[Any]				,		UpperCamelCase__      :     Union[str, Any]      )				->					str:
								'''simple docstring'''







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








import tempfile
import unittest

from transformers import AutoModelForSeqaSeqLM, AutoTokenizer
from transformers.testing_utils import (
    is_torch_available,
    require_optimum,
    require_torch,
    slow,
)


if is_torch_available():
		import torch





@require_torch
@require_optimum
@slow
class  __magic_name__   (							unittest.TestCase						):






		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					Optional[int]:
								'''simple docstring'''







								UpperCAmelCase         =     "hf-internal-testing/tiny-random-t5"
								UpperCAmelCase         =     AutoTokenizer.from_pretrained(UpperCamelCase__      )
								UpperCAmelCase         =     AutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__      )

								UpperCAmelCase         =     tokenizer("This is me"				,		return_tensors="pt"      )

								UpperCAmelCase         =     model.to_bettertransformer()

								self.assertTrue(any("BetterTransformer" in mod.__class__.__name__ for _, mod in model.named_modules()      )      )

								UpperCAmelCase         =     model.generate(**UpperCamelCase__      )

								UpperCAmelCase         =     model.reverse_bettertransformer()

								self.assertFalse(any("BetterTransformer" in mod.__class__.__name__ for _, mod in model.named_modules()      )      )

								with tempfile.TemporaryDirectory() as tmpdirname:
														model.save_pretrained(UpperCamelCase__      )

														UpperCAmelCase         =     AutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__      )

														self.assertFalse(
														    any("BetterTransformer" in mod.__class__.__name__ for _, mod in model_reloaded.named_modules()      )      )

														UpperCAmelCase         =     model_reloaded.generate(**UpperCamelCase__      )
														self.assertTrue(torch.allclose(UpperCamelCase__				,		UpperCamelCase__      )      )







		def SCREAMING_SNAKE_CASE_			(						self      :     List[str]      )				->					Optional[int]:
								'''simple docstring'''







								UpperCAmelCase         =     "hf-internal-testing/tiny-random-t5"
								UpperCAmelCase         =     AutoModelForSeqaSeqLM.from_pretrained(UpperCamelCase__      )

								UpperCAmelCase         =     model.to_bettertransformer()

								with tempfile.TemporaryDirectory() as tmpdirname:
														with self.assertRaises(UpperCamelCase__      ):
																				model.save_pretrained(UpperCamelCase__      )

														UpperCAmelCase         =     model.reverse_bettertransformer()
														model.save_pretrained(UpperCamelCase__      )



