
    dh_                         S SK r S SKrS SKrS SKrS SKJrJrJrJrJ	r	J
r
JrJr  S SKJr  S SKJr  S SKJr  S SKJr  \R*                  " \5      r " S S\5      r " S	 S
\5      r " S S\5      r " S S\5      r " S S\5      rS\S\4S jrg)    N)AnyCallableDictIteratorLiteralOptionalTupleUnion)Document)BaseBlobParser)Blob)is_openai_v1c                       \ rS rSrSrSSSSSSSSSS.	S\\   S\\   S\\   S	\\/ \4   S4   S
\\   S\\   S\\	S   S4   S\\
   S\S\4S jjr\S\\\4   4S j5       rS\S\\   4S jrSrg)AzureOpenAIWhisperParser   a	  
Transcribe and parse audio files using Azure OpenAI Whisper.

This parser integrates with the Azure OpenAI Whisper model to transcribe
audio files. It differs from the standard OpenAI Whisper parser, requiring
an Azure endpoint and credentials. The parser is limited to files under 25 MB.

**Note**:
This parser uses the Azure OpenAI API, providing integration with the Azure
 ecosystem, and making it suitable for workflows involving other Azure services.

For files larger than 25 MB, consider using Azure AI Speech batch transcription:
https://learn.microsoft.com/azure/ai-services/speech-service/batch-transcription-create?pivots=rest-api#use-a-whisper-model

Setup:
    1. Follow the instructions here to deploy Azure Whisper:
       https://learn.microsoft.com/azure/ai-services/openai/whisper-quickstart?tabs=command-line%2Cpython-new&pivots=programming-language-python
    2. Install ``langchain`` and set the following environment variables:

    .. code-block:: bash

        pip install -U langchain langchain-community

        export AZURE_OPENAI_API_KEY="your-api-key"
        export AZURE_OPENAI_ENDPOINT="https://your-endpoint.openai.azure.com/"
        export OPENAI_API_VERSION="your-api-version"

Example Usage:
    .. code-block:: python

        from langchain.community import AzureOpenAIWhisperParser

        whisper_parser = AzureOpenAIWhisperParser(
            deployment_name="your-whisper-deployment",
            api_version="2024-06-01",
            api_key="your-api-key",
            # other params...
        )

        audio_blob = Blob(path="your-audio-file-path")
        response = whisper_parser.lazy_parse(audio_blob)

        for document in response:
            print(document.page_content)

Integration with Other Loaders:
    The AzureOpenAIWhisperParser can be used with video/audio loaders and
    `GenericLoader` to automate retrieval and parsing.

YoutubeAudioLoader Example:
    .. code-block:: python

        from langchain_community.document_loaders.blob_loaders import (
            YoutubeAudioLoader
            )
        from langchain_community.document_loaders.generic import GenericLoader

        # Must be a list
        youtube_url = ["https://your-youtube-url"]
        save_dir = "directory-to-download-videos"

        loader = GenericLoader(
            YoutubeAudioLoader(youtube_url, save_dir),
            AzureOpenAIWhisperParser(deployment_name="your-deployment-name")
        )

        docs = loader.load()
N   )	api_keyazure_endpointapi_versionazure_ad_token_providerlanguagepromptresponse_formattemperaturemax_retriesr   r   r   r   r   r   r   jsontextsrtverbose_jsonvttr   deployment_namer   c       
         <   U=(       d    [         R                  R                  S5      U l        U=(       d    [         R                  R                  S5      U l        U=(       d    [         R                  R                  S5      U l        X@l        XPl        X`l        Xpl	        Xl
        Xl        Xl         SSKn[        5       (       aK  UR!                  U R                  U R                  U R
                  U R                  U R                  S9U l        gU R                  (       a  U R                  Ul        U R                  (       a  U R                  Ul        U R
                  (       a  U R
                  Ul        SUl        Xl        g! [         a    [        S5      ef = f)	a  
Initialize the AzureOpenAIWhisperParser.

Args:
    api_key (Optional[str]):
        Azure OpenAI API key. If not provided, defaults to the
        `AZURE_OPENAI_API_KEY` environment variable.
    azure_endpoint (Optional[str]):
        Azure OpenAI service endpoint. Defaults to `AZURE_OPENAI_ENDPOINT`
        environment variable if not set.
    api_version (Optional[str]):
        API version to use,
        defaults to the `OPENAI_API_VERSION` environment variable.
    azure_ad_token_provider (Union[Callable[[], str], None]):
        Azure Active Directory token for authentication (if applicable).
    language (Optional[str]):
        Language in which the request should be processed.
    prompt (Optional[str]):
        Custom instructions or prompt for the Whisper model.
    response_format (Union[str, None]):
        The desired output format. Options: "json", "text", "srt",
        "verbose_json", "vtt".
    temperature (Optional[float]):
        Controls the randomness of the model's output.
    deployment_name (str):
        The deployment name of the Whisper model.
    max_retries (int):
        Maximum number of retries for failed API requests.
Raises:
    ImportError:
        If the required package `openai` is not installed.
AZURE_OPENAI_API_KEYAZURE_OPENAI_ENDPOINTOPENAI_API_VERSIONr   NEopenai package not found, please install it with `pip install openai`)r   r   r   r   r   azure)osenvirongetr   r   r   r   r   r   r   r   r"   r   openaiImportErrorr   AzureOpenAI_clientapi_baseapi_type)selfr   r   r   r   r   r   r   r   r"   r   r,   s               j/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/document_loaders/parsers/audio.py__init__!AzureOpenAIWhisperParser.__init__V   s>   ` H"**..1G"H,W

?V0W&N"**..9M*N'>$ .&.&	 >>!--#22 ,, ,,(,(D(D . DL ||!%"""&"5"5%)%5%5"%FO!L+  	W 	s   3F Freturnc                     U R                   U R                  U R                  U R                  S.nUR	                  5        VVs0 sH  u  p#Uc  M
  X#_M     snn$ s  snnf N)r   r   r   r   r   r   r   r   itemsr2   paramskvs       r3   _create_params'AzureOpenAIWhisperParser._create_params   S     kk#33++	
 "(A1AAA   AAblobc              #     #    [        [        UR                  5      S5      n [        5       (       aH  U R                  R
                  R                  R                  " SU R                  US.U R                  D6nOIU R                  R                  R                  " SU R                  U R                  US.U R                  D6n [        [        U[        5      (       d  UR                  OUSUR                   0S9v   g! [         a    e f = f7f)a  
Lazily parse the provided audio blob for transcription.

Args:
    blob (Blob):
        The audio file in Blob format to be transcribed.

Yields:
    Document:
        Parsed transcription from the audio file.

Raises:
    Exception:
        If an error occurs during transcription.
rbmodelfile)rG   deployment_idrH   sourcepage_contentmetadataN )openstrpathr   r/   audiotranscriptionscreater"   r?   Audio
transcribe	Exceptionr   
isinstancer   rJ   )r2   rC   file_obj
transcripts       r3   
lazy_parse#AzureOpenAIWhisperParser.lazy_parse   s     " DII-	~~!\\//>>EE ..! ))
 "\\//:: .."&"6"6! ))	
 j#.. $,	
 	
  		s*    DAC= 9D:AC= ;D=D		D)r/   r   r   r   r   r"   r   r   r   r   r   )__name__
__module____qualname____firstlineno____doc__r   rP   r
   r   r   floatintr4   propertyr   r   r?   r   r   r   r[   __static_attributes__rN       r3   r   r      s   CP "&(,%)BF"& $ '+T" #T" !	T"
 c]T" "'xC'8$'>!?T" 3-T" T" @A4G
T" e_T" T" T"l BS#X B B*
t *
(: *
rf   r   c                       \ rS rSrSr SSSSSSSSS.S\\   S\S	\\   S
\\S4   S\\S4   S\\	S   S4   S\\S4   S\4S jjjr
\S\\\4   4S j5       rS\S\\   4S jrSrg)OpenAIWhisperParser   az  Transcribe and parse audio files.

Audio transcription is with OpenAI Whisper model.

Args:
    api_key: OpenAI API key
    chunk_duration_threshold: Minimum duration of a chunk in seconds
        NOTE: According to the OpenAI API, the chunk duration should be at least 0.1
        seconds. If the chunk duration is less or equal than the threshold,
        it will be skipped.
Ng?z	whisper-1)chunk_duration_thresholdbase_urlr   r   r   r   rG   r   rj   rk   r   r   r   r   r   rG   c                    Xl         X l        Ub  UO[        R                  R	                  S5      U l        X@l        XPl        X`l        Xpl	        Xl
        g )NOPENAI_API_BASE)r   rj   r)   r*   r+   rk   r   r   r   r   rG   )	r2   r   rj   rk   r   r   r   r   rG   s	            r3   r4   OpenAIWhisperParser.__init__   sK     (@% ,H"**..AR2S 	 !.&
rf   r6   c                     U R                   U R                  U R                  U R                  S.nUR	                  5        VVs0 sH  u  p#Uc  M
  X#_M     snn$ s  snnf r8   r9   r;   s       r3   r?   "OpenAIWhisperParser._create_params	  rA   rB   rC   c           
   #     #     SSK n[        U5      n[        5       (       a%  UR	                  U R
                  U R                  S9nODU R
                  (       a  U R
                  Ul        U R                  (       a  U R                  Ul        SnUS-  S-  n[        [        S[        U5      U5      5       GHa  u  pxX8X-    n	U	R                  U R                  ::  a  M(  [        R                  " U	R                  SS	9R!                  5       5      n
UR"                  b  UR"                  S
U S3-   U
l        OSU S3U
l        ['        SUS-    S35        SnUS:  au   [        5       (       a>  WR(                  R*                  R,                  " SU R.                  U
S.U R0                  D6nO&UR2                  R5                  U R.                  U
5      nO['        S5        GM'  [?        [A        U[8        5      (       d  URB                  OUUR"                  US.S9v   GMd     g! [         a    [        S5      ef = f! [6         a?  nUS-  n['        SU S[9        U5       35        [:        R<                  " S5         SnAOSnAff = fUS:  a  GM1  N7f)Lazily parse the blob.r   Nr'   )r   rk      <     mp3format_part_z.mp3part_Transcribing part    !r   rF   zAttempt z failed. Exception:    z&Failed to transcribe after 3 attempts.)rJ   chunkrK   rN   )"r,   r-   _get_audio_from_blobr   OpenAIr   rk   r0   	enumeraterangelenduration_secondsrj   ioBytesIOexportreadrJ   nameprintrR   rS   rT   rG   r?   rU   rV   rW   rP   timesleepr   rX   r   )r2   rC   r,   rR   clientchunk_durationchunk_duration_mssplit_numberir   rY   attemptsrZ   es                 r3   r[   OpenAIWhisperParser.lazy_parse  sF    	 %T*>>]]4<<$--]PF ||!%}}"&-- *R/$6  )q#e*>O)PQOLa34E%%)F)FFzz%,,e,"<"A"A"CDH{{& $|nD.I I"'~T : &|a'7&8:;HQ,"#~~%+\\%@%@%G%G &"&**8&?C?R?R&
 &,\\%<%<TZZ%R
 >?!*c22 (__$(KK,G	 A  R/  	W 	^ ! "MHHXJ.B3q6(KLJJqMM" Q,sH   J
H EJ
A3H4AJ
H11J
4
I=>5I83J
8I==	J
)r   rk   rj   r   rG   r   r   r   )N)r]   r^   r_   r`   ra   r   rP   rb   r
   r   r4   rd   r   r   r?   r   r   r   r[   re   rN   rf   r3   rh   rh      s    
 "& +."&%)#' *. # #(	
 3- T	" c4i  @A4G
 5$;' 2 BS#X B BAt A(: Arf   rh   c                   n    \ rS rSrSr     SS\S\\   S\S\S\\\	      4
S	 jjr
S
\S\\   4S jrSrg)OpenAIWhisperParserLocaliW  a  Transcribe and parse audio files with OpenAI Whisper model.

Audio transcription with OpenAI Whisper model locally from transformers.

Parameters:
device - device to use
    NOTE: By default uses the gpu if available,
    if you want to use cpu, please set device = "cpu"
lang_model - whisper model to use, for example "openai/whisper-medium"
forced_decoder_ids - id states for decoder in multilanguage model,
    usage example:
    from transformers import WhisperProcessor
    processor = WhisperProcessor.from_pretrained("openai/whisper-medium")
    forced_decoder_ids = WhisperProcessor.get_decoder_prompt_ids(language="french",
      task="transcribe")
    forced_decoder_ids = WhisperProcessor.get_decoder_prompt_ids(language="french",
    task="translate")



Ndevice
lang_model
batch_sizechunk_lengthforced_decoder_idsc                     SSK Jn   SSKnUS:X  a  SU l        O(UR
                  R                  5       (       a  SOSU l        U R                  S:X  a  SnU(       a  UOUU l        O_UR
                  R                  U R                  5      R                  S	-  n	U	S
:  a  Sn
OU	S:  a  Sn
OU	S:  a  Sn
OSn
U(       a  UOU
U l        [        SU R                  5        X0l        U" SU R                  UU R                  S9U l        Ub&   XPR                  R                  R                  l        gg! [         a    [        S5      ef = f! [         a    [        S5      ef = f! [          a#  n["        R%                  SU S35         SnAgSnAff = f)a  Initialize the parser.

Args:
    device: device to use.
    lang_model: whisper model to use, for example "openai/whisper-medium".
      Defaults to None.
    forced_decoder_ids: id states for decoder in a multilanguage model.
      Defaults to None.
    batch_size: batch size used for decoding
      Defaults to 8.
    chunk_length: chunk length used during inference.
      Defaults to 30s.
r   )pipelinezQtransformers package not found, please install it with `pip install transformers`NCtorch package not found, please install it with `pip install torch`cpuzcuda:0zopenai/whisper-base     iX  zopenai/whisper-smalli.  zopenai/whisper-mediumzopenai/whisper-largezUsing the following model: zautomatic-speech-recognition)rG   chunk_length_sr   zOUnable to set forced_decoder_ids parameter for whisper modelText of exception: z9Therefore whisper model will use default mode for decoder)transformersr   r-   torchr   cudais_availabler   get_device_propertiestotal_memoryr   r   piperG   configr   rW   loggerinfo)r2   r   r   r   r   r   r   r   default_modelmem	rec_modelexception_texts               r3   r4   !OpenAIWhisperParserLocal.__init__n  s   *	-	 U?DK&+jj&=&=&?&?(UDK;;%1M,6jMDO **224;;?LLPWXCTz1	t2	u3	2	,6jIDO+T__=$ *//';;	
	 )<N		&&9 *[  	- 	  	U 	R  **8)9OP s.   D- E $E -EE
F)FFrC   r6   c              #     #     SSK n[        U5      n[        R                  " UR                  SS9R                  5       5      n[        SUR                   S35        UR                  USS	9u  pVU R                  UR                  5       U R                  S
9S   n[        USUR                  0S9v   g! [         a    [        S5      ef = f7f)rr   r   NzGlibrosa package not found, please install it with `pip install librosa`rv   rw   r{   r}   i>  )sr)r   r   rJ   rK   )librosar-   r   r   r   r   r   r   rQ   loadr   copyr   r   rJ   )r2   rC   r   rR   rY   yr   
predictions           r3   r[   #OpenAIWhisperParserLocal.lazy_parse  s     	 %T*::ell%l8==?@ 	"499+Q/0X%0YYqvvxDOOYDVL
#,
 	
#  	( 	s   CB3 B+C3C		C)r   r   r   r   )0N      N)r]   r^   r_   r`   ra   rP   r   rc   r	   r   r4   r   r   r   r[   re   rN   rf   r3   r   r   W  sy    0 $(48LL SML 	L
 L %U4[1L\
t 
(: 
rf   r   c            	       d    \ rS rSrSrSSSSS.S\\   S\\   S	\S
\4S jjrS\S\	\
   4S jrSrg)YandexSTTParseri  zSTranscribe and parse audio files.
Audio transcription is with OpenAI Whisper model.Ngeneralauto)r   	iam_tokenrG   r   r   r   rG   r   c                ^    USL USL :X  a  [        S5      eXl        X l        X0l        X@l        g)a  Initialize the parser.

Args:
    api_key: API key for a service account
    with the `ai.speechkit-stt.user` role.
    iam_token: IAM token for a service account
    with the `ai.speechkit-stt.user` role.
    model: Recognition model name.
      Defaults to general.
    language: The language in ISO 639-1 format.
      Defaults to automatic language recognition.
Either `api_key` or `iam_token` must be provided, but not both.
Nz?Either 'api_key' or 'iam_token' must be provided, but not both.)
ValueErrorr   r   rG   r   )r2   r   r   rG   r   s        r3   r4   YandexSTTParser.__init__  s;    * tOd!23Q  "
 rf   rC   r6   c              #     #     SSK JnJnJn  SSKJn  [        U5      nU R                  (       a  U" UR                  U R                  S9S9  OU" UR                  U R                  S9S9  UR                  5       nU R                  Ul        U R                  Ul        UR                  Ul        UR!                  U5      nU H%  n	[#        U	R$                  SUR&                  0S	9v   M'     g
! [         a    [        S5      ef = f7f)rr   r   )configure_credentialscredsmodel_repository)AudioProcessingTypezYyandex-speechkit package not found, please install it with `pip install yandex-speechkit`)r   )yandex_credentials)r   rJ   rK   N)	speechkitr   r   r   speechkit.sttr   r-   r   r   YandexCredentialsr   recognition_modelrG   r   Fullaudio_processing_typerV   r   normalized_textrJ   )
r2   rC   r   r   r   r   rR   rG   resultress
             r3   r[   YandexSTTParser.lazy_parse  s     	PP9 %T*<<!#(#:#:4<<#:#P "#(#:#:T^^#:#T !224jj&9&>&>#!!%(C 00"DKK0  3  	1 	s   DC+ CD+DD)r   r   r   rG   r]   r^   r_   r`   ra   r   rP   r4   r   r   r   r[   re   rN   rf   r3   r   r     sb    9 "&#'! #! C=	!
 ! !<#t #(: #rf   r   c                   X    \ rS rSrSrSSS.S\\   S\\   4S jjrS	\S
\	\
   4S jrSrg)FasterWhisperParseri   a  Transcribe and parse audio files with faster-whisper.

faster-whisper is a reimplementation of OpenAI's Whisper model using CTranslate2,
which is up to 4 times faster than openai/whisper for the same accuracy while using
less memory. The efficiency can be further improved with 8-bit quantization on both
CPU and GPU.

It can automatically detect the following 14 languages and transcribe the text
into their respective languages: en, zh, fr, de, ja, ko, ru, es, th, it, pt, vi,
ar, tr.

The gitbub repository for faster-whisper is :
https://github.com/SYSTRAN/faster-whisper

Example: Load a YouTube video and transcribe the video speech into a document.
    .. code-block:: python

        from langchain.document_loaders.generic import GenericLoader
        from langchain_community.document_loaders.parsers.audio
            import FasterWhisperParser
        from langchain.document_loaders.blob_loaders.youtube_audio
            import YoutubeAudioLoader


        url="https://www.youtube.com/watch?v=your_video"
        save_dir="your_dir/"
        loader = GenericLoader(
            YoutubeAudioLoader([url],save_dir),
            FasterWhisperParser()
        )
        docs = loader.load()

r   N)r   
model_sizer   r   c                    SSK nUS:X  a  SU l        O(UR                  R	                  5       (       a  SOSU l        U R                  S:X  a  SU l        OcUR                  R                  U R                  5      R                  S-  nUS:  a  SU l        O#US	:  a  S
U l        OUS:  a  SU l        OSU l        Ub  US;   a  X l        ggg! [         a    [        S5      ef = f)zInitialize the parser.

Args:
    device: It can be "cuda" or "cpu" based on the available device.
    model_size: There are four model sizes to choose from: "base", "small",
                "medium", and "large-v3", based on the available GPU memory.
r   Nr   r   r   baser   ru   i  smallr   mediumlarge-v3)r   r   r   r   )r   r-   r   r   r   r   r   r   )r2   r   r   r   r   s        r3   r4   FasterWhisperParser.__init__C  s    	 U?DK$)JJ$;$;$=$=&5DK ;;%$DO **224;;?LLPWXCTz"(t")t"*",!DD", E "5  	U 	s   C	 	CrC   r6   c              #   
  #     SSK Jn  [        U5      n[        R
                  " UR                  SS9R                  5       5      nU" U R                  U R                  S9nUR                  USS9u  pgU Hp  n[        UR                  UR                  S	UR                  UR                  4-  UR                   S
[#        UR$                  S-  5      -  S.UR&                  ES9v   Mr     g! [         a    [        S5      ef = f7f)rr   r   )WhisperModelzUfaster_whisper package not found, please install it with `pip install faster-whisper`rv   rw   )r   r~   )	beam_sizez[%.2fs -> %.2fs]z%d%%d   )rJ   
timestampsr   probabilityrK   N)faster_whisperr   r-   r   r   r   r   r   r   r   rV   r   r   rJ   startendr   roundlanguage_probabilityrM   )	r2   rC   r   rR   rY   rG   segmentsr   segments	            r3   r[   FasterWhisperParser.lazy_parsep  s     	3 %T*::ell%l8==?@ T__T[[A))(a)@G$\\"kk"4w{{7S"S $#)E$2K2Kc2Q,R#R	
 mm	 	    	/ 	s   DC* C D*D  Dr   rN   rf   r3   r   r      sJ     J !'$(	+- +- SM	+-Zt (: rf   r   rC   r6   c                 n    SSK Jn  [        U R                  [
        5      (       a1  UR                  [        R                  " U R                  5      5      nU$ U R                  c.  U R                  (       a  UR                  U R                  5      nU$ [        S5      e! [         a    [        S5      ef = f)a  Get audio data from blob.

Args:
    blob: Blob object containing the audio data.

Returns:
    AudioSegment: Audio data from the blob.

Raises:
    ImportError: If the required package `pydub` is not installed.
    ValueError: If the audio data is not found in the blob
r   )AudioSegmentzCpydub package not found, please install it with `pip install pydub`zUnable to get audio from blob)pydubr   r-   rX   databytes	from_filer   r   rQ   r   )rC   r   rR   s      r3   r   r     s    
& $))U##&&rzz$))'<= L 
	tyy&&tyy1 L 899  
Q
 	

s   B B4)r   loggingr)   r   typingr   r   r   r   r   r   r	   r
   langchain_core.documentsr   )langchain_community.document_loaders.baser   1langchain_community.document_loaders.blob_loadersr    langchain_community.utils.openair   	getLoggerr]   r   r   rh   r   r   r   r   rN   rf   r3   <module>r      s    	  	  Q Q Q - D B 9			8	$P
~ P
fq. qh~
~ ~
BEn EPn. nbt  rf   