o
    
j                     @   s   d dl Z d dlmZmZmZ d dlZd dlZd dlZd dl	m
Z
 d dlmZ d dlmZ G dd deZdd	 Zd
efddZdS )    N)AnyDictList)Coqpit)check_config_and_model_args)BaseIDManagerc                       s   e Zd ZdZ		ddedef fddZedefd	d
Z	ede
fddZededefddZdeddfddZede
dedefddZde
dedefddZdeddfddZededd fddZ  ZS )LanguageManageruR  Manage the languages for multi-lingual 🐸TTS models. Load a datafile and parse the information
    in a way that can be queried by language.

    Args:
        language_ids_file_path (str, optional): Path to the metafile that maps language names to ids used by
        TTS models. Defaults to "".
        config (Coqpit, optional): Coqpit config that contains the language information in the datasets filed.
        Defaults to None.

    Examples:
        >>> manager = LanguageManager(language_ids_file_path=language_ids_file_path)
        >>> language_id_mapper = manager.language_ids
     Nlanguage_ids_file_pathconfigc                    s$   t  j|d |r| | d S d S )N)id_file_path)super__init__set_language_ids_from_config)selfr
   r   	__class__ J/home/kuhnn/.local/lib/python3.10/site-packages/TTS/tts/utils/languages.pyr      s   zLanguageManager.__init__returnc                 C   s   t t| j S N)lenlist
name_to_idkeysr   r   r   r   num_languages&   s   zLanguageManager.num_languagesc                 C   s   t | j S r   )r   r   r   r   r   r   r   language_names*   s   zLanguageManager.language_namescc                 C   sX   t i }| jD ]}d|v r||d  qtd|d  ddd ttt|D S )zSet language id from config.

        Args:
            c (Coqpit): Config

        Returns:
            Tuple[Dict, int]: Language ID mapping and the number of languages.
        languagezDataset namez has no language specified.c                 S   s   i | ]\}}||qS r   r   ).0ir    r   r   r   
<dictcomp>>       zBLanguageManager.parse_language_ids_from_config.<locals>.<dictcomp>)setdatasetsadd
ValueError	enumeratesortedr   )r   	languagesdatasetr   r   r   parse_language_ids_from_config.   s   

z.LanguageManager.parse_language_ids_from_configc                 C   s   |  || _dS )z]Set language IDs from config samples.

        Args:
            c (Coqpit): Config.
        N)r-   r   )r   r   r   r   r   r   @   s   z,LanguageManager.set_language_ids_from_configitems	parse_keyc                 C      t r   NotImplementedError)r.   r/   r   r   r   parse_ids_from_dataH   s   z#LanguageManager.parse_ids_from_datac                 C   r0   r   r1   )r   r.   r/   r   r   r   set_ids_from_dataL   s   z!LanguageManager.set_ids_from_data	file_pathc                 C   s   |  || j dS )zoSave language IDs to a json file.

        Args:
            file_path (str): Path to the output file.
        N)
_save_jsonr   )r   r5   r   r   r   save_ids_to_fileO   s   z LanguageManager.save_ids_to_filec                 C   s6   d}t | ddr| ddrt| jd}t| d}|S )zyInitialize the language manager from a Coqpit config.

        Args:
            config (Coqpit): Coqpit config.
        Nuse_language_embeddingTlanguage_ids_file)r
   )r   )r   getr   r9   )r   language_managerr   r   r   init_from_configW   s   
z LanguageManager.init_from_config)r	   N)__name__
__module____qualname____doc__strr   r   propertyintr   r   r   staticmethodr   r-   r   r   r3   r4   r7   r<   __classcell__r   r   r   r   r      s,    
r   c                 C   sP   t jt j| d}t j| d}t| j}||r|S ||r&|S dS )zFind the language_ids.json under the given path or the above it.
    Intended to band aid the different paths returned in restored and continued training.zlanguage_ids.jsonN)ospathjoindirnamefsspec
get_mapperfsexists)rG   path_restorepath_continuerL   r   r   r   _set_file_pathf   s   

rP   r.   c                    s   t dd | D  t   fdd D }t  fddD }d| t fdd|D }|t j| }t| S )Nc                 S   s   g | ]}|d  qS )r   r   )r!   itemr   r   r   
<listcomp>t       z1get_language_balancer_weights.<locals>.<listcomp>c                    s   g | ]}  |qS r   )indexr!   l)unique_language_namesr   r   rR   v   r$   c                    s"   g | ]}t t |kd  qS )r   )r   npwhererU   )r   r   r   rR   w   s   " g      ?c                    s   g | ]} | qS r   r   rU   )weight_languager   r   rR   z   rS   )	rX   arrayuniquetolistlinalgnormtorch
from_numpyfloat)r.   language_idslanguage_countdataset_samples_weightr   )r   rW   rZ   r   get_language_balancer_weightss   s   rf   )rF   typingr   r   r   rJ   numpyrX   r`   coqpitr   
TTS.configr   TTS.tts.utils.managersr   r   rP   r   rf   r   r   r   r   <module>   s    Y