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 d dlmZmZ d dlmZ d dlmZ d d	lmZmZ d
gZejejejdejejejdG dd
 d
eZdS )    )AnyDictOptionalUnionN)	Pipelines)Model)
OutputKeys)PipelineTensor)	PIPELINES)Preprocessor)	ModelFileTasksFillMaskPipeline)module_namec                	       s   e Zd Z						ddeeef dee ded	ef fd
dZde	ee
f de	ee
f fddZde	eef de	eef fddZ  ZS )r   NgpuTsentence   modelpreprocessorconfig_filedevicec           	         s   t  j||||||dd|di d t| jts#J dtj |du r6tj	| jj
f||d|| _| j  t| jdsEJ d	dS )
aj  The inference pipeline for all the fill mask sub-tasks.

        Args:
            model (`str` or `Model` or module instance): A model instance or a model local dir
                or a model id in the model hub.
            preprocessor (`Preprocessor`, `optional`): A Preprocessor instance.
            kwargs (dict, `optional`):
                Extra kwargs passed into the preprocessor's constructor.

        Examples:

        >>> from modelscope.pipelines import pipeline
        >>> pipeline_ins = pipeline('fill-mask', model='damo/nlp_structbert_fill-mask_english-large')
        >>> input = 'Everything in [MASK] you call reality is really [MASK] a reflection of your [MASK].'
        >>> print(pipeline_ins(input))

        Examples:

        >>> from modelscope.pipelines import pipeline
        >>> pipeline_ins = pipeline('fill-mask', model='damo/nlp_ponet_fill-mask_english-base')
        >>> input = 'Everything in [MASK] you call reality is really [MASK] a reflection of your [MASK].'
        >>> print(pipeline_ins(input))

        NOTE2: Please pay attention to the model's special tokens.
        If bert based model(bert, structbert, etc.) is used, the mask token is '[MASK]'.
        If the xlm-roberta(xlm-roberta, veco, etc.) based model is used, the mask token is '<mask>'.
        To view other examples plese check tests/pipelines/test_fill_mask.py.
        compileFcompile_options)r   r   r   r   auto_collater   r   z,please check whether model config exists in N)first_sequencesequence_lengthmask_idz9The input preprocessor should have the mask_id attribute.)super__init__pop
isinstancer   r   r   CONFIGURATIONr   from_pretrained	model_dirr   evalhasattr)	selfr   r   r   r   r   r   r   kwargs	__class__ _/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/nlp/fill_mask_pipeline.pyr      s6   %

	

zFillMaskPipeline.__init__inputsreturnc                 K   s   | j di ||S )Nr+   )r   )r'   r-   forward_paramsr+   r+   r,   forwardR   s   zFillMaskPipeline.forwardc           	      C   s   |t j    }|t j    }tj|dd}t|| j	j
k||}g }|D ]}| j	j|ddd}|| q,t j|iS )a  process the prediction results

        Args:
            inputs (Dict[str, Any]): The model outputs.
            The output should follow some rules:
                1. Values can be retrieved by keys(dict-like, or the __getitem__ method is overriden)
                2. 'logits' and 'input_ids' key exists.
            Models in modelscope will return the output dataclass `modelscope.outputs.FillMaskModelOutput`.
        Returns:
            Dict[str, str]: the prediction results
        )axisT)skip_special_tokensclean_up_tokenization_spaces)r   LOGITSdetachcpunumpy	INPUT_IDSnpargmaxwherer   r   decodeappendTEXT)	r'   r-   logits	input_idspred_idsrst_idspred_stringsidspred_stringr+   r+   r,   postprocessV   s   
zFillMaskPipeline.postprocess)NNr   Tr   r   )__name__
__module____qualname__r   r   strr   r   r   r   r   r0   r
   rG   __classcell__r+   r+   r)   r,   r      s(    
;

*)typingr   r   r   r   r8   r:   modelscope.metainfor   modelscope.modelsr   modelscope.outputsr   modelscope.pipelines.baser	   r
   modelscope.pipelines.builderr   modelscope.preprocessorsr   modelscope.utils.constantr   r   __all__register_module	fill_maskfill_mask_ponetr   r+   r+   r+   r,   <module>   s   