o
    "iTQ                     @   s   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
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   )core)ApiError)AsyncClientWrapperSyncClientWrapper)jsonable_encoder)remove_none_from_dict)RequestOptions.c                   @      e Zd ZdefddZddddddedeje de	j
d	eje d
eje deje deje fddZddddddedeje de	j
d	eje d
eje deje deje fddZdS )SpeechToSpeechClientclient_wrapperc                C   
   || _ d S N_client_wrapperselfr    r   f/var/www/html/voicebot/backend/venv/lib/python3.10/site-packages/elevenlabs/speech_to_speech/client.py__init__      
zSpeechToSpeechClient.__init__Noptimize_streaming_latencymodel_idvoice_settingsrequest_optionsvoice_idr   audior   r   r   returnc          
      c   s   | j jjdtj| j   ddt| ttd|i|dur&|	di ni |du s5|	ddu r>tt||dni tt||dtt|	di t
td	|itti | j  |durl|	d
i ni |dur|	ddur|	dndd|dur|	dndd	B}d|j  krdk rn n| D ]}|V  q	 W d   dS |  z| }	W n ty   t|j|jdw t|j|	d1 sw   Y  dS )an  
        Create speech by combining the content and emotion of the uploaded audio with a voice of your choice.

        Parameters:
            - voice_id: str. Voice ID to be used, you can use https://api.elevenlabs.io/v1/voices to list all the available voices.

            - optimize_streaming_latency: typing.Optional[int]. You can turn on latency optimizations at some cost of quality. The best possible final latency varies by model. Possible values:
                                                                0 - default mode (no latency optimizations)
                                                                1 - normal latency optimizations (about 50% of possible latency improvement of option 3)
                                                                2 - strong latency optimizations (about 75% of possible latency improvement of option 3)
                                                                3 - max latency optimizations
                                                                4 - max latency optimizations, but also with text normalizer turned off for even more latency savings (best latency, but can mispronounce eg numbers and dates).

                                                                Defaults to 0.
            - audio: core.File. See core.File for more documentation

            - 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 speech to speech, you can check this using the can_do_voice_conversion property.

            - voice_settings: typing.Optional[str]. Voice settings overriding stored setttings for the given voice. They are applied only on the given request. Needs to be send as a JSON encoded string.

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

        client = ElevenLabs(
            api_key="YOUR_API_KEY",
        )
        client.speech_to_speech.convert(
            voice_id="string",
            optimize_streaming_latency=1,
        )
        POST/v1/speech-to-speech/r   Nadditional_query_parametersadditional_body_parametersr   r   r   additional_headerstimeout_in_seconds<   r   max_retriesparamsdatafilesheaderstimeoutretriesr)      ,  status_codebodyr   httpx_clientstreamurllibparseurljoinget_base_urlr   r	   getr   !convert_file_dict_to_httpx_tuplesget_headersr4   
iter_bytesreadjsonr   r   text
r   r   r   r   r   r   r   	_response_chunk_response_jsonr   r   r   convert   sj   *	
%*zSpeechToSpeechClient.convertc          
      c   s   | j jjdtj| j   ddt| dttd|i|dur'|	di ni |du s6|	ddu r?tt||d	ni tt||d	tt|	di t
td
|itti | j  |durm|	di ni |dur|	ddur|	dndd|dur|	dndd	B}d|j  krdk rn n| D ]}|V  q	 W d   dS |  z| }	W n ty   t|j|jdw t|j|	d1 sw   Y  dS )a  
        Create speech by combining the content and emotion of the uploaded audio with a voice of your choice and returns an audio stream.

        Parameters:
            - voice_id: str. Voice ID to be used, you can use https://api.elevenlabs.io/v1/voices to list all the available voices.

            - optimize_streaming_latency: typing.Optional[int]. You can turn on latency optimizations at some cost of quality. The best possible final latency varies by model. Possible values:
                                                                0 - default mode (no latency optimizations)
                                                                1 - normal latency optimizations (about 50% of possible latency improvement of option 3)
                                                                2 - strong latency optimizations (about 75% of possible latency improvement of option 3)
                                                                3 - max latency optimizations
                                                                4 - max latency optimizations, but also with text normalizer turned off for even more latency savings (best latency, but can mispronounce eg numbers and dates).

                                                                Defaults to 0.
            - audio: core.File. See core.File for more documentation

            - 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 speech to speech, you can check this using the can_do_voice_conversion property.

            - voice_settings: typing.Optional[str]. Voice settings overriding stored setttings for the given voice. They are applied only on the given request. Needs to be send as a JSON encoded string.

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

        client = ElevenLabs(
            api_key="YOUR_API_KEY",
        )
        client.speech_to_speech.convert_as_stream(
            voice_id="string",
            optimize_streaming_latency=1,
        )
        r    r!   r"   /streamr   Nr#   r$   r%   r   r&   r'   r(   r   r)   r*   r1   r2   r3   r6   rD   r   r   r   convert_as_streamq   sj   *	
%*z&SpeechToSpeechClient.convert_as_stream)__name__
__module____qualname__r   r   strtypingOptionalintr   Filer
   IteratorbytesrH   rJ   r   r   r   r   r      P    	
_	r   c                   @   r   )AsyncSpeechToSpeechClientr   c                C   r   r   r   r   r   r   r   r      r   z"AsyncSpeechToSpeechClient.__init__Nr   r   r   r   r   r   r   r   c          
      C  s  | j jjdtj| j   ddt| ttd|i|dur&|	di ni |du s5|	ddu r>tt||dni tt||dtt|	di t
td	|itti | j  |durl|	d
i ni |dur|	ddur|	dndd|dur|	dndd	4 I dH M}d|j  krdk rn n| 2 z	3 dH W }|V  q6 	 W d  I dH  dS | I dH  z| }	W n ty   t|j|jdw t|j|	d1 I dH sw   Y  dS )a~  
        Create speech by combining the content and emotion of the uploaded audio with a voice of your choice.

        Parameters:
            - voice_id: str. Voice ID to be used, you can use https://api.elevenlabs.io/v1/voices to list all the available voices.

            - optimize_streaming_latency: typing.Optional[int]. You can turn on latency optimizations at some cost of quality. The best possible final latency varies by model. Possible values:
                                                                0 - default mode (no latency optimizations)
                                                                1 - normal latency optimizations (about 50% of possible latency improvement of option 3)
                                                                2 - strong latency optimizations (about 75% of possible latency improvement of option 3)
                                                                3 - max latency optimizations
                                                                4 - max latency optimizations, but also with text normalizer turned off for even more latency savings (best latency, but can mispronounce eg numbers and dates).

                                                                Defaults to 0.
            - audio: core.File. See core.File for more documentation

            - 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 speech to speech, you can check this using the can_do_voice_conversion property.

            - voice_settings: typing.Optional[str]. Voice settings overriding stored setttings for the given voice. They are applied only on the given request. Needs to be send as a JSON encoded string.

            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
        ---
        from elevenlabs.client import AsyncElevenLabs

        client = AsyncElevenLabs(
            api_key="YOUR_API_KEY",
        )
        await client.speech_to_speech.convert(
            voice_id="string",
            optimize_streaming_latency=1,
        )
        r    r!   r"   r   Nr#   r$   r%   r   r&   r'   r(   r   r)   r*   r1   r2   r3   r   r7   r8   r9   r:   r;   r<   r   r	   r=   r   r>   r?   r4   aiter_bytesareadrB   r   r   rC   rD   r   r   r   rH      sl   *	
%*z!AsyncSpeechToSpeechClient.convertc          
      C  s  | j jjdtj| j   ddt| dttd|i|dur'|	di ni |du s6|	ddu r?tt||d	ni tt||d	tt|	di t
td
|itti | j  |durm|	di ni |dur|	ddur|	dndd|dur|	dndd	4 I dH M}d|j  krdk rn n| 2 z	3 dH W }|V  q6 	 W d  I dH  dS | I dH  z| }	W n ty   t|j|jdw t|j|	d1 I dH sw   Y  dS )a  
        Create speech by combining the content and emotion of the uploaded audio with a voice of your choice and returns an audio stream.

        Parameters:
            - voice_id: str. Voice ID to be used, you can use https://api.elevenlabs.io/v1/voices to list all the available voices.

            - optimize_streaming_latency: typing.Optional[int]. You can turn on latency optimizations at some cost of quality. The best possible final latency varies by model. Possible values:
                                                                0 - default mode (no latency optimizations)
                                                                1 - normal latency optimizations (about 50% of possible latency improvement of option 3)
                                                                2 - strong latency optimizations (about 75% of possible latency improvement of option 3)
                                                                3 - max latency optimizations
                                                                4 - max latency optimizations, but also with text normalizer turned off for even more latency savings (best latency, but can mispronounce eg numbers and dates).

                                                                Defaults to 0.
            - audio: core.File. See core.File for more documentation

            - 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 speech to speech, you can check this using the can_do_voice_conversion property.

            - voice_settings: typing.Optional[str]. Voice settings overriding stored setttings for the given voice. They are applied only on the given request. Needs to be send as a JSON encoded string.

            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
        ---
        from elevenlabs.client import AsyncElevenLabs

        client = AsyncElevenLabs(
            api_key="YOUR_API_KEY",
        )
        await client.speech_to_speech.convert_as_stream(
            voice_id="string",
            optimize_streaming_latency=1,
        )
        r    r!   r"   rI   r   Nr#   r$   r%   r   r&   r'   r(   r   r)   r*   r1   r2   r3   rW   rD   r   r   r   rJ   ,  sl   *	
%*z+AsyncSpeechToSpeechClient.convert_as_stream)rK   rL   rM   r   r   rN   rO   rP   rQ   r   rR   r
   AsyncIteratorrT   rH   rJ   r   r   r   r   rV      rU   rV   )rO   urllib.parser9   json.decoderr    r   core.api_errorr   core.client_wrapperr   r   core.jsonable_encoderr   core.remove_none_from_dictr	   core.request_optionsr
   castAnyOMITr   rV   r   r   r   r   <module>   s    <