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
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 offical model id
        r   anchor_sizeN )super__init__r   model_configr   )selfr   kwargs	__class__r   o/home/ubuntu/.local/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      )axis)len
isinstancenpndarrayshaper   zerosonesstackflattenlisttorch
from_numpy	unsqueezefloat
preprocessforwardpostprocess)r   r   r   
output_resembd1embd2embd3embd4anchorsoutputpr   r   r   __call__4   s2   


z&SpeakerChangeLocatingPipeline.__call__inputr6   c                 C   s   |  ||}|S )N)r   )r   r:   r6   r7   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diffargmaxnumpyroundr   TEXT	Exception)r   r:   predictr   r   r   r0   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)Nfloat32)dtyper   r   sample_ratez0The sample rate of audio is not %d, resample it.rate)effects)int16int32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
torchaudiosox_effectsapply_effects_tensorr"   r#   rF   astype
ValueError)r   r:   
file_bytesdatafsr   r   r   r.   e   s>   






z(SpeakerChangeLocatingPipeline.preprocess)NF)__name__
__module____qualname____doc__r	   r   r   rM   r"   r#   r)   r9   r*   Tensorr/   r0   r.   __classcell__r   r   r   r   r      s    
"&)!rP   typingr   r   r   r   r?   r"   	soundfilerO   r*   rT   modelscope.fileior   modelscope.metainfor   modelscope.outputsr   modelscope.pipelines.baser	   r
   modelscope.pipelines.builderr   modelscope.utils.constantr   modelscope.utils.loggerr   rR   __all__register_modulespeaker_diarizationspeaker_change_locatingr   r   r   r   r   <module>   s&   