o
    *j                     @   s  d dl mZ d dlmZmZ d dlZd dlZd dl	Z
d dlmZmZ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 d
lmZ d dlmZ d dlm Z m!Z! d dl"m#Z# e
j$dkroe
j%j&Z
e
'  e# Z(dgZ)ej*e!j+ej,dG dd deZ-dS )    N)AnyDict)MosesDetokenizerMosesPunctNormalizerMosesTokenizer)	apply_bpe)	Pipelines)Model)
OutputKeys)Pipeline)	PIPELINES)TranslationPipeline)Config)	ModelFileTasks)
get_loggerz2.0InteractiveTranslationPipeline)module_namec                       s   e Zd Zdef fddZde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   modelc                    s  t  jdd|i| | jj}t  tt|tj	d}t
dd tt| jD | _t| j| _tjtjddgdd| _tjtjddgdd| _tjtjddgd	d| _i | _| jd
krct| _nt| jd| _t| jd| _| | jd| j| j}| j| tj dd}d|j!_"tj#|d| _$| j$% }t&'d|  tj()t* }|+|| W d   dS 1 sw   Y  dS )u  Build a interactive translation pipeline with a model dir or a model id in the model hub.

        Args:
            model (`str` or `Model` or module instance): A model instance or a model local dir
                or a model id in the model hub.

        Example:
            >>> from modelscope.pipelines import pipeline
            >>> pipeline_ins = pipeline(task=Tasks.translation,
                model='damo/nlp_imt_translation_zh2en')
            >>> input_sequence = 'Elon Musk, co-founder and chief executive officer of Tesla Motors.'
            >>> input_prefix = "特斯拉汽车公司"
            >>> print(pipeline_ins(input_sequence + "<PREFIX_SPLIT>" + input_prefix))
        r   zckpt-0c                 S   s   g | ]
\}}|  |fqS  )strip).0iwr   r   z/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/nlp/interactive_translation_pipeline.py
<listcomp>8   s    z;InteractiveTranslationPipeline.__init__.<locals>.<listcomp>N
input_wids)Zdtypeshapenameprefix_wids
prefix_hitzh)langT)Zallow_soft_placement)configzloading model from r   ),super__init__r   Z	model_dirtfZreset_default_graphospjoinr   ZTF_CHECKPOINT_FOLDERdict	enumerateopenZ_trg_vocab_path
_trg_vocablen_trg_rvocab_len_tgt_vocabplaceholderZint64r   r   boolr    output	_tgt_langjieba_tgt_tokr   _tgt_punct_normalizerr   updateZConfigProtoZgpu_optionsZallow_growthSession_session
as_defaultloggerinfotrainZSaverZglobal_variablesZrestore)selfr   kwargsZ
model_pathr2   Z	tf_configsessZmodel_loader	__class__r   r   r%   #   sL   

"z'InteractiveTranslationPipeline.__init__inputreturnc                    s  | dd\}} jdkr j|}dt|}n j|} jj|ddd} j	dkrA j
|}dt|d d }n j|} j
j|ddd  }d|d d }tt|dkrjt|d }nd	} j|} j|}t fd
d|   D g}	t fdd|   D g}
dg jd  g}|d	krd} j D ]\}}||rd|d |< d}q|du rdg jd  g}|	|
t|d}|S )Nz<PREFIX_SPLIT>   r!    T)Z
return_strZaggressive_dash_splitsr    c                    .   g | ]}| j v r j | n jd  d qS )r   Zsrc_vocab_size)Z
_src_vocabcfgr   r   r>   r   r   r   y       

z=InteractiveTranslationPipeline.preprocess.<locals>.<listcomp>c                    rI   )r   Ztrg_vocab_size)r,   rJ   rK   rL   r   r   r      rM   F)	input_ids
prefix_idsr    )splitZ	_src_langZ_tokcutr(   listZ_punct_normalizer	normalizetokenizer3   r5   Zlcutr6   r-   Z_bpeZprocess_linenparrayr   r/   r.   items
startswith)r>   rC   Z	input_srcprefixZ	input_tokZ
prefix_tokZsubwordZ	input_bpeZ
prefix_bperN   rO   r    Z	hit_stater   r   resultr   rL   r   
preprocess_   sX   






z)InteractiveTranslationPipeline.preprocessc                 C   sf   | j  $ | j|d | j|d | j|d i}| j j| j|d}|W  d    S 1 s,w   Y  d S )NrN   rO   r    )	feed_dict)r9   r:   r   r   r    runr2   )r>   rC   r\   Zsess_outputsr   r   r   forward   s   


$z&InteractiveTranslationPipeline.forwardinputsc                    sv   |d d }t |d dg }|d |d }d fdd|D dddd} j| }tj|i}|S )	Noutput_seqsr   rF   c                    s$   g | ]}| j v r j | nd qS )z<unk>)r.   )r   ZwidrL   r   r   r      s    z>InteractiveTranslationPipeline.postprocess.<locals>.<listcomp>z@@ rH   z@@)	rR   indexr(   replaceZ_detokZ
detokenizerP   r
   ZTRANSLATION)r>   r_   r`   ZwidsZtranslation_outrZ   r   rL   r   postprocess   s   
z*InteractiveTranslationPipeline.postprocess)__name__
__module____qualname__r	   r%   strr   r   r[   r^   rc   __classcell__r   r   rA   r   r      s
    <"7*
).Zos.pathpathr'   typingr   r   r4   numpyrU   Z
tensorflowr&   Z
sacremosesr   r   r   Zsubword_nmtr   Zmodelscope.metainfor   Zmodelscope.models.baser	   Zmodelscope.outputsr
   Zmodelscope.pipelines.baser   Zmodelscope.pipelines.builderr   Z-modelscope.pipelines.nlp.translation_pipeliner   Zmodelscope.utils.configr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   __version__compatZv1Zdisable_eager_executionr;   __all__Zregister_moduletranslationZinteractive_translationr   r   r   r   r   <module>   s2   
