o
    ॵi                     @   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)	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__ r/home/ubuntu/.local/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!   )torchno_gradr   )r   r#   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)axis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)_idr   r!   r"   map_to_label`   s   

zDUserSatisfactionEstimationPipeline.postprocess.<locals>.map_to_label)r   LOGITScpunumpyshapeminr.   argpartitiontake_along_axistolist	vectorizelistreversedSCORESLABELS)
r   r#   r)   r2   r3   probsnum_classestop_indicesr9   v_funcr!   r8   r"   postprocessC   s   

z.UserSatisfactionEstimationPipeline.postprocess)NNr   T)N)__name__
__module____qualname__r   r   r5   r
   r   r   r   r(   intrK   __classcell__r!   r!   r   r"   r      s2    
&



)typingr   r   r   r<   r.   r%   modelscope.metainfor   modelscope.modelsr   modelscope.outputsr   modelscope.pipelines.baser   modelscope.pipelines.builderr	   modelscope.preprocessorsr
   modelscope.utils.constantr   __all__register_moduletext_classificationuser_satisfaction_estimationr   r!   r!   r!   r"   <module>   s    