o
    xi5u                     @   s  d dl Z d dlmZ d dlmZ d dlZd dlmZ ddlm	Z	m
Z
mZmZmZ e eZi dg dg d	ejddddgd
dg dg dejejddddgd
dg dg dejddgd
dg dg dejejddgd
dg dg dejejejddgd
dg dg dejddgd
dg dg dejddgd
dg dg dejejejgd
dg d d!d"gejejgd
d#g d$d%d&gddgd
d'g d$d%d&gddgd
d(g d$d%d&gddgd
d)d*d+gd%gdgd
d,d*d+gd%gdgd
d-g d$d%d&gddgd
d.g d$d%d&gddgd
d/g d$d%d&gddgd
i d0g d$d%d&gddgd
d1g d$d%d&gddgd
d2g d$d%d&gddgd
d3g d$d%d&gddgd
d4g d$d%d&gddgd
d5g dg dejddgd
d6g dg dejddgd
d7g dg dejddgd
d8g dg dejddgd
d9g d:g d;g d<d=d>d?g d@d!dAgejdgd=d>dBg dCg dDg d<dEd>dFg dCg dDg d<dEd>dGg dCg dDg d<dEd>dHg d$d%d&gddgd
dIg dJdKdLgddgd
dMd*dNgd%gdgd=d>i dOg dg dejddgd
dPg dQg dRejejddgd
dSg dg dejddgd
dTdUd+gd!gejgd
dVg d$d%d&gddgd
dWg dXg dejddgd
dYg dg dejddgd
dZg d[g d\dddejdgd
d]g d$d%d&gddgd
d^g d_d!d&gejdgd
d`g dg dejddgd
dag dJdKdLgddgd
dbg d:g d;d=dcddg d:g dedfdgg dhd%digddgd
djg dkg dlddejddgd
dmg dng doddejejddgd
g d$d%d&gddgd
g dpg dqddejgd
g drg dsg dtd
g dug dvddddejgd
dwZG dxdy dyZdS )z    N)Sequence)Any)Response   )chunkifydecode_sdxl_t2i_latentsget_updated_kwargspostprocess_np_arrays_for_videopostprocess_pils_to_npBlipDiffusionPipeline)Reference-ImagePromptNegative-PromptSource-Subject-CategoryTarget-Subject-CategoryGenerated-Image)reference_imageprompt
neg_promptsource_subject_categorytarget_subject_category)table-schemakwarg-loggingkwarg-actionsBlipDiffusionControlNetPipeline)r   Control-Imager   r   r   r   r   )r   condtioning_imager   r   r   r   !StableDiffusionControlNetPipeline)r   r   r   r   )imager   negative_prompt(StableDiffusionControlNetImg2ImgPipeline)Source-Imager   r   r   r   )r   control_imager   r   (StableDiffusionControlNetInpaintPipeline)r!   
Mask-Imager   r   r   r   )r   
mask_imager"   r   r   CycleDiffusionPipeline)r!   r   zSource-Promptr   )r   r   source_prompt&StableDiffusionInstructPix2PixPipeline)r!   r   r   r   PaintByExamplePipeline)r!   zExample-ImageMask-Promptr   )r   example_imager%   RePaintPipeline)r!   r*   r   r   r%   StableDiffusionPipeline)r   r   r   r   r   KandinskyCombinedPipelineKandinskyV22CombinedPipelineLatentConsistencyModelPipeliner   r   LDMTextToImagePipelineStableDiffusionPanoramaPipelinePixArtAlphaPipelineStableDiffusionSAGPipelineSemanticStableDiffusionPipelineWuerstchenCombinedPipeline
IFPipelineAltDiffusionPipeline&StableDiffusionAttendAndExcitePipeline KandinskyImg2ImgCombinedPipeline KandinskyInpaintCombinedPipeline#KandinskyV22Img2ImgCombinedPipeline#KandinskyV22InpaintCombinedPipelineAnimateDiffPipeline)r   r   zNumber-of-FramesGenerated-Video)r   r   
num_frames)NNNvideo)r   r   r   output-typeStableVideoDiffusionPipeline)Input-ImagezFrames-Per-Secondr?   fpsAudioLDMPipeline)r   r   zAudio-Length-in-SecondszGenerated-Audio)r   r   audio_length_in_saudioAudioLDM2PipelineMusicLDMPipeline"StableDiffusionPix2PixZeroPipelinePNDMPipeline)z
Batch-SizezNumber-of-Inference-Stepsr   
batch_sizenum_inference_stepsShapEPipeliner?   StableDiffusionImg2ImgPipelineStableDiffusionInpaintPipeline)r!   r$   r   r   r   )r   r%   r   r    StableDiffusionDepth2ImgPipeline%StableDiffusionImageVariationPipeliner!   StableDiffusionPipelineSafeStableDiffusionUpscalePipeline)r!   r   r   zUpscaled-ImageStableDiffusionAdapterPipelineStableDiffusionGLIGENPipeline)r   zGLIGEN-PhraseszGLIGEN-BoxeszGLIGEN-Inpaint-Imager   r   )r   gligen_phrasesgligen_boxesgligen_inpaint_imager   %VersatileDiffusionTextToImagePipeline(VersatileDiffusionImageVariationPipeline)r!   r   r   $VersatileDiffusionDualGuidedPipelineLDMPipelineTextToVideoSDPipeline)r   r   rB   TextToVideoZeroPipeline)r   r   video_length)r   r   AmusedPipeline)r   zGuidance Scaler   guidance_scale#StableDiffusionXLControlNetPipeline)Prompt-1Prompt-2r   Negative-Prompt-1Negative-Prompt-2r   )r   prompt_2r   r   negative_prompt_2*StableDiffusionXLControlNetImg2ImgPipeline)re   rf   rD   r   rg   rh   r   )r   ri   r   r"   r   rj   )r   r   rD   r   )r   r   r   )r   r   rf   rh   r   )r   r   ri   rj   )NNNN)r   r   rf   rh   rD   r   )r   r   ri   rj   r   )Kandinsky3PipelineKandinsky3Img2ImgPipelineStableDiffusionXLPipeline StableDiffusionXLImg2ImgPipelinec                   @   s   e Zd ZdZdededdfddZdee d	e	eef d
e
dededefddZd
e
defddZdedededdfddZdedededdfddZded
e
d	e	eef de	eef fddZdS )#DiffusersMultiModalPipelineResolvera
  Resolver for  request and responses from [HuggingFace Diffusers](https://huggingface.co/docs/diffusers/index) multi-modal Diffusion Pipelines, providing necessary data transformations, formatting, and logging.

    This resolver is internally involved in the
    `__call__` for `wandb.integration.diffusers.pipeline_resolver.DiffusersPipelineResolver`.
    This is based on `wandb.sdk.integration_utils.auto_logging.RequestResponseResolver`.

    Args:
        pipeline_name: (str) The name of the Diffusion Pipeline.
    pipeline_namepipeline_call_countreturnNc                 C   sF   || _ || _g }|tv r|t| d 7 }ntd tj|d| _d S )Nr   z"Pipeline not supported for logging)columns)rq   rr   SUPPORTED_MULTIMODAL_PIPELINESwandbErrorTablewandb_table)selfrq   rr   rt    r{   d/home/ubuntu/.local/lib/python3.10/site-packages/wandb/integration/diffusers/resolvers/multimodal.py__init__]  s   
z,DiffusersMultiModalPipelineResolver.__init__argskwargsresponse
start_timetime_elapsedc              
   C   s   zU|d |dd }}t |||}t|j}| j|d< dtjvr3tjd||d| j dgi ntjj}|||d| j dg }	tjjd|	idd	 | |||W S  t	ym }
 zt
|
 W Y d}
~
dS d}
~
ww )
a1  Main call method for the `DiffusersPipelineResolver` class.

        Args:
            args: (Sequence[Any]) List of arguments.
            kwargs: (Dict[str, Any]) Dictionary of keyword arguments.
            response: (wandb.sdk.integration_utils.auto_logging.Response) The response from
                the request.
            start_time: (float) Time when request started.
            time_elapsed: (float) Time elapsed for the request.

        Returns:
            Packed data as a dictionary for logging to wandb, None if an exception occurred.
        r   r   Nzpipeline-nameworkflowzPipeline-Call-)pipelineparamsstageT)allow_val_change)r   dictconfigrq   rv   updaterr   r   prepare_loggable_dict	Exceptionloggerwarning)rz   r~   r   r   r   r   r   pipeline_configsexisting_workflowupdated_workflower{   r{   r|   __call__g  s>   




z,DiffusersMultiModalPipelineResolver.__call__c                 C   sX   dt | j vr
|jS t | j d dkr| jdv r|jS |jS t | j d dkr*|jS dS )a3  Unpack the generated images, audio, video, etc. from the Diffusion Pipeline's response.

        Args:
            response: (wandb.sdk.integration_utils.auto_logging.Response) The response from
                the request.

        Returns:
            List of generated images, audio, video, etc.
        rB   rA   )rO   rH   N)ru   rq   imagesframesaudios)rz   r   r{   r{   r|   get_output_images  s   

z5DiffusersMultiModalPipelineResolver.get_output_imagesr   loggable_kwarg_chunksidxc                 C   s  dt | j vrcz>d}| jdv r4t | j d d}t | j d d}d|| |  d|| |  }nt | j d d}|| | }W n tyP   d	}Y nw td
| j tj||di d	S t | j d dkrzt | j d d}|| | }W n ty   d	}Y nw td| j tjt	|d|di d	S t | j d dkrzt | j d d}|| | }W n ty   d	}Y nw td| j tj
|d|di d	S d	S )af  Log the generated images, audio, video, etc. from the Diffusion Pipeline's response along with an optional caption to a media panel in the run.

        Args:
            image: (Any) The generated images, audio, video, etc. from the Diffusion
                Pipeline's response.
            loggable_kwarg_chunks: (List) Loggable chunks of kwargs.
        rB    )rn   ro   r   r   ri   z
Prompt-1: z
Prompt-2: NzGenerated-Image/Pipeline-Call-)captionrA   Generated-Video/Pipeline-Call-   rE   r   rH   zGenerated-Audio/Pipeline-Call->  )sample_rater   )ru   rq   index
ValueErrorrv   logrr   ImageVideor
   Audio)rz   r   r   r   r   prompt_indexprompt2_indexr{   r{   r|   	log_media  s   
"	
z-DiffusersMultiModalPipelineResolver.log_mediac                 C   s   g }t | j d }t|D ]%\}}|| du r'||| dur#|| nd q||| ||  qdt | j vrC|t| n)t | j d dkrY|tjt|dd nt | j d dkrl|tj|d	d
 | j	j
|  dS )a!  Populate the row of the `wandb.Table`.

        Args:
            image: (Any) The generated images, audio, video, etc. from the Diffusion
                Pipeline's response.
            loggable_kwarg_chunks: (List) Loggable chunks of kwargs.
            idx: (int) Chunk index.
        r   Nr   rB   rA   r   rE   rH   r   )r   )ru   rq   	enumerateappendrv   r   r   r
   r   ry   add_data)rz   r   r   r   	table_rowkwarg_actions
column_idxloggable_kwarg_chunkr{   r{   r|   add_data_to_table
  s.   z5DiffusersMultiModalPipelineResolver.add_data_to_tabler   c                    s`  |  |}| jdkr d dkrt||j}| jdv r]t|| jdkd}td| j tj|d d	 d
i t	| j d  fddt
tD }|tj|dd | jj|  nJt	| j d g }D ]}|t | trw | n | g qht|t|d }t
t|d D ]}	||	 D ]}
| |
||	 | |
||	 qqd| j | jiS )a  Prepare the loggable dictionary, which is the packed data as a dictionary for logging to wandb, None if an exception occurred.

        Args:
            pipeline: (Any) The Diffusion Pipeline.
            response: (wandb.sdk.integration_utils.auto_logging.Response) The response from
                the request.
            kwargs: (Dict[str, Any]) Dictionary of keyword arguments.

        Returns:
            Packed data as a dictionary for logging to wandb, None if an exception occurred.
        rn   output_typelatent)r_   r`   r`   )	normalizer   r   r   r   r   c                    s   g | ]} |  qS r{   r{   ).0r   r   loggable_kwarg_idsr{   r|   
<listcomp>V  s    
zMDiffusersMultiModalPipelineResolver.prepare_loggable_dict.<locals>.<listcomp>r   r   zResult-Table/Pipeline-Call-)r   rq   r   r   r	   rv   r   rr   r   ru   rangelenr   ry   r   
isinstancelistr   r   r   )rz   r   r   r   r   rA   r   r   loggable_kwarg_idr   r   r{   r   r|   r   1  sP   






z9DiffusersMultiModalPipelineResolver.prepare_loggable_dict)__name__
__module____qualname____doc__strintr}   r   r   r   r   floatr   r   r   r   r   r   r{   r{   r{   r|   rp   R  sH    



?J
'

rp   )loggingcollections.abcr   typingr   rv   (wandb.sdk.integration_utils.auto_loggingr   utilsr   r   r   r	   r
   	getLoggerr   r   r   ru   rp   r{   r{   r{   r|   <module>   s   
	'
1<N
\
jx
   
          #  (  -  2  7  <  A  F
  P
  Z
  d
  n  y               %    .    7    @
    J    U
    _    i    r
    |
                 "      +
      5      >      H      Q      ]      o	       
        C