o
    ॵi                     @   s   d dl Z 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mZ dgZejejejdG dd deZdS )    N)AnyDictOptionalUnion)	Pipelines)Model)
OutputKeys)PipelineTensor)	PIPELINES) FillMaskTransformersPreprocessorPreprocessor)Config)	ModelFileTasksFeatureExtractionPipeline)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gpuTF   modelpreprocessorconfig_filedevicec           	         sz   t  j||||||dd|di d t| jts#J dtj |du r6tj	| jj
f||d|| _| j  dS )a_  Use `model` and `preprocessor` to create a nlp feature extraction pipeline for prediction

        Args:
            model (str or Model): Supply either a local model dir which supported feature extraction task, or a
            no-head model id from the model hub, or a torch model instance.
            preprocessor (Preprocessor): An optional preprocessor instance, please make sure the preprocessor fits for
            the model if supplied.
            kwargs (dict, `optional`):
                Extra kwargs passed into the preprocessor's constructor.

        Examples:
            >>> from modelscope.pipelines import pipeline
            >>> pipe_ins = pipeline('feature_extraction', model='damo/nlp_structbert_feature-extraction_english-large')
            >>> input = 'Everything you love is treasure'
            >>> print(pipe_ins(input))


        compileFcompile_options)r   r   r   r   auto_collater   r   z,please check whether model config exists in N)paddingsequence_length)super__init__pop
isinstancer   r   r   CONFIGURATIONr   from_pretrained	model_dirr   eval)	selfr   r   r   r   r   r   r   kwargs	__class__ h/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/nlp/feature_extraction_pipeline.pyr      s,   

	
z"FeatureExtractionPipeline.__init__inputsreturnc                 K   s@   t   | jdi ||W  d    S 1 sw   Y  d S )Nr*   )torchno_gradr   )r&   r,   forward_paramsr*   r*   r+   forwardF   s   
$z!FeatureExtractionPipeline.forwardc                 C   s   t j|t j  iS )zprocess the prediction results

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

        Returns:
            Dict[str, str]: the prediction results
        )r   TEXT_EMBEDDINGtolist)r&   r,   r*   r*   r+   postprocessK   s   z%FeatureExtractionPipeline.postprocess)NNr   TFr   )__name__
__module____qualname__r   r   strr   r   r   r   r   r1   r
   r4   __classcell__r*   r*   r(   r+   r      s(    
.

*)ostypingr   r   r   r   r.   modelscope.metainfor   modelscope.modelsr   modelscope.outputsr   modelscope.pipelines.baser	   r
   modelscope.pipelines.builderr   modelscope.preprocessorsr   r   modelscope.utils.configr   modelscope.utils.constantr   r   __all__register_modulefeature_extractionr   r*   r*   r*   r+   <module>   s    