
    3fi`                        d dl mZ d dl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 d dlmZ  ej                   e      Z G d d	e      Zy)
    )annotationsN)AnyDictOptional)CallbackManagerForToolRun)BaseTool)get_from_dict_or_env)model_validatorc                      e Zd ZU dZdZded<   dZded<   dZded<   ded	<   d
Zded<   dZ	ded<    e
d      edd              ZddZ	 d	 	 	 	 	 ddZy)AzureCogsText2SpeechToola  Tool that queries the Azure Cognitive Services Text2Speech API.

    In order to set this up, follow instructions at:
    https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/get-started-text-to-speech?pivots=programming-language-python
     strazure_cogs_keyazure_cogs_regionzen-USspeech_languager   speech_config$azure_cognitive_services_text2speechnamezkA wrapper around Azure Cognitive Services Text2Speech. Useful for when you need to convert text to speech. descriptionbefore)modec                    t        |dd      }t        |dd      }	 ddlmc m} |j	                  ||      |d<   |S # t
        $ r t        d	      w xY w)
z9Validate that api key and endpoint exists in environment.r   AZURE_COGS_KEYr   AZURE_COGS_REGIONr   N)subscriptionregionr   zmazure-cognitiveservices-speech is not installed. Run `pip install azure-cognitiveservices-speech` to install.)r	   azure.cognitiveservices.speechcognitiveservicesspeechSpeechConfigImportError)clsvaluesr   r   	speechsdks        |/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/tools/azure_cognitive_services/text2speech.pyvalidate_environmentz-AzureCogsText2SpeechTool.validate_environment!   s     .$&6
 1')<

	>>&/&<&<+4E '= 'F?#   	O 	s	   = Ac                   	 dd l mc m} || j                  _        j                  | j                  d       }|j                  |      }|j                  |j                  j                  k(  rY|j                  |      }t        j                  ddd      5 }|j                  |j                         d d d        |j                  S |j                  |j                  j                   k(  rj|j"                  }t$        j'                  d|j                          |j                  |j(                  j*                  k(  rt-        d|j.                         y	d
|j                   S # t        $ r Y cw xY w# 1 sw Y   j                  S xY w)Nr   )r   audio_configwbz.wavF)r   suffixdeletezSpeech synthesis canceled: zSpeech synthesis error: zSpeech synthesis canceled.zSpeech synthesis failed: )r   r   r   r!   r   speech_synthesis_languageSpeechSynthesizer
speak_textreasonResultReasonSynthesizingAudioCompletedAudioDataStreamtempfileNamedTemporaryFilesave_to_wav_filer   Canceledcancellation_detailsloggerdebugCancellationReasonErrorRuntimeErrorerror_details)	selftextr   r$   speech_synthesizerresultstreamfr7   s	            r%   _text2speechz%AzureCogsText2SpeechTool._text2speech;   sn   	>> 8G4&88,,4 9 
 $..t4==I22MMM..v6F,,& 0''/0
 66M]]i44===#)#>#> LL67K7R7R6STU#**i.J.J.P.PP"./C/Q/Q.RS  0 /v}}o>>;  		0
 66Ms   	E" E2"	E/.E/2FNc                ~    	 | j                  || j                        }|S # t        $ r}t        d|       d}~ww xY w)zUse the tool.z.Error while running AzureCogsText2SpeechTool: N)rD   r   	Exceptionr<   )r>   queryrun_managerspeech_filees        r%   _runzAzureCogsText2SpeechTool._run]   sL    	U++E43G3GHK 	U!OPQsSTT	Us     	<7<)r#   r   returnr   )r?   r   r   r   rL   r   )N)rG   r   rH   z#Optional[CallbackManagerForToolRun]rL   r   )__name__
__module____qualname____doc__r   __annotations__r   r   r   r   r
   classmethodr&   rD   rK        r%   r   r      s     NCs"OS"6D#6	?  
 (#  $0 ?J <@
U
U 9
U 
	
UrT   r   )
__future__r   loggingr3   typingr   r   r   langchain_core.callbacksr   langchain_core.toolsr   langchain_core.utilsr	   pydanticr
   	getLoggerrM   r8   r   rS   rT   r%   <module>r]      sB    "   & & > ) 5 $			8	$XUx XUrT   