o
    *jq                     @   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 please 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   ZCONFIGURATIONr   r   Z	model_dir	vocab_tgtr   Ztgt_langdetokenizer)selfr   r   r   r   r   kwargs	__class__ t/var/www/html/Deteccion_Ine/venv/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)torchZno_gradr   forward)r    r&   Z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@@ )Zextra_symbols_to_ignore)lenrangecputolistr   stringpadr   Z
detokenizesplitappendr
   ZTRANSLATIONZSCORE)
r    r&   Zpostprocess_paramsZ
batch_sizeZhyposZscoresiZhypo_tensorZscoreZ	hypo_sentr$   r$   r%   postprocessK   s   

z$CanmtTranslationPipeline.postprocess)NNr   T)__name__
__module____qualname__r   r   strr   r   r   r   r   r)   r   r5   __classcell__r$   r$   r"   r%   r      s(    
,

)#Zos.pathpathZosptypingr   r   r   r   r(   Z
sacremosesr   Zmodelscope.metainfor   Zmodelscope.modelsr   Zmodelscope.models.nlpr	   Zmodelscope.outputsr
   Zmodelscope.pipelines.baser   r   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessorsr   r   Zmodelscope.utils.constantr   r   __all__Zregister_moduleZcompetency_aware_translationZcanmt_translationr   r$   r$   r$   r%   <module>   s$   