o
    *j#                     @   s   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 d d	lmZmZ d d
lmZ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)	asr_utils)	Pipelines)Model)
OutputKeys)Pipeline)	PIPELINES)generate_scp_from_urlupdate_local_model)
Frameworks	ModelFileTasks)
get_loggerSeparationPipeline)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	f d	ed
ededede
deeef fddZdeeef fddZdeeef 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 Z  ZS )r   aQ  Speech Separation Inference Pipeline
    use `model` to create a speech separation pipeline for prediction.

    Args:
        model: 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.

    Example:
        >>> from modelscope.pipelines import pipeline
        >>> pipeline = pipeline(
        >>>    task=Tasks.speech_separation, model='damo/speech_separation_mossformer_8k_pytorch')
        >>> audio_in = 'mix_speech.wav'
        >>> print(pipeline(audio_in))

    N   modelngpuc                    s   t  jdd|i| tj|tj}| |||| _ddl	m
} |jd| jd | jd || jd | jd | jd | jd	 | jd
 | jd | jd | jd | jd d|| _dS )zKuse `model` to create an speech separation pipeline for prediction
        r   r   )ss_inference_launchmode
batch_size	log_levelss_infer_configss_model_file
output_dirdtypeseednum_workersnum_spks
param_dict)r   r   r   r   r   r   r   r    r!   r"   r#   r$   N )super__init__ospathjoinr   ZCONFIGURATIONget_cmdcmdZ
funasr.binr   Zinference_launchfunasr_infer_modelscope)selfr   r   kwargsconfig_pathr   	__class__r%   v/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/audio/speech_separation_pipeline.pyr'   ,   s(   zSeparationPipeline.__init__audio_inaudio_fs
recog_typeaudio_formatr   r$   returnc                 K   sj  d| _ d| _|| _|| _d| _d}|dur|| jd< |dur#|| jd< t|tr1t|\| _ | _n-t|t	r=|| _ d| _n!ddl
}	ddl}
t||
jrRd| _ || _nt||	jr^d| _ || _|dure|| _|du sm|du r|tj| j ||d\| _| _| _ ttdr| j durt| j | j}|dur|| _|dur|| jd d< n| j| jd d< | j| j fi |}|S )	a  
        Decoding the input audios
        Args:
            audio_in('str' or 'bytes'):
                - A string containing a local path to a wav file
                - A string containing a local path to a scp
                - A string containing a wav url
                - A bytes input
            audio_fs('int'):
                frequency of sample
            recog_type('str'):
                recog type for wav file or datasets file ('wav', 'test', 'dev', 'train')
            audio_format('str'):
                audio format ('pcm', 'scp', 'kaldi_ark', 'tfrecord')
            output_dir('str'):
                output dir
            param_dict('dict'):
                extra kwargs
        Return:
            A dictionary of result or a list of dictionary of result.

            The dictionary contain the following keys:
            - **text** ('str') --The vad result.
        Nr   r$   r   )r4   r6   r7   sample_rate_checkingfsr5   )r4   
raw_inputsr6   r7   r5   r,   
isinstancestrr   bytesnumpytorchZTensorZndarrayr   Ztype_checkinghasattrr9   forward)r.   r4   r5   r6   r7   r   r$   r/   Zchecking_audio_fsr?   r@   outputr%   r%   r3   __call__G   sZ    




zSeparationPipeline.__call__c                 C   s8  t t| }tj|}tj||d d d }tj||d d d }|d d d }d }	tj|rPt|dd}
t	
|
}|
  d|v rP|d }	t|d d || |d	d	d
||d ddddd d d dd}|	d ur}d|	v r}|	d |d d< g d}|D ]}||v r||d ur|| ||< ||= q|S )Nr   Zmodel_configZss_model_namess_model_configr   zutf-8)encodingfrontend_confr   ERRORZfloat32r      )model_fsr5   )r   r   r   r   r   r   r   r    r!   r"   r#   r$   r:   r:   rJ   )r   r   r   r   r$   r"   r:   )jsonloadsopenreadr(   r)   dirnamer*   existsyamlZ	full_loadcloser   get)r.   r0   
extra_argsZ
model_pathZ	model_cfgZ	model_dirZss_model_pathrE   r   rG   config_filerootr,   Zuser_args_dictZ	user_argsr%   r%   r3   r+      sZ   
zSeparationPipeline.get_cmdinputsc                 K   s   |S )Nr%   )r.   rW   Zpost_paramsr%   r%   r3   postprocess   s   zSeparationPipeline.postprocessc                 K   s   t d t| jtr| jddg}nt| jtr| jddg}n| jdur&d}|| jd< | j| jd< | j| jd< | j| jfi |}|S )	zDecoding
        z Speech Separation Processing ...Zspeechr>   soundNname_and_typer;   r4   )	loggerinfor<   r4   r>   r=   r;   r,   run_inference)r.   r4   r/   Zdata_cmd	ss_resultr%   r%   r3   rB      s   


zSeparationPipeline.forwardc                 K   sL   g }| j tjkr"| jd|d |d |d |d |d d|}|S td)	NrZ   r;   r   r:   r$   )Zdata_path_and_name_and_typer;   Zoutput_dir_v2r:   r$   zmodel type is mismatchingr%   )Z	frameworkr   r@   r-   
ValueError)r.   r,   r/   r^   r%   r%   r3   r]      s   
z SeparationPipeline.run_inference)Nr   )NNNNN)__name__
__module____qualname____doc__r   r
   r=   intr'   r>   dictr   r   rD   r+   rX   rB   r]   __classcell__r%   r%   r1   r3   r      sF    



R5

")%r(   typingr   r   r   r   r   r   rK   rQ   Zfunasr.utilsr   Z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   r   Zmodelscope.utils.loggerr   r[   __all__Zregister_moduleZspeech_separationZfunasr_speech_separationr   r%   r%   r%   r3   <module>   s&    