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mZ d dl	m
Z
 d dlmZ ddlmZ ddlmZ d	gZejejejd
G dd	 d	eZdS )    N)AnyDictListUnion)Preprocessors)Model)Fields   )Preprocessor)PREPROCESSORS
WavToLists)module_namec                   @   s   e Zd ZdZdd Zdedeee ee	f de
eef fddZde
eef dee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ef de
eef fddZdS )r   z'generate audio lists file from wav
    c                 C   s   d S )N )selfr   r   P/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/preprocessors/kws.py__init__   s   zWavToLists.__init__modelaudio_inreturnc                 C   s   || _ | | j  |}|S )a  Call functions to load model and wav.

        Args:
            model (Model): model should be provided
            audio_in (Union[List[str], str, bytes]):
                audio_in[0] is positive wav path, audio_in[1] is negative wav path;
                audio_in (str) is positive wav path;
                audio_in (bytes) is audio pcm data;
        Returns:
            Dict[str, Any]: the kws result
        )r   forward)r   r   r   outr   r   r   __call__   s   zWavToLists.__call__c                 C   s   t |d dksJ dtj|d sJ d| }dd l}|j|}|dv s2J d| d||d< |d	kr?||d
< n|dkrG||d< |dv rQ|d |d
< |dv r[|d |d< | |}| 	|}|S )Nconfig_pathr   z&preprocess model[config_path] is emptyzmodel config.yaml is absent)wavpcmpos_testsetsneg_testsetsrocz	kws_type z$ is invalid, please check audio datakws_typer   pos_wav_pathr   pos_datar   r   r   r   r	   neg_wav_path)
lenospathexistscopykws_util.commoncommontype_checkingread_configgenerate_wav_lists)r   r   r   inputskws_utilr   r   r   r   r   r   ,   s<   




zWavToLists.forwardr.   c                 C   s   t j|d sJ dt|d dd}t|}|  |d |d< t j|d |d |d< |d |d< t j|d |d |d	< |d
 |d
< |S )z:read and parse config.yaml to get all model files
        r   z%model config yaml file does not existzutf-8)encodingcfg_filemodel_workspacecfg_file_pathkeyword_grammarkeyword_grammar_pathsample_rate)r%   r&   r'   openyaml	full_loadclosejoin)r   r.   config_filerootr   r   r   r,   J   s$   
zWavToLists.read_configc                 C   s  ddl }|d dkr!g }|d }|| ||d< d|d< d|d	< |d d
kr4dg|d< d|d< d|d	< |d dv r`g }|d }|j||}||d< t|}||d< |dkr\||d	< nd|d	< |d dv rg }|d }|j||}||d< t|}||d< |dkr||d< |S d|d< |S )zassemble wav lists
        r   Nr   r   r   pos_wav_listr	   pos_wav_countpos_num_threadr   pcm_datar!      r"   r#   neg_wav_listneg_wav_countneg_num_thread)r)   appendr*   recursion_dir_all_wavr$   )r   r.   r/   wav_listwave_scp_contentwav_dir
list_countr   r   r   r-   _   sD   


zWavToLists.generate_wav_listsN)__name__
__module____qualname____doc__r   r   r   r   strbytesr   r   r   r   r,   r-   r   r   r   r   r      s    



"&)r%   typingr   r   r   r   r8   modelscope.metainfor   modelscope.models.baser   modelscope.utils.constantr   baser
   builderr   __all__register_moduleaudiowav_to_listsr   r   r   r   r   <module>   s   