
    >|h                     ,   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 j0                  e j2                  d      Zde j6                  e   de j6                  e   fdZ G d de      Zy)    N)connect   )ApiError)SyncClientWrapper)jsonable_encoder)remove_none_from_dict)RequestOptions)VoiceSettings)TextToSpeechClient)OutputFormat.chunksreturnc              #     K   d}d}| D ]n  }|j                  |      r|j                  d      r|n|dz    |}1|j                  |      r(||d   z   }|j                  d      r|n|dz    |dd }j||z  }p |dk7  r|dz    yyw)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outputs        V/var/www/html/test/engine/venv/lib/python3.12/site-packages/elevenlabs/realtime_tts.pytext_chunkerr%      s     XIF 	??9%"OOC0&fslBF__Y'd1g%F"OOC0&fslB!"XFdNF	 |sl s   BBc                        e Zd Zdef f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Z xZS )RealtimeTextToSpeechClientclient_wrapperc                    t         |   |       t        j                  j	                  | j
                  j                               j                  d      j                         | _	        y )N)r(   wss)scheme)
super__init__urllibparseurlparse_client_wrapperget_base_url_replacegeturl_ws_base_url)selfr(   	__class__s     r$   r-   z#RealtimeTextToSpeechClient.__init__+   sQ    7"LL11$2F2F2S2S2UV__gl_mttv    mp3_44100_128N)model_idoutput_formatvoice_settingsrequest_optionsvoice_idr"   r:   r;   r<   r=   r   c             #   ~  K   t        t        j                  j                  | j                  dt        |       d| d|       t        t        i | j                  j                         ||j                  di       ni                   5 }	 |j                  t        j                  t        dd|r|j                         ndt        d	g
                         	 t)        |      D ]{  }	t        |	d      }
|j                  t        j                  |
             	 t        j*                  |j-                  d            }
d|
v r|
d   rt/        j0                  |
d          } |j                  t        j                  t        d                   	 t        j*                  |j-                               }
d|
v r|
d   rt/        j0                  |
d          G# t        j                  j                   $ r&}t#        |j$                  |j&                        d}~ww xY w# t2        $ r Y Fw xY w# t        j                  j4                  $ rU}d
v rt#        |
|j&                        |j&                  dk7  r!t#        |j$                  |j&                        Y d}~nd}~ww xY w	 ddd       y# 1 sw Y   yxY ww)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_generationr<   generation_config)bodystatus_code)r"   rC   g{Gz?audior   )r"   messagei  )r   r.   r/   urljoinr5   r   r   r1   get_headersgetsendjsondumpsdict
websockets
exceptionsConnectionClosedErrorr   reasoncoder%   loadsrecvbase64	b64decodeTimeoutErrorConnectionClosed)r6   r>   r"   r:   r;   r<   r=   socketce
text_chunkdatas              r$   convert_realtimez+RealtimeTextToSpeechClient.convert_realtime/   s    \ LL  "#3H#=">>UV^U__no|n}~  0%..::<L[Lg?../CRHmo 
 2	H DDJJ /3@N~':':'<TX*.35$+		 	H".t"4 JZMDKK

4 01#zz&++d*;<"d?tG}"("2"24="AA DJJt}56::fkkm4D$4=$..tG}==	 ! ((>> DBII277CCD (  ((99 H$""''BBWW_"		rwwGG %H %c2	H 2	H 2	Hs   B
J=J1AG?H2AH"$A8H2H9!HHJ1"	H/+H2.H//H22J$AJJ1J$$J1(	J=1J:6J=)__name__
__module____qualname__r   r-   OMITstrtypingIteratorOptionalr   r
   r	   bytesr_   __classcell__)r7   s   @r$   r'   r'   *   s    w*; w *.7F9=;?`H`H ooc"	`H
 //#&`H |4`H 6`H  8`H 
	`Hr8   r'   )re   urllib.parser.   rM   rW   rP   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Anyrc   rf   rd   r%   r'    r8   r$   <module>rw      s         * $ 2 3 = 0 / 5  v{{6::s#- &//#2F $eH!3 eHr8   