o
    *jp                     @   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 d dlmZ d dl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G dd
 d
eZdS )    )AnyDictOptionalUnionN)	Pipelines)Model)
OutputKeys)Pipeline)	PIPELINES)Preprocessor)	ModelFileTasksSentenceEmbeddingPipeline)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   modelpreprocessorconfig_filedevicec                    sr   t  j||||||dd|di d t| jts#J dtj |du r7tj	| jj
fd|i|| _dS dS )a1  Use `model` and `preprocessor` to create a nlp text dual encoder then generates the text representation.
        Args:
            model (str or Model): Supply either a local model dir which supported the WS task,
            or a 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.
        compileFcompile_options)r   r   r   r   auto_collater   r   z,please check whether model config exists in Nsequence_length)super__init__pop
isinstancer   r   r   ZCONFIGURATIONr   Zfrom_pretrainedZ	model_dirr   )selfr   r   r   r   r   r   kwargs	__class__ u/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/nlp/sentence_embedding_pipeline.pyr      s*   

	
z"SentenceEmbeddingPipeline.__init__inputsreturnc                 K   s   | j di ||S )Nr"   )r   )r   r$   Zforward_paramsr"   r"   r#   forward:   s   z!SentenceEmbeddingPipeline.forwardc                 C   s   |d }|d }|durt j||fdd}|   }|dur:t|ddf t|ddf d d }ng }t	j
|t	j|iS )zprocess the prediction results

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

        Returns:
            Dict[str, Any]: the predicted text representation
        Zquery_embeddingsdoc_embeddingsNr   )dim   )r)   r   )torchcatdetachcpunumpynpdotZ	transposetolistr   ZTEXT_EMBEDDINGZSCORES)r   r$   Z
embeddingsr'   Zscoresr"   r"   r#   postprocess>   s   	z%SentenceEmbeddingPipeline.postprocess)NNr   Tr   )__name__
__module____qualname__r   r   strr   r   r   r   r   r&   r2   __classcell__r"   r"   r    r#   r      s&    
#

*)typingr   r   r   r   r.   r/   r*   Zmodelscope.metainfor   Zmodelscope.modelsr   Zmodelscope.outputsr   Zmodelscope.pipelines.baser	   Zmodelscope.pipelines.builderr
   Zmodelscope.preprocessorsr   Zmodelscope.utils.constantr   r   __all__Zregister_moduleZsentence_embeddingr   r"   r"   r"   r#   <module>   s   