



"""simple docstring"""

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__):
 _lowerCAmelCase     :    Tuple	      =	"""microsoft/speecht5_tts"""
 _lowerCAmelCase     :    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."""
 )
 _lowerCAmelCase     :    Dict	      =	"""text_reader"""
 _lowerCAmelCase     :    Optional[int]	      =	SpeechTaProcessor
 _lowerCAmelCase     :    Optional[int]	      =	SpeechTaForTextToSpeech
 _lowerCAmelCase     :    Union[str, Any]	      =	SpeechTaHifiGan

 _lowerCAmelCase     :    List[str]	      =	["""text"""]
 _lowerCAmelCase     :    List[Any]	      =	["""audio"""]

 def 		_snake_case      (  self	: List[str]       ):




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

 def 		_snake_case      (  self	: Any						,  lowercase_	: str						,  lowercase_	: Optional[Any]=None       ):
     snake_case_				:  str       =			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.'''       )

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

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

 def 		_snake_case      (  self	: List[Any]						,  lowercase_	: List[str]       ):




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






 def 		_snake_case      (  self	: Tuple						,  lowercase_	: Optional[int]       ):




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










"""simple docstring"""

import unittest

from transformers import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device


if is_torch_available():
     import torch

     from transformers import AutoModelForImageClassification

if is_vision_available():
     from transformers import AutoImageProcessor




@require_torch
@require_vision
class  _UpperCAmelCase (    unittest.TestCase):

 @slow
 def 		_snake_case      (  self	: List[Any]       ):
     snake_case_				:  List[Any]       =			AutoImageProcessor.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip'''       )
     snake_case_				:  List[Any]       =			AutoModelForImageClassification.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip'''       )
     model.to(lowercase_       )

     from datasets import load_dataset

     snake_case_				:  str       =			load_dataset('''nielsr/rvlcdip-demo'''       )

     snake_case_				:  Tuple       =			dataset['''train'''][0]['''image'''].convert('''RGB'''       )

     snake_case_				:  Union[str, Any]       =			image_processor(lowercase_						,  return_tensors='''pt'''       ).to(lowercase_       )

     # forward pass
     with torch.no_grad():
         snake_case_				:  List[Any]       =			model(**lowercase_       )
         snake_case_				:  int       =			outputs.logits

     snake_case_				:  Any       =			torch.Size((1, 16)       )
     self.assertEqual(logits.shape						,  lowercase_       )

     snake_case_				:  Any       =			torch.tensor(
         [-0.41_58, -0.40_92, -0.43_47]						,  device=lowercase_						,  dtype=torch.float						,  )
     self.assertTrue(torch.allclose(logits[0, :3]						,  lowercase_						,  atol=1E-4       )       )





