
    qiw                         d Z ddlZddlZddlmZmZ ddlmZmZ ddl	Z	ddl
mZ ddlmZmZmZ ddlmZ ddlmZmZmZmZ e G d	 d
e             Z G d de      Zy)zAzure OpenAI image generation service implementation.

This module provides integration with Azure's OpenAI image generation API
using REST endpoints for creating images from text prompts.
    N)	dataclassfield)AsyncGeneratorOptional)Image)
ErrorFrameFrameURLImageRawFrame)ImageGenService)	NOT_GIVENImageGenSettings	_NotGiven_warn_deprecated_paramc                   <    e Zd ZU dZ ed       Zedz  ez  ed<   y)AzureImageGenSettingszSettings for the Azure image generation service.

    Parameters:
        model: Azure image generation model identifier.
        image_size: Target size for generated images.
    c                      t         S )N)r        N/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/services/azure/image.py<lambda>zAzureImageGenSettings.<lambda>#   s    y r   )default_factoryN
image_size)	__name__
__module____qualname____doc__r   r   strr   __annotations__r   r   r   r   r      s"     */?P)QJd
Y&Qr   r   c                        e Zd ZU dZeZeed<   ddddddee   deded	ee   d
e	j                  dee   f fdZdedeedf   fdZ xZS )AzureImageGenServiceRESTzAzure OpenAI REST-based image generation service.

    Provides image generation using Azure's OpenAI service via REST API.
    Supports asynchronous image generation with polling for completion
    and automatic image download and processing.
    	_settingsNz2023-06-01-preview)r   modelapi_versionsettingsr   api_keyendpointr"   aiohttp_sessionr$   c                   t        dd      }|t        dt         d       ||_        |t        dt         d       ||_        ||j	                  |       t
        	|   |       || _        || _        || _	        || _
        y)ac  Initialize the AzureImageGenServiceREST.

        Args:
            image_size: Size specification for generated images (e.g., "1024x1024").

                .. deprecated:: 0.0.105
                    Use ``settings=AzureImageGenSettings(image_size=...)`` instead.

            api_key: Azure OpenAI API key for authentication.
            endpoint: Azure OpenAI endpoint URL.
            model: The image generation model to use.

                .. deprecated:: 0.0.105
                    Use ``settings=AzureImageGenSettings(model=...)`` instead.

            aiohttp_session: Shared aiohttp session for HTTP requests.
            api_version: Azure API version string. Defaults to "2023-06-01-preview".
            settings: Runtime-updatable settings. When provided alongside deprecated
                parameters, ``settings`` values take precedence.
        N)r"   r   r"   r   )r$   )r   r   r"   r   apply_updatesuper__init___api_key_azure_endpoint_api_version_aiohttp_session)
selfr   r%   r&   r"   r'   r#   r$   default_settings	__class__s
            r   r+   z!AzureImageGenServiceREST.__init__1   s    @ 1
 "7,A7K%*"!"<1FU*4' ))(3"23'' /r   promptreturnc                  K   | j                    d| j                   }| j                  dd}|dd}| j                  j                  | j                  j                  |d<   | j
                  j                  |||      4 d{   }|j                  d	   }d
}d}d}	|dk7  r|dz  }|dk(  r t        d       	 ddd      d{    yt        j                  d       d{    | j
                  j                  ||       d{   }
|
j                          d{   }	|	d   }|dk7  r|	r|	d   d   d   d   nd}|s t        d       	 ddd      d{    y| j
                  j                  |      4 d{   }
t        j                  |
j                  j!                          d{         }t#        j$                  |      }t'        ||j)                         |j*                  |j,                        }| ddd      d{    ddd      d{    y7 7 g7 L7 *7 7 7 7 7 *# 1 d{  7  sw Y   :xY w7 1# 1 d{  7  sw Y   yxY ww)a  Generate an image from a text prompt using Azure OpenAI.

        Args:
            prompt: The text prompt describing the desired image.

        Yields:
            URLImageRawFrame containing the generated image data, or
            ErrorFrame if generation fails.
        z-openai/images/generations:submit?api-version=zapplication/json)zapi-keyzContent-Type   )r3   nNsize)headersjsonzoperation-location x   	succeededr   zImage generation timed out)r9   statusresultdataurlzImage generation failed)rA   imager8   format)r-   r.   r,   r!   r   r/   postr9   r   asynciosleepgetr:   ioBytesIOcontentreadr   openr
   tobytesr8   rC   )r0   r3   rA   r9   body
submissionoperation_locationr>   attempts_leftjson_responseresponse	image_urlimage_streamrB   frames                  r   run_image_genz&AzureImageGenServiceREST.run_image_genj   sk     %%&&STXTeTeSfg"mm=OP 

 >>$$0>>44DL((--c7-N !	 !	R\ ",!3!34H!IFM MK'" A%$%ABB!	 !	 !	 mmA&&&!%!6!6!:!:;MW^!:!__&.mmo 5&x0 K' FSh/7:5AX\I !:;;1!	 !	 !	6 ,,00;  x!zz0@0@0E0E0G*GH

<0(!uzzRWR^R^  7!	 !	 !	 !	 '_ 5%!	6*G   7!	 !	 !	 !	s  BI-H*I-4I;I-H-I-I$H0%$I	H3
I!H6"I1#II-H9 I-%IH;I-I5H=6AI	IH?II-$I%I--I-0I3I6I9I-;I=I?II	I
I	II-I*I!I*&I-)r   r   r   r   r   Settingsr   r   r   aiohttpClientSessionr+   r   r	   rW   __classcell__)r2   s   @r   r    r    &   s     %H$$
 %)  $(4870 SM70 	70
 70 }70 !..70 0170r7# 7.2M 7r   r    )r   rE   rH   dataclassesr   r   typingr   r   rY   PILr   pipecat.frames.framesr   r	   r
   pipecat.services.image_servicer   pipecat.services.settingsr   r   r   r   r   r    r   r   r   <module>rb      sZ     	 ( +   E E : d d R, R R{ {r   