a
    dx	                     @   sZ   d dl Z ddlmZmZmZ ddlmZ ddlmZ e rFd dl	m
Z
 G dd	 d	eZdS )
    N   )SpeechT5ForTextToSpeechSpeechT5HifiGanSpeechT5Processor)is_datasets_available   )PipelineTool)load_datasetc                       sZ   e Zd ZdZdZdZeZeZ	e
ZdgZdgZ fddZdd	d
Zdd Zdd Z  ZS )TextToSpeechToolzmicrosoft/speecht5_ttszThis 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.Ztext_readertextaudioc                    s   | j d u rd| _ t   d S )Nzmicrosoft/speecht5_hifigan)post_processorsupersetup)self	__class__ j/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/transformers/tools/text_to_speech.pyr   *   s    
zTextToSpeechTool.setupNc                 C   sX   | j |ddd}|d u rJt s&tdtddd}t|d d	 d
}|d |dS )NptT)r   Zreturn_tensorsZ
truncationzADatasets needs to be installed if not passing speaker embeddings.zMatthijs/cmu-arctic-xvectorsZ
validation)spliti  Zxvectorr   	input_ids)r   speaker_embeddings)Zpre_processorr   ImportErrorr	   torchZtensorZ	unsqueeze)r   r   r   inputsZembeddings_datasetr   r   r   encode/   s    zTextToSpeechTool.encodec                 C   s>   t  " | jjf i |W  d    S 1 s00    Y  d S N)r   no_gradmodelZgenerate_speech)r   r   r   r   r   forward;   s    
zTextToSpeechTool.forwardc                 C   s>   t  " | |  W  d    S 1 s00    Y  d S r   )r   r   r   cpudetach)r   outputsr   r   r   decode?   s    
zTextToSpeechTool.decode)N)__name__
__module____qualname__Zdefault_checkpointdescriptionnamer   Zpre_processor_classr   Zmodel_classr   Zpost_processor_classr   r#   r   r   r    r$   __classcell__r   r   r   r   r
      s   
r
   )r   Zmodels.speecht5r   r   r   utilsr   baser   Zdatasetsr	   r
   r   r   r   r   <module>   s   