
    9iq                        S SK Jr  S SKJrJrJrJr  S SKrS SK	J
r
  S SKJr  S SKJr  S SKJr  S SKJr  S SKJrJr  S S	KJr  S S
KJrJr  S SKJrJr  S/r\R>                  " \R@                  \RB                  S9 " S S\5      5       r"g)    N)AnyDictOptionalUnion)MosesDetokenizer)	Pipelines)Model)CanmtForTranslation)
OutputKeys)PipelineTensor)	PIPELINES)CanmtTranslationPreprocessorPreprocessor)	ModelFileTasksCanmtTranslationPipeline)module_namec            	          ^  \ rS rSr    SS\\\4   S\\   S\S\4U 4S jjjr	S\
\\4   S\
\\4   4U 4S	 jjrS\
\\4   S\
\\4   4S
 jrSrU =r$ )r      modelpreprocessorconfig_filedevicec                 ~  > [         TU ]  UUUUUS9  [        U R                  [        5      (       d   S[
        R                   35       eU R                  c*  Uc   [        U R                  R                  U5      OUU l        U R                  R                  U l
        [        U R                  R                  S9U l        g)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   CONFIGURATIONr   r   	model_dir	vocab_tgtr   tgt_langdetokenizer)selfr   r   r   r   r   kwargs	__class__s          s/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/pipelines/nlp/canmt_translation_pipeline.pyr   !CanmtTranslationPipeline.__init__   s    8 	%#% 	 	' $**e,, 	U:9;R;R:ST	U, $ (/ !=

$$!5A  **44+1B1B1K1KL    inputsreturnc                    > [         R                  " 5          [        TU ]  " U40 UD6sS S S 5        $ ! , (       d  f       g = f)N)torchno_gradr   forward)r&   r,   forward_paramsr(   s      r)   r1    CanmtTranslationPipeline.forwardF   s'    ]]_7?6<^< __s	   2
A c                    [        US   5      n/ n/ n[        U5       H  nUS   U   S   S   nUS   U   S   R                  5       R                  5       nU R                  R                  USU R                  R                  5       1S9n	U R                  R                  U	R                  5       5      n	UR                  U	5        UR                  U5        M     [        R                  U[        R                  U0$ )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_sents
             r)   postprocess$CanmtTranslationPipeline.postprocessK   s    ^
z"A )A,q/(3K1IaLO'')002E--)-););)=(> . @I ((33IOO4EFILL#MM%  # &&z/?/?HHr+   )r%   r   r#   )NNgpuT)__name__
__module____qualname____firstlineno__r   r	   strr   r   r   r   r   r1   r   rK   __static_attributes____classcell__)r(   s   @r)   r   r      s     9=$($"*MeSj)*M'5*M "*M 	*M *MX=d38n =%)#s(^=
I$sF{"3 I-1#s(^I Ir+   )#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)   <module>rg      st     - -  ' ) # 5 ) 6 2 O 6%
& 	&&++-CIx CI-CIr+   