o
    ưi8                     @   s~   d dl mZmZ d dlmZmZmZmZmZm	Z	 d dl
Z
d dlmZ d dlmZ d dlmZmZ d dlmZ G dd	 d	ZdS )
    )BufferedReaderBytesIO)AnyDictListOptionalcastget_type_hintsN)get_image_type)BaseImageEditConfig)FILE_MIME_TYPESFileType)ImageEditOptionalRequestParamsc                   @   sx   e Zd Ze		ddedededee dee	e  de
fdd	Zed
e
eef defddZededefddZdS )ImageEditRequestUtilsNmodelimage_edit_provider_configimage_edit_optional_paramsdrop_paramsadditional_drop_paramsreturnc           
         s   | |  tjdu p|du }t|}|r|D ]}||d q fdd|D }|rH|r8|D ]}||d q.ntj| d|  dd| d|jtt	|| |d	}	|	S )
a"  
        Get optional parameters for the image edit API.

        Args:
            model: The model name
            image_edit_provider_config: The provider configuration for image edit API
            image_edit_optional_params: The optional parameters for the image edit API
            drop_params: If True, silently drop unsupported parameters instead of raising
            additional_drop_params: List of additional parameter names to drop

        Returns:
            A dictionary of supported parameters for the image edit API
        TNc                    s   g | ]}| vr|qS  r   ).0paramZsupported_paramsr   K/home/app/Keep/.python/lib/python3.10/site-packages/litellm/images/utils.py
<listcomp>*   s
    zHImageEditRequestUtils.get_optional_params_image_edit.<locals>.<listcomp>z5The following parameters are not supported for model z: z, )r   message)r   r   r   )
Zget_supported_openai_paramslitellmr   dictpopZUnsupportedParamsErrorjoinZmap_openai_paramsr   r   )
r   r   r   r   r   Zshould_dropZfiltered_optional_paramsr   Zunsupported_paramsZmapped_paramsr   r   r   get_optional_params_image_edit   s4   

z4ImageEditRequestUtils.get_optional_params_image_editparamsc                    s,   t t   fdd|  D }tt|S )a  
        Filter parameters to only include those defined in ImageEditOptionalRequestParams.

        Args:
            params: Dictionary of parameters to filter

        Returns:
            ImageEditOptionalRequestParams instance with only the valid parameters
        c                    s&   i | ]\}}| v r|d ur||qS )Nr   )r   kvZ
valid_keysr   r   
<dictcomp>R   s     zQImageEditRequestUtils.get_requested_image_edit_optional_param.<locals>.<dictcomp>)r	   r   keysitemsr   )r"   Zfiltered_paramsr   r%   r   'get_requested_image_edit_optional_paramD   s
   

z=ImageEditRequestUtils.get_requested_image_edit_optional_param
image_datac                 C   sT  zt | tr|  }| d | d}| | nSt | tr3|  }| d | d}| | n:t | tr?| dd }n.t| drgt| ddd  }t| drW| d | d}t| drf| | nt	t
j W S t|}|du r{t	t
j W S t
jt
jt
jt
jt
jd	}||}|du rt	t
j W S t	| W S  ty   t	t
j  Y S w )
a  
        Detect the content type of image data using existing LiteLLM utils.

        Args:
            image_data: Can be BytesIO, bytes, BufferedReader, or other file-like objects

        Returns:
            The MIME type string (e.g., "image/png", "image/jpeg")
        r   d   Nreadtellc                   S   s   dS )Nr   r   r   r   r   r   <lambda>y   s    z>ImageEditRequestUtils.get_image_content_type.<locals>.<lambda>seek)ZpngZjpegZgifZwebpZheic)
isinstancer   r-   r/   r,   r   byteshasattrgetattrr   r   ZPNGr
   ZJPEGZGIFZWEBPZHEICget	Exception)r*   Zcurrent_pos
bytes_dataZimage_type_strZtype_mapping	file_typer   r   r   get_image_content_typeW   sP   













z,ImageEditRequestUtils.get_image_content_type)NN)__name__
__module____qualname__staticmethodstrr   r   r   boolr   r   r!   r   r)   r8   r   r   r   r   r      s2    
7
r   )ior   r   typingr   r   r   r   r   r	   r   Z(litellm.litellm_core_utils.token_counterr
   Z/litellm.llms.base_llm.image_edit.transformationr   Zlitellm.types.filesr   r   Zlitellm.types.images.mainr   r   r   r   r   r   <module>   s     