o
    ॵi                     @   s   d dl mZmZmZmZ d dlZd dlZd dlm	Z	 d dl
mZmZmZ d dlmZmZ d dlmZ d dlmZ d dlmZmZmZmZmZ d d	lmZmZ d d
lmZ e Z ej!ej"e	j"dG dd deZ#dS )    )AnyDictOptionalUnionN)	Pipelines)CLIP_InterrogatorMPlugForAllTasksOfaForAllTasks)ModelPipeline)	PIPELINES)batch_process)+ImageCaptioningClipInterrogatorPreprocessorMPlugPreprocessorOfaPreprocessorPreprocessor
load_image)	ModelFileTasks)
get_logger)module_namec                       s   e Zd Z	ddeeef dee f fddZ fddZ	de
eef d	e
eef f fd
dZde
eef d	e
eef fddZ  ZS )ImageCaptioningPipelineNmodelpreprocessorc                    s   t  jd||d| | j  t| jtsJ dtj |du rMt| jtr2t	| jj
| _dS t| jtrAt| jj
| _dS t| jtrOt | _dS dS dS )a  
        use `model` and `preprocessor` to create a image captioning pipeline for prediction
        Args:
            model: model id on modelscope hub.
        Examples:
        from modelscope.pipelines import pipeline
        from modelscope.utils.constant import Tasks

        model_id = 'damo/cv_clip-interrogator'
        input_image = "test.png"

        pipeline_ci = pipeline(Tasks.image_captioning, model=model_id)
        print(pipeline_ci(input_image))


        )r   r   z,please check whether model config exists in N )super__init__r   eval
isinstancer
   r   CONFIGURATIONr	   r   	model_dirr   r   r   r   r   )selfr   r   kwargs	__class__r   n/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/multi_modal/image_captioning_pipeline.pyr      s   

z ImageCaptioningPipeline.__init__c                    s   t | jtrt| j|S t | jtrPddlm} t|d d d}t	dd |D |d< i }|d d 
 D ] t	 fd	d|D | < q7|||d< |S tt| |S )
Nr   )BatchEncodingtrain)r'   c                 S   s   g | ]}|d  qS )imager   .0dr   r   r%   
<listcomp>B   s    z2ImageCaptioningPipeline._batch.<locals>.<listcomp>r(   questionc                    s   g | ]}|d    qS )r-   r   r)   kr   r%   r,   E   s    )r   r   r	   r   r   $transformers.tokenization_utils_baser&   dicttorchcatkeysr   r   _batch)r!   datar&   
batch_datar-   r#   r.   r%   r5   <   s   zImageCaptioningPipeline._batchinputsreturnc                    s@   t   t j|fi |W  d    S 1 sw   Y  d S N)r2   no_gradr   forward)r!   r8   forward_paramsr#   r   r%   r<   K   s   
$zImageCaptioningPipeline.forwardc                 C   s   |S r:   r   )r!   r8   r   r   r%   postprocessP   s   z#ImageCaptioningPipeline.postprocessr:   )__name__
__module____qualname__r   r
   strr   r   r   r5   r   r   r<   r>   __classcell__r   r   r#   r%   r      s    
"
*r   )$typingr   r   r   r   numpynpr2   modelscope.metainfor   modelscope.models.multi_modalr   r   r	   modelscope.pipelines.baser
   r   modelscope.pipelines.builderr   modelscope.pipelines.utilr   modelscope.preprocessorsr   r   r   r   r   modelscope.utils.constantr   r   modelscope.utils.loggerr   loggerregister_moduleimage_captioningr   r   r   r   r%   <module>   s    