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
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d
g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SpeakerChangeLocatingPipeline)module_namec                       s   e Zd ZdZdef fddZ		ddeeej	f dee
ej	f fd	d
ZdejdejfddZdejfddZdeeej	f dejfddZ  ZS )r   a_  Speaker Change Locating Inference Pipeline
    use `model` to create a speaker change Locating pipeline.

    Args:
        model (SpeakerChangeLocatingPipeline): 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_diarization, model='damo/speech_campplus-transformer_scl_zh-cn_16k-common')
    >>> print(p(audio))

    modelc                    s0   t  jdd|i| | jj| _| jd | _dS )zuse `model` to create a speaker change Locating pipeline for prediction
        Args:
            model (str): a valid official model id
        r   anchor_sizeN )super__init__r   model_configr   )selfr   kwargs	__class__r   |/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/audio/speaker_change_locating_pipeline.pyr   +   s   
z&SpeakerChangeLocatingPipeline.__init__NFaudioembdsc                 C   s>  |d ur3t |dksJ t|d tjrt|d tjsJ |d j| jfkr0|d j| jfks2J n<t| jd }t| j| jd  }t| jd }t| j| jd  }tj||gdd	 tj||gdd	 g}t|t
r{tj|dd}t|d }| |}	| |	|}	| |	\}	}
|r|	|
fS |	S )N   r      )Zaxis)len
isinstancenpndarrayshaper   ZzerosZonesstackflattenlisttorch
from_numpy	unsqueezefloat
preprocessforwardpostprocess)r   r   r   Z
output_resZembd1Zembd2Zembd3Zembd4anchorsoutputpr   r   r   __call__4   s2   


z&SpeakerChangeLocatingPipeline.__call__inputr.   c                 C   s   |  ||}|S )N)r   )r   r2   r.   r/   r   r   r   r,   V   s   z%SpeakerChangeLocatingPipeline.forwardc                 C   sr   t t |d }z|d d d d }t|d}tjd| di|fW S  ty8   tjdid f Y S w )	Nr   g{Gz?g{Gz?r   zThe change point is at zs.zNo change point is found.)	r!   wherediffZargmaxnumpyroundr   ZTEXT	Exception)r   r2   Zpredictr   r   r   r-   Z   s   
z)SpeakerChangeLocatingPipeline.postprocessreturnc                 C   s  t |trSt|}tjt|dd\}}t|jdkr%|d d df }t	
|d}|| jd krQtd| jd   tjj||dt| jd ggd\}}|S t |tjr~|jd	v rf|d
 d}n|d}t	
|}t|jdkr||d}|S td)NZfloat32)dtyper   r   Zsample_ratez0The sample rate of audio is not %d, resample it.Zrate)Zeffects)Zint16Zint32Zint64i   r   zUmodelscope error: The input type is restricted to audio file address and numpy array.)r    strr   readsfioBytesIOr   r#   r'   r(   r)   r   loggerwarning
torchaudioZsox_effectsZapply_effects_tensorr!   r"   r:   Zastype
ValueError)r   r2   Z
file_bytesdatafsr   r   r   r+   e   s>   






z(SpeakerChangeLocatingPipeline.preprocess)NF)__name__
__module____qualname____doc__r	   r   r   r;   r!   r"   r&   r1   r'   ZTensorr,   r-   r+   __classcell__r   r   r   r   r      s    
"&)!r>   typingr   r   r   r   r6   r!   Z	soundfiler=   r'   rB   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   r@   __all__Zregister_moduleZspeaker_diarizationZspeaker_change_locatingr   r   r   r   r   <module>   s&   