
    ;i                         U d Z ddlZddlmZ ddlZddlmZmZmZm	Z	m
Z
mZmZ ddlmZ  G d d      Zd	ed
efdZdaee   ed<   d
efdZy)zr
Whisper Speech-to-Text Module

Provides transcription using OpenAI's Whisper model with hallucination detection.
    N)Optional   )WHISPER_MODEL_NAMEWHISPER_LANGUAGEWHISPER_FP16SAMPLE_RATE_OUTBYTES_PER_SAMPLElog_sttlog_call)save_wavc                   ^    e Zd ZdZeeefdededefdZ	d Z
ed        Zded	efd
Zd	efdZy)WhisperTranscriberzs
    Whisper-based speech transcription.

    Handles model loading, transcription, and prompt leak detection.
    
model_namelanguagefp16c                 <    || _         || _        || _        d| _        y)z
        Initialize Whisper transcriber.

        Args:
            model_name: Whisper model size (tiny, base, small, medium, large)
            language: Target language code (e.g., 'en')
            fp16: Use FP16 inference (requires CUDA)
        Nr   r   r   _model)selfr   r   r   s       -/home/sas/my/fyp/ringai/ringai/stt/whisper.py__init__zWhisperTranscriber.__init__   s      % 	    c                     | j                   Zt        j                  d| j                         t	        j
                  | j                        | _         t        j                  d       yy)zLoad Whisper model into memoryNzLoading Whisper model=%sz!Whisper model loaded successfully)r   r   infor   whisper
load_modelr   s    r   r   zWhisperTranscriber.load_model2   sG    ;;MM4dooF!,,T__=DKMM=> r   c                 R    | j                   | j                          | j                   S )z&Get loaded model, loading if necessary)r   r   r   s    r   modelzWhisperTranscriber.model9   s"     ;;OO{{r   pcm_16kreturnc                 P   t        t        t        z  dz        }|rt        |      |k  ry	 t	        j
                  dd      5 }t        ||j                  t               | j                  j                  |j                  | j                  | j                  dddddd	d

      }ddd       j                  d      xs dj                         }t        |      rt        j                   d       y|S # 1 sw Y   OxY w# t"        $ r }t        j$                  d|       Y d}~yd}~ww xY w)z
        Transcribe 16kHz PCM audio to text.

        Args:
            pcm_16k: 16kHz 16-bit PCM audio data

        Returns:
            Transcribed text, or empty string on failure
        g      ? z.wavT)suffixdeleteFg           g333333?g      )	r   r   verbosecondition_on_previous_textinitial_prompttemperature	beam_sizeno_speech_thresholdlogprob_thresholdNtextzDropped prompt-leak utterancezWhisper failed: %r)intr   r	   lentempfileNamedTemporaryFiler   namer   
transcriber   r   getstripis_prompt_leakr
   warningRuntimeErrorerror)r   r    	min_bytestmpresultr.   es          r   r4   zWhisperTranscriber.transcribe@   s	    *::TAB	#g,2	,,F4H C#((O<..HH!]]!/4#% #(+&* /   JJv&,"335D d# ?@K/ 2  	MM.2	s7   C< AC0"AC< .C< 0C95C< <	D%D  D%c                 d    | j                   | j                  | j                  | j                  dudS )z"Get information about loaded modelN)r   r   r   loadedr   r   s    r   get_model_infoz!WhisperTranscriber.get_model_infom   s/     //IIkk-	
 	
r   N)__name__
__module____qualname____doc__r   r   r   strboolr   r   propertyr   bytesr4   dictrA    r   r   r   r      sl     -(!	  	&?  +% +C +Z
 
r   r   r.   r!   c                 ~    | j                         dv rdv ryt              dkD  rt        fddD              ryy)a+  
    Detect when Whisper hallucinates its own prompt instead of transcribing real speech.

    This is a conservative check - only drops obvious prompt leaks, not real user speech.

    Args:
        text: Transcribed text to check

    Returns:
        True if text appears to be a prompt leak
    zcustomer support phone callz"technical or service-related issueT   c              3   &   K   | ]  }|v  
 y w)NrK   ).0kts     r   	<genexpr>z!is_prompt_leak.<locals>.<genexpr>   s     \qAF\s   )customersupport	technicalserviceF)lowerr0   all)r.   rQ   s    @r   r7   r7   w   sG     	

A %).RVW.W 1v|\,[\\r   _transcriberc                  .    t         
t               a t         S )zGet global transcriber instance)rY   r   rK   r   r   get_transcriberr[      s     )+r   )rE   r1   typingr   r   configr   r   r   r   r	   r
   r   audio.processingr   r   rF   rG   r7   rY   __annotations__r[   rK   r   r   <module>r`      sh         (\
 \
~  4 .2h)* 1+ r   