o
    ZhR                     @  s   d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
 d dlmZ d dlZd dlZd dlmZ G dd deZG d	d
 d
Z	ddddZdS )    )annotationsN)Enum)AnyDict)urlparse)Partc                   @  s   e Zd ZdZdZdZdZdS )RoutezImage Loading Route         N)__name__
__module____qualname____doc__BASE64
LOCAL_FILEURL r   r   Z/var/www/html/lang_env/lib/python3.10/site-packages/langchain_google_genai/_image_utils.pyr      s
    r   c                   @  sL   e Zd ZdZdddZdd	d
ZdddZdddZdddZdddZ	dS )ImageBytesLoaderzLoads image bytes from multiple sources given a string.

    Currently supported:
        - B64 Encoded image string
        - URL
    image_stringstrreturnbytesc                 C  sT   |  |}|tjkr| |S |tjkr| |S |tjkr"tdtd| d)zRoutes to the correct loader based on the image_string.

        Args:
            image_string: Can be either:
                    - B64 Encoded image string
                    - URL

        Returns:
            Image bytes.
        zLoading from local files is no longer supported for security reasons. Please pass in images as Google Cloud Storage URI, b64 encoded image string (data:image/...), or valid image url.zImage string must be one of: Google Cloud Storage URI, b64 encoded image string (data:image/...), or valid image url.Instead got ''.)	_router   r   _bytes_from_b64r   _bytes_from_urlr   
ValueErrorZ_bytes_from_file)selfr   router   r   r   
load_bytes    s   





zImageBytesLoader.load_bytesr   c           	      C  s   |  |}|tjkr| |}|tjkr| |}|tjkr$d}t|d|i}t	|\}}|s;t
|}|r;|j}|rA||d< t|dS )zGets Part for loading from Gemini.

        Args:
            image_string: Can be either:
                    - B64 Encoded image string
                    - URL
        zLoading from local files is no longer supported for security reasons. Please specify images as Google Cloud Storage URI, b64 encoded image string (data:image/...), or valid image url.data	mime_type)inline_data)r   r   r   r   r   r   r   r   	mimetypes
guess_typefiletypeguessmimer   )	r   r   r    bytes_msgr$   r#   _kindr   r   r   	load_partB   s$   







zImageBytesLoader.load_partr   c                 C  sB   | drtjS | |rtjS tj|rtjS t	d| d)Ndata:image/zhImage string must be one of: b64 encoded image string (data:image/...) or valid image url. Instead got 'r   )

startswithr   r   _is_urlr   ospathexistsr   r   )r   r   r   r   r   r   g   s   

zImageBytesLoader._routebase64_imagec                 C  s:   d}t ||}|dur|d}t|S td| )zGets image bytes from a base64 encoded string.

        Args:
            base64_image: Encoded image in b64 format.

        Returns:
            Image bytes
        zdata:image/\w{2,4};base64,(.*)Nr	   z1Error in b64 encoded image. Must follow pattern: )researchgroupbase64	b64decoder   )r   r5   patternmatchencoded_stringr   r   r   r   w   s   


z ImageBytesLoader._bytes_from_b64urlc                 C  s   t |}|js|  |jS )zGets image bytes from a public url.

        Args:
            url: Valid url.

        Raises:
            HTTP Error if there is one.

        Returns:
            Image bytes
        )requestsgetokraise_for_statuscontent)r   r>   responser   r   r   r      s   
z ImageBytesLoader._bytes_from_url
url_stringboolc                 C  s0   zt |}t|j|jgW S  ty   Y dS w )zChecks if a url is valid.

        Args:
            url_string: Url to check.

        Returns:
            Whether the url is valid.
        F)r   allschemenetloc	Exception)r   rE   resultr   r   r   r1      s   	zImageBytesLoader._is_urlN)r   r   r   r   )r   r   r   r   )r   r   r   r   )r5   r   r   r   )r>   r   r   r   )rE   r   r   rF   )
r   r   r   r   r!   r.   r   r   r   r1   r   r   r   r   r      s    

"
%

r   asciipngimage_bytesr   encodingr   image_formatr   c                 C  s    t | |}d| d| S )a  Encodes image bytes into a b64 encoded string.

    Args:
        image_bytes: Bytes of the image.
        encoding: Type of encoding in the string. 'ascii' by default.
        image_format: Format of the image. 'png' by default.

    Returns:
        B64 image encoded string.
    r/   z;base64,)r9   	b64encodedecode)rN   rO   rP   Zencoded_bytesr   r   r   image_bytes_to_b64_string   s   rS   )rL   rM   )rN   r   rO   r   rP   r   r   r   )
__future__r   r9   r%   r2   r6   enumr   typingr   r   urllib.parser   r'   r?   Z)google.ai.generativelanguage_v1beta.typesr   r   r   rS   r   r   r   r   <module>   s      