o
    miPu                     @   s  d dl Z d dlmZmZmZ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 d/g d#d$d%gddgd	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g dej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 d9g d:g d;d<d=d>g d?d d@gejdgd<d=dAg dBg dCg d;dDd=dEg dBg dCg d;dDd=dFg dBg dCg d;dDd=dGg d#d$d%gddgd	dHg dIdJdKgddgd	dLd)dMgd$gdgd<d=i dNg dg dejddgd	dOg dPg dQejejddgd	dRg dg dejddgd	dSdTd*gd gejgd	dUg d#d$d%gddgd	dVg dWg dejddgd	dXg dg dejddgd	dYg dZ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	d`g dIdJdKgddgd	dag d9g d:d<dbdcg d9g dddedfg dgd$dhgddgd	dig djg dkddejddgd	dlg dmg dnddejejddgd	g d#d$d%gddgd	g dog dpddejgd	g dqg drg dsd	g dtg duddddejgd	dvZG dwdx dxZdS )y    N)AnyDictListSequence)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-SecondrA   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ShapEPipelinerA   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   rD   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)rg   rh   rF   r   ri   rj   r   )r   rk   r    r$   r!   rl   )r   r   rF   r   )r   r!   r    )r   r   rh   rj   r   )r   r!   rk   rl   )NNNN)r   r   rh   rj   rF   r   )r   r!   rk   rl   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)rs   rt   SUPPORTED_MULTIMODAL_PIPELINESwandbErrorTablewandb_table)selfrs   rt   rv    r}   n/home/ubuntu/SoloSpeech/.venv/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   zW|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	yo } 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configrs   rx   updatert   r   prepare_loggable_dict	Exceptionloggerwarning)r|   r   r   r   r   r   r   pipeline_configsexisting_workflowupdated_workflowloggable_dicter}   r}   r~   __call__f  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.
        rD   rC   )rQ   rJ   N)rw   rs   imagesframesaudios)r|   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.
        rD    )rp   rq   r   r   rk   z
Prompt-1: z
Prompt-2: NzGenerated-Image/Pipeline-Call-)captionrC   Generated-Video/Pipeline-Call-   rG   r   rJ   zGenerated-Audio/Pipeline-Call->  )sample_rater   )rw   rs   index
ValueErrorrx   logrt   ImageVideor   Audio)r|   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   rD   rC   r   rG   rJ   r   )r   )rw   rs   	enumerateappendrx   r   r   r   r   r{   add_data)r|   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.
        rp   output_typelatent)ra   rb   rb   )	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   rs   r	   r   r   rx   r   rt   r   rw   rangelenr   r{   r   
isinstancelistr   r   r   )r|   r   r   r   r   rC   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~   rr   Q  sH    



@J
'

rr   )loggingtypingr   r   r   r   rx   (wandb.sdk.integration_utils.auto_loggingr   utilsr   r	   r
   r   r   	getLoggerr   r   r   rw   rr   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