
    qi                     j    d Z ddlZddlmZ ddlmZ dedee   fdZdedee   fdZdedee   fd	Z	y)
zGUtility functions for extracting probability metrics from STT services.    N)Optional)TranscriptionFrameframereturnc                     | j                   syt        | j                   d      rS| j                   j                  r=| j                   j                  d   }t        |dd      }|t	        j
                  |      S y)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	   s      P/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/services/whisper/utils.pyextract_whisper_probabilityr      sd    8 << u||Z(U\\-B-B,,''*g}d;"88K((    c                     | j                   syt        | j                   d      rD| j                   j                  }|r,t        |      t	        |      z  }t        j                  |      S y)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	   s      r    extract_openai_gpt4o_probabilityr   8   sT    . << u||Z(<<((h-#h-7K88K((r   c                    | j                   sy| j                   }t        |d      r|j                  rt        |j                  d      r|j                  j                  r|j                  j                  d   }t	        |dd      }|t        |      S t	        |dd      }|rN|D cg c]  }t	        |dd       }}|D cg c]  }||	 }}|r t        t        |      t        |      z        S yc c}w c c}w )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words)r
   r   r   r   r   floatr   r   )r   r
   altconfr   w
word_confscs           r   extract_deepgram_probabilityr#   ]   s    0 <<\\Fvy!fnn6>>>2v~~7R7R..--a0C3d3DT{"C$/EFKLgat<L
L)3EAq}aE
E Z3z?!BCC MEs   $C/>C4C4)
__doc__r   typingr   pipecat.frames.framesr   r   r   r   r#    r   r   <module>r(      s^    N   4&'9 &huo &R",> "8E? "J*(: *x *r   