o
    *j                     @   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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) MplugOwlForConditionalGeneration)
OutputKeysTokenGeneratorOutput)ModelPipeline)	PIPELINES)MplugOwlPreprocessorPreprocessor)Tasks)
get_logger)module_namec                       s   e Zd ZdZ	ddeeef dee f fddZ	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 )MultimodalDialoguePipelineas   Multimodal Dialogue Pipeline.

    Examples:
    >>> from modelscope.pipelines import pipeline
    >>> chatbot = pipeline('multimodal-dialogue', 'damo/multi-modal_mplug_owl_multimodal-dialogue_7b')
    >>> image = 'data/resource/portrait_input.png'
    >>> system_prompt_1 = 'The following is a conversation between a curious human and AI assistant.'
    >>> system_prompt_2 = "The assistant gives helpful, detailed, and polite answers to the user's questions."
    >>> messages = {
    >>>       'messages': [
    >>>            {
    >>>                'role': 'system',
    >>>                'content': system_prompt_1 + ' ' + system_prompt_2
    >>>            },
    >>>            {
    >>>                'role': 'user',
    >>>                'content': [{
    >>>                    'image': image
    >>>                }]
    >>>            },
    >>>            {
    >>>                'role': 'user',
    >>>                'content': 'Describe the facial expression of the man.'
    >>>            },
    >>>        ]
    >>>    }
    >>> chatbot(messages)
    >>> {
    >>>     "text": he is angry.
    >>> }
    >>>
    N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 multimodal dialogue pipeline for prediction
        Args:
            model: model id on modelscope hub.
        )r   r   N )	super__init__r   eval
isinstancer   r   Z	model_dirr   )selfr   r   kwargs	__class__r   ~/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/multi_modal/multimodal_dialogue_pipeline.pyr   6   s   	
z#MultimodalDialoguePipeline.__init__c                 K   s
   |i i fS Nr   )r   Zpipeline_parametersr   r   r   _sanitize_parametersE   s   
z/MultimodalDialoguePipeline._sanitize_parametersinputsreturnc                    s8   t   t |W  d    S 1 sw   Y  d S r   )torchZno_gradr   forward)r   r!   r   r   r   r$   H   s   

$z"MultimodalDialoguePipeline.forwardc                 C   s0   t | jtr| jjj|d dd}tj|i}|S )zprocess the prediction results

        Args:
            inputs (Dict[str, Any]): _description_

        Returns:
            Dict[str, str]: the prediction results
        r   T)Zskip_special_tokens)r   r   r   r   	tokenizerdecoder   ZTEXT)r   r!   outputr   r   r   postprocessL   s   	
z&MultimodalDialoguePipeline.postprocessr   )__name__
__module____qualname____doc__r   r
   strr   r   r   r    r   r   r$   r(   __classcell__r   r   r   r   r      s    #
&*r   )typingr   r   r   r   r#   Zmodelscope.metainfor   Zmodelscope.models.multi_modalr   Zmodelscope.outputsr   r	   Zmodelscope.pipelines.baser
   r   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessorsr   r   Zmodelscope.utils.constantr   Zmodelscope.utils.loggerr   loggerZregister_moduleZmultimodal_dialoguer   r   r   r   r   <module>   s   