
    *`i                        d dl Z d dl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 dedej        fdZd	ej        ez  ez  dej        fd
Zdej        dedefdZe
ej         ee           ee          df         ZdS )    N)Image)BeforeValidatorPlainSerializerSerializationInfo)	Annotated)__version__urlreturnc                 l   ddt            i}	 t          j        | |          }|                                 t	          j        t          j        |j                            }|S # t          j	        j
        $ r}t          d|  d| d          d}~wt          $ r}t          d|           d}~ww xY w)	zDownload an image from a URL and return it as a PIL Image.

    Args:
        url: The URL of the image to download.

    Returns:
       The downloaded image as a PIL Image object.
    z
User-Agentzmistral-common/)headersz!Error downloading the image from z: .NzError converting to PIL image: )r   requestsgetraise_for_statusr   openioBytesIOcontent
exceptionsRequestExceptionRuntimeError	Exception)r	   r   responseimges        h/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/mistral_common/image.pydownload_imager      s     <{<<=GB<W555!!### jH$45566
/ L L LJsJJaJJJKKK B B B@Q@@AAABs$   AA% %B39BB3B..B3xc           	         t          | t          j                  r| S t          | t                    rD	 t          j        t	          j        |                     S # t          $ r t          d          w xY w	 t          j        t	          j        t          j	        | 
                    d                                        }|S # t          $ r!}t          d| dd          d          |d}~ww xY w)aU  Load an image from a string or bytes.

    If the input is already a PIL Image, return it as is.

    Args:
        x: The input to load the image from. Can be a PIL Image, a string, or bytes.
            If it's a string, it's assumed to be a base64 encoded string of bytes.

    Returns:
       The loaded image as a PIL Image object.
    z3Encountered an error when loading image from bytes.asciizBEncountered an error when loading image from bytes starting with 'N   zI'. Expected either a PIL.Image.Image or a base64 encoded string of bytes.)
isinstancer   bytesr   r   r   r   r   base64	b64decodeencode)r   imager   s      r   "maybe_load_image_from_str_or_bytesr(   %   s    !U[!! !U V	V:bjmm,,, 	V 	V 	VTUUU	V
2:f&6qxx7H7H&I&IJJKK   (ssV( ( (
 
 		s$   %A A37AC 
C/C**C/iminfoc                    t          |d          r
|j        pi }ni }t          j                    }| j        pd}|                     ||           t          j        |                                          	                    d          }|r$|
                    d          x}r|d|         dz   S |r0|
                    d          rd	|                                 d
|z   }|S )zSerialize an image to a base64 encoded string of bytes.

    Args:
        im: The image to serialize.
        info: The serialization info.

    Returns:
        The serialized image as a base64 encoded string of bytes.
    contextPNG)formatr    max_image_b64_lenNz...add_format_prefixzdata:image/z;base64,)hasattrr,   r   r   r.   saver$   	b64encodegetvaluedecoder   lower)r)   r*   r,   stream	im_formatim_b64r/   s          r   serialize_image_to_byte_strr:   D   s     tY ,$"Z\\F	"UIGGF9G%%%foo//0077@@F 25H)I)II% 2((()E11 D7;;233 D:y00:::VCM    zCA normal PIL image that supports serialization to b64 bytes string.)r$   r   r   PILr   pydanticr   r   r   typing_extensionsr   mistral_commonr   strr   r#   r(   r:   SerializableImage r;   r   <module>rC      s7    				        H H H H H H H H H H ' ' ' ' ' ' & & & & & &B B B B B B2%+*;e*C     >EK 7H S    4 	KO677O/00IK   r;   