o
    ॵi#	                     @   s   d dl Z d dlZd dl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	gZejeje	jd
G dd	 d	eZdS )    N)AnyDict)XLMRobertaTokenizer)	Pipelines)
OutputKeys)Pipeline)	PIPELINES)	ModelFileTasks$TranslationQualityEstimationPipeline)module_namec                       s   e Zd Zddedef fddZdee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   gpumodeldevicec                    s   t  j||d tj| jtj}t|d}t	
| }W d    n1 s(w   Y  t| j| _tjj|| jd| j| _d S )N)r   r   rb)map_location)super__init__ospathjoinr   r	   TORCH_MODEL_FILEopenioBytesIOreadr   from_pretrained	tokenizertorchjitloadr   to)selfr   r   kwargs
model_filefbuffer	__class__ t/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/nlp/translation_quality_estimation_pipeline.pyr      s   z-TranslationQualityEstimationPipeline.__init__inputsc                 C   sf   |d   }|d   }| jj||ggdddd}|d | j}|d | j}|||d |S )	Nsource_texttarget_textptT)return_tensorspadding
truncation	input_idsattention_mask)r2   r3   )stripr   batch_encode_plusr!   r   update)r"   r+   src_texttgt_textencoded_inputsr2   r3   r)   r)   r*   
preprocess!   s   z/TranslationQualityEstimationPipeline.preprocessreturnc                 C   sJ   d|vr	|  |}| |d |d }tjdtj|d    i}|S )Nr2   r3   z-1r   )r:   r   r   LABELSSCORESdetachsqueezetolist)r"   r+   resresultr)   r)   r*   forward1   s   
z,TranslationQualityEstimationPipeline.forwardc                 C   s   |S )zprocess the prediction results

        Args:
            inputs (Dict[str, Any]): input data dict

        Returns:
            Dict[str, str]: the prediction results
        r)   )r"   r+   r)   r)   r*   postprocess;   s   	z0TranslationQualityEstimationPipeline.postprocess)r   )__name__
__module____qualname__strr   r   r   r:   rC   rD   __classcell__r)   r)   r'   r*   r      s
    	"*
)r   r   typingr   r   r   transformersr   modelscope.metainfor   modelscope.outputsr   modelscope.pipelines.baser   modelscope.pipelines.builderr   modelscope.utils.constantr	   r
   __all__register_modulesentence_similaritytranslation_quality_estimationr   r)   r)   r)   r*   <module>   s    