o
    "i                     @   s   d dl Z d dlZd dlZd dlZd dl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de je de je fddZG dd deZdS )    N)connect   )ApiError)jsonable_encoder)remove_none_from_dict)RequestOptions)VoiceSettings)TextToSpeechClient.chunksreturnc                 c   s    d}d}| D ]8}| |r| dr|n|d V  |}q||r;||d  }| dr/|n|d V  |dd }q||7 }q|dkrK|d V  dS dS )zKUsed during input streaming to chunk text blocks and set last char to space).,?!;:u   —-()[]}  r   r   r   N)endswith
startswith)r
   	splittersbuffertextoutput r    [/var/www/html/voicebot/backend/venv/lib/python3.10/site-packages/elevenlabs/realtime_tts.pytext_chunker   s   


r"   c                   @   sV   e Zd Zeeddd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 )RealtimeTextToSpeechClientN)model_idvoice_settingsrequest_optionsvoice_idr   r$   r%   r&   r   c          	      c   s   t tjddt| d| tti | j |dur"|di ni d}|	t
tdd|r7| ndtd	gd
d t|D ]3}t|dd}|	t
| zt
|d}d|v ro|d rot|d V  W qF tyy   Y qFw |	t
tdd 	 zt
| }d|v r|d rt|d V  W n tjjy   d|v rt|dY nw qW d   dS 1 sw   Y  dS )a  
        Converts text into speech using a voice of your choice and returns audio.

        Parameters:
            - voice_id: str. Voice ID to be used, you can use https://api.elevenlabs.io/v1/voices to list all the available voices.
            
            - text: typing.Iterator[str]. The text that will get converted into speech.

            - model_id: typing.Optional[str]. Identifier of the model that will be used, you can query them using GET /v1/models. The model needs to have support for text to speech, you can check this using the can_do_text_to_speech property.

            - voice_settings: typing.Optional[VoiceSettings]. Voice settings overriding stored setttings for the given voice. They are applied only on the given request.

            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
        ---
        from elevenlabs import PronunciationDictionaryVersionLocator, VoiceSettings
        from elevenlabs.client import ElevenLabs

        def get_text() -> typing.Iterator[str]:
            yield "Hello, how are you?"
            yield "I am fine, thank you."

        client = ElevenLabs(
            api_key="YOUR_API_KEY",
        )
        client.text_to_speech.convert_realtime(
            voice_id="string",
            text=get_text(),
            model_id="string",
            voice_settings=VoiceSettings(
                stability=1.1,
                similarity_boost=1.1,
                style=1.1,
                use_speaker_boost=True,
            ),
        )
        zwss://api.elevenlabs.io/zv1/text-to-speech/z/stream-input?model_id=Nadditional_headers)r(   r   T2   )chunk_length_schedule)r   try_trigger_generationr%   generation_config)r   r+   g-C6?audior   )r   message)body)r   urllibparseurljoinr   r   _client_wrapperget_headersgetsendjsondumpsdictr"   loadsrecvbase64	b64decodeTimeoutError
websockets
exceptionsConnectionClosedr   )	selfr'   r   r$   r%   r&   socket
text_chunkdatar    r    r!   convert_realtime*   sh   -
"z+RealtimeTextToSpeechClient.convert_realtime)__name__
__module____qualname__OMITstrtypingIteratorOptionalr   r   bytesrF   r    r    r    r!   r#   (   s"    r#   )rL   urllib.parser0   r7   r<   r?   websockets.sync.clientr   core.api_errorr   core.jsonable_encoderr   core.remove_none_from_dictr   core.request_optionsr   types.voice_settingsr   text_to_speech.clientr	   castAnyrJ   rM   rK   r"   r#   r    r    r    r!   <module>   s   