o
    *j                     @   s   d dl 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 d
gZejejejdG dd
 d
eZdS )    )AnyDictUnionN)	Pipelines)Model)
OutputKeys)Pipeline)	PIPELINES)%DialogueClassificationUsePreprocessor)Tasks"UserSatisfactionEstimationPipeline)Z	group_keymodule_namec                	       s   e Zd Z				ddeeef d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d
eee	f dedeee	f fddZ  ZS )r   NgpuTmodelpreprocessorconfig_filedevicec                    sN   t  j||||||dd|di d t| jdr | jj| _| j  dS )u  The inference pipeline for the user satisfaction estimation task.

        Args:
            model (str or Model): Supply either a local model dir which supported user satisfaction estimation task, or
            a model id from the model hub, or a torch model instance.
            preprocessor (DialogueClassificationUsePreprocessor): An optional preprocessor instance.
            device (str): device str, should be either cpu, cuda, gpu, gpu:X or cuda:X
            auto_collate (bool): automatically to convert data to tensor or not.

        Examples:
            >>> from modelscope.pipelines import pipeline
            >>> pipeline_ins = pipeline('text-classification',
                model='damo/nlp_user-satisfaction-estimation_chinese')
            >>> input = [('返修退换货咨询|||', '手机有质量问题怎么办|||稍等，我看下', '开不开机了|||',
                       '说话|||谢谢哈')]
            >>> print(pipeline_ins(input))
        compileFcompile_options)r   r   r   r   auto_collater   r   id2labelN)super__init__pophasattrr   r   r   eval)selfr   r   r   r   r   kwargs	__class__ /var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/nlp/user_satisfaction_estimation_pipeline.pyr      s   

	
z+UserSatisfactionEstimationPipeline.__init__inputsreturnc                 K   s@   t   | jdi ||W  d    S 1 sw   Y  d S )Nr    )torchZno_gradr   )r   r"   Zforward_paramsr    r    r!   forward>   s   
$z*UserSatisfactionEstimationPipeline.forwardtopkc           
         s   |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 )
aS  Process the prediction results

                Args:
                    inputs (`Dict[str, Any]` or `DialogueUseClassificationModelOutput`): The model output, please check
                        the `DialogueUseClassificationModelOutput` 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 largest probability.
                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!   softmaxV   s   z?UserSatisfactionEstimationPipeline.postprocess.<locals>.softmaxr(   N)r)   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)Z_idr   r    r!   map_to_label`   s   

zDUserSatisfactionEstimationPipeline.postprocess.<locals>.map_to_label)r   ZLOGITScpunumpyshapeminr*   ZargpartitionZtake_along_axistolistZ	vectorizelistreversedZSCORESZLABELS)
r   r"   r&   r.   r/   ZprobsZnum_classesZtop_indicesr4   Zv_funcr    r3   r!   postprocessC   s   

z.UserSatisfactionEstimationPipeline.postprocess)NNr   T)N)__name__
__module____qualname__r   r   r1   r
   r   r   r   r%   intr<   __classcell__r    r    r   r!   r      s2    
&



)typingr   r   r   r6   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   __all__Zregister_moduleZtext_classificationZuser_satisfaction_estimationr   r    r    r    r!   <module>   s    