o
    ॵi                     @   s8  d dl mZmZmZ d dlZd dl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 d dlmZ d d	lmZ d d
lmZmZmZ d dlmZ e Zejejej dejej!ej!dejej"ej"dejejejdejejej#dejejej"dejej#ej#dG dd deZ$dS )    )AnyDictUnionN)	PipelinesPreprocessors)Model)
OutputKeysTextClassificationModelOutput)Pipeline)	PIPELINES)batch_process)Preprocessor)Fields	ModelFileTasks)
get_logger)module_namec                	       s   e Zd Z				ddeeef dededef fdd	Z fd
dZde	ee
f de	ee
f f fddZ	ddee	ee
f ef dede	ee
f fddZ  ZS )TextClassificationPipelineNgpuTmodelpreprocessorconfig_filedevicec           
         s   t  j||||||dd|di d t| jts#J dtj |du rd| jjj	dkr@t
jd| jjtjtjd|| _n$|d	d
}|dd}|dd}	t
j| jjfi |||	d|| _t| jdrq| jj| _dS dS )u  The inference pipeline for all the text classification 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('text-classification',
                model='damo/nlp_structbert_sentence-similarity_chinese-base')
            >>> input = ('这是个测试', '这也是个测试')
            >>> print(pipeline_ins(input))
        compileFcompile_options)r   r   r   r   auto_collater   r   z,please check whether model config exists in NOfaForAllTasks)model_name_or_pathtypefieldfirst_sequencetextsecond_sequencesequence_lengthi   )r    r"   r#   id2label )super__init__pop
isinstancer   r   r   CONFIGURATION	__class____name__r   from_pretrained	model_dirr   ofa_tasks_preprocessorr   multi_modalr   hasattrr$   )
selfr   r   r   r   r   kwargsr    r"   r#   r+   r%   i/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/nlp/text_classification_pipeline.pyr'   $   sJ   

	

z#TextClassificationPipeline.__init__c                    s*   | j jjdkrt| j |S tt| |S )Nr   )r   r+   r,   r   r&   r   _batch)r2   datar4   r%   r5   r6   ]   s   z!TextClassificationPipeline._batchinputsreturnc                    s^   | j jjdkr%t  t j|fi |W  d    S 1 s w   Y  | j di ||S )Nr   r%   )r   r+   r,   torchno_gradr&   forward)r2   r8   forward_paramsr4   r%   r5   r<   c   s
   
 z"TextClassificationPipeline.forwardtopkc           
         s    j jjdkr	|S t dddu rtd |tj  	 }|j
d dkr*|d }dd }||}|j
d	 }|dur@t||n|}t|| | d }tj||d	d
 } fdd}t|}	|	| }tt|}tt|}tj|tj|iS )a  Process the prediction results

        Args:
            inputs (`Dict[str, Any]` or `TextClassificationModelOutput`): The model output, please check
                the `TextClassificationModelOutput` class for details.
            topk (int): The topk probs to take
        Returns:
            Dict[str, Any]: the prediction results.
                scores: The probabilities of each label.
                labels: The real labels.
            Label at index 0 is the smallest probability.
        r   r$   Nz7The id2label mapping is None, will return original ids.r      c                 S   s,   t | t j| ddd }||jddd S )NT)axiskeepdims)npexpmaxsum)logitsrD   r%   r%   r5   softmax   s   z7TextClassificationPipeline.postprocess.<locals>.softmaxr@   )rA   c                    sZ   t  dd d ur+|  jv r j|  S t|  jv r  jt|  S td|  d j | S )Nr$   zid z not found in id2label: )getattrr$   str	Exception)idr2   r%   r5   map_to_label   s   

z<TextClassificationPipeline.postprocess.<locals>.map_to_label)r   r+   r,   rI   loggerwarningr   LOGITScpunumpyshapeminrC   argpartitiontake_along_axistolist	vectorizelistreversedSCORESLABELS)
r2   r8   r>   rG   rH   probsnum_classestop_indicesrN   v_funcr%   rM   r5   postprocessj   s*   

z&TextClassificationPipeline.postprocess)NNr   T)N)r,   
__module____qualname__r   r   rJ   r   r'   r6   r   r   r<   r	   intrb   __classcell__r%   r%   r4   r5   r      s8    
9


r   )%typingr   r   r   rS   rC   r:   modelscope.metainfor   r   modelscope.models.baser   modelscope.outputsr   r	   modelscope.pipelines.baser
   modelscope.pipelines.builderr   modelscope.pipelines.utilr   modelscope.preprocessorsr   modelscope.utils.constantr   r   r   modelscope.utils.loggerr   rO   register_moduletext_classificationsentiment_analysisnlisentence_similaritysentiment_classificationr   r%   r%   r%   r5   <module>   sD   "