o
    ॵi                     @   s   d dl m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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# Z$dgZ%ej&e!j'ej'dG dd deZ(dS )    N)Enum)AnyDictListOptionalUnion)	Pipelines)Model)InputFormat)
OutputKeys)
InputModelPipeline)	PIPELINES)Preprocessor)Config)	ModelFileTasks)
get_loggerTranslationEvaluationPipeline)module_namec                	       s   e Zd Zdejdfdedee dedef fddZ	d	d
 Z
ejfdefddZdeeeeee f f f fddZdeeejf deeejf fddZdejdeeef fddZ  ZS )r   Ngpumodelpreprocessorinput_formatdevicec                    s~   t  j|||dd|di d || _|   t| jts'J dtj	 | jj
t| jjtj| jdd | j  dS )a  Build a translation evaluation pipeline with a model dir or a model id in the model hub.

        Args:
            model: A Model instance.
            preprocessor: The preprocessor for this pipeline.
            input_format: Input format, choosing one from `"InputFormat.SRC_REF"`,
                `"InputFormat.SRC"`, `"InputFormat.REF"`. Aside from hypothesis, the
                source/reference/source+reference can be presented during evaluation.
            device: Used device for this pipeline.
        compileFcompile_options)r   r   r   r   z,please check whether model config exists in )r   plm_onlyN)super__init__popr   checking_input_format
isinstancer   r	   r   CONFIGURATIONload_checkpointospjoin	model_dirTORCH_MODEL_BIN_FILEr   eval)selfr   r   r   r   kwargs	__class__ l/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/nlp/translation_evaluation_pipeline.pyr      s$   



z&TranslationEvaluationPipeline.__init__c                 C   sV   | j tjkrtd d S | j tjkrtd d S | j tjkr'td d S td)NzEvaluation mode: source-onlyzEvaluation mode: reference-onlyz*Evaluation mode: source-reference-combinedziEvaluation mode should be one choice among'InputFormat.SRC', 'InputFormat.REF', and'InputFormat.SRC_REF'.)r   r
   SRCloggerinfoREFSRC_REF
ValueError)r*   r.   r.   r/   r!   A   s   z3TranslationEvaluationPipeline.checking_input_formatc                 C   s(   t d || _|   | j| d S )NzChanging the evaluation mode.)r1   r2   r   r!   r   change_input_format)r*   r   r.   r.   r/   r6   M   s
   
z1TranslationEvaluationPipeline.change_input_format
input_dictc                    s   t  jdd|i|S )u  Implementation of __call__ function.

        Args:
            input: The formatted dict containing the inputted sentences.
            An example of the formatted dict:
                ```
                input = {
                    'hyp': [
                        'This is a sentence.',
                        'This is another sentence.',
                    ],
                    'src': [
                        '这是个句子。',
                        '这是另一个句子。',
                    ],
                    'ref': [
                        'It is a sentence.',
                        'It is another sentence.',
                    ]
                }
                ```
        inputNr.   )r   __call__)r*   r7   r+   r,   r.   r/   r9   U   s   z&TranslationEvaluationPipeline.__call__returnc                 C   s   | j di |S )Nr.   )r   )r*   r7   r.   r.   r/   forwardn   s   z%TranslationEvaluationPipeline.forwardoutputc                 C   s   |S )Nr.   )r*   r<   r.   r.   r/   postprocesss   s   z)TranslationEvaluationPipeline.postprocess)__name__
__module____qualname__r
   r4   r   r   r   strr   r!   r6   r   r   r   r9   torchTensorr;   r   r=   __classcell__r.   r.   r,   r/   r      s6    #
&
$))os.pathpathr%   enumr   typingr   r   r   r   r   numpynprB   modelscope.metainfor   modelscope.models.baser	   )modelscope.models.nlp.unite.configurationr
   modelscope.outputsr   modelscope.pipelines.baser   r   modelscope.pipelines.builderr   modelscope.preprocessorsr   modelscope.utils.configr   modelscope.utils.constantr   r   modelscope.utils.loggerr   r1   __all__register_moduletranslation_evaluationr   r.   r.   r.   r/   <module>   s*   