o
    pi                     @   s\   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 deZdS )	    N)Pipeline)	AudioFile)
AnnotationSegment)Client)DiarizeOutputc                       sx   e Zd ZdZddededB f fddZdee d	efd
dZ				dde
dedB dedB dedB d	ef
ddZ  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")
    precision-2Nmodeltokenc                    s4   t    || _|ptjdd | _t| j| _d S )NPYANNOTEAI_API_KEY)	super__init__r
   osenvirongetr   r   _client)selfr
   r   kwargs	__class__ [/home/ubuntu/.local/lib/python3.10/site-packages/pyannote/audio/pipelines/pyannoteai/sdk.pyr   4   s   
zSDK.__init__diarizationreturnc                 C   sJ   t  }t|D ]\}}t|d |d d}|d }||||f< q|dS )Nstartend)r   r   speakerstring)r   	enumerater   rename_tracks)r   r   
annotationtturnsegmentr   r   r   r   _deserialize;   s   
zSDK._deserializefilenum_speakersmin_speakersmax_speakersc           
   	   C   sd   | j |}| j j||||d| jdd}| j |}| |d d }| |d d }	t||	d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_outputr.   r/   r   r   r   applyE   s"   )z	SDK.apply)r	   N)NNN)__name__
__module____qualname____doc__strr   listdictr   r%   r   intr   r6   __classcell__r   r   r   r   r   !   s$    r   )r   pyannote.audior   pyannote.audio.core.ior   pyannote.corer   r   pyannoteai.sdkr   ,pyannote.audio.pipelines.speaker_diarizationr   r   r   r   r   r   <module>   s   