o
    ॵim                     @   s   d dl Z d dlZd dlmZmZmZmZ d dlZd dl	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mZ d dlmZ d dlmZ d d	lmZ e Zejejejd
G dd deZ dS )    N)AnyDictListUnion)File)	Pipelines)
OutputKeys)
InputModelPipeline)	PIPELINES)Tasks)
get_logger)module_namec                	       s   e Zd ZdZdef fddZ			ddeeje	f de
d	ed
efddZde	fddZ	ddejdeeje	f fddZdeeje	f fddZdeejejf deejejf defddZ  ZS )ERes2Net_PipelineaY  Speaker Verification Inference Pipeline
    use `model` to create a Speaker Verification pipeline.

    Args:
        model (SpeakerVerificationPipeline): A model instance, or a model local dir, or a model id in the model hub.
        kwargs (dict, `optional`):
            Extra kwargs passed into the pipeline's constructor.
    Example:
    >>> from modelscope.pipelines import pipeline
    >>> from modelscope.utils.constant import Tasks
    >>> p = pipeline(
    >>>    task=Tasks.speaker_verification, model='damo/speech_ecapa-tdnn_sv_en_voxceleb_16k')
    >>> print(p([audio_1, audio_2]))

    modelc                    s@   t  jdd|i| | jj| _| jj| _| jd | _i | _dS )zuse `model` to create a speaker verification pipeline for prediction
        Args:
            model (str): a valid offical model id
        r   yesOrno_thrN )super__init__r   model_configother_configconfigthr	save_dict)selfr   kwargs	__class__r   u/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/audio/speaker_verification_eres2net_pipeline.pyr   +   s
   


zERes2Net_Pipeline.__init__NF	in_audiossave_dir
output_embr   c                 C   sx   |d ur|| _ | j dk s| j dkrtd| j  | |}| |}| |||}|r:|| jd< | | jd< | jS |S )N   zImodelscope error: the thr value should be in [-1, 1], but found to be %f.outputsembs)r   
ValueError
preprocessforwardpostprocessr   numpy)r   r   r    r!   r   wavsr%   r$   r   r   r   __call__6   s    


zERes2Net_Pipeline.__call__inputsc                 C   s,   g }|D ]
}| | | qt|}|S N)appendr   torchcat)r   r-   r%   xr   r   r   r(   K   s
   
zERes2Net_Pipeline.forwardc           
   	   C   s   t |d tr8|d ur8tj|dd t|D ]!\}}tj|dtj|ddd  }t	
|||   qt|dkra| |d |d }t|d}|| jkrUd	}nd
}tj|tj|i}	|	S tjdi}	|	S )Nr   T)exist_okz%s.npy.r#         yesnozNo similarity score output)
isinstancestrosmakedirs	enumeratepathjoinbasenamersplitnpsaver*   lencompute_cos_similarityroundr   r   SCORETEXT)
r   r-   r   r    ip	save_pathscoreansoutputr   r   r   r)   R   s&   


zERes2Net_Pipeline.postprocessc                 C   sD  g }t t|D ]}t|| tret|| }tjt|dd\}}t|j	dkr3|d d df }t
|d}|| jd kr_td| jd   tjj||dt| jd ggd\}}|d}n5t|| tjrt|| j	d	kszJ d
|| }|jdv r|d d}n|d}t
|}ntd|| q|S )Nfloat32)dtyper5   r   sample_ratez0The sample rate of audio is not %d, resample it.rate)effectsr#   z.modelscope error: Input array should be [N, T])int16int32int64i   zOmodelscope error: The input type is restricted to audio address and nump array.)rangerD   r9   r:   r   readsfioBytesIOshaper0   
from_numpy	unsqueezer   loggerwarning
torchaudiosox_effectsapply_effects_tensorsqueezerB   ndarrayrP   astyper&   r/   )r   r-   rN   rI   
file_bytesdatafsr   r   r   r'   m   sP   


zERes2Net_Pipeline.preprocessemb1emb2returnc                 C   s   t |tjrt|}t |tjrt|}t|jr |d}t|jr*|d}t|jdkr8t|jdks:J tjj	ddd}|||}|
 S )Nr   r5   r#   gư>)dimeps)r9   rB   re   r0   r]   rD   r\   r^   nnCosineSimilarityitem)r   rj   rk   coscosiner   r   r   rE      s   





 
z(ERes2Net_Pipeline.compute_cos_similarity)NFNr.   )__name__
__module____qualname____doc__r	   r   r   rB   re   listr:   boolfloatr,   r(   r0   Tensorr)   r'   rE   __classcell__r   r   r   r   r      s8    


&r   )!rZ   r;   typingr   r   r   r   r*   rB   	soundfilerY   r0   ra   modelscope.fileior   modelscope.metainfor   modelscope.outputsr   modelscope.pipelines.baser	   r
   modelscope.pipelines.builderr   modelscope.utils.constantr   modelscope.utils.loggerr   r_   register_modulespeaker_verificationspeaker_verification_eres2netr   r   r   r   r   <module>   s(   