
    (HJj:'                     ~   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m	Z	m
Z
mZ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 i ddddd	d	d
dddddddddddddddddddddddddddddddddd d!d!d"d#
Zd$Zg d%Zd&e	e   fd'Zd(ed)e	e   d&eeef   fd*Z	 	 dAd+ed,ed-ed.ed&ej@                  f
d/Z!	 	 dAd+eeef   d,ed-ed.ed&ej@                  f
d0Z"	 dBd+ed,ed.ed&eej@                  e#f   fd1Z$d2ed3ed4efd5Z%	 	 	 	 	 	 	 	 	 	 	 dCd4ed6ed7ed8e
e&   d9e
e&   d:ed3ed;e
e   d<ed=ed>e
e   d?efd@Z'y)D    N)Path)dedent)AnyListOptionalTupleUnion)tree_flatten)base_load_modelget_model_classget_model_pathload_config	qwen3_ttsoutettssparkmarvissesamecsmvoxcpmz	voxcpm1.5voxcpm2vibevoice_streaming	vibevoicechatterbox_turbosoprano	bailingmmkitten
kitten_ttsecho_ttsfish_qwen3_omniirodori_ttsvoxtral_tts
kugelaudiolongcat_audioditdramabox	omnivoicemelottsmoss_tts_nanomoss_ttshiggs_audio_v3)
r"   audioditlongcatzdramabox-ttsr%   r&   r'   moss_tts_delaymoss_tts_localhiggs_multimodal_qwen3   )float16bfloat16float32returnc                  <   t        t              j                  dz  } g }| j                         rm| j	                         r]| j                         D ]J  }|j	                         s|j                  j                  d      r0|j                  |j                         L |S )z
    Get a list of all available TTS model types by scanning the models directory.

    Returns:
        List[str]: A list of available model type names
    models__)	r   __file__parentexistsis_diriterdirname
startswithappend)
models_diravailable_modelsitems      ]/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/mlx_audio/tts/utils.pyget_available_modelsrC   5   s|     h&&1Jz002&&(D{{}TYY%9%9$%? ''		2 )     
model_type
model_namec                 (    t        | |dt              S )a  
    Retrieve the model architecture module based on the model type and name.

    This function attempts to find the appropriate model architecture by:
    1. Checking if the model_type is directly in the MODEL_REMAPPING dictionary
    2. Looking for partial matches in segments of the model_name

    Args:
        model_type (str): The type of model to load (e.g., "outetts").
        model_name (List[str]): List of model name components that might contain
                               remapping information.

    Returns:
        Tuple[module, str]: A tuple containing:
            - The imported architecture module
            - The resolved model_type string after remapping

    Raises:
        ValueError: If the model type is not supported (module import fails).
    tts)rE   rF   categorymodel_remapping)r   MODEL_REMAPPING)rE   rF   s     rB   get_model_and_argsrL   G   s    * '	 rD   
model_pathlazystrictkwargsc           	      ,    t        d| dt        ||d|S )aC  
    Load and initialize the model from a given path.

    Args:
        model_path (Path): The path to load the model from.
        lazy (bool): If False eval the model parameters to make sure they are
            loaded in memory before returning, otherwise they will be loaded
            when needed. Default: ``False``

    Returns:
        nn.Module: The loaded and initialized model.

    Raises:
        FileNotFoundError: If the weight files (.safetensors) are not found.
        ValueError: If the model class or args class are not found or cannot be instantiated.
    rH   )rM   rI   rJ   rN   rO    )r   rK   rM   rN   rO   rP   s       rB   
load_modelrT   d   s/    ,  '  rD   c                      t        | f||d|S )ac  
    Load a text-to-speech model from a local path or HuggingFace repository.

    This is the main entry point for loading TTS models. It automatically
    detects the model type and initializes the appropriate model class.

    Args:
        model_path: The local path or HuggingFace repo ID to load from.
        lazy: If False, evaluate model parameters immediately.
        strict: If True, raise an error if any weights are missing.
        **kwargs: Additional keyword arguments such as `revision` and
            `force_download`.

    Returns:
        nn.Module: The loaded and initialized model.

    )rN   rO   )rT   rS   s       rB   loadrV      s    . jEtFEfEErD   c                 <    t        | |fi |}t        | fi |}||fS N)rT   r   )rM   rN   rP   modelconfigs        rB   fetch_from_hubr[      s.     z4262E.v.F&=rD   pathupload_repohf_pathc                 
   ddl }ddlm}m}m} ddlm} |j                  |      }|j                  j                  dgn|j                  j                  dgz   |j                  _	        t        d| d| d	| d
| d| d| d      |_        |j                  |j                  j                  | d              |j                           |       }	|	j!                  |d       |	j#                  | |d       t%        d| d       y)z
    Uploads the model to Hugging Face hub.

    Args:
        path (str): Local path to the model.
        upload_repo (str): Name of the HF repo to upload to.
        hf_path (str): Path to the original Hugging Face model.
    r   N)HfApi	ModelCardlogging   )__version__mlxz
        # z7
        This model was converted to MLX format from [`z`](https://huggingface.co/z) using mlx-audio version **zF**.
        Refer to the [original model card](https://huggingface.co/z) for more details on the model.
        ## Use with mlx

        ```bash
        pip install -U mlx-audio
        ```

        ```bash
        python -m mlx_audio.tts.generate --model z3 --text "Describe this image."
        ```
        z	README.mdT)repo_idexist_okrY   )folder_pathrf   	repo_typez0Upload successful, go to https://huggingface.co/z for details.)oshuggingface_hubr`   ra   rb   versionrd   rV   datatagsr   textsaver\   joinset_verbosity_infocreate_repoupload_folderprint)
r\   r]   r^   rj   r`   ra   rb   rd   cardapis
             rB   upload_to_hubrx      s    99%>>'"D $		 6eWDIINNeW<TDIIN- 77>i?YZaYbb~  @K  L LCCJ) L2 3> ?		DI  	IIbggll4-.G 
'COOK$O7  
 
<[M
WXrD   mlx_pathquantizeq_group_sizeq_bitsdtyperevision
dequantizetrust_remote_codequant_predicateq_modec                 b   ddl m} ddlm}m}m}m} t        d       t        | |      }t        |d|	      \  }}t        |
t              r	 ||
|      }
t        |dd	       fd
|
}
n|
fd}
t        t        |j                                     }||j!                  dd       }|t"        v rNt        d|       t        t$        |      }|j'                         D ci c]  \  }}||j)                  |       }}}|r|rt+        d      |rDt        d       |j-                  t/        |j'                                       |||||||
      \  }}|r5t        d        ||      }t        t        |j                                     }t        |t              rt1        |      }|j3                  dd       dD ]  }t5        j4                  t        ||z              }|D ]  }t7        j8                  ||        t5        j4                  t        |dz  |z        d      }|D ]T  }t1        |      j;                  |      }||j<                  z  }|j3                  dd       t7        j8                  ||       V   |||d        |||dz         |t?        |||        y y c c}}w )Nr   )mixed_quant_predicate_builder)dequantize_modelquantize_modelsave_config
save_modelz[INFO] Loading)r~   T)rN   r   model_quant_predicatec                      y)NTrR   )pms     rB   <lambda>zconvert.<locals>.<lambda>   s    QUrD   c                     t        |d      xr8 |j                  j                  d   dz  dk(  xr t        |d      xr	  | |      S )Nweight@   r   to_quantized)hasattrr   shape)r   r   r   s     rB   base_quant_requirementsz(convert.<locals>.base_quant_requirements   sQ    Ax  ,r"R'1,,>*, &a+		
rD   c                 ,     | |      xr	  | |      S rX   rR   )r   r   r   original_predicates     rB   r   zconvert.<locals>.<lambda>   s    #Aq)F.@A.FFrD   torch_dtypez[INFO] Using dtype:z/Choose either quantize or dequantize, not both.z[INFO] Quantizing)moder   z[INFO] Dequantizing)parentsrg   )z*.pyz*.jsonz*.wavz*.ptz*.safetensorsz*.yamlz*.txtz*.jinjaz**)	recursive)donate_modelzconfig.json)config_path) mlx_lm.convertr   mlx_lm.utilsr   r   r   r   ru   r   r[   
isinstancestrgetattrdictr
   
parametersgetMODEL_CONVERSION_DTYPESmxitemsastype
ValueErrorload_weightslistr   mkdirglobshutilcopyrelative_tor8   rx   ) r^   ry   rz   r{   r|   r}   r]   r~   r   r   r   r   r   r   r   r   r   rM   rY   rZ   weightskvpatternfilesfilesubdir_filesrel_pathdest_dirr   r   r   s                                 @@@rB   convertr      s    =VV	
(;J"1BME6 /3'7O $E+BDUV
 1,
 < 0 0 234G}

=$/''#U+E"29--/B/$!Q1ahhuo%/BJJKK!"401(+
 #$ '|E$4$4$678(C > NN4$N/	 		#j7234DKKh'  yyZ$%6%@!ATR DDz--j9H(//1HNN4$N7KKh' !!	. xT2H}$<=hW5 q Cs   %J+)FT)F)	mlx_modelFNNNNNFTNaffine)(r   rb   r   pathlibr   textwrapr   typingr   r   r   r   r	   mlx.corecorer   mlx.nnnn	mlx.utilsr
   mlx_audio.utilsr   r   r   r   rK   MAX_FILE_SIZE_GBr   r   rC   rL   boolModulerT   rV   r   r[   rx   intr   rR   rD   rB   <module>r      s        4 4   " y W h	
 
8 h  y ; * y  l 
 (  =!" =#$ "!$  .7:  < d3i $3 DI %S/ > 
  	
 YYD Fc4i F
F F 	F
 YYF6 $) 47
299d?,Y ,Y# ,Y ,Yb  "& ""%)o6o6o6 o6 3-	o6
 SMo6 o6 o6 smo6 o6 o6 c]o6 o6rD   