
     i                     b    d dl Z d dlmZ d dlmZ d dlmZmZ d dlm	Z	 d dl
mZ  G d de      Zy)	    N)Pipeline)	AudioFile)
AnnotationSegment)Client)DiarizeOutputc                        e Zd ZdZddededz  f fdZdee   defdZ		 	 	 dd	e
d
edz  dedz  dedz  def
dZ xZS )SDKa.  Wrapper around official pyannoteAI API client

    Parameters
    ----------
    model : str, optional
        pyannoteAI speaker diarization model.
        Defaults to "precision-2".
    token : str, optional
        pyannoteAI API key created from https://dashboard.pyannote.ai.
        Defaults to using `PYANNOTEAI_API_KEY` environment variable.

    Usage
    -----
    >>> from pyannote.audio.pipelines.pyannoteai.sdk import SDK
    >>> pipeline = SDK(token="{PYANNOTEAI_API_KEY}")
    >>> speaker_diarization = pipeline("/path/to/your/audio.wav")
    Nmodeltokenc                     t         |           || _        |xs  t        j                  j                  dd       | _        t        | j                        | _        y )NPYANNOTEAI_API_KEY)	super__init__r   osenvirongetr   r   _client)selfr   r   kwargs	__class__s       |/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/pyannote/audio/pipelines/pyannoteai/sdk.pyr   zSDK.__init__4   sA    
Hbjjnn-A4H
djj)    diarizationreturnc                     t               }t        |      D ]$  \  }}t        |d   |d         }|d   }||||f<   & |j                  d      S )Nstartend)r   r   speakerstring)r   	enumerater   rename_tracks)r   r   
annotationtturnsegmentr   s          r   _deserializezSDK._deserialize;   sZ    \
 -GAtDMtE{CG9oG%,Jwz" . ''11r   filenum_speakersmin_speakersmax_speakersc           	      <   | j                   j                  |      }| j                   j                  ||||d| j                  d      }| j                   j	                  |      }| j                  |d   d         }| j                  |d   d         }	t        ||	      S )al  Speaker diarization using pyannoteAI web API

        This method will upload `file`, initiate a diarization job,
        retrieve its output, and deserialize the latter into a good
        old pyannote.core.Annotation instance.

        Parameters
        ----------
        file : AudioFile
            Processed file.
        num_speakers : int, optional
            Force number of speakers to diarize. If not provided, the
            number of speakers will be determined automatically.
        min_speakers : int, optional
            Not supported yet. Minimum number of speakers. Has no effect when `num_speakers` is provided.
        max_speakers : int, optional
            Not supported yet. Maximum number of speakers. Has no effect when `num_speakers` is provided.

        Returns
        -------
        speaker_diarization : Annotation
            Speaker diarization result (when successful)

        Raises
        ------
        PyannoteAIFailedJob
            If the job failed
        PyannoteAICanceledJob
            If the job was canceled
        HTTPError
            If something else went wrong
        FT)r)   r*   r+   
confidencer   	exclusiveoutputr   exclusiveDiarization)speaker_diarizationexclusive_speaker_diarization)r   uploaddiarizer   retriever'   r   )
r   r(   r)   r*   r+   	media_urljob_id
job_outputr1   r2   s
             r   applyz	SDK.applyE   s    R ,,T2	 %%%%%** & 
 \\**62
*.*;*;Jx<PQ^<_*`484E4EjQYFZ[qFr4s% 3*G
 	
r   )zprecision-2N)NNN)__name__
__module____qualname____doc__strr   listdictr   r'   r   intr   r9   __classcell__)r   s   @r   r
   r
   !   s    $*c *#* *2T
 2z 2 $(#'#'?
?
 Dj?
 Dj	?

 Dj?
 
?
r   r
   )r   pyannote.audior   pyannote.audio.core.ior   pyannote.corer   r   pyannoteai.sdkr   ,pyannote.audio.pipelines.speaker_diarizationr   r
    r   r   <module>rI      s'   . 
 # , - ! Fc
( c
r   