o
    *jE!                     @   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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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)generate_scp_from_urlupdate_local_model)
FrameworksTasks)
get_loggerAudioQuantizationPipeline)module_namec                       s   e Zd ZdZ		ddeeef def fddZ			ddee	ee
f d	ed
edeee
f fddZdedeee
f fddZdeee
f fddZddee	ee
f defddZdd Z  ZS )r   a  Audio Quantization Inference Pipeline
    use `model` to create a audio quantization pipeline.

    Args:
        model (AudioQuantizationPipeline): 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
        >>> from modelscope.utils.constant import Tasks
        >>> pipeline_aq = pipeline(
        >>>    task=Tasks.audio_quantization,
        >>>    model='damo/audio_codec-encodec-zh_en-general-16k-nq32ds640-pytorch'
        >>> )
        >>> audio_in='example.wav'
        >>> print(pipeline_aq(audio_in))

    N   modelngpuc                    s$  t  jdd|i| | j | _| ||| _ddlm} |j	di d| jd d| jd d| jd d| jd d|d	| jd	 d
| jd
 d| jd d| jd d| jd d| jd d| jd d| jd d| jd d| jd d| jd d| jd d| jd || _
dS )z=use `model` to create an asr pipeline for prediction
        r   r   )codec_inferencemode
output_dir
batch_sizedtyper   seednum_workers	log_levelkey_fileconfig_file
model_file	model_tagallow_variable_data_keys	streamingsampling_rate	bit_width	use_scale
param_dictN )super__init__r   forward	model_cfgget_cmdcmdZfuncodec.binr   Zinference_modelscopefunasr_infer_modelscope)selfr   r   kwargsr   	__class__r)   w/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/audio/audio_quantization_pipeline.pyr+   /   sT   








	









z"AudioQuantizationPipeline.__init__audio_inr   r(   returnc                 C   sP   t |dkr
td|| _|d ur|| jd< || jd< | | j}| |}|S )Nr   zThe input should not be null.r   r(   )len
ValueErrorr6   r/   r,   postprocess)r1   r6   r   r(   outputresultr)   r)   r5   __call__P   s   


z"AudioQuantizationPipeline.__call__inputsc                 C   s~   i }t t|D ]4}t|dkr0|dkr0|d d }|  d }|d tj}||tj< q|| d ||| d < q|S )zPostprocessing
        r   r   valuei   key)	ranger8   cpunumpyZastypenpZint16r
   Z
OUTPUT_WAV)r1   r>   rstiZ	recon_wavZ
output_wavr)   r)   r5   r:   `   s   z%AudioQuantizationPipeline.postprocessc           	      C   s  | j d d }tj| j d | j d d }tj| j d | j d d }t| j d || i d|dd ddd	d
dddddddddd d|d|dd dddddddddddd i}g d}|D ]9}|| j d v r| j d | d urt|| trt| j d | tr|| | j d |  qm| j d | ||< qm|D ].}||v r||d urt|| trt|| tr|| ||  n|| ||< ||= q|S )NZmodel_configr   Zmodel_workspacer!   r    r   r   r   r   Zfloat32r   r   r   r   r   ERRORr   r"   r#   Tr$   Fr%   i>  r&   i@  r'   r(   )r   r   r   r   r#   r$   r   r%   r&   r'   r(   )	r-   ospathjoinr   
isinstancedictupdateget)	r1   
extra_argsZ
model_pathr   Z_model_pathZ_model_configr/   Zuser_args_dictZ	user_argsr)   r)   r5   r.   o   s   	
z!AudioQuantizationPipeline.get_cmdc           	      C   s   t |trtd| d ntdt|dd  d d\}}t |trBt|ddkr9t|dg}n2t|\}}|}n)t |trM|dd	f}nd
dl	}d
dl
}t ||jr^|}nt ||jrg|}ntd|| jd< || jd< | | j}|S )zDecoding
        zAudio Quantization Processing: z ...Nd   )NN,   Zspeechbytesr   zUnsupported data type.name_and_type
raw_inputs)rK   strloggerinfor8   splittupler   rS   torchrC   ZTensorZndarray	TypeErrorr/   run_inference)	r1   r6   Zdata_cmdrU   Z	audio_scp_r[   rD   r<   r)   r)   r5   r,      s0   




z!AudioQuantizationPipeline.forwardc                 C   s:   | j tjkr| j|d |d |d |d d}|S td)NrT   rU   r   r(   )Zdata_path_and_name_and_typerU   Zoutput_dir_v2r(   zmodel type is mismatching)Z	frameworkr   r[   r0   r9   )r1   r/   Z	sv_resultr)   r)   r5   r]      s   z'AudioQuantizationPipeline.run_inference)Nr   )NNN)N)__name__
__module____qualname____doc__r   r	   rV   intr+   rZ   r   rL   r   r=   listr:   r.   r,   r]   __classcell__r)   r)   r3   r5   r      s2    
"

E')$rH   shutiltypingr   r   r   r   r   r   rC   rD   yamlZmodelscope.metainfor   Zmodelscope.modelsr	   Zmodelscope.outputsr
   Zmodelscope.pipelines.baser   Zmodelscope.pipelines.builderr   Z"modelscope.utils.audio.audio_utilsr   r   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   rW   __all__Zregister_moduleZaudio_quantizationZaudio_quantization_inferencer   r)   r)   r)   r5   <module>   s(    