o
    *j                     @   s   d dl Z d dlZd dlmZmZmZmZmZm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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)AnyDictListSequenceTupleUnion)	Pipelines)Model)
OutputKeys)Pipeline)	PIPELINES)
FrameworksTasks)
get_loggerInverseTextProcessingPipeline)module_namec                       s   e Zd ZdZddeeef f fddZd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dedefddZdd Z  ZS )r   a  Inverse Text Processing Inference Pipeline
    use `model` to create a Inverse Text Processing pipeline.

    Args:
        model (BartForTextErrorCorrection): A model instance, or a model local dir, or a model id in the model hub.
        kwargs (dict, `optional`):
            Extra kwargs passed into the preprocessor's constructor.

    Examples:

    >>> from modelscope.pipelines import pipeline
    >>> pipeline_itn = pipeline(
    >>>    task=Tasks.inverse_text_processing, model='damo/speech_inverse_text_processing_fun-text-processing-itn-id')
    >>> sentence = 'sembilan ribu sembilan ratus sembilan puluh sembilan'
    >>> print(pipeline_itn(sentence))

    To view other examples please check tests/pipelines/test_inverse_text_processing.py.
    Nmodelc                    s&   t  jdd|i| | j | _dS )z=use `model` to create an asr pipeline for prediction
        r   N )super__init__r   forward	model_cfg)selfr   kwargs	__class__r   |/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/audio/inverse_text_processing_pipeline.pyr   +   s   z&InverseTextProcessingPipeline.__init__text_inreturnc                 C   s6   t |dkr
td|| _i }| | j}||d< |S )Nr   z$The input of ITN should not be null.text)len
ValueErrorr   r   )r   r   output
itn_resultr   r   r   __call__1   s   z&InverseTextProcessingPipeline.__call__inputsc                 C   s   |S )zPostprocessing
        r   )r   r%   r   r   r   postprocess=   s   z)InverseTextProcessingPipeline.postprocessc           
      C   s   t d| | jd d }| jd }| jd }tj||d}tj|s/tj|ddd	 d
|}tj||}t	
|| dd|||ddd|dd
}| |}	|	S )zDecoding
        z#Inverse Text Normalization: {0} ...Zmodel_configlangZmodel_workspaceitn_model_pathz.cachei  T)modeexist_okz_{0}_itn.farr   ERRORF)
ZngpuZ	log_levelr   Zitn_model_file	cache_diroverwrite_cacheenable_standalone_numberenable_0_to_9r'   verbose)loggerinfoformatr   ospathjoinisdirmakedirsshutilcopyrun_inference)
r   r   r'   Z	model_dirr(   r,   nameZfar_filecmdr#   r   r   r   r   B   s.   



z%InverseTextProcessingPipeline.forwardc                 C   s   d}| j tjkrBddlm} |d dkr(||d |d |d |d |d	 d
}n||d |d |d d}|j|d |d d}|S td)N r   )InverseNormalizerr'   jar,   r-   r.   r/   )r'   r,   r-   r.   r/   )r'   r,   r-   r   r0   )r0   zmodel type is mismatching)Z	frameworkr   ZtorchZ@fun_text_processing.inverse_text_normalization.inverse_normalizer?   Zinverse_normalizer!   )r   r=   r#   r?   Zitn_normalizerr   r   r   r;   g   s*   z+InverseTextProcessingPipeline.run_inference)N)__name__
__module____qualname____doc__r   r	   strr   r   r   r$   r&   r   r;   __classcell__r   r   r   r   r      s    "%)r4   r9   typingr   r   r   r   r   r   yamlZmodelscope.metainfor   Zmodelscope.modelsr	   Zmodelscope.outputsr
   Zmodelscope.pipelines.baser   Zmodelscope.pipelines.builderr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   r1   __all__Zregister_moduleZinverse_text_processingZitn_inferencer   r   r   r   r   <module>   s"    