o �J�h�� @sddlmZmZmZmZddlZddlZddlZ ddl Z ddl Z ddl Z ddl ZddlmZddlZddlmZmZmZddlmZmZmZddlmZddlmZe�ZzddlmZm Z m!Z!m"Z"Wn"e#y�Z$ze�%d e&e$�j'�d e�(����WYdZ$[$ndZ$[$wwGd d �d �Z)dS) �)�Optional�Union�List�DictN)�datetime)�DEFAULT_PARAMETERS_CONFIG_PATH�UVR_MODELS_DIR�UVR_OUTPUT_DIR)� load_yaml� save_yaml�is_video)� load_audio)� get_logger)�MDX�Demucs� VrNetwork�MDXCzFailed to import uvr. BGM separation feature will not work. Please open an issue on GitHub if you encounter this error. Error: z: c@seZdZeefdeedeefdd�Z   dded eed efd d �Z ddd e � �fde ee jfded eed edede j dee je jeffdd�Zddde � �fdeded eed edede j deefdd�Zedd��Zdd�Zeded efdd��ZdS) �MusicSeparator� model_dir� output_dircCs�d|_|��|_gd�|_||_||_tj�|jd�}tj�|jd�}tj |dd�tj |dd�d|_ ddg|_ |j d|_ |j |_ d dd �|_dS) N)�cpu�cuda�xpu�mps� instrumental�vocalsT)�exist_okzUVR-MDX-NET-Inst_HQ_4zUVR-MDX-NET-Inst_3r���segment�split)�model� get_device�device�available_devicesrr�os�path�join�makedirs� audio_info�available_modelsZ default_model�current_model_size� model_config)�selfrrZinstrumental_output_dirZvocals_output_dir�r.�CC:\pinokio\api\whisper-webui.git\app\modules\uvr\music_separator.py�__init__s      �zMusicSeparator.__init__�UVR-MDX-NET-Inst_1Nr� model_namer#� segment_sizecCs>|dur|j}||_|dd�|_t||j|jd|jd�|_dS)z� Update model with the given model name Args: model_name (str): Model name. device (str): Device to use for the model. segment_size (int): Segment size for the prediction. NTr)�name�other_metadatar#�loggerr)r#r,rrr!)r-r2r#r3r.r.r/� update_model2s � �zMusicSeparator.update_modelF�audio� save_file�progress�returncCs�t|t�r,tj�|�d}}tj�|�\}} t|�r!t|�}d} nt� |�|_ |j j } nt � ��d�} d| ��d}}d} |dd�} |jdus\|j|ks\|j| ks\|jj | ks\|j|krn|dd d �|j|||d �| |j_ |dd d �|�|�} | d j| dj}}g}|r�tj�|jd |�d|���}tj�|jd|�d|���}tj||| dd�tj||| dd�|||g7}|||fS)a� Separate the background music from the audio. Args: audio (Union[str, np.ndarray]): Audio path or numpy array. model_name (str): Model name. device (str): Device to use for the model. segment_size (int): Segment size for the prediction. save_file (bool): Whether to save the separated audio to output path or not. progress (gr.Progress): Gradio progress indicator. Returns: A Tuple of np.ndarray: Instrumental numpy arrays. np.ndarray: Vocals numpy arrays. file_paths: List of file paths where the separated audio is saved. Return empty when save_file is False. z.wavi�>z %m%d%H%M%SzUVR-TrNrzInitializing UVR Model..)�desc)r2r#r3zbSeparating background music from the audio.. (It will only display 0% until the job is complete.) rrz -instrumentalz-vocals�WAV)�format)� isinstance�strr%r&�basename�splitextr r � torchaudio�infor)� sample_rater�now�strftimer!r+r,r#r7�Tr'r�sf�write)r-r8r2r#r3r9r:Zoutput_filename�extZorig_extrE� timestampr,�resultrr� file_pathsZinstrumental_output_pathZvocals_output_pathr.r.r/�separateLsJ   �      �    zMusicSeparator.separateT�filesc Cs8|j||d�|D]}|j||||||d�\}} } q | S)z�Separate the background music from the audio files. Returns only last Instrumental and vocals file paths to display into gr.Audio()�� model_sizer3)r8r2r#r3r9r:)�cache_parametersrO) r-rPr2r#r3r9r:� file_pathrrrNr.r.r/�separate_files�s �zMusicSeparator.separate_filescCs0tj��rdStj��rdStjj��rdSdS)Nrrrr)�torchr� is_availabler�backendsrr.r.r.r/r"�s   zMusicSeparator.get_devicecCsl|jdur |`d|_|jdkrtj��tj��|jdkr-tj��tj��tj��t � �d|_ dS)z(Offload the model and free up the memoryNrr) r!r#rVr� empty_cache�reset_max_memory_allocatedr�reset_accumulated_memory_stats�reset_peak_memory_stats�gc�collectr))r-r.r.r/�offload�s         zMusicSeparator.offloadrRcCs<tt�}|d}||d�}i|�|�}||d<t|t�dS)N�bgm_separationrQ)r rr )rRr3� cached_paramsZcached_uvr_paramsZuvr_params_to_cacher.r.r/rS�s� zMusicSeparator.cache_parameters)r1Nr)�__name__� __module__� __qualname__rr rr@r0�intr7�gr�Progressr�np�ndarray�bool�tuplerrOrU� staticmethodr"r_rSr.r.r.r/rs~�� ���� �� ������ �K������� �  �r)*�typingrrrr�numpyrhrC� soundfilerIr%rVr]�gradiorfr� traceback�modules.utils.pathsrrr �modules.utils.files_managerr r r Zmodules.diarize.audio_loaderr �modules.utils.loggerrr6Z uvr.modelsrrrr� Exception�e�warning�typerb� format_excrr.r.r.r/�<module>s8   �����
Memory