o
    ॵiA                     @   s   d dl Z d dlZd dlZd dlZd dlmZmZmZmZ d dl	Z	d dl
Zd dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ ejejejdG dd deZ dS )    N)AnyDictOptionalUnion)StableDiffusionInpaintPipeline)	Pipelines)
OutputKeys)	PIPELINES)DiffusersPipeline)
load_image)Config)Tasks)module_namec                       sx   e Zd ZdZddedef fddZdd Zd	eeef d
eeef fddZ	d	eeef d
eeef fddZ
  ZS )ImageInpaintingSDV2Pipelinea&   Stable Diffusion for Image Inpainting Pipeline.

    Example:

    >>> import cv2
    >>> from modelscope.outputs import OutputKeys
    >>> from modelscope.pipelines import pipeline
    >>> from modelscope.utils.constant import Tasks

    >>> input_location = 'data/test/images/image_inpainting/image_inpainting.png'
    >>> input_mask_location = 'data/test/images/image_inpainting/image_inpainting_mask.png'
    >>> prompt = 'background'

    >>> input = {
    >>>     'image': input_location,
    >>>     'mask': input_mask_location,
    >>>     'prompt': prompt
    >>> }
    >>> image_inpainting = pipeline(Tasks.image_inpainting, model='damo/cv_stable-diffusion-v2_image-inpainting_base')
    >>> output = image_inpainting(input)[OutputKeys.OUTPUT_IMG]
    >>> cv2.imwrite('result.png', output)

    gpumodeldevicec                    sd   t  j||fi | |dtj}tj||d| _| j| j	 |dd}|r0| j
  dS dS )z
        Use `model` to create a stable diffusion pipeline for image inpainting.
        Args:
            model: model id on modelscope hub.
            device: str = 'gpu'
        torch_dtype)r   enable_attention_slicingTN)super__init__gettorchfloat16r   from_pretrainedpipelinetor   r   )selfr   r   kwargsr   r   	__class__ j/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/image_inpainting_sdv2_pipeline.pyr   3   s   z$ImageInpaintingSDV2Pipeline.__init__c                 K   s
   i ||fS )a|  
        this method should sanitize the keyword args to preprocessor params,
        forward params and postprocess params on '__call__' or '_process_single' method

        Returns:
            Dict[str, str]:  preprocess_params = {}
            Dict[str, str]:  forward_params = pipeline_parameters
            Dict[str, str]:  postprocess_params = pipeline_parameters
        r!   )r   pipeline_parametersr!   r!   r"   _sanitize_parametersG   s   

z0ImageInpaintingSDV2Pipeline._sanitize_parametersinputsreturnc                 K   s  t |tstdtt |dd}|dd}|dd }|dd}|d	d
}d| v r7|d }n|dd}td|  t|d }	t|d }
|	j	\}}td| d| d t
dd ||f\}}|	||f}	|
||f}
| j||	|
|||||||d
jd }d|iS )Nz/Expected the input to be a dictionary, but got num_inference_steps2   guidance_scaleg      @negative_promptnum_images_per_prompt   etag        prompt
backgroundzTest with prompt: imagemaskzloaded input image of size (z, )c                 S   s   | | d  S )N@   r!   )xr!   r!   r"   <lambda>l   s    z5ImageInpaintingSDV2Pipeline.forward.<locals>.<lambda>)
r.   r0   
mask_imageheightwidthr'   r)   r*   r+   r-   r   result)
isinstancedict
ValueErrortypeinputr   keysprintr   sizemapresizer   images)r   r%   forward_paramsr'   r)   r*   r+   r-   r.   r0   r1   whr8   r7   	out_imager!   r!   r"   forwardS   sJ   



z#ImageInpaintingSDV2Pipeline.forwardc                 K   s0   t |d }tj|d d d d d d df iS )Nr9   )nparrayr   
OUTPUT_IMG)r   r%   r   r9   r!   r!   r"   postprocess~   s   "z'ImageInpaintingSDV2Pipeline.postprocess)r   )__name__
__module____qualname____doc__strr   r$   r   r   rI   rN   __classcell__r!   r!   r   r"   r      s    

*+r   )!mathossystempfiletypingr   r   r   r   cv2numpyrK   r   	diffusersr   modelscope.metainfor   modelscope.outputsr   modelscope.pipelines.builderr	   Emodelscope.pipelines.multi_modal.diffusers_wrapped.diffusers_pipeliner
   modelscope.preprocessors.imager   modelscope.utils.configr   modelscope.utils.constantr   register_moduleimage_inpaintingimage_inpainting_sdv2r   r!   r!   r!   r"   <module>   s(   