
    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 d dlmZmZ  ej&                  e      Z G d	 d
e      Zy)    )annotationsN)AnyDictOptional)CallbackManagerForToolRun)BaseTool)get_from_dict_or_env)model_validator)detect_file_src_typedownload_audio_from_urlc                      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Z	 d	 	 	 	 	 ddZy)AzureCogsSpeech2TextToola  Tool that queries the Azure Cognitive Services Speech2Text API.

    In order to set this up, follow instructions at:
    https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/get-started-speech-to-text?pivots=programming-language-python
     strazure_cogs_keyazure_cogs_regionzen-USspeech_languager   speech_config$azure_cognitive_services_speech2textnamezA wrapper around Azure Cognitive Services Speech2Text. Useful for when you need to transcribe audio to text. Input should be a url to an audio file.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/speech2text.pyvalidate_environmentz-AzureCogsSpeech2TextTool.validate_environment'   s     .$&6
 1')<

	>>&/&<&<+4E '= 'F?#   	O 	s	   = Ac                $   dddfd}dfd}j                   j                  |       j                  j                  |       j                  j                  |       j	                          st        j                  d       sS )NFr   c                *    j                          dy)z)callback that stop continuous recognitionTN)!stop_continuous_recognition_async)evtdonespeech_recognizers    r'   stop_cbz?AzureCogsSpeech2TextTool._continuous_recognize.<locals>.stop_cbE   s    ??AD    c                8    | j                   j                  z  y)z<callback that retrieves the intermediate recognition resultsN)resulttext)r,   r3   s    r'   retrieve_cbzCAzureCogsSpeech2TextTool._continuous_recognize.<locals>.retrieve_cbK   s     CJJOO#Dr0   g      ?)r,   r   returnNone)
recognizedconnectsession_stoppedcanceled"start_continuous_recognition_asynctimesleep)selfr.   r/   r4   r-   r3   s    `  @@r'   _continuous_recognizez.AzureCogsSpeech2TextTool._continuous_recognizeA   s{    		$ 	$$,,[9))11':""**73 	<<>JJsO r0   c                X   	 dd l mc m} t	        |      }|dk(  rj                  |      }n1|dk(  rt        |      }j                  |      }nt        d|       || j                  _	        |j                  | j                  |      }| j                  |      S # t        $ r Y w xY w)Nr   local)filenameremotezInvalid audio path: )r   r    r!   r#   r   AudioConfigr   
ValueErrorr   speech_recognition_languageSpeechRecognizerr?   )r>   
audio_pathr   r&   audio_src_typeaudio_configtmp_audio_pathr.   s           r'   _speech2textz%AzureCogsSpeech2TextTool._speech2text\   s    	>> .j9W$$00*0ELx'4Z@N$00.0IL3J<@AA9H6%66t7I7I<X))*;<<  		s   	B 	B)(B)Nc                ~    	 | j                  || j                        }|S # t        $ r}t        d|       d}~ww xY w)zUse the tool.z.Error while running AzureCogsSpeech2TextTool: N)rL   r   	ExceptionRuntimeError)r>   queryrun_managerr3   es        r'   _runzAzureCogsSpeech2TextTool._runo   sK    	U$$UD,@,@ADK 	U!OPQsSTT	Us     	<7<)r%   r   r5   r   )r.   r   r5   r   )rH   r   r   r   r5   r   )N)rP   r   rQ   z#Optional[CallbackManagerForToolRun]r5   r   )__name__
__module____qualname____doc__r   __annotations__r   r   r   r   r
   classmethodr(   r?   rL   rS    r0   r'   r   r      s     NCs"OS"6D#6	2   (#  $06=, <@
U
U 9
U 
	
Ur0   r   )
__future__r   loggingr<   typingr   r   r   langchain_core.callbacksr   langchain_core.toolsr   langchain_core.utilsr	   pydanticr
   8langchain_community.tools.azure_cognitive_services.utilsr   r   	getLoggerrT   loggerr   rZ   r0   r'   <module>re      sI    "   & & > ) 5 $
 
		8	$eUx eUr0   