o
    Zh                     @  s   d dl mZ d dlZd dlmZmZmZ d dlmZ d dl	m
Z
 d dlmZ er.d dlmZ dd
dZdddZG dd de
ZdS )    )annotationsN)TYPE_CHECKINGAnyOptional)CallbackManagerForToolRun)BaseTool)get_client_infotexttospeechreturnr   c               
   C  s6   z	ddl m}  W | S  ty } ztd|d }~ww )Nr   r	   zCould not import google-cloud-texttospeech python package. Please, install texttospeech dependency group: `pip install langchain-google-community[texttospeech]`)google.cloudr
   ImportError)r
   e r   ^/var/www/html/lang_env/lib/python3.10/site-packages/langchain_google_community/texttospeech.py!_import_google_cloud_texttospeech   s   r   encodingtexttospeech.AudioEncodingOptional[str]c              
   C  s<   t  }|jjd|jjd|jjd|jjd|jjdi}|| S )Nz.wavz.mp3z.ogg)r   AudioEncodingZLINEAR16MP3ZOGG_OPUSZMULAWZALAWget)r   r
   ZENCODING_FILE_EXTENSION_MAPr   r   r   _encoding_file_extension_map   s   
r   c                      sX   e Zd ZU dZdZded< dZded< ded< d fddZ				ddddZ  Z	S )TextToSpeechToolzTool that queries the Google Cloud Text to Speech API.

    In order to set this up, follow instructions at:
    https://cloud.google.com/text-to-speech/docs/before-you-begin
    Zgoogle_cloud_texttospeechstrnamezA wrapper around Google Cloud Text-to-Speech. Useful for when you need to synthesize audio from text. It supports multiple languages, including English, German, Polish, Spanish, Italian, French, Portuguese, and Hindi. descriptionr   _clientkwargsr   Nonec                   s0   t  }t jdi | |jtddd| _dS )zInitializes private fields.ztext-to-speech)module)Zclient_infoNr   )r   super__init__ZTextToSpeechClientr   r   )selfr   r
   	__class__r   r   r"   9   s
   zTextToSpeechTool.__init__en-USN
input_textlanguage_codessml_gender&Optional[texttospeech.SsmlVoiceGender]audio_encoding$Optional[texttospeech.AudioEncoding]run_manager#Optional[CallbackManagerForToolRun]c           
      C  s   t  }|p|jj}|p|jj}| jj|j|d|j||d|j	|dd}t
|}tjd|dd}	|	|j W d   |	jS 1 sDw   Y  |	jS )	zUse the tool.)text)r(   r)   )r+   )inputZvoiceZaudio_configbxF)modesuffixdeleteN)r   ZSsmlVoiceGenderZNEUTRALr   r   r   Zsynthesize_speechZSynthesisInputZVoiceSelectionParamsZAudioConfigr   tempfileNamedTemporaryFilewriteZaudio_contentr   )
r#   r'   r(   r)   r+   r-   r
   responser3   fr   r   r   _runC   s"   	


zTextToSpeechTool._run)r   r   r   r   )r&   NNN)r'   r   r(   r   r)   r*   r+   r,   r-   r.   r   r   )
__name__
__module____qualname____doc__r   __annotations__r   r"   r:   __classcell__r   r   r$   r   r   (   s   
 
r   )r   r   )r   r   r   r   )
__future__r   r5   typingr   r   r   Zlangchain_core.callbacksr   Zlangchain_core.toolsr   Z!langchain_google_community._utilsr   r   r
   r   r   r   r   r   r   r   <module>   s    

