o
    Yix                     @   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   Q/home/kim/smarthome/.venv/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.ZweightedPromptsc                 S   s   g | ]	}t j|d dqS )TZconvert_keys)r   convert_to_dict).0promptr   r   r   
<listcomp>=   s    z:AsyncMusicSession.set_weighted_prompts.<locals>.<listcomp>ZclientContent)r   vertexaiNotImplementedErrorr   sendjsondumps)r   r   Z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   ZmusicGenerationConfig)	r   r   r    r   r   r   r!   r"   r#   )r   r%   Z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   ZplaybackControl)r   r   r    valuer   r!   r"   r#   )r   r'   Z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)r)   r	   LiveMusicPlaybackControlZPLAYr   r   r   r   play[      zAsyncMusicSession.playc                    r*   )z2Sends a playback signal to pause the music stream.N)r)   r	   r+   ZPAUSEr,   r   r   r   pause_   r.   zAsyncMusicSession.pausec                    r*   )zSends a playback signal to stop the music stream.

    Resets the music generation context while retaining the current config.
    N)r)   r	   r+   STOPr,   r   r   r   stopc   s   zAsyncMusicSession.stopc                    r*   )zKReset the context (prompts retained) without stopping the music generation.N)r)   r	   r+   ZRESET_CONTEXTr,   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ConnectionClosedZrcvdr8   reason
websocketsframesZCLOSE_CODE_EXPLANATIONSgetr   ZAPIErrorZraise_errorr"   loadsdecoderJSONDecodeError
ValueErrorr   r   r    Z_from_response
model_dump)r   Zparameter_modelZraw_responseer8   r?   r9   Zresponse_dictr   r   r   r3   z   s<   
zAsyncMusicSession._receivec                    s   | j  I dH  dS )z<Closes the bi-directional stream and terminates the session.N)r   closer,   r   r   r   rI      r.   zAsyncMusicSession.close)r   N)__name__
__module____qualname____doc__r
   r   r   listr	   ZWeightedPromptr$   ZLiveMusicGenerationConfigr&   r+   r)   r-   r/   r1   r2   r   r;   r5   r3   rI   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=)rP   T)Zexclude_none)Zfrom_objectsetuprP   r   )Zadditional_headersNFr6   )r   r   )extra_headers)r   Z_websocket_base_url
isinstancebytesr7   tZt_modelapi_keyZ_http_optionsapi_versionheadersr   r   live_convertersZ$_LiveMusicConnectParameters_to_mldevr	   ZLiveMusicConnectParametersrG   setvr"   r#   r    r   r!   loggerinfor<   r   r=   )r   rP   base_urlZtransformed_modelrV   versionurirX   Zrequest_dictrequestwsr   r   r   r      sF   




22zAsyncLiveMusic.connectN)rJ   rK   rL   rM   r   Zexperimental_warning
contextlibasynccontextmanagerstrr   r   r   r   r   r   r   rO      s    rO   )rM   rb   r"   loggingtypingr   r@    r   r   r   rY   r   rU   r   r	   r   r
   r   rZ   r>   Zwebsockets.asyncio.clientr   r   ModuleNotFoundErrorZwebsockets.client	getLoggerr[   r   Z
BaseModulerO   r   r   r   r   <module>   s2   
s