
    Mil              	      X   S r SSKJr  SSKrSSKrSSKJr  SSKJrJ	r	  SSK
Jr  SSK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Jr  SSKJr  SSKJr  \R:                  R<                  r\" 5       \ SSSSSSS.               SS jjj5       5       r         SS jr!SS jr"g)zGPartition audio files into elements using speech-to-text transcription.    )annotationsN)Path)IOAny)add_chunking_strategy)ElementElementMetadataNarrativeText)detect_filetype)FileType)exactly_one)apply_metadataget_last_modified_date)SpeechToTextAgent)is_temp_file_path)filelanguage	stt_agentmetadata_filenamemetadata_last_modifiedc               4   [        XS9  U (       a  [        U 5      OSn[        XU5      nU b  U n	OaUc   eUR                  S5        [	        X5      n
[
        R                  " U
SS9 n[        R                  " X5        UR                  n	SSS5         [        R                  " U5      nUR                  W	US9nU c(  [        U	5      (       a  [        U	5      R                  SS9  U(       d  / $ / nU Ha  nUS	   R!                  5       nU(       d  M  [#        US
9n[%        UUUS   US   S9Ul        SUR&                  l        UR+                  U5        Mc     U$ ! , (       d  f       N= f! U c*  [        W	5      (       a  [        U	5      R                  SS9  f f f = f)a  Partition an audio file into elements using speech-to-text transcription.

Supports any audio format handled by the configured STT agent. The default Whisper
agent accepts any format that ffmpeg supports (WAV, MP3, FLAC, M4A, OGG, OPUS, WEBM,
and more). Transcription produces one NarrativeText element per segment, preserving
segment-level timestamps in ``metadata.segment_start_seconds`` and
``metadata.segment_end_seconds``. Requires the optional ``audio`` extra:
``pip install "unstructured[audio]"``.

.. note::
    **Chunking drops segment timestamps.** When elements are merged by a chunking
    strategy, ``segment_start_seconds`` and ``segment_end_seconds`` are currently
    discarded (consolidation strategy ``DROP``). If audio timeline alignment matters
    for your use-case, consume the un-chunked elements directly.

Parameters
----------
filename
    Path to the audio file.
file
    File-like object opened in binary mode (e.g. ``open("audio.mp3", "rb")``).
language
    Optional ISO 639-1 language code for the spoken language (e.g. ``"en"``).
    When ``None``, the speech-to-text agent may auto-detect.
stt_agent
    Optional fully-qualified class name of the SpeechToTextAgent implementation.
    Defaults to the Whisper agent when the ``audio`` extra is installed.
metadata_filename
    Filename to store in element metadata when partitioning from a file object.
metadata_last_modified
    Last modified date to store in element metadata.
)filenamer   Nr   F)suffixdelete)r   T)
missing_oktext)r   startend)filetypelast_modifiedsegment_start_secondssegment_end_secondsspeech_to_text)r   r   _audio_mime_typeseek_audio_suffixtempfileNamedTemporaryFileshutilcopyfileobjnamer   	get_agenttranscribe_segmentsr   r   unlinkstripr
   r	   metadatadetection_originappend)r   r   r   r   r   r   kwargsr    	mime_type
audio_pathr   tmpagentsegmentselementssegr   elements                     v/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/unstructured/partition/audio.pypartition_audior=      s   X - 9A*84dM 1BCI 
		!t7((uEt)J F5!++I6,,Z(,K 1* = =##t#4	 H6{  "T**'"%g, #E
	
 -=)   O; FE  1* = =##t#4 !>s   &#E&E( 
E%(/Fc                    SnU b
  [        U S9nOUb
  [        UUS9nUb0  U[        R                  [        R                  4;  a  UR                  $ [
        $ )zQReturn the MIME type for the audio source, falling back to WAV when undetectable.N)	file_path)r   metadata_file_path)r   r   UNKEMPTYr4   _AUDIO_MIME_TYPE_FALLBACK)r   r   r   detecteds       r<   r$   r$   q   s]     !%H"X6		"0
 x~~0N N!!!$$    c                    U[        U SS5      4 H,  nU(       d  M  [        U5      R                  nU(       d  M*  Us  $    g)aE  Return the file-extension suffix for a temp file that wraps `file`.

Preference order:
1. Extension of `metadata_filename` when provided (e.g. ".mp3" from "recording.mp3").
2. Extension of `file.name` when the file object exposes a name (e.g. a real opened file).
3. ".wav" as a safe fallback recognised by all STT backends.
r+   Nz.wav)getattrr   r   )r   r   r+   r   s       r<   r&   r&      s@     #GD&$$?@4$Z&&Fv	 A
 rE   )N)r   
str | Noner   IO[bytes] | Noner   rH   r   rH   r   rH   r   rH   r3   r   returnzlist[Element])r   rH   r   rI   r   rH   rJ   str)r   z	IO[bytes]r   rH   rJ   rK   )#__doc__
__future__r   r)   r'   pathlibr   typingr   r   unstructured.chunkingr   unstructured.documents.elementsr   r	   r
    unstructured.file_utils.filetyper   unstructured.file_utils.modelr   $unstructured.partition.common.commonr   &unstructured.partition.common.metadatar   r   Dunstructured.partition.utils.speech_to_text.speech_to_text_interfacer   unstructured.utilsr   WAVr4   rC   r=   r$   r&    rE   r<   <module>rZ      s   M "     7 S S < 2 < Y 1$LL22  T " $()-TT T 	T
 T "T 'T T T  Tn%%
% "% 		%&rE   