o
    ॵi                     @   s   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	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 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e	ee
f de	ee
f fddZdee de	ee
f fddZdejdejdefddZ  ZS )RDINO_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 | _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)selfr   kwargs	__class__r   r/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/audio/speaker_verification_rdino_pipeline.pyr   (   s   

zRDINO_Pipeline.__init__N	in_audiosr   returnc                 C   sR   |d ur|| _ | j dk s| j dkrtd| j  | |}| |}| |}|S )N   zImodelscope error: the thr value should be in [-1, 1], but found to be %f.)r   
ValueError
preprocessforwardpostprocess)r   r   r   outputsr   r   r   __call__2   s   


zRDINO_Pipeline.__call__inputsc                 C   s&   |  |d }|  |d }||dS )Ndata1data2)emb1emb2)r   )r   r(   r+   r,   r   r   r   r$   A   s   
zRDINO_Pipeline.forwardc                 C   sB   |  |d |d }t|d}|| jkrd}nd}tj|tj|iS )Nr+   r,      yesno)compute_cos_similarityroundr   r   SCORETEXT)r   r(   scoreansr   r   r   r%   G   s   

zRDINO_Pipeline.postprocessc                 K   s   t |dkr
tdi }tt |D ]O}t|| tr\t|| }tjt	|dd\}}t |j
dkr=|d d df }|| jd krMtd| jd  t|d|d|d	  < qtd
| |S )N   z5modelscope error: Two input audio files are required.float32)dtyper   sample_ratez3modelscope error: Only support %d sample rate fileszdata%dr!   zPmodelscope error: The input type is temporarily restricted to audio file address)lenr"   range
isinstancestrr   readsfioBytesIOshaper   	model_cfgtorch
from_numpy	unsqueeze)r   r(   preprocess_paramsoutputi
file_bytesdatafsr   r   r   r#   Q   s6   zRDINO_Pipeline.preprocessr+   r,   c                 C   sB   t |jdkrt |jdksJ tjjddd}|||}| S )Nr6   r!   gư>)dimeps)r:   rB   rD   nnCosineSimilarityitem)r   r+   r,   coscosiner   r   r   r0   i   s    
z%RDINO_Pipeline.compute_cos_similarity)N)__name__
__module____qualname____doc__r	   r   r   r=   floatr   r   r'   r$   r%   r#   rD   Tensorr0   __classcell__r   r   r   r   r      s*    

""



r   )r@   typingr   r   r   r   	soundfiler?   rD   modelscope.fileior   modelscope.metainfor   modelscope.outputsr   modelscope.pipelines.baser	   r
   modelscope.pipelines.builderr   modelscope.utils.constantr   modelscope.utils.loggerr   loggerregister_modulespeaker_verificationspeaker_verification_rdinor   r   r   r   r   <module>   s"   