
    :i              	       B   S SK r S SKrS SKJr  S SKJr  S SKJr  S SKr	S SK
Jr  S SKJrJr  S SKJrJr  S SKJrJr  \R*                  " \5      r\" 5       (       a  S SKr\ " S	 S
5      5       r\ " S S5      5       rS\\-  S\4S jrSrSr " S S\\5      r\ " S S5      5       r S\R                  S\R                  4S jr!S\	RD                  S\#\$\$\$4   S\#\$\$\$4   S\	RD                  4S jr%S\R                  S\#\&\&4   S\	RD                  4S jr' " S S5      r(g)     N)	dataclass)Enum)BytesIO)Image)SerializableImagedownload_image)assert_opencv_installedis_opencv_installed)
ImageChunkImageURLChunkc                   H    \ rS rSr% Sr\\   \S'   \R                  \S'   Sr
g)ImageEncoding   zA tokenized image.

Attributes:
    tokens: The token ids.
    image: The image as a numpy array.

Examples:
    >>> import numpy as np
    >>> image_encoding = ImageEncoding(tokens=[1, 2, 3], image=np.array([[0., 0.5, 1.]]))
tokensimage N)__name__
__module____qualname____firstlineno____doc__listint__annotations__npndarray__static_attributes__r       `/home/james-whalen/.local/lib/python3.13/site-packages/mistral_common/tokens/tokenizers/image.pyr   r      s    	 I::r   r   c                   8    \ rS rSr% Sr\\S'   \\S'   \\S'   Srg)SpecialImageIDs&   zSpecial image tokens ids.

Attributes:
    img: The image token id.
    img_break: The image break token id.
    img_end: The image end token id.

Examples:
    >>> special_image_ids = SpecialImageIDs(img=1, img_break=2, img_end=3)
img	img_breakimg_endr   N)r   r   r   r   r   r   r   r   r   r   r   r!   r!   &   s    	 
HNLr   r!   chunkreturnc                    [        U [        5      (       a  U R                  $ U R                  5       R	                  S5      (       aW  U R                  5       R                  S5      S   n[        R                  " U5      n[        R                  " [        U5      5      $ U R                  5       R	                  S5      (       a>  [        R                  " [        U R                  5       R                  SS5      S5      5      $ U R                  5       R	                  S5      (       a  [        U R                  5       5      $ [        S	U R                  5        35      e)
zGet a serializable image from a chunk.

Args:
    chunk: The chunk to get the image from.

Returns:
    The image as a PIL Image object.
z
data:image,   filezfile:// rbhttpzUnsupported image url scheme )
isinstancer   r   get_url
startswithsplitbase64	b64decoder   openr   replacer   RuntimeError)r&   data
image_datas      r   image_from_chunkr:   8   s     %$${{}}!!,//}}$$S)!,%%d+
zz'*-..}}!!&))zz$u}}66y"EtLMM}}!!&))emmo..
6u}}6GH
IIr   )g3<4'?gwgM?gy{ ?)gB91?gwt.?g	U?c                   0    \ rS rSrSrSr\SS j5       rSrg)MultiModalVersionT   zVersion of the image tokenizer.m1c                 f    U R                   S:X  a  [        SS5      $ [        U R                    5      e)Nr>      i   )nameImageConfigNotImplementedErrorselfs    r   configMultiModalVersion.configY   s,    99r4((!TYYK11r   r   N)r'   rB   )	r   r   r   r   r   r>   propertyrF   r   r   r   r   r<   r<   T   s    *	B2 2r   r<   c                   <    \ rS rSr% Sr\\S'   \\S'   Sr\\S'   Srg)	rB   a   z'Configuration for the image tokenizers.image_patch_sizemax_image_sizer*   spatial_merge_sizer   N)	r   r   r   r   r   r   r   rM   r   r   r   r   rB   rB   a   s    2r   rB   r   c                     U R                   S:X  a  U $ U R                   S:w  a  U R                  S5      n [        R                  " SU R                  S5      nUR                  U SU 5        UR                  S5      $ )zMConvert a PIL image to RGB.

We ensure transparent background becomes white.
RGBRGBAWHITE)r   r   )modeconvertr   newsizepaste)r   white_bgs     r   _convert_to_rgbrX   j   sf    
 zzUzzVf%!IIfejj'BHNN5&%(E""r   np_imagemeanstdc                 2   U S-  n [        U R                  5      S:X  d   SU R                  < 35       eU R                  S   [        U5      s=:X  a  [        U5      :X  d  O   SU R                  < SU< SU< 35       eX-
  U-  n U R                  SSS5      $ )	zNormalize a tensor image with mean and standard deviation.

Args:
    np_image: Image to be normalized.
    mean: Mean for each channel.
    std: Standard deviation for each channel.

Returns:
    Normalized image with shape (C, H, W).
g     o@   znp_image.shape=   z, mean=z, std=r   r*   )lenshape	transpose)rY   rZ   r[   s      r   	normalizerb   x   s     %Hx~~!#9'8%99#>>!D	5SX5\/(..9J(TGSZVYU[7\\53&HaA&&r   new_sizec                     [        5         [        R                  " [        R                  " [        U 5      [        R                  S9U[        R                  S9n[        U[        [        5      $ )zTransform an image to a numpy array with the given size.

Args:
    image: Image to be transformed.
    new_size: New size of the image.

Returns:
    Transformed image with shape (C, H, W).
)dtype)interpolation)r	   cv2resizer   arrayrX   float32INTER_CUBICrb   DATASET_MEANDATASET_STD)r   rc   rY   s      r   transform_imagern      sF     zz"((?5#9LhfifufuvHX|[99r   c                       \ rS rSrSrS\S\SS4S jr\S\4S j5       r	S	\
R                  S\\\4   4S
 jrS\\-  S\4S jr\S\4S j5       rSrg)ImageEncoder   z&Image encoder for the image tokenizer.image_configspecial_idsr'   Nc                     Xl         X l        g)zInitialize the image encoder.

Args:
    image_config: Configuration for the image tokenizer.
    special_ids: Special image tokens ids.
Nrr   rs   )rE   rr   rs   s      r   __init__ImageEncoder.__init__   s     )&r   c                     U R                   $ N)rr   rD   s    r   	mm_configImageEncoder.mm_config   s    
    r   r#   c                    UR                   u  p#[        X0R                  R                  -  X R                  R                  -  5      nUS:  a  [	        X$-  5      n[	        X4-  5      nUS-
  U R                  R
                  U R                  R                  -  -  S-   nUS-
  U R                  R
                  U R                  R                  -  -  S-   nXV4$ )Nr*   )rU   maxrr   rL   roundrK   rM   )rE   r#   whratiowidth_tokensheight_tokenss          r   _image_to_num_tokens!ImageEncoder._image_to_num_tokens   s     xxA))888!>O>O>^>^:^_19ai Aai AA4#4#4#E#EHYHYHlHl#lmpqqQD$5$5$F$FIZIZImIm$mnqrr**r   contentc                    [        U5      nU R                  U5      u  p4US:  d   eUS:  d   eU R                  R                  /U-  U R                  R                  /-   U-  nU R                  R
                  US'   X0R                  R                  -  U R                  R                  -  X@R                  R                  -  U R                  R                  -  4n[        X&5      n[        XWS9$ )z}Converts an image chunk to an image encoding.

Args:
    content: image chunk to be converted.

Returns:
    Image encoding.
r   )r   r   )r:   r   rs   r#   r$   r%   rr   rK   rM   rn   r   )rE   r   r   r   r   image_tokensnew_image_sizeprocessed_images           r   __call__ImageEncoder.__call__   s     !)((/1uu1uu))--.2d6F6F6P6P5QQUVV++33R!!222T5F5F5Y5YY!!222T5F5F5Y5YY
 *%@LHHr   c                 .    U R                   R                  $ ry   )rs   r#   rD   s    r   image_tokenImageEncoder.image_token   s    ###r   ru   )r   r   r   r   r   rB   r!   rv   rH   rz   r   tupler   r   r   r   r   r   r   r   r   r   r   rp   rp      s    1'[ ' 'SW ' !; ! !+ +c3h +I
] : I} I, $S $ $r   rp   ))r3   loggingdataclassesr   enumr   ior   numpyr   PILr   mistral_common.imager   r   mistral_common.importsr	   r
   &mistral_common.protocol.instruct.chunkr   r   	getLoggerr   loggerrg   r   r!   r:   rl   rm   strr<   rB   rX   r   r   floatrb   r   rn   rp   r   r   r   <module>r      sZ     !     B O L			8	$        "JMJ6 J;L J. 32
2T 
2      #5;; #5;; #'jj'
ue#
$' 
ueU"	#' ZZ	'2:5;; :%S/ :bjj : ;$ ;$r   