o
    ix                     @   s  d Z ddlZddlZddlZddlm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 ejZzddlmZ ddlmZ W n eyn   ddlmZ ddlmZ Y nw edZG dd dZG dd dejZdS )z%[Experimental] Live Music API client.    N)AsyncIterator   )_api_module)_common)_live_converters)_transformers)errors)types)BaseApiClient)set_value_by_path)ClientConnection)connectzgoogle_genai.live_musicc                   @   s   e Zd ZdZdedefddZdeej	 ddfd	d
Z
dejddfddZdejddfddZdddZdddZdddZdddZdeej fddZdejfddZdddZdS ) AsyncMusicSessionz![Experimental] AsyncMusicSession.
api_client	websocketc                 C   s   || _ || _d S )N)_api_client_ws)selfr   r    r   K/home/ubuntu/.local/lib/python3.10/site-packages/google/genai/live_music.py__init__0   s   
zAsyncMusicSession.__init__promptsreturnNc                    sD   | j jr	tdddd |D i}| jtd|iI d H  d S )N4Live music generation is not supported in Vertex AI.weightedPromptsc                 S   s   g | ]	}t j|d dqS )Tconvert_keys)r   convert_to_dict).0promptr   r   r   
<listcomp>=   s    z:AsyncMusicSession.set_weighted_prompts.<locals>.<listcomp>clientContent)r   vertexaiNotImplementedErrorr   sendjsondumps)r   r   client_content_dictr   r   r   set_weighted_prompts4   s    z&AsyncMusicSession.set_weighted_promptsconfigc                    s@   | j jr	tdtj|dd}| jtd|iI d H  d S )Nr   Tr   musicGenerationConfig)	r   r"   r#   r   r   r   r$   r%   r&   )r   r)   config_dictr   r   r   set_music_generation_configE   s    z-AsyncMusicSession.set_music_generation_configplayback_controlc                    s8   | j jr	tdd|ji}| jt|I d H  d S )Nr   playbackControl)r   r"   r#   valuer   r$   r%   r&   )r   r-   playback_control_dictr   r   r   _send_control_signalP   s   
z&AsyncMusicSession._send_control_signalc                       |  tjjI dH S )z0Sends playback signal to start the music stream.N)r1   r	   LiveMusicPlaybackControlPLAYr   r   r   r   play[      zAsyncMusicSession.playc                    r2   )z2Sends a playback signal to pause the music stream.N)r1   r	   r3   PAUSEr5   r   r   r   pause_   r7   zAsyncMusicSession.pausec                    r2   )zSends a playback signal to stop the music stream.

    Resets the music generation context while retaining the current config.
    N)r1   r	   r3   STOPr5   r   r   r   stopc   s   zAsyncMusicSession.stopc                    r2   )zKReset the context (prompts retained) without stopping the music generation.N)r1   r	   r3   RESET_CONTEXTr5   r   r   r   reset_contextj   s   
zAsyncMusicSession.reset_contextc                 C  s4   |   I dH  }r|V  |   I dH  }s
dS dS )zbReceive model responses from the server.

    Yields:
      The audio chunks from the server.
    N)_receive)r   resultr   r   r   receivep   s   zAsyncMusicSession.receivec              
      s"  t  }z| jjddI d H }W n@ ty"   | j I d H }Y n0 tyQ } z$|jr5|jj}|jj}n
d}t	j
j|d}tj||d  W Y d }~nd }~ww |rlzt|}W n tjjyk   td|w i }| jjrvtd|}|s|rtj|d|d  t jj|| dS )	NFdecodei  zAbnormal closure.zFailed to parse response: r   code)responsekwargs)r	   LiveMusicServerMessager   recv	TypeErrorConnectionClosedrcvdrC   reason
websocketsframesCLOSE_CODE_EXPLANATIONSgetr   APIErrorraise_errorr%   loadsdecoderJSONDecodeError
ValueErrorr   r"   r#   _from_response
model_dump)r   parameter_modelraw_responseerC   rK   rD   response_dictr   r   r   r>   z   s<   
zAsyncMusicSession._receivec                    s   | j  I dH  dS )z<Closes the bi-directional stream and terminates the session.N)r   closer5   r   r   r   r\      r7   zAsyncMusicSession.close)r   N)__name__
__module____qualname____doc__r
   r   r   listr	   WeightedPromptr(   LiveMusicGenerationConfigr,   r3   r1   r6   r9   r;   r=   r   rF   r@   r>   r\   r   r   r   r   r   -   s2    







!r   c                   @   s6   e Zd ZdZedejdede	e
 fddZdS )AsyncLiveMusicz_[Experimental] Live music module.

  Live music can be accessed via `client.aio.live.music`.
  zLRealtime music generation is experimental and may change in future versions.modelr   c                C  s  | j  }t|tr|d}t| j |}| j jrR| j j}| j jj	}| d| d| }| j jj
}ttjtj|djddd}t|dd	g| t|}	ntd
zAt||d4 I dH )}
|
|	I dH  t|
jddI dH  t| j |
dV  W d  I dH  W dS 1 I dH sw   Y  W dS  ty   t||d4 I dH '}
|
|	I dH  t|
 I dH  t| j |
dV  W d  I dH  Y dS 1 I dH sw   Y  Y dS w )z0[Experimental] Connect to the live music server.zutf-8z!/ws/google.ai.generativelanguage.z).GenerativeService.BidiGenerateMusic?key=)re   T)exclude_none)from_objectsetupre   r   )additional_headersNFrA   )r   r   )extra_headers)r   _websocket_base_url
isinstancebytesrB   tt_modelapi_key_http_optionsapi_versionheadersr   r   live_converters$_LiveMusicConnectParameters_to_mldevr	   LiveMusicConnectParametersrW   setvr%   r&   r#   r   r$   loggerinforG   r   rH   )r   re   base_urltransformed_modelrp   versionurirs   request_dictrequestwsr   r   r   r      sF   




22zAsyncLiveMusic.connectN)r]   r^   r_   r`   r   experimental_warning
contextlibasynccontextmanagerstrr   r   r   r   r   r   r   rd      s    rd   )r`   r   r%   loggingtypingr   rL    r   r   r   rt   r   rn   r   r	   r   r
   r   rw   rI   websockets.asyncio.clientr   r   ModuleNotFoundErrorwebsockets.client	getLoggerrx   r   
BaseModulerd   r   r   r   r   <module>   s2   
s