o
    Ã¿i  ã                   @   sj   d Z ddlZddlmZ ddlmZ dedee fdd„Zdedee fd	d
„Zdedee fdd„Z	dS )zGUtility functions for extracting probability metrics from STT services.é    N)ÚOptional)ÚTranscriptionFrameÚframeÚreturnc                 C   sL   | j sdS t| j dƒr$| j jr$| j jd }t|ddƒ}|dur$t |¡S dS )aÅ  Extract probability from Whisper-based TranscriptionFrame result.

    Works with Groq, OpenAI Whisper, or other Whisper-based services that use
    verbose_json format with segments containing avg_logprob.

    Converts avg_logprob to probability.

    Args:
        frame: TranscriptionFrame with result from GroqSTTService or OpenAISTTService
            (when include_prob_metrics=True and using Whisper models).

    Returns:
        Probability (0-1) if available, None otherwise.

    Example::

        from pipecat.services.groq.stt import GroqSTTService
        from pipecat.services.whisper.utils import extract_whisper_probability

        stt = GroqSTTService(include_prob_metrics=True)
        # ... use stt in pipeline ...
        # In your frame processor:
        if isinstance(frame, TranscriptionFrame):
            prob = extract_whisper_probability(frame)
            if prob:
                print(f"Transcription confidence: {prob:.2%}")
    NÚsegmentsr   Úavg_logprob)ÚresultÚhasattrr   ÚgetattrÚmathÚexp)r   Úsegmentr   © r   úR/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/services/whisper/utils.pyÚextract_whisper_probability   s   
r   c                 C   s@   | j sdS t| j dƒr| j j}|rt|ƒt|ƒ }t |¡S dS )a7  Extract probability from OpenAI GPT-4o-transcribe TranscriptionFrame result.

    Args:
        frame: TranscriptionFrame with result from OpenAISTTService
            using GPT-4o-transcribe model (when include_prob_metrics=True).

    Returns:
        Probability (0-1) if available, None otherwise.

    Example::

        from pipecat.services.openai.stt import OpenAISTTService
        from pipecat.services.whisper.utils import extract_openai_gpt4o_probability

        stt = OpenAISTTService(model="gpt-4o-transcribe", include_prob_metrics=True)
        # ... use stt in pipeline ...
        # In your frame processor:
        if isinstance(frame, TranscriptionFrame):
            prob = extract_openai_gpt4o_probability(frame)
            if prob:
                print(f"Transcription confidence: {prob:.2%}")
    NÚlogprobs)r   r	   r   ÚsumÚlenr   r   )r   r   r   r   r   r   Ú extract_openai_gpt4o_probability8   s   
r   c                 C   s¤   | j sdS | j }t|dƒrP|jrPt|jdƒrP|jjrP|jjd }t|ddƒ}|dur.t|ƒS t|ddƒ}|rPdd„ |D ƒ}d	d„ |D ƒ}|rPtt|ƒt|ƒ ƒS dS )
a0  Extract probability from Deepgram TranscriptionFrame result.

    Args:
        frame: TranscriptionFrame with result from DeepgramSTTService.

    Returns:
        Probability (0-1) if available, None otherwise.
        Returns alternative-level confidence if available, otherwise calculates
        average confidence from word-level confidences.

    Example::

        from pipecat.services.deepgram.stt import DeepgramSTTService
        from pipecat.services.whisper.utils import extract_deepgram_probability

        stt = DeepgramSTTService()
        # ... use stt in pipeline ...
        # In your frame processor:
        if isinstance(frame, TranscriptionFrame):
            prob = extract_deepgram_probability(frame)
            if prob:
                print(f"Transcription confidence: {prob:.2%}")
    NÚchannelÚalternativesr   Ú
confidenceÚwordsc                 S   s   g | ]}t |d dƒ‘qS )r   N)r
   )Ú.0Úwr   r   r   Ú
<listcomp>‚   ó    z0extract_deepgram_probability.<locals>.<listcomp>c                 S   s   g | ]}|d ur|‘qS )Nr   )r   Úcr   r   r   r   ƒ   r   )r   r	   r   r   r
   Úfloatr   r   )r   r   ÚaltÚconfr   Ú
word_confsr   r   r   Úextract_deepgram_probability]   s    r"   )
Ú__doc__r   Útypingr   Úpipecat.frames.framesr   r   r   r   r"   r   r   r   r   Ú<module>   s   )%