o
    lWi                     @   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 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)SyncClientWrapper)jsonable_encoder)remove_none_from_dict)RequestOptions)VoiceSettings)TextToSpeechClient)OutputFormat.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/asistente-voz-ia/venv/lib/python3.10/site-packages/elevenlabs/realtime_tts.pytext_chunker   s   


r$   c                       sx   e Zd Zdef fddZed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 deje fddZ  ZS )RealtimeTextToSpeechClientclient_wrapperc                   s8   t  j|d || _tj| j jdd | _	d S )N)r&   wss)scheme)
super__init___client_wrapperurllibparseurlparseget_base_url_replacegeturl_ws_base_url)selfr&   	__class__r"   r#   r*   +   s   $z#RealtimeTextToSpeechClient.__init__mp3_44100_128N)model_idoutput_formatvoice_settingsrequest_optionsvoice_idr    r7   r8   r9   r:   r   c                c   s   t tj| jdt| d| d| tti | j |dur&|	di ni d}z|
ttdd|r<| ndtd	gd
d W n tjjy^ } zt|j|jdd}~ww z\t|D ]3}	t|	dd}
|
t|
 zt|d}
d|
v r|
d rt|
d V  W qd ty   Y qdw |
ttdd 	 t| }
d|
v r|
d rt|
d V  q tjjy } zd|
v rt|
|jd|jdkrt|j|jdW Y d}~nd}~ww W 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v1/text-to-speech/z/stream-input?model_id=z&output_format=Nadditional_headers)r<   r   T2   )chunk_length_schedule)r    try_trigger_generationr9   generation_config)bodystatus_code)r    r?   g{Gz?audior   )r    messagei  )r   r,   r-   urljoinr2   r   r   r+   get_headersgetsendjsondumpsdict
websockets
exceptionsConnectionClosedErrorr   reasoncoder$   loadsrecvbase64	b64decodeTimeoutErrorConnectionClosed)r3   r;   r    r7   r8   r9   r:   socketce
text_chunkdatar"   r"   r#   convert_realtime0   sx   .


"z+RealtimeTextToSpeechClient.convert_realtime)__name__
__module____qualname__r   r*   OMITstrtypingIteratorOptionalr   r	   r   bytesr[   __classcell__r"   r"   r4   r#   r%   *   s*    
	r%   )ra   urllib.parser,   rI   rS   rL   websockets.sync.clientr   core.api_errorr   core.client_wrapperr   core.jsonable_encoderr   core.remove_none_from_dictr   core.request_optionsr   types.voice_settingsr	   text_to_speech.clientr
   typesr   castAnyr_   rb   r`   r$   r%   r"   r"   r"   r#   <module>   s"   