
'''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  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''microsoft/speecht5_tts'''
					lowerCAmelCase_        =     (
					    '''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.'''
					)
					lowerCAmelCase_        =     '''text_reader'''
					lowerCAmelCase_        =     SpeechTaProcessor
					lowerCAmelCase_        =     SpeechTaForTextToSpeech
					lowerCAmelCase_        =     SpeechTaHifiGan

					lowerCAmelCase_        =     ['''text''']
					lowerCAmelCase_        =     ['''audio''']






					def 	_snake_case    (   self		):


											"""simple docstring"""





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






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE=None		):


											"""simple docstring"""





											lowercase_	:     Tuple													=       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.'''		)

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

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






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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






					def 	_snake_case    (   self				,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





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











import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionImageVariationPipeline
from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device


lowerCamelCase	:							Tuple				=False





class 			__a  (    unittest.TestCase       ):
 pass







@slow
@require_torch_gpu
class 			__a  (    unittest.TestCase       ):







 def 					__lowercase					(		self					:   int						):


      '''simple docstring'''






      UpperCamelCase__			:   Optional[Any]       				=  VersatileDiffusionImageVariationPipeline.from_pretrained("shi-labs/versatile-diffusion"						)
      pipe.to(SCREAMING_SNAKE_CASE						)
      pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE						)

      UpperCamelCase__			:   Union[str, Any]       				=  load_image(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"						)
      UpperCamelCase__			:   List[Any]       				=  torch.manual_seed(0						)
      UpperCamelCase__			:   List[Any]       				=  pipe(
          image=SCREAMING_SNAKE_CASE							,   generator=SCREAMING_SNAKE_CASE							,   guidance_scale=7.5							,   num_inference_steps=50							,   output_type="numpy"							,   ).images

      UpperCamelCase__			:   Union[str, Any]       				=  image[0, 2_53:2_56, 2_53:2_56, -1]

      assert image.shape == (1, 5_12, 5_12, 3)
      UpperCamelCase__			:   Optional[Any]       				=  np.array([0.0_4_4_1, 0.0_4_6_9, 0.0_5_0_7, 0.0_5_7_5, 0.0_6_3_2, 0.0_6_5_0, 0.0_8_6_5, 0.0_9_0_9, 0.0_9_4_5]						)

      assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2