
    qi|N                     "   d Z ddlZddlZddlmZmZ ddlmZmZm	Z	 ddl
mZ ddlmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZmZm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z* e G d de             Z+ G d de$      Z,y)aF  Deepgram speech-to-text service for AWS SageMaker.

This module provides a Pipecat STT service that connects to Deepgram models
deployed on AWS SageMaker endpoints. Uses HTTP/2 bidirectional streaming for
low-latency real-time transcription with support for interim results, multiple
languages, and various Deepgram features.
    N)	dataclassfields)AnyAsyncGeneratorOptional)logger)	CancelFrameEndFrame
ErrorFrameFrameInterimTranscriptionFrame
StartFrameTranscriptionFrameVADUserStartedSpeakingFrameVADUserStoppedSpeakingFrame)FrameDirection)SageMakerBidiClient)DeepgramSTTSettingsLiveOptions)STTSettings_warn_deprecated_paramis_given)DEEPGRAM_SAGEMAKER_TTFS_P99)
STTService)Language)time_now_iso8601)
traced_sttc                       e Zd ZdZy)DeepgramSageMakerSTTSettingszqSettings for the Deepgram SageMaker STT service.

    Inherits all fields from :class:`DeepgramSTTSettings`.
    N)__name__
__module____qualname____doc__     Y/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/services/deepgram/sagemaker/stt.pyr   r   ,   s    
 	r%   r   c                   t    e Zd ZU dZeZeed<   dddddddedded	ed
ede	de
dee	   dee
   dee   dee   dee   f fdZde
fdZdedeeef   f fdZdef fdZdef fdZdef fdZdedeedf   fdZdefdZd Zd Z d  Z!d! Z"d"efd#Z#e$	 d+d$ed%e
d&ee%   fd'       Z&d( Z'ded)e(f fd*Z) xZ*S ),DeepgramSageMakerSTTServicea~  Deepgram speech-to-text service for AWS SageMaker.

    Provides real-time speech recognition using Deepgram models deployed on
    AWS SageMaker endpoints. Uses HTTP/2 bidirectional streaming for low-latency
    transcription with support for interim results, speaker diarization, and
    multiple languages.

    Requirements:

    - AWS credentials configured (via environment variables, AWS CLI, or instance metadata)
    - A deployed SageMaker endpoint with Deepgram model: https://developers.deepgram.com/docs/deploy-amazon-sagemaker

    Example::

        stt = DeepgramSageMakerSTTService(
            endpoint_name="my-deepgram-endpoint",
            region="us-east-2",
            settings=DeepgramSageMakerSTTSettings(
                model="nova-3",
                language="en",
                interim_results=True,
                punctuate=True,
            ),
        )
    	_settingslinear16   FN)encodingchannelsmultichannelsample_ratemip_opt_outlive_optionssettingsttfs_p99_latencyendpoint_nameregionr,   r-   r.   r/   r0   r1   r2   r3   c       
         r   t        di dddt        j                  dddddddd	d
ddd	dd	dddddddd	dd	dd	dddd	dd}|t        dt                |	s|j                  ||j                  }|j
                  |j
                  }|j                  |j                  }|j                  |j                  }|j                  |j                  }h d}|j                         j                         D ci c]  \  }}||vs|| }}}t         j                  |      }|j                  |       |	|j                  |	       |j                          t        | <  d||
|d| || _        || _        || _        || _        || _        || _        d	| _        d	| _        d	| _        y	c c}}w )a  Initialize the Deepgram SageMaker STT service.

        Args:
            endpoint_name: Name of the SageMaker endpoint with Deepgram model
                deployed (e.g., "my-deepgram-nova-3-endpoint").
            region: AWS region where the endpoint is deployed (e.g., "us-east-2").
            encoding: Audio encoding format. Defaults to "linear16".
            channels: Number of audio channels. Defaults to 1.
            multichannel: Transcribe each audio channel independently.
                Defaults to False.
            sample_rate: Audio sample rate in Hz. If None, uses the pipeline
                sample rate.
            mip_opt_out: Opt out of Deepgram model improvement program.
            live_options: Legacy configuration options.

                .. deprecated:: 0.0.105
                    Use ``settings=DeepgramSageMakerSTTSettings(...)`` for
                    runtime-updatable fields and direct init parameters for
                    connection-level config.

            settings: Runtime-updatable settings. When provided alongside
                ``live_options``, ``settings`` values take precedence (applied
                after the ``live_options`` merge).
            ttfs_p99_latency: P99 latency from speech end to final transcript in seconds.
                Override for your deployment. See https://github.com/pipecat-ai/stt-benchmark
            **kwargs: Additional arguments passed to the parent STTService.
        modelznova-3languagedetect_entitiesFdiarize	dictationendpointingNinterim_resultsTkeytermkeywordsnumeralsprofanity_filter	punctuateredactreplacesearchsmart_formatutterance_end_ms
vad_eventsr1   >   r-   r,   r0   r/   r.   )r/   r3   r2   r$   )r   r   ENr   r/   r,   r-   r.   r0   to_dictitemsfrom_mappingapply_update_sync_extra_to_fieldssuper__init___endpoint_name_region	_encoding	_channels_multichannel_mip_opt_out_client_response_task_keepalive_task)selfr4   r5   r,   r-   r.   r/   r0   r1   r2   r3   kwargsdefault_settings	init_onlykvlo_dictdelta	__class__s                     r&   rP   z$DeepgramSageMakerSTTService.__init__T   sF   V 8 

[[
 "
 	

 
 
 !
 
 
 
 "
 
 
 
 
  !
" "#
$ %
, #">3OP++7K<O".":":K((4+44H((4+44H,,8#/#<#<L++7".":":K	 -9,@,@,B,H,H,JaDAqaW`N`1a4aa4AA'J --e4 ))(3 	..0 	
#-%	
 		
 , "!)'6:6:7;; bs   F3F3returnc                      y)zCheck if this service can generate processing metrics.

        Returns:
            True, as Deepgram SageMaker service supports metrics generation.
        Tr$   rZ   s    r&   can_generate_metricsz0DeepgramSageMakerSTTService.can_generate_metrics   s     r%   ra   c                    K   t         |   |       d{   }|s|S t        | j                  t              r| j                  j                          | j                  |       |S 7 Ow)z8Apply a settings delta and warn about unhandled changes.N)rO   _update_settings
isinstancer)   r   rN    _warn_unhandled_updated_settings)rZ   ra   changedrb   s      r&   rh   z,DeepgramSageMakerSTTService._update_settings   s\     077N dnn&9:NN002 	--g6! 8s   A(A&AA(framec                 t   K   t         |   |       d{    | j                          d{    y7 7 w)zStart the Deepgram SageMaker STT service.

        Args:
            frame: The start frame containing initialization parameters.
        N)rO   start_connectrZ   rl   rb   s     r&   rn   z!DeepgramSageMakerSTTService.start   s3      gmE"""mmo 	#   848688c                 t   K   t         |   |       d{    | j                          d{    y7 7 w)zbStop the Deepgram SageMaker STT service.

        Args:
            frame: The end frame.
        N)rO   stop_disconnectrp   s     r&   rs   z DeepgramSageMakerSTTService.stop   s6      gl5!!!    	" rq   c                 t   K   t         |   |       d{    | j                          d{    y7 7 w)zgCancel the Deepgram SageMaker STT service.

        Args:
            frame: The cancel frame.
        N)rO   cancelrt   rp   s     r&   rv   z"DeepgramSageMakerSTTService.cancel   s6      gnU###    	$ rq   audioc                   K   | j                   r:| j                   j                  r$	 | j                   j                  |       d{    d y7 
# t        $ r}t	        d|        Y d}~(d}~ww xY ww)zSend audio data to Deepgram for transcription.

        Args:
            audio: Raw audio bytes to transcribe.

        Yields:
            Frame: None (transcription results come via BiDi stream callbacks).
        NUnknown error occurred: )error)rW   	is_activesend_audio_chunk	Exceptionr   )rZ   rw   es      r&   run_sttz#DeepgramSageMakerSTTService.run_stt  sl      <<DLL22Gll33E::: 
 ; G )A!'EFFFGs@   #A9A AA 	A9A 	A6A1,A91A66A9c                    i }| j                   }t        |      D ]  }|j                  dv s|j                  j                  d      r-t	        ||j                        }t        |      r|Qt        |t              rt        |      j                         n
t        |      ||j                  <    t        |j                        r$|j                  t        |j                        |d<   t        |j                        r$|j                  t        |j                        |d<   | j                  |d<   t        | j                        |d<   t        | j                        j                         |d<   t        | j                        |d<   | j                   &t        | j                         j                         |d	<   |j"                  r\|j"                  j%                         D ]?  \  }}|	t        |t              rt        |      j                         n
t        |      ||<   A d
j'                  d |j%                         D              S )zIBuild query string from current settings and init-only connection config.)r7   r8   extra_r7   r8   r,   r-   r.   r/   r0   &c              3   0   K   | ]  \  }}| d |   yw)=Nr$   ).0r^   r_   s      r&   	<genexpr>zBDeepgramSageMakerSTTService._build_query_string.<locals>.<genexpr>5  s     >tq!1#Qqc
>s   )r)   r   name
startswithgetattrr   ri   boolstrlowerr7   r8   rS   rT   rU   r/   rV   r   rK   join)rZ   paramssfvaluekeys         r&   _build_query_stringz/DeepgramSageMakerSTTService._build_query_string  s   NN  	[Avv77166;L;LS;QAqvv&EE?em3=eT3JSZ--/PSTYPZF166N	[ AGG!4!!''lF7OAJJAJJ$:!$QZZF: "^^z 0z!$T%7%7!8!>!>!@~ #D$4$4 5}($'(9(9$:$@$@$BF=! 77ggmmo `
U$8B5$8O#e*"2"2"4UXY^U_F3K` xx>v||~>>>r%   c                   K   t        j                  d       | j                         }t        | j                  | j
                  d|      | _        	 | j                  j                          d{    | j                  | j                               | _
        | j                  | j                               | _        t        j                  d       | j                  d       d{    y7 {7 # t        $ rM}| j                  d| |       d{  7   | j                  d	t!        |             d{  7   Y d}~yd}~ww xY ww)
a  Connect to the SageMaker endpoint and start the BiDi session.

        Builds the Deepgram query string from settings, creates the BiDi client,
        starts the streaming session, and launches background tasks for processing
        responses and sending KeepAlive messages.
        z&Connecting to Deepgram on SageMaker...z	v1/listen)r4   r5   model_invocation_pathmodel_query_stringNz"Connected to Deepgram on SageMakeron_connectedry   	error_msg	exceptionon_connection_error)r   debugr   r   rQ   rR   rW   start_sessioncreate_task_process_responsesrX   _send_keepaliverY   _call_event_handlerr}   
push_errorr   )rZ   query_stringr~   s      r&   ro   z$DeepgramSageMakerSTTService._connect7  s     	=>//1 +--<<"-+	
	J,,,,... #'"2"243J3J3L"MD $(#3#3D4H4H4J#KD LL=>**>::: / ; 	J//.Fqc,JVW/XXX**+@#a&III	Jsm   AEC. .C*/A5C. $C,%C. )E*C. ,C. .	E7D?D#D?4D75D?:E?EEc                   K   | j                   r6| j                   j                  rt        j                  d       	 | j                   j	                  ddi       d{    | j                  r=| j                  j                         s#| j                  | j                         d{    | j                  r=| j                  j                         s#| j                  | j                         d{    | j                   j                          d{    t        j                  d       | j                  d       d{    yyy7 # t
        $ r#}t        j                  d|        Y d}~d}~ww xY w7 7 7 j7 >w)zDisconnect from the SageMaker endpoint.

        Sends a CloseStream message to Deepgram, cancels background tasks
        (KeepAlive and response processing), and closes the BiDi session.
        Safe to call multiple times.
        z+Disconnecting from Deepgram on SageMaker...typeCloseStreamNz$Failed to send CloseStream message: z'Disconnected from Deepgram on SageMakeron_disconnected)rW   r{   r   r   	send_jsonr}   warningrY   donecancel_taskrX   close_sessionr   rZ   r~   s     r&   rt   z'DeepgramSageMakerSTTService._disconnect[  s>     <<DLL22LLFGKll,,fm-DEEE
 ##D,@,@,E,E,G&&t';';<<< ""4+>+>+C+C+E&&t':':;;; ,,,,...LLBC**+<===+ 3<
 F K!EaSIJJK
 = < / >s   :F  E	 EE	 "AF &E8'AF /E:0!F E<-F ?E> F E	 		E5E0*F 0E55F :F <F >F c                   K   | j                   r| j                   j                  rt        j                  d       d{    | j                   r<| j                   j                  r&	 | j                   j	                  ddi       d{    | j                   r| j                   j                  ryyyy7 s7 -# t
        $ r"}t        j                  d|        Y d}~Qd}~ww xY ww)a  Send periodic KeepAlive messages to maintain the connection.

        Sends a KeepAlive JSON message to Deepgram every 5 seconds while the
        connection is active. This prevents the connection from timing out during
        periods of silence.
           Nr   	KeepAlivezFailed to send KeepAlive: )rW   r{   asynciosleepr   r}   r   r   r   s     r&   r   z+DeepgramSageMakerSTTService._send_keepalivey  s      llt||55--"""|| 6 6E,,00&+1FGGG	 llt||55l5l" H  ENN%?s#CDDEsQ   ;C#B1&C#% B5 B3B5 
"C#-C#3B5 5	C >CC#C  C#c                   K   	 | j                   r| j                   j                  r| j                   j                          d{   }|nt        |d      rt        |j                  d      rn|j                  j
                  rX|j                  j
                  j                  d      }	 t        j                  |      }d|v r| j                  |       d{    | j                   r| j                   j                  rt        j                  d
       y7 7 ?# t        j                  $ r t        j                  d|        Y jw xY w# t        j                  $ r t        j                  d       Y ut         $ r)}| j#                  d| |	       d{  7   Y d}~d}~ww xY w# t        j                  d
       w xY ww)a5  Process streaming responses from Deepgram on SageMaker.

        Continuously receives responses from the BiDi stream, decodes the payload,
        parses JSON responses from Deepgram, and processes transcription results.
        Runs as a background task until the connection is closed or cancelled.
        Nr   bytes_zutf-8channelzNon-JSON response: zResponse processor cancelledry   r   zResponse processor stopped)rW   r{   receive_responsehasattrr   r   decodejsonloads_handle_transcript_responseJSONDecodeErrorr   r   r   CancelledErrorr   r}   r   )rZ   resultresponse_dataparsedr~   s        r&   r   z.DeepgramSageMakerSTTService._process_responses  sd    	7,,4<<#9#9#||<<>>> 67+h0O||**(.(;(;(B(B7(K	R%)ZZ%>F  )F2&*&F&Fv&N N N# ,,4<<#9#96 LL565 ?  !O#33 R"NN-@+PQR %% 	9LL78 	Y//.Fqc,JVW/XXX	Y LL56s   G?E	 DA$E	 )-D DD "E	 >GE	 D +EE	 EE	 	(F$1F' 3F$;FFFF' F$$F' 'F>>Gr   c           	        K   |j                  di       j                  dg       }|r|d   j                  d      sy|d   d   }|j                         sy|j                  dd      }d}|d   j                  d      r|d   d   d   }t        |      }|r|j                  d	d      }|r| j                          | j	                  t        || j                  t               ||
             d{    | j                  |||       d{    | j                          d{    y| j	                  t        || j                  t               ||
             d{    y7 r7 Y7 C7 w)a  Handle a transcript response from Deepgram.

        Extracts the transcript text, determines if it's final or interim, extracts
        language information, and pushes the appropriate frame (TranscriptionFrame
        or InterimTranscriptionFrame) downstream.

        Args:
            parsed: The parsed JSON response from Deepgram containing channel,
                alternatives, transcript, and metadata.
        r   alternativesr   
transcriptNis_finalF	languagesfrom_finalize)r   )getstripr   confirm_finalize
push_framer   _user_idr   _handle_transcriptionstop_processing_metricsr   )rZ   r   r   r   r   r8   r   s          r&   r   z7DeepgramSageMakerSTTService._handle_transcript_response  so     zz)R044^RH<?#6#6|#D!!_\2
!::j%0 ?{+#A{3A6H)H #JJ>M%%'//"MM$&!   ,,Z8LLL..000 //)MM$&!   M0sH   C+E(-E .E(E"	E( E$!9E(E&E("E($E(&E(r   r   r8   c                    K   yw)a  Handle a transcription result with tracing.

        This method is decorated with @traced_stt for observability and tracing
        integration. The actual transcription processing is handled by the parent
        class and observers.

        Args:
            transcript: The transcribed text.
            is_final: Whether this is a final transcription result.
            language: The detected language of the transcription, if available.
        Nr$   )rZ   r   r   r8   s       r&   r   z1DeepgramSageMakerSTTService._handle_transcription  s      	s   c                 @   K   | j                          d{    y7 w)z$Start processing metrics collection.N)start_processing_metricsre   s    r&   _start_metricsz*DeepgramSageMakerSTTService._start_metrics  s     ++---s   	directionc                   K   t         |   ||       d{    t        |t              r| j	                          d{    yt        |t
              r~| j                          | j                  r<| j                  j                  r&	 | j                  j                  ddi       d{    t        j                  d|j                  d|       yy7 7 7 /# t        $ r"}t        j                  d|        Y d}~Sd}~ww xY ww)zProcess frames with Deepgram SageMaker-specific handling.

        Args:
            frame: The frame to process.
            direction: The direction of frame processing.
        Nr   Finalizez Error sending Finalize message: z(Triggered finalize event on: frame.name=z, direction=)rO   process_frameri   r   r   r   request_finalizerW   r{   r   r}   r   r   tracer   )rZ   rl   r   r~   rb   s       r&   r   z)DeepgramSageMakerSTTService.process_frame  s      g#E9555 e89%%''':; !!#|| 6 6K,,00&*1EFFF LLD]	|TU < 	6 ( G  KNN%EaS#IJJKs]   DC'DC AD C (C)C -(DDC 	D#D ;D DD)N)+r    r!   r"   r#   r   Settings__annotations__r   r   intr   r   r   floatrP   rf   r   dictr   rh   r   rn   r
   rs   r	   rv   bytesr   r   r   r   ro   rt   r   r   r   r   r   r   r   r   r   __classcell__)rb   s   @r&   r(   r(   6   s   4 ,H++ #"%)&*.2;?,Gu< u< 	u<
 u< u< u< c]u< d^u< {+u< 78u< #5/u<nd K DcN ( ! !!+ !5 ^E4K-H  #?S #?J"JH><E#7J6 6p NR)-9A(9K  .V V> V Vr%   r(   )-r#   r   r   dataclassesr   r   typingr   r   r   logurur   pipecat.frames.framesr	   r
   r   r   r   r   r   r   r   "pipecat.processors.frame_processorr   *pipecat.services.aws.sagemaker.bidi_clientr   pipecat.services.deepgram.sttr   r   pipecat.services.settingsr   r   r   pipecat.services.stt_latencyr   pipecat.services.stt_servicer   pipecat.transcriptions.languager   pipecat.utils.timer   (pipecat.utils.tracing.service_decoratorsr   r   r(   r$   r%   r&   <module>r      sy      ) 0 0 
 
 
 > J J S S D 3 4 / ? 	#6 	 	YV* YVr%   