o
    ॵiE!                     @   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cmdfuncodec.binr   inference_modelscopefunasr_infer_modelscope)selfr   r   kwargsr   	__class__r)   j/home/ubuntu/.local/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
ValueErrorr8   r/   r,   postprocess)r3   r8   r   r(   outputresultr)   r)   r7   __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)	ranger:   cpunumpyastypenpint16r
   
OUTPUT_WAV)r3   r@   rsti	recon_wav
output_wavr)   r)   r7   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 )Nmodel_configr   model_workspacer!   r    r   r   r   r   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)	r3   
extra_args
model_pathr   _model_path_model_configr/   user_args_dict	user_argsr)   r)   r7   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,   speechbytesr   zUnsupported data type.name_and_type
raw_inputs)rU   strloggerinfor:   splittupler   rc   torchrE   Tensorndarray	TypeErrorr/   run_inference)	r3   r8   data_cmdre   	audio_scp_rk   rG   r>   r)   r)   r7   r,      s0   




z!AudioQuantizationPipeline.forwardc                 C   s:   | j tjkr| j|d |d |d |d d}|S td)Nrd   re   r   r(   )data_path_and_name_and_typere   output_dir_v2r(   zmodel type is mismatching)	frameworkr   rk   r2   r;   )r3   r/   	sv_resultr)   r)   r7   ro      s   z'AudioQuantizationPipeline.run_inference)Nr   )NNN)N)__name__
__module____qualname____doc__r   r	   rf   intr+   rj   r   rV   r   r?   listr<   r.   r,   ro   __classcell__r)   r)   r5   r7   r      s2    
"

E')$rR   shutiltypingr   r   r   r   r   r   rE   rG   yamlmodelscope.metainfor   modelscope.modelsr	   modelscope.outputsr
   modelscope.pipelines.baser   modelscope.pipelines.builderr   "modelscope.utils.audio.audio_utilsr   r   modelscope.utils.constantr   r   modelscope.utils.loggerr   rg   __all__register_moduleaudio_quantizationaudio_quantization_inferencer   r)   r)   r)   r7   <module>   s(    