o
    WliK                  	   @   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)urlopenzhttps://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   O/home/kim/smarthome/.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)responseZ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|
rtjddd
i|	}t	d||
 t
|#}t|
d}t|| W d   n1 suw   Y  W d   n1 sw   Y  t	d|
 || d }|st|rtjdddi|	}t	d|| t
|#}t|d}t|| W d   n1 sw   Y  W d   n1 sw   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_Z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   Zvoice_matchr*   r/   r,   r-   Z
voice_codeZformat_argsZ
model_pathZ	model_urlr)   Z
model_fileZconfig_pathZ
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)r?   r   r   r    r7   {   s
   r7   __main__)r   N)F)__doc__r   r$   r   rer;   pathlibr   urllib.requestr   r8   r#   compiler3   	getLogger__name__r"   r!   r   strboolr   r7   r   r   r   r    <module>   s>    


,
/
