o
    ॵi                     @   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 plese 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   o/home/ubuntu/.local/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} ...model_configlangmodel_workspaceitn_model_pathz.cachei  T)modeexist_okz_{0}_itn.farr   ERRORF)
ngpu	log_levelr   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(   	model_dirr*   r1   name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(   jar1   r2   r3   r4   )r(   r1   r2   r3   r4   )r(   r1   r2   r   r5   )r5   zmodel type is mismatching)	frameworkr   torch@fun_text_processing.inverse_text_normalization.inverse_normalizerF   inverse_normalizer!   )r   rD   r#   rF   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    "%)r9   r>   typingr   r   r   r   r   r   yamlmodelscope.metainfor   modelscope.modelsr	   modelscope.outputsr
   modelscope.pipelines.baser   modelscope.pipelines.builderr   modelscope.utils.constantr   r   modelscope.utils.loggerr   r6   __all__register_moduleinverse_text_processingitn_inferencer   r   r   r   r   <module>   s"    