o
    ॵi                     @   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   	model_dirr   )selfr   r   kwargs	__class__r   q/home/ubuntu/.local/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   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    )torch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)skip_special_tokens)r   r   r   r   	tokenizerdecoder   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%   modelscope.metainfor   modelscope.models.multi_modalr   modelscope.outputsr   r	   modelscope.pipelines.baser
   r   modelscope.pipelines.builderr   modelscope.preprocessorsr   r   modelscope.utils.constantr   modelscope.utils.loggerr   loggerregister_modulemultimodal_dialoguer   r   r   r   r   <module>   s   