o
    *j                     @   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 )SDPNPipelineaY  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 official model id
        r   ZyesOrno_thrN )super__init__r   model_configZother_configconfigthr)selfr   kwargs	__class__r   ~/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/audio/speaker_verification_sdpn_pipeline.pyr   (   s   

zSDPNPipeline.__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   Zoutputsr   r   r   __call__2   s   


zSDPNPipeline.__call__inputsc                 C   s&   |  |d }|  |d }||dS )NZdata1Zdata2)emb1emb2)r   )r   r%   r&   r'   r   r   r   r"   A   s   
zSDPNPipeline.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   ZSCOREZTEXT)r   r%   ZscoreZansr   r   r   r#   G   s   

zSDPNPipeline.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.Zfloat32)Zdtyper   Z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   Z	model_cfgtorchZ
from_numpyZ	unsqueeze)r   r%   Zpreprocess_paramsoutputiZ
file_bytesdatafsr   r   r   r!   Q   s6   zSDPNPipeline.preprocessr&   r'   c                 C   sB   t |jdkrt |jdksJ tjjddd}|||}| S )Nr-   r   gư>)dimeps)r.   r6   r7   nnZCosineSimilarityitem)r   r&   r'   cosZcosiner   r   r   r+   i   s    
z#SDPNPipeline.compute_cos_similarity)N)__name__
__module____qualname____doc__r	   r   r   r1   floatr   r   r$   r"   r#   r!   r7   ZTensorr+   __classcell__r   r   r   r   r      s*    

""



r   )r4   typingr   r   r   r   Z	soundfiler3   r7   Zmodelscope.fileior   Zmodelscope.metainfor   Zmodelscope.outputsr   Zmodelscope.pipelines.baser	   r
   Zmodelscope.pipelines.builderr   Zmodelscope.utils.constantr   Zmodelscope.utils.loggerr   loggerZregister_moduleZspeaker_verificationZspeaker_verification_sdpnr   r   r   r   r   <module>   s"   