o
    ±ƒðjK  ã                	   @   sº   d Z ddl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Z
dZe d¡Ze e¡Zdd	d
„Zddd„Z	ddedededdfdd„Zdedefdd„Zedkr[eƒ  dS dS )z2Command-line utility for downloading Piper voices.é    N)ÚPath)Úurlopenz±https://huggingface.co/rhasspy/piper-voices/resolve/main/{lang_family}/{lang_code}/{voice_name}/{voice_quality}/{lang_code}-{voice_name}-{voice_quality}{extension}?download=truezRhttps://huggingface.co/rhasspy/piper-voices/resolve/main/voices.json?download=truez[^(?P<lang_family>[^-]+)_(?P<lang_region>[^-]+)-(?P<voice_name>[^-]+)-(?P<voice_quality>.+)$Úreturnc                  C   sÈ   t  ¡ } | jdddd� | jddddd	d
� | jddddd� | jdddd� |  ¡ }tj|jr3tjntjd� |j	s@t
ƒ  dS |jrIt|jƒ}nt ¡ }|jddd� |j	D ]
}t|||jd� qWdS )zDownload Piper voices.ÚvoiceÚ*z(Name of voice like 'en_US-lessac-medium')ÚnargsÚhelpz--download-dirz--download_dirz
--data-dirz
--data_dirz>Directory to download voices into (default: current directory))r   z--force-redownloadz--force_redownloadÚ
store_truez=Force redownloading of voice files even if they exist already)Úactionr   z--debugzPrint DEBUG logs to console)ÚlevelNT)ÚparentsÚexist_ok)Úforce_redownload)ÚargparseÚArgumentParserÚadd_argumentÚ
parse_argsÚloggingÚbasicConfigÚdebugÚDEBUGÚINFOr   Úlist_voicesÚdownload_dirr   ÚcwdÚmkdirÚdownload_voicer   )ÚparserÚargsr   r   © r   ú—/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/piper/download_voices.pyÚmain   s@   ÿûüÿ
ÿr!   c                  C   s\   t  dt¡ ttƒ�} t | ¡}W d  ƒ n1 sw   Y  t| ¡ ƒD ]}t|ƒ q%dS )zList available voices and exit.z"Downloading voices.json file: '%s'N)	Ú_LOGGERr   ÚVOICES_JSONr   ÚjsonÚloadÚsortedÚkeysÚprint)ÚresponseÚvoices_dictr   r   r   r    r   B   s   
ÿ
ÿr   Fr   r   r   c              	   C   sÔ  |   ¡ } t | ¡}|std| › d�ƒ‚| d¡}|d | d¡ }| d¡}| d¡}|› d|› d|› �}||||d	œ}	||› d
� }
|sIt|
ƒr�tjddd
i|	¤Ž}t 	d||
¡ t
|ƒ�#}t|
dƒ�}t ||¡ W d  ƒ n1 suw   Y  W d  ƒ n1 s„w   Y  t 	d|
¡ ||› d� }|sœt|ƒrâtjdddi|	¤Ž}t 	d||¡ t
|ƒ�#}t|dƒ�}t ||¡ W d  ƒ n1 sÈw   Y  W d  ƒ n1 s×w   Y  t 	d|¡ t d| ¡ dS )z6Download a voice model and config file to a directory.zVoice 'zO' did not match pattern: <language>-<name>-<quality> like 'en_US-lessac-medium'Úlang_familyÚ_Úlang_regionÚ
voice_nameÚvoice_qualityú-)r+   Ú	lang_coder.   r/   z.onnxÚ	extensionz#Downloading model from '%s' to '%s'ÚwbNzDownloaded: '%s'z
.onnx.jsonz$Downloading config from '%s' to '%s'zDownloaded: %sr   )ÚstripÚVOICE_PATTERNÚmatchÚ
ValueErrorÚgroupÚ_needs_downloadÚ
URL_FORMATÚformatr"   r   r   ÚopenÚshutilÚcopyfileobjÚinfo)r   r   r   Úvoice_matchr+   r1   r.   r/   Ú
voice_codeÚformat_argsÚ
model_pathÚ	model_urlr)   Ú
model_fileÚconfig_pathÚ
config_urlÚconfig_filer   r   r    r   L   sN   

ÿ


ü
ÿ€ÿ
ÿ€ÿr   Úpathc                 C   s"   |   ¡ sdS |  ¡ jdkrdS dS )z+Return True if file needs to be downloaded.Tr   F)ÚexistsÚstatÚst_size)rI   r   r   r    r9   {   s
   r9   Ú__main__)r   N)F)Ú__doc__r   r$   r   Úrer=   Úpathlibr   Úurllib.requestr   r:   r#   Úcompiler5   Ú	getLoggerÚ__name__r"   r!   r   ÚstrÚboolr   r9   r   r   r   r    Ú<module>   s>    ÿÿ


,ÿÿÿÿ
þ/
ÿ