o
    i                     @   s   d 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 zddlmZ ddlmZ W n eya   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)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   ]/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/google/genai/live_music.py__init__-   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_prompts1   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_configB   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_signalM   s   
z&AsyncMusicSession._send_control_signalc                       |  tjjI dH S )z0Sends playback signal to start the music stream.N)r0   r   LiveMusicPlaybackControlPLAYr   r   r   r   playX      zAsyncMusicSession.playc                    r1   )z2Sends a playback signal to pause the music stream.N)r0   r   r2   PAUSEr4   r   r   r   pause\   r6   zAsyncMusicSession.pausec                    r1   )zSends a playback signal to stop the music stream.

    Resets the music generation context while retaining the current config.
    N)r0   r   r2   STOPr4   r   r   r   stop`   s   zAsyncMusicSession.stopc                    r1   )zKReset the context (prompts retained) without stopping the music generation.N)r0   r   r2   RESET_CONTEXTr4   r   r   r   reset_contextg   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   receivem   s   zAsyncMusicSession.receivec                    s   t  }z| jjddI d H }W n ty"   | j I d H }Y nw |r=zt|}W n tjjy<   t	d|w i }| j
jrGtd|}t jj|| dS )NFdecodezFailed to parse response: r   )responsekwargs)r   LiveMusicServerMessager   recv	TypeErrorr$   loadsdecoderJSONDecodeError
ValueErrorr   r!   r"   _from_response
model_dump)r   parameter_modelraw_responserB   response_dictr   r   r   r=   w   s(   zAsyncMusicSession._receivec                    s   | j  I dH  dS )z<Closes the bi-directional stream and terminates the session.N)r   closer4   r   r   r   rP      r6   zAsyncMusicSession.close)r   N)__name__
__module____qualname____doc__r	   r   r   listr   WeightedPromptr'   LiveMusicGenerationConfigr+   r2   r0   r5   r8   r:   r<   r   rD   r?   r=   rP   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=)rY   T)exclude_none)from_objectsetuprY   r   )additional_headersNFr@   )r   r   )extra_headers)r   _websocket_base_url
isinstancebytesrA   tt_modelapi_key_http_optionsapi_versionheadersr   r   live_converters$_LiveMusicConnectParameters_to_mldevr   LiveMusicConnectParametersrL   setvr$   r%   r"   r   r#   loggerinforE   r   rF   )r   rY   base_urltransformed_modelrd   versionurirg   request_dictrequestwsr   r   r   r      sF   




22zAsyncLiveMusic.connectN)rQ   rR   rS   rT   r   experimental_warning
contextlibasynccontextmanagerstrr   r   r   r   r   r   r   rX      s    rX   )rT   rv   r$   loggingtypingr    r   r   r   rh   r   rb   r   r   r	   r
   rk   websockets.asyncio.clientr   r   ModuleNotFoundErrorwebsockets.client	getLoggerrl   r   
BaseModulerX   r   r   r   r   <module>   s,   
i