o
    ॵip                     @   s   d dl mZ d dlmZmZ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 d	lmZ d d
lmZ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OptionalUnion)MosesDetokenizer)	Pipelines)Model)CanmtForTranslation)
OutputKeys)PipelineTensor)	PIPELINES)CanmtTranslationPreprocessorPreprocessor)	ModelFileTasksCanmtTranslationPipeline)module_namec                	       s   e Zd Z				ddeeef dee dedef fdd	Zd
e	ee
f de	ee
f f fddZd
e	eef de	eef fddZ  ZS )r   NgpuTmodelpreprocessorconfig_filedevicec                    sv   t  j|||||d t| jtsJ dtj | jdu r,|du r)t| jj	|n|| _| jj
| _
t| jjd| _dS )u(  Use `model` and `preprocessor` to create a canmt translation pipeline for prediction.

        Args:
            model (str or Model): Supply either a local model dir which supported the canmt translation task,
            or a model id from the model hub, or a torch model instance.
            preprocessor (Preprocessor): An optional preprocessor instance, please make sure the preprocessor fits for
            the model if supplied.
            kwargs (dict, `optional`):
                Extra kwargs passed into the preprocessor's constructor.

        Examples:
            >>> from modelscope.pipelines import pipeline
            >>> pipeline_ins = pipeline(task='competency_aware_translation',
            >>>    model='damo/nlp_canmt_translation_zh2en_large')
            >>> sentence1 = '世界是丰富多彩的。'
            >>> print(pipeline_ins(sentence1))
            >>> # Or use the list input:
            >>> print(pipeline_ins([sentence1])

            To view other examples plese check tests/pipelines/test_canmt_translation.py.
        )r   r   r   r   auto_collatez,please check whether model config exists in N)lang)super__init__
isinstancer   r   r   CONFIGURATIONr   r   	model_dir	vocab_tgtr   tgt_langdetokenizer)selfr   r   r   r   r   kwargs	__class__ g/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/nlp/canmt_translation_pipeline.pyr      s(   


z!CanmtTranslationPipeline.__init__inputsreturnc                    s@   t   t j|fi |W  d    S 1 sw   Y  d S )N)torchno_gradr   forward)r#   r)   forward_paramsr%   r'   r(   r-   F   s   
$z CanmtTranslationPipeline.forwardc           
      K   s   t |d }g }g }t|D ]7}|d | d d }|d | d   }| jj|d| j hd}	| j|		 }	|
|	 |
| qtj|tj|iS )Nr   tokens   z@@ )extra_symbols_to_ignore)lenrangecputolistr    stringpadr"   
detokenizesplitappendr
   TRANSLATIONSCORE)
r#   r)   postprocess_params
batch_sizehyposscoresihypo_tensorscore	hypo_sentr'   r'   r(   postprocessK   s   

z$CanmtTranslationPipeline.postprocess)NNr   T)__name__
__module____qualname__r   r   strr   r   r   r   r   r-   r   rE   __classcell__r'   r'   r%   r(   r      s(    
,

)#os.pathpathosptypingr   r   r   r   r+   
sacremosesr   modelscope.metainfor   modelscope.modelsr   modelscope.models.nlpr	   modelscope.outputsr
   modelscope.pipelines.baser   r   modelscope.pipelines.builderr   modelscope.preprocessorsr   r   modelscope.utils.constantr   r   __all__register_modulecompetency_aware_translationcanmt_translationr   r'   r'   r'   r(   <module>   s$   