o
    ॵif                     @   s   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	 d dl
mZ d dlmZmZ d dlmZ d dlmZmZ d d	l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)HiTeAForAllTasks)
OutputKeys)ModelPipeline)	PIPELINES)HiTeAPreprocessorPreprocessor)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 )VideoCaptioningPipelineNmodelpreprocessorc                    sP   t  jd||d| | j  |du r$t| jtr&t| jj| _dS dS dS )z
        use `model` and `preprocessor` to create a video captioning pipeline for prediction
        Args:
            model: model id on modelscope hub.
        )r   r   N )	super__init__r   eval
isinstancer   r   	model_dirr   )selfr   r   kwargs	__class__r   n/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/multi_modal/video_captioning_pipeline.pyr      s   	
z VideoCaptioningPipeline.__init__c                    s   t | jtrDddlm} t|d d d}tdd |D |d< i }|d d  D ] t fd	d|D | < q+|||d< |S t	 
|S )
Nr   )BatchEncodingtrain)r    c                 S   s   g | ]}|d  qS )videor   .0dr   r   r   
<listcomp>)   s    z2VideoCaptioningPipeline._batch.<locals>.<listcomp>r!   questionc                    s   g | ]}|d    qS )r&   r   r"   kr   r   r%   ,   s    )r   r   r   $transformers.tokenization_utils_baser   dicttorchcatkeysr   _collate_batch)r   datar   
batch_datar&   r   r'   r   _batch%   s   zVideoCaptioningPipeline._batchinputsreturnc                    s@   t   t j|fi |W  d    S 1 sw   Y  d S N)r+   no_gradr   forward)r   r2   forward_paramsr   r   r   r6   2   s   
$zVideoCaptioningPipeline.forwardc                 C   s   |S r4   r   )r   r2   r   r   r   postprocess7   s   z#VideoCaptioningPipeline.postprocessr4   )__name__
__module____qualname__r   r	   strr   r   r   r1   r   r   r6   r8   __classcell__r   r   r   r   r      s    

*r   )typingr   r   r   r   r+   modelscope.metainfor   modelscope.models.multi_modalr   modelscope.outputsr   modelscope.pipelines.baser	   r
   modelscope.pipelines.builderr   modelscope.preprocessorsr   r   modelscope.utils.constantr   modelscope.utils.loggerr   loggerregister_modulevideo_captioningr   r   r   r   r   <module>   s   