o
    qmiiS                     @   s
  d dl 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
 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 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 ddlm Z  ddl!m"Z" ddl#m$Z$ e %e j&dZ'G dd dZ(G dd dZ)dS )    N)JSONDecodeError   )ApiError)AsyncClientWrapperSyncClientWrapper)AsyncHttpResponseHttpResponse)parse_obj_as)RequestOptions)BadRequestError)ForbiddenError)InternalServerError)TooManyRequestsError)UnprocessableEntityError)SpeechSampleRate)TextToSpeechLanguage)TextToSpeechModel)TextToSpeechOutputAudioCodec)TextToSpeechResponse)TextToSpeechSpeaker.c                   @      e Zd ZdefddZeeeeeeeeedd
ded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 dej	e dej	e dej	e dee fddZdS )RawTextToSpeechClientclient_wrapperc                C   
   || _ d S N_client_wrapperselfr    r   V/home/ubuntu/.local/lib/python3.10/site-packages/sarvamai/text_to_speech/raw_client.py__init__      
zRawTextToSpeechClient.__init__N
speakerpitchpaceloudnessspeech_sample_rateenable_preprocessingmodeloutput_audio_codectemperaturerequest_optionstexttarget_language_coder$   r%   r&   r'   r(   r)   r*   r+   r,   r-   returnc                C   s
  | j jjd| j  jd|||||||||	|
|dddi|td}zd|j  kr,dk rAn ntt	t
t	| d	}t||d
W S |jdkratt|jttjtj t
tjtj | d	d|jdkrtt|jttjtj t
tjtj | d	d|jdkrtt|jttjtj t
tjtj | d	d|jdkrtt|jttjtj t
tjtj | d	d|jdkrtt|jttjtj t
tjtj | d	d| }W n ty   t|jt|j|jdw t|jt|j|d)an  
        Convert text into spoken audio. The output is a base64-encoded audio string that must be decoded before use.

        **Available Models:**
        - **bulbul:v3**: Latest model with improved quality, 30+ voices, and temperature control
        - **bulbul:v2**: Legacy model with pitch and loudness controls

        **Important Notes for bulbul:v3:**
        - Pitch and loudness parameters are NOT supported
        - Pace range: 0.5 to 2.0
        - Preprocessing is automatically enabled
        - Default sample rate is 24000 Hz
        - Supports sample rates: 8000, 16000, 22050, 24000 Hz (REST API also supports 32000, 44100, 48000 Hz)

        Parameters
        ----------
        text : str
            The text(s) to be converted into speech.

            **Features:**
            - Supports code-mixed text (English and Indic languages)

            **Model-specific limits:**
            - **bulbul:v3:** Max 2500 characters
            - **bulbul:v2:** Max 1500 characters

            **Important Note:**
            - For numbers larger than 4 digits, use commas (e.g., '10,000' instead of '10000')
            - This ensures proper pronunciation as a whole number

        target_language_code : TextToSpeechLanguage
            The language code in BCP-47 format. This parameter is primarily used by the pre-TTS text normalization model to handle language-specific text processing such as number formatting, abbreviations, and special characters.

        speaker : typing.Optional[TextToSpeechSpeaker]
            The speaker voice to be used for the output audio.

            **Default:** Shubh (for bulbul:v3), Anushka (for bulbul:v2)

            **Model Compatibility (Speakers compatible with respective model):**
            - **bulbul:v3:**
              - Shubh (default), Aditya, Ritu, Priya, Neha, Rahul, Pooja, Rohan, Simran, Kavya, Amit, Dev, Ishita, Shreya, Ratan, Varun, Manan, Sumit, Roopa, Kabir, Aayan, Ashutosh, Advait, Amelia, Sophia, Anand, Tanya, Tarun, Sunny, Mani, Gokul, Vijay, Shruti, Suhani, Mohit, Kavitha, Rehan, Soham, Rupali
            - **bulbul:v2:**
              - Female: Anushka, Manisha, Vidya, Arya
              - Male: Abhilash, Karun, Hitesh

            **Note:** Speaker selection must match the chosen model version.

        pitch : typing.Optional[float]
            Controls the pitch of the audio. Lower values result in a deeper voice, while higher values make it sharper. The suitable range is between -0.75 and 0.75. Default is 0.0.

            **Note:** This parameter is only supported for bulbul:v2. It is NOT supported for bulbul:v3.

        pace : typing.Optional[float]
            Controls the speed of the audio. Lower values result in slower speech, while higher values make it faster. Default is 1.0.

            **Model-specific ranges:**
            - **bulbul:v3:** 0.5 to 2.0
            - **bulbul:v2:** 0.3 to 3.0

        loudness : typing.Optional[float]
            Controls the loudness of the audio. Lower values result in quieter audio, while higher values make it louder. The suitable range is between 0.3 and 3.0. Default is 1.0.

            **Note:** This parameter is only supported for bulbul:v2. It is NOT supported for bulbul:v3.

        speech_sample_rate : typing.Optional[SpeechSampleRate]
            Specifies the sample rate of the output audio. Supported values are 8000, 16000, 22050, 24000, 32000, 44100, 48000 Hz.

            **Note:** Higher sample rates (32000, 44100, 48000 Hz) are only available with bulbul:v3 via the REST API, not in streaming mode.

            **Default:** 24000 Hz

        enable_preprocessing : typing.Optional[bool]
            Controls whether normalization of English words and numeric entities (e.g., numbers, dates) is performed. Set to true for better handling of mixed-language text.

            **Model-specific behavior:**
            - **bulbul:v3:** Not Supported
            - **bulbul:v2:** Default is false

        model : typing.Optional[TextToSpeechModel]
            Specifies the model to use for text-to-speech conversion.

            **Available models:**
            - **bulbul:v3:** Latest model with improved quality, 30+ voices, pace, and temperature control
            - **bulbul:v2:** Legacy model with pitch, loudness, and pace controls

        output_audio_codec : typing.Optional[TextToSpeechOutputAudioCodec]
            Specifies the audio codec for the output audio file. Different codecs offer various compression and quality characteristics.

        temperature : typing.Optional[float]
            Temperature controls how much randomness and expressiveness the TTS model uses while generating speech.

            Lower values produce more stable and consistent output, while higher values sound more expressive but may introduce artifacts or errors. The suitable range is between 0.01 and 2.0. Default is 0.6.

            **Note:** This parameter is only supported for bulbul:v3. It has no effect on bulbul:v2.

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

        Returns
        -------
        HttpResponse[TextToSpeechResponse]
            Successful Response
        text-to-speechPOSTr.   r/   r$   r%   r&   r'   r(   r)   r*   r+   r,   content-typeapplication/jsonbase_urlmethodjsonheadersr-   omit   ,  type_object_responsedata  r:   body        status_coder:   rF   )r   httpx_clientrequestget_environmentbaseOMITrL   typingcastr   r	   r9   r   r   dictr:   OptionalAnyr   r   r   r   r   r   r.   r   r.   r/   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   	_response_data_response_jsonr   r   r    convert   s   w




















zRawTextToSpeechClient.convert)__name__
__module____qualname__r   r!   rQ   strr   rR   rU   r   floatr   boolr   r   r
   r   r   r[   r   r   r   r    r      N    	
r   c                   @   r   )AsyncRawTextToSpeechClientr   c                C   r   r   r   r   r   r   r    r!      r"   z#AsyncRawTextToSpeechClient.__init__Nr#   r.   r/   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r0   c                   s  | j jjd| j  jd|||||||||	|
|dddi|tdI dH }zd|j  kr0d	k rEn ntt	t
t	| d
}t||dW S |jdkrett|jttjtj t
tjtj | d
d|jdkrtt|jttjtj t
tjtj | d
d|jdkrtt|jttjtj t
tjtj | d
d|jdkrtt|jttjtj t
tjtj | d
d|jdkrtt|jttjtj t
tjtj | d
d| }W n ty   t|jt|j|jdw t|jt|j|d)as  
        Convert text into spoken audio. The output is a base64-encoded audio string that must be decoded before use.

        **Available Models:**
        - **bulbul:v3**: Latest model with improved quality, 30+ voices, and temperature control
        - **bulbul:v2**: Legacy model with pitch and loudness controls

        **Important Notes for bulbul:v3:**
        - Pitch and loudness parameters are NOT supported
        - Pace range: 0.5 to 2.0
        - Preprocessing is automatically enabled
        - Default sample rate is 24000 Hz
        - Supports sample rates: 8000, 16000, 22050, 24000 Hz (REST API also supports 32000, 44100, 48000 Hz)

        Parameters
        ----------
        text : str
            The text(s) to be converted into speech.

            **Features:**
            - Supports code-mixed text (English and Indic languages)

            **Model-specific limits:**
            - **bulbul:v3:** Max 2500 characters
            - **bulbul:v2:** Max 1500 characters

            **Important Note:**
            - For numbers larger than 4 digits, use commas (e.g., '10,000' instead of '10000')
            - This ensures proper pronunciation as a whole number

        target_language_code : TextToSpeechLanguage
            The language code in BCP-47 format. This parameter is primarily used by the pre-TTS text normalization model to handle language-specific text processing such as number formatting, abbreviations, and special characters.

        speaker : typing.Optional[TextToSpeechSpeaker]
            The speaker voice to be used for the output audio.

            **Default:** Shubh (for bulbul:v3), Anushka (for bulbul:v2)

            **Model Compatibility (Speakers compatible with respective model):**
            - **bulbul:v3:**
              - Shubh (default), Aditya, Ritu, Priya, Neha, Rahul, Pooja, Rohan, Simran, Kavya, Amit, Dev, Ishita, Shreya, Ratan, Varun, Manan, Sumit, Roopa, Kabir, Aayan, Ashutosh, Advait, Amelia, Sophia, Anand, Tanya, Tarun, Sunny, Mani, Gokul, Vijay, Shruti, Suhani, Mohit, Kavitha, Rehan, Soham, Rupali
            - **bulbul:v2:**
              - Female: Anushka, Manisha, Vidya, Arya
              - Male: Abhilash, Karun, Hitesh

            **Note:** Speaker selection must match the chosen model version.

        pitch : typing.Optional[float]
            Controls the pitch of the audio. Lower values result in a deeper voice, while higher values make it sharper. The suitable range is between -0.75 and 0.75. Default is 0.0.

            **Note:** This parameter is only supported for bulbul:v2. It is NOT supported for bulbul:v3.

        pace : typing.Optional[float]
            Controls the speed of the audio. Lower values result in slower speech, while higher values make it faster. Default is 1.0.

            **Model-specific ranges:**
            - **bulbul:v3:** 0.5 to 2.0
            - **bulbul:v2:** 0.3 to 3.0

        loudness : typing.Optional[float]
            Controls the loudness of the audio. Lower values result in quieter audio, while higher values make it louder. The suitable range is between 0.3 and 3.0. Default is 1.0.

            **Note:** This parameter is only supported for bulbul:v2. It is NOT supported for bulbul:v3.

        speech_sample_rate : typing.Optional[SpeechSampleRate]
            Specifies the sample rate of the output audio. Supported values are 8000, 16000, 22050, 24000, 32000, 44100, 48000 Hz.

            **Note:** Higher sample rates (32000, 44100, 48000 Hz) are only available with bulbul:v3 via the REST API, not in streaming mode.

            **Default:** 24000 Hz

        enable_preprocessing : typing.Optional[bool]
            Controls whether normalization of English words and numeric entities (e.g., numbers, dates) is performed. Set to true for better handling of mixed-language text.

            **Model-specific behavior:**
            - **bulbul:v3:** Not Supported
            - **bulbul:v2:** Default is false

        model : typing.Optional[TextToSpeechModel]
            Specifies the model to use for text-to-speech conversion.

            **Available models:**
            - **bulbul:v3:** Latest model with improved quality, 30+ voices, pace, and temperature control
            - **bulbul:v2:** Legacy model with pitch, loudness, and pace controls

        output_audio_codec : typing.Optional[TextToSpeechOutputAudioCodec]
            Specifies the audio codec for the output audio file. Different codecs offer various compression and quality characteristics.

        temperature : typing.Optional[float]
            Temperature controls how much randomness and expressiveness the TTS model uses while generating speech.

            Lower values produce more stable and consistent output, while higher values sound more expressive but may introduce artifacts or errors. The suitable range is between 0.01 and 2.0. Default is 0.6.

            **Note:** This parameter is only supported for bulbul:v3. It has no effect on bulbul:v2.

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

        Returns
        -------
        AsyncHttpResponse[TextToSpeechResponse]
            Successful Response
        r1   r2   r3   r4   r5   r6   Nr<   r=   r>   rA   rD   rE   rG   rH   rI   rJ   rK   )r   rM   rN   rO   rP   rQ   rL   rR   rS   r   r	   r9   r   r   rT   r:   rU   rV   r   r   r   r   r   r   r.   rW   r   r   r    r[      s   w




















z"AsyncRawTextToSpeechClient.convert)r\   r]   r^   r   r!   rQ   r_   r   rR   rU   r   r`   r   ra   r   r   r
   r   r   r[   r   r   r   r    rc      rb   rc   )*rR   json.decoderr   core.api_errorr   core.client_wrapperr   r   core.http_responser   r   core.pydantic_utilitiesr	   core.request_optionsr
   errors.bad_request_errorr   errors.forbidden_errorr   errors.internal_server_errorr   errors.too_many_requests_errorr   !errors.unprocessable_entity_errorr   types.speech_sample_rater   types.text_to_speech_languager   types.text_to_speech_modelr   'types.text_to_speech_output_audio_codecr   types.text_to_speech_responser   types.text_to_speech_speakerr   rS   rV   rQ   r   rc   r   r   r   r    <module>   s,    Z