o
    XiZ                     @   s   d Z ddlZddlZddlZddlZddlmZm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mZmZmZmZmZmZmZ g d
ZdddddZdddddZdededeeef fddZdee fddZ	ddededefddZdS )zRModified from
    https://github.com/openai/whisper/blob/main/whisper/__init__.py
    N)ListUnion)tqdm)S3TokenizerV2)S3TokenizerV3   )S3Tokenizer)
load_audiolog_mel_spectrogrammake_non_pad_maskmask_to_biasmerge_tokenized_segments
onnx2torchonnx2torch_v3padding)r	   r
   r   r   r   r   r   r   z[https://www.modelscope.cn/models/iic/cosyvoice-300m/resolve/master/speech_tokenizer_v1.onnxz`https://www.modelscope.cn/models/iic/CosyVoice-300M-25Hz/resolve/master/speech_tokenizer_v1.onnxz\https://www.modelscope.cn/models/iic/CosyVoice2-0.5B/resolve/master/speech_tokenizer_v2.onnxzmhttps://www.modelscope.cn/models/FunAudioLLM/Fun-CosyVoice3-0.5B-2512/resolve/master/speech_tokenizer_v3.onnx)speech_tokenizer_v1speech_tokenizer_v1_25hzspeech_tokenizer_v2_25hzspeech_tokenizer_v3_25hz@23b5a723ed9143aebfd9ffda14ac4c21231f31c35ef837b6a13bb9e5488abb1e@56285ddd4a83e883ee0cb9f8d69c1089b53a94b1f78ff7e4a0224a27eb4cb486@d43342aa12163a80bf07bffb94c9de2e120a8df2f9917cd2f642e7f4219c6f71@23236a74175dbdda47afc66dbadd5bcb41303c467a57c261cb8539ad9db9208dnamerootreturnc              
   C   s  t j|dd t|  }t|  }t j||  d}t j|r,t j|s,t| dt j|r^t	|d}|
 }W d    n1 sFw   Y  t| |krV|S t| d tj|T}t	|d?}tt| dd	d
dddd}		 |
d}
|
sn||
 |	t|
 q~W d    n1 sw   Y  W d    n1 sw   Y  W d    n1 sw   Y  t	|d
 }t| |krtd|S )NT)exist_okz.onnxz! exists and is not a regular filerbzH exists, but the SHA256 checksum does not match; re-downloading the filewbzContent-LengthP   iBi   zDownloading onnx checkpoint)totalncolsunit
unit_scaleunit_divisordesci    zeModel has been downloaded but the SHA256 checksum does not not match. Please retry loading the model.)osmakedirs_SHA256S_MODELSpathjoinexistsisfileRuntimeErroropenreadhashlibsha256	hexdigestwarningswarnurllibrequesturlopenr   intinfogetwriteupdatelen)r   r   expected_sha256urldownload_targetfmodel_bytessourceoutputloopbuffer rI   H/home/ubuntu/.local/lib/python3.10/site-packages/s3tokenizer/__init__.py	_downloadB   sb   


 rK   c                   C   s   t t S )z%Returns the names of available models)listr*   keysrI   rI   rI   rJ   available_modelsr   s   rN   download_rootc                 C   s   |du rt jt jdd}t jt d|d}| tv r$t| |}nt j| r-| }ntd|  dt	  d| v rAt
| }nd	| v rJt| }nt| }|| |S )
a  
    Load a S3Tokenizer ASR model

    Parameters
    ----------
    name : str
        one of the official model names listed by
        `s3tokenizer.available_models()`, or path to a model checkpoint
         containing the model dimensions and the model state_dict.
    download_root: str
        path to download the model files; by default,
        it uses "~/.cache/s3tokenizer"

    Returns
    -------
    model : S3Tokenizer
        The S3Tokenizer model instance
    N~z.cacheXDG_CACHE_HOMEs3tokenizerzModel z not found; available models = v3v2)r'   r+   r,   
expandusergetenvr*   rK   r.   r/   rN   r   r   r   init_from_onnx)r   rO   defaultcheckpoint_filemodelrI   rI   rJ   
load_modelw   s&   


r[   )N) __doc__r2   r'   r7   r5   typingr   r   r   s3tokenizer.model_v2r   s3tokenizer.model_v3r   rZ   r   utilsr	   r
   r   r   r   r   r   r   __all__r*   r)   strbytesrK   rN   r[   rI   rI   rI   rJ   <module>   s@   (0