o
    
j                     @   s  d dl Z d dlZd dl 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 dlmZ 													d0d
dZedkre jdedZejdeddd ejdeddd ejdeddd ejdeddd ejdeddd ejddd	dd  ejd!ed"d	d ejd#d$d	dd  ejd%ed&dd ejd'ed(dd ejd)ed*dd ejd+ed,dd ejd-ed.dd e Zeejejejejejejejejej ej!ej"ej#ej$d/ dS dS )1    N)RawTextHelpFormatter)tqdm)load_config)BaseDatasetConfig)load_tts_samples)	save_file)SpeakerManagerFc                 C   s  t j o| }|d urt|}t|j| d\}}n#t }||_||_||_	|	d ur.|	|_
|
d ur5|
|_t|| d\}}|d u rE|}n|| }t| |||d}|jj}|d ur_|r_|j}ni }t|D ]=}|| }|d }|d }||v r~||| d< qe|d ur||jv r||}n||}i ||< ||| d< ||| d< qe|rtj	|rtj	|d}n|}tj	|dkrtjtj	|d	d
 t|| td| d S d S )N)
eval_split)encoder_model_pathencoder_config_pathd_vectors_file_pathuse_cuda
audio_fileaudio_unique_namename	embeddingspeakers.pth T)exist_okzSpeaker embeddings saved at:)torchcudais_availabler   r   datasetsr   	formatterdataset_namepathmeta_file_trainmeta_file_valr   encoder_configclass_name_key
embeddingsr   clip_idsget_embedding_by_clipcompute_embedding_from_cliposisdirjoindirnamemakedirsr   print)
model_pathconfig_pathoutput_pathold_speakers_file
old_appendconfig_dataset_pathformatter_namer   dataset_pathr   r   disable_cudano_evalr   	c_datasetmeta_data_trainmeta_data_evalsamplesencoder_managerr   speaker_mappingfields
class_namer   embedding_keyembeddmapping_file_path r?   M/home/kuhnn/.local/lib/python3.10/site-packages/TTS/bin/compute_embeddings.pycompute_embeddings   s`   

rA   __main__a~  Compute embedding vectors for each audio file in a dataset and store them keyed by `{dataset_name}#{file_path}` in a .pth file


        Example runs:
        python TTS/bin/compute_embeddings.py --model_path speaker_encoder_model.pth --config_path speaker_encoder_config.json  --config_dataset_path dataset_config.json

        python TTS/bin/compute_embeddings.py --model_path speaker_encoder_model.pth --config_path speaker_encoder_config.json  --formatter_name coqui --dataset_path /path/to/vctk/dataset --dataset_name my_vctk --meta_file_train /path/to/vctk/metafile_train.csv --meta_file_val /path/to/vctk/metafile_eval.csv
        )descriptionformatter_classz--model_pathzKPath to model checkpoint file. It defaults to the released speaker encoder.zXhttps://github.com/coqui-ai/TTS/releases/download/speaker_encoder_model/model_se.pth.tar)typehelpdefaultz--config_pathzNPath to model config file. It defaults to the released speaker encoder config.zVhttps://github.com/coqui-ai/TTS/releases/download/speaker_encoder_model/config_se.jsonz--config_dataset_pathz~Path to dataset config file. You either need to provide this or `formatter_name`, `dataset_name` and `dataset_path` arguments.z--output_pathz%Path for output `pth` or `json` file.r   z
--old_fileztThe old existing embedding file, from which the embeddings will be directly loaded for already computed audio clips.z--old_appendz~Append new audio clip embeddings to the old embedding file, generate a new non-duplicated merged embedding file. Default False
store_true)rF   rG   actionz--disable_cudazFlag to disable cuda.z	--no_evalz#Do not compute eval?. Default Falsez--formatter_namezVName of the formatter to use. You either need to provide this or `config_dataset_path`z--dataset_namezTName of the dataset to use. You either need to provide this or `config_dataset_path`z--dataset_pathzMPath to the dataset. You either need to provide this or `config_dataset_path`z--meta_file_trainzPath to the train meta file. If not set, dataset formatter uses the default metafile if it is defined in the formatter. You either need to provide this or `config_dataset_path`z--meta_file_valzPath to the evaluation meta file. If not set, dataset formatter uses the default metafile if it is defined in the formatter. You either need to provide this or `config_dataset_path`)
r-   r.   r/   r0   r   r1   r   r   r2   r3   )
NFNNNNNNFF)%argparser$   r   r   r   
TTS.configr   TTS.config.shared_configsr   TTS.tts.datasetsr   TTS.tts.utils.managersr   TTS.tts.utils.speakersr   rA   __name__ArgumentParserparseradd_argumentstrbool
parse_argsargsr*   r+   r,   old_filer.   r/   r0   r   r1   r   r   r2   r3   r?   r?   r?   r@   <module>   s    
W

