o
    Zh                      @   s   d dl Z d dlmZ d dlmZmZmZmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ defd	d
ZG dd deeZG dd deZdS )    N)Enum)AnyDictOptionalUnion)CallbackManagerForToolRun)BaseToolget_from_dict_or_env)model_validatorreturnc               
   C   s2   zdd l } W | S  ty } ztd|d }~ww )Nr   zBCannot import elevenlabs, please install `pip install elevenlabs`.)
elevenlabsImportError)r   e r   h/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/tools/eleven_labs/text2speech.py_import_elevenlabs   s   
r   c                   @   s   e Zd ZdZdZdZdZdS )ElevenLabsModelz-Models available for Eleven Labs Text2Speech.Zeleven_multilingual_v2Zeleven_flash_v2_5Zeleven_flash_v2N)__name__
__module____qualname____doc__MULTI_LINGUALZMULTI_LINGUAL_FLASHZMONO_LINGUALr   r   r   r   r      s
    r   c                   @   s   e Zd ZU dZejZeeef e	d< dZ
ee	d< dZee	d< dZee	d< ed	d
ededefddZ	ddedee defddZdeddfddZdeddfddZdS )ElevenLabsText2SpeechToolzTool that queries the Eleven Labs Text2Speech API.

    In order to set this up, follow instructions at:
    https://elevenlabs.io/docs
    modelZJBFqnCBsd6RMkjVDRZzbvoiceZeleven_labs_text2speechnamezA wrapper around Eleven Labs Text2Speech. Useful for when you need to convert text to speech. It supports more than 30 languages, including English, German, Polish, Spanish, Italian, French, Portuguese, and Hindi. descriptionbeforemodevaluesr   c                 C   s   t |dd}|S )z,Validate that api key exists in environment.Zelevenlabs_api_keyZELEVENLABS_API_KEYr	   )clsr!   _r   r   r   validate_environment/   s   z.ElevenLabsText2SpeechTool.validate_environmentNqueryrun_managerc              
   C   s   t  }|j }z0|jj|| j| jdd}tjdddd}|	| W d   |j
W S 1 s0w   Y  |j
W S  tyK } ztd| d}~ww )	zUse the tool.Zmp3_44100_128)textmodel_idvoice_idZoutput_formatbxz.mp3F)r    suffixdeleteNz/Error while running ElevenLabsText2SpeechTool: )r   client
ElevenLabstext_to_speechconvertr   r   tempfileNamedTemporaryFilewriter   	ExceptionRuntimeError)selfr%   r&   r   r-   speechfr   r   r   r   _run7   s,   

zElevenLabsText2SpeechTool._runspeech_filec                 C   sH   t  }t|dd}| }W d   n1 sw   Y  || dS )zPlay the text as speech.rbr   N)r   openreadplay)r6   r:   r   r8   r7   r   r   r   r>   L   s
   
zElevenLabsText2SpeechTool.playc                 C   s4   t  }|j }|jj|| j| jd}|| dS )zUStream the text as speech as it is generated.
        Play the text in your speakers.)r'   r(   r)   N)r   r-   r.   r/   Zconvert_as_streamr   r   stream)r6   r%   r   r-   Zspeech_streamr   r   r   stream_speechT   s   

z'ElevenLabsText2SpeechTool.stream_speech)N)r   r   r   r   r   r   r   r   str__annotations__r   r   r   r   classmethodr   r   r$   r   r   r9   r>   r@   r   r   r   r   r      s(   
 

r   )r1   enumr   typingr   r   r   r   Zlangchain_core.callbacksr   Zlangchain_core.toolsr   Zlangchain_core.utilsr
   Zpydanticr   r   rA   r   r   r   r   r   r   <module>   s    
