o
    *j                     @   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)Zmodel_name_or_pathtypefieldfirst_sequencetextsecond_sequencesequence_lengthi   )r   r!   r"   id2label )super__init__pop
isinstancer   r   r   ZCONFIGURATION	__class____name__r   Zfrom_pretrainedZ	model_dirr   Zofa_tasks_preprocessorr   Zmulti_modalr   hasattrr#   )
selfr   r   r   r   r   kwargsr   r!   r"   r)   r$   v/var/www/html/Deteccion_Ine/venv/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)r,   datar.   r$   r/   r0   ]   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*   torchZno_gradr%   forward)r,   r2   Zforward_paramsr.   r$   r/   r5   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)axisZkeepdims)npexpmaxsum)logitsr;   r$   r$   r/   softmax   s   z7TextClassificationPipeline.postprocess.<locals>.softmaxr8   )r9   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r,   r$   r/   map_to_label   s   

z<TextClassificationPipeline.postprocess.<locals>.map_to_label)r   r)   r*   r@   loggerwarningr   ZLOGITScpunumpyshapeminr:   ZargpartitionZtake_along_axistolistZ	vectorizelistreversedZSCORESZLABELS)
r,   r2   r6   r>   r?   ZprobsZnum_classesZtop_indicesrE   Zv_funcr$   rD   r/   postprocessj   s*   

z&TextClassificationPipeline.postprocess)NNr   T)N)r*   
__module____qualname__r   r   rA   r   r&   r0   r   r   r5   r	   intrO   __classcell__r$   r$   r.   r/   r      s8    
9


r   )%typingr   r   r   rI   r:   r4   Zmodelscope.metainfor   r   Zmodelscope.models.baser   Zmodelscope.outputsr   r	   Zmodelscope.pipelines.baser
   Zmodelscope.pipelines.builderr   Zmodelscope.pipelines.utilr   Zmodelscope.preprocessorsr   Zmodelscope.utils.constantr   r   r   Zmodelscope.utils.loggerr   rF   Zregister_moduleZtext_classificationZsentiment_analysisZnliZsentence_similarityZsentiment_classificationr   r$   r$   r$   r/   <module>   sD   "