o
    *j                     @   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   NZ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   Zplm_onlyN)super__init__popr   checking_input_format
isinstancer   r	   r   ZCONFIGURATIONZload_checkpointospjoinZ	model_dirZTORCH_MODEL_BIN_FILEr   eval)selfr   r   r   r   kwargs	__class__ y/var/www/html/Deteccion_Ine/venv/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
   ZSRCloggerinfoZ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.)r*   r+   r   r   r   change_input_format)r$   r   r(   r(   r)   r.   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$   r/   r%   r&   r(   r)   r1   U   s   z&TranslationEvaluationPipeline.__call__returnc                 C   s   | j di |S )Nr(   )r   )r$   r/   r(   r(   r)   forwardn   s   z%TranslationEvaluationPipeline.forwardoutputc                 C   s   |S )Nr(   )r$   r4   r(   r(   r)   postprocesss   s   z)TranslationEvaluationPipeline.postprocess)__name__
__module____qualname__r
   r,   r   r   r   strr   r   r.   r   r   r   r1   torchZTensorr3   r   r5   __classcell__r(   r(   r&   r)   r      s6    #
&
$))Zos.pathpathr!   enumr   typingr   r   r   r   r   numpynpr:   Zmodelscope.metainfor   Zmodelscope.models.baser	   Z)modelscope.models.nlp.unite.configurationr
   Zmodelscope.outputsr   Zmodelscope.pipelines.baser   r   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessorsr   Zmodelscope.utils.configr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   r*   __all__Zregister_moduleZtranslation_evaluationr   r(   r(   r(   r)   <module>   s*   