o
    *j|                     @   s   d dl Z d dlmZ d dlmZmZ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mZ d	gZejejejd
G dd	 d	eZdS )    N)AnyDictListOptionalTuple)Tensor)Models)
TorchModel)MODELS)Config)	ModelFileTasksCanmtForTranslation)module_namec                       sV   e Zd Z fddZdd Zdd Zdedejfd	d
Z	de
ee
f fddZ  ZS )r   c                    s   t  jdd|i| || _t|tj}t|| _	ddl
m} |t|d| _|t|d| _| || _| | j| j| j	d | _dS )	a  
            CanmtForTranslation implements a Competency-Aware Neural Machine Translaton,
            which has both translation and self-estimation abilities.

            For more details, please refer to https://aclanthology.org/2022.emnlp-main.330.pdf
        	model_dirr   )
Dictionaryzdict.src.txtzdict.tgt.txtdecodeN )super__init__argsospjoinr   ZCONFIGURATIONr   	from_filecfgZfairseq.datar   loadZ	vocab_src	vocab_tgtbuild_modelmodelbuild_generator	generator)selfr   r   Zcfg_filer   	__class__r   n/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/models/nlp/canmt/canmt_translation.pyr      s   
zCanmtForTranslation.__init__c                 C   sT   ddl m} | t|tjd}|d }||d | }|j|d |d d |S )N   )
CanmtModelcpur   r   )Z	model_cfg)	Zcanmt_modelr&   load_checkpointr   r   r   ZTORCH_MODEL_FILEr   load_state_dict)r!   r   r&   stater   r   r   r   r$   r   *   s   zCanmtForTranslation.build_modelc                 C   s$   ddl m} ||||d |d dS )Nr%   )SequenceGeneratorZbeamZlenpen)Z	beam_sizeZlen_penalty)Zsequence_generatorr+   )clsr   r   r   r+   r   r   r$   r   3   s   z#CanmtForTranslation.build_generatorpathdevicec                 C   s    t j||d}| j|dd |S )N)Zmap_locationF)strict)torchr   r)   )r!   r-   r.   Z
state_dictr   r   r$   r(   ;   s   z#CanmtForTranslation.load_checkpointinputc                 C   s   d|i}| j |S )a  return the result by the model

        Args:
            input (Dict[str, Tensor]): the preprocessed data which contains following:
                - src_tokens: tensor with shape (2478,242,24,4),
                - src_lengths: tensor with shape (4)


        Returns:
            Dict[str, Tensor]: results which contains following:
                - predictions: tokens need to be decode by tokenizer with shape [1377, 4959, 2785, 6392...]
        Z	net_input)r    generate)r!   r1   r   r   r$   forward@   s   zCanmtForTranslation.forward)__name__
__module____qualname__r   r   r   strr0   r.   r(   r   r3   __classcell__r   r   r"   r$   r      s    	)mathZos.pathr-   r   typingr   r   r   r   r   numpyr0   Ztorch.nnnnr   Zmodelscope.metainfor   Zmodelscope.models.baser	   Zmodelscope.models.builderr
   Zmodelscope.utils.configr   Zmodelscope.utils.constantr   r   __all__Zregister_moduleZcompetency_aware_translationZcanmtr   r   r   r   r$   <module>   s"   