o
    *j                     @   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   ZCONFIGURATIONr	   r   Z	model_dirr   r   r   r   r   )selfr   r   kwargs	__class__r   {/var/www/html/Deteccion_Ine/venv/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   Z$transformers.tokenization_utils_baser$   dicttorchcatkeysr   r   _batch)r   datar$   Z
batch_datar+   r!   r,   r#   r2   <   s   zImageCaptioningPipeline._batchinputsreturnc                    s@   t   t j|fi |W  d    S 1 sw   Y  d S N)r/   Zno_gradr   forward)r   r4   Zforward_paramsr!   r   r#   r7   K   s   
$zImageCaptioningPipeline.forwardc                 C   s   |S r6   r   )r   r4   r   r   r#   postprocessP   s   z#ImageCaptioningPipeline.postprocessr6   )__name__
__module____qualname__r   r
   strr   r   r   r2   r   r   r7   r8   __classcell__r   r   r!   r#   r      s    
"
*r   )$typingr   r   r   r   numpynpr/   Zmodelscope.metainfor   Zmodelscope.models.multi_modalr   r   r	   Zmodelscope.pipelines.baser
   r   Zmodelscope.pipelines.builderr   Zmodelscope.pipelines.utilr   Zmodelscope.preprocessorsr   r   r   r   r   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   loggerZregister_moduleZimage_captioningr   r   r   r   r#   <module>   s    