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 d dlmZ d dlmZ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#TextRankingTransformersPreprocessor)	ModelFileTasksTextRankingPipeline)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 )a  Use `model` and `preprocessor` to create a nlp word segment pipeline for prediction.

        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   CONFIGURATIONr   from_pretrained	model_dirr   )selfr   r   r   r   r   r   kwargs	__class__ b/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/nlp/text_ranking_pipeline.pyr      s*   

	
zTextRankingPipeline.__init__inputsreturnc                 K   s   | j di ||S )Nr&   )r   )r"   r(   forward_paramsr&   r&   r'   forward;   s   zTextRankingPipeline.forwardc                 C   s:   dd }|t j d   }|| }t j|iS )zprocess the prediction results
        Args:
            inputs (Dict[str, Any]): _description_

        Returns:
            Dict[str, Any]: the predicted text representation
        c                 S   s   t | dt |   S )N   )npexp)logitsr&   r&   r'   sigmoidH   s   z0TextRankingPipeline.postprocess.<locals>.sigmoid)r   LOGITSsqueezedetachcpunumpytolistSCORES)r"   r(   r0   r/   	pred_listr&   r&   r'   postprocess?   s   	
zTextRankingPipeline.postprocess)NNr   Tr   )__name__
__module____qualname__r   r   strr   r   r   r   r   r+   r:   __classcell__r&   r&   r$   r'   r      s&    
$

*)typingr   r   r   r   r6   r-   modelscope.metainfor   modelscope.modelsr   modelscope.outputsr   modelscope.pipelines.baser	   modelscope.pipelines.builderr
   modelscope.preprocessorsr   r   modelscope.utils.constantr   r   __all__register_moduletext_rankingr   r&   r&   r&   r'   <module>   s   