o
    qmi%.                     @   s  d dl Z d dlmZmZ d dlZd dlZd dlm  m	Z
 ddlmZ ddlmZmZ ddlmZ ddlmZmZ dd	lmZmZ dd
lmZ ddlmZ ddlmZ ddlmZ zd dl m!Z" W n e#yp   d dlm!Z" Y nw G dd dZ$G dd dZ%dS )    N)asynccontextmanagercontextmanager   )ApiError)AsyncClientWrapperSyncClientWrapper)RequestOptions   ),AsyncRawSpeechToTextTranslateStreamingClient'RawSpeechToTextTranslateStreamingClient)/AsyncSpeechToTextTranslateStreamingSocketClient*SpeechToTextTranslateStreamingSocketClient))SpeechToTextTranslateStreamingFlushSignal)0SpeechToTextTranslateStreamingHighVadSensitivity)-SpeechToTextTranslateStreamingInputAudioCodec)(SpeechToTextTranslateStreamingVadSignals)connectc                   @      e Zd ZdefddZedefddZedddddddddd	e	j
e	jd
  de	j
e de	j
e de	j
e de	j
e de	j
e de	j
e de	j
e de	je fddZdS )$SpeechToTextTranslateStreamingClientclient_wrapperc                C      t |d| _d S N)r   )r   _raw_clientselfr    r   f/home/ubuntu/.local/lib/python3.10/site-packages/sarvamai/speech_to_text_translate_streaming/client.py__init__      z-SpeechToTextTranslateStreamingClient.__init__returnc                 C      | j S )z
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawSpeechToTextTranslateStreamingClient
        r   r   r   r   r   with_raw_response      	z6SpeechToTextTranslateStreamingClient.with_raw_responseNmodelsample_ratehigh_vad_sensitivityvad_signalsflush_signalinput_audio_codecapi_subscription_keyrequest_optionsr&   saaras:v2.5r'   r(   r)   r*   r+   r,   r-   c             
   c   sp   | j j jd }	t }
|dur|
d|}
|dur"|
d|}
|dur,|
d|}
|dur6|
d|}
|dur@|
d|}
|durJ|
d|}
|	d	|
  }	| j j }|durat||d
< |rnd|v rn|	|d  z"t
j|	|d}t|dV  W d   W dS 1 sw   Y  W dS  tjjy } z|j}|dkrt|t|ddt|t|ddd}~ww )u  
        WebSocket channel for real-time speech to text streaming with English translation.

        **Note:** This API Reference page is provided for informational purposes only.
        The Try It playground may not provide the best experience for streaming audio.
        For optimal streaming performance, please use the SDK or implement your own WebSocket client.

        Parameters
        ----------
        model : typing.Optional[typing.Literal["saaras:v2.5"]]
            Model to be used for speech to text translation.

            - **saaras:v2.5** (default): Translation model that translates audio from any spoken Indic language to English.
              - Example: Hindi audio → English text output

        sample_rate : typing.Optional[str]
            Audio sample rate for the WebSocket connection. When specified as a connection parameter, only 16kHz and 8kHz are supported. 8kHz is only available via this connection parameter. If not specified, defaults to 16kHz.

        high_vad_sensitivity : typing.Optional[SpeechToTextTranslateStreamingHighVadSensitivity]
            Enable high VAD (Voice Activity Detection) sensitivity

        vad_signals : typing.Optional[SpeechToTextTranslateStreamingVadSignals]
            Enable VAD signals in response

        flush_signal : typing.Optional[SpeechToTextTranslateStreamingFlushSignal]
            Signal to flush the audio buffer and finalize transcription and translation

        input_audio_codec : typing.Optional[SpeechToTextTranslateStreamingInputAudioCodec]
            Audio codec/format of the input stream. Use this when sending raw PCM audio.
            Supported values: wav, pcm_s16le, pcm_l16, pcm_raw.

        api_subscription_key : typing.Optional[str]
            API subscription key for authentication

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        SpeechToTextTranslateStreamingSocketClient
        /speech-to-text-translate/wsNr&   r'   r(   r)   r*   r+   ?Api-Subscription-Keyadditional_headers)r2   	websocket  /Websocket initialized with invalid credentials.status_codeheadersbody8Unexpected error when initializing websocket connection.)r   _client_wrapperget_environment
productionhttpxQueryParamsaddget_headersstrupdatewebsockets_sync_clientr   r   
websockets
exceptionsInvalidStatusCoder8   r   dictr   r&   r'   r(   r)   r*   r+   r,   r-   ws_urlquery_paramsr9   protocolexcr8   r   r   r   r   *   sP   6&z,SpeechToTextTranslateStreamingClient.connect)__name__
__module____qualname__r   r   propertyr   r#   r   typingOptionalLiteralrC   r   r   r   r   r   Iteratorr   r   r   r   r   r   r      @    
	
r   c                   @   r   ))AsyncSpeechToTextTranslateStreamingClientr   c                C   r   r   )r
   r   r   r   r   r   r      r   z2AsyncSpeechToTextTranslateStreamingClient.__init__r   c                 C   r    )z
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawSpeechToTextTranslateStreamingClient
        r!   r"   r   r   r   r#      r$   z;AsyncSpeechToTextTranslateStreamingClient.with_raw_responseNr%   r&   r.   r'   r(   r)   r*   r+   r,   r-   c             
   C  s  | j j jd }	t }
|dur|
d|}
|dur"|
d|}
|dur,|
d|}
|dur6|
d|}
|dur@|
d|}
|durJ|
d|}
|	d	|
  }	| j j }|durat||d
< |rnd|v rn|	|d  z+t
|	|d4 I dH }t|dV  W d  I dH  W dS 1 I dH sw   Y  W dS  tjjy } z|j}|dkrt|t|ddt|t|ddd}~ww )u  
        WebSocket channel for real-time speech to text streaming with English translation.

        **Note:** This API Reference page is provided for informational purposes only.
        The Try It playground may not provide the best experience for streaming audio.
        For optimal streaming performance, please use the SDK or implement your own WebSocket client.

        Parameters
        ----------
        model : typing.Optional[typing.Literal["saaras:v2.5"]]
            Model to be used for speech to text translation.

            - **saaras:v2.5** (default): Translation model that translates audio from any spoken Indic language to English.
              - Example: Hindi audio → English text output

        sample_rate : typing.Optional[str]
            Audio sample rate for the WebSocket connection. When specified as a connection parameter, only 16kHz and 8kHz are supported. 8kHz is only available via this connection parameter. If not specified, defaults to 16kHz.

        high_vad_sensitivity : typing.Optional[SpeechToTextTranslateStreamingHighVadSensitivity]
            Enable high VAD (Voice Activity Detection) sensitivity

        vad_signals : typing.Optional[SpeechToTextTranslateStreamingVadSignals]
            Enable VAD signals in response

        flush_signal : typing.Optional[SpeechToTextTranslateStreamingFlushSignal]
            Signal to flush the audio buffer and finalize transcription and translation

        input_audio_codec : typing.Optional[SpeechToTextTranslateStreamingInputAudioCodec]
            Audio codec/format of the input stream. Use this when sending raw PCM audio.
            Supported values: wav, pcm_s16le, pcm_l16, pcm_raw.

        api_subscription_key : typing.Optional[str]
            API subscription key for authentication

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncSpeechToTextTranslateStreamingSocketClient
        r/   Nr&   r'   r(   r)   r*   r+   r0   r1   r2   )extra_headersr3   r5   r6   r7   r;   )r   r<   r=   r>   r?   r@   rA   rB   rC   rD   websockets_client_connectr   rF   rG   rH   r8   r   rI   rJ   r   r   r   r      sP   62z1AsyncSpeechToTextTranslateStreamingClient.connect)rO   rP   rQ   r   r   rR   r
   r#   r   rS   rT   rU   rC   r   r   r   r   r   AsyncIteratorr   r   r   r   r   r   rX      rW   rX   )&rS   
contextlibr   r   r?   websockets.exceptionsrF   websockets.sync.clientsyncclientrE   core.api_errorr   core.client_wrapperr   r   core.request_optionsr   
raw_clientr
   r   socket_clientr   r   5types.speech_to_text_translate_streaming_flush_signalr   =types.speech_to_text_translate_streaming_high_vad_sensitivityr   :types.speech_to_text_translate_streaming_input_audio_codecr   4types.speech_to_text_translate_streaming_vad_signalsr   websockets.legacy.clientr   rZ   ImportErrorr   rX   r   r   r   r   <module>   s*   k