CoolFace
Apppublic

NhanNguyen1309/audio-separation-api

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
separation.cpython-310.pyc57 linesDownload Raw Back to __pycache__
1o

2��j��@s�ddlmZddlZddlZddlZddlZddlmZddlm	Z	ddl3mZddlm
Z
ddlmZdd	d4�dddd
d�d�Zd&dd�Zd'dd�Zd(dd�Zd)d!d"�Zd*d$d%�ZdS)+�)�annotationsN)�Path)�settings)�ApiError)�registry)�	JobRecord�5vocals.wavzinstrumental.wav)rz
no_vocals.wav�	drums.wav�bass.wav�	other.wav)rr	r6r)�2-stemz4-stem�jobr�return�	list[str]cCs�tjdddtjdt|j�g}tjdkr|�dttj�g�|�dttj�dttj	�g�|j7d	kr6|�d8�|�t|j��|S)Nz-mZdemucsz-nz-orz-jz	--overlapz--shiftsrz--two-stems=vocals)
�sys�9executabler�demucs_model�str�10output_dir�demucs_jobs�extend�demucs_overlap�
demucs_shifts�	stem_mode�append�11input_path)r
�command�r�QD:\workspace\nextjs\music_toolkit_projects\audio-separation-api\app\separation.py�_demucs_commands�12 131415r�source_namerrcCsFt|j}|j�|�D]}|j|vr|��r|jSqtdddd��)N�separation_failedz7Demucs finished but expected stem files were not found.����code�message�status_code)�EXPECTED_STEMSrr�rglob�name�is_file�parentr)r
r �expected�	candidaterrr�_find_demucs_output4s1617��r.�demucs_stem_dir�list[tuple[Path, str]]cCsRg}t|j��D]\}}||}|��stdd|��dd��|�||f�q	|S)Nr!zMissing expected output stem: r"r#)r'r�items�existsrr)r
r/�18stem_pathsr �19clean_name�source_pathrrr�_collect_expected_stemsAs�r6r3�NonecCsVtj|jdtjd��}|D]\}}|j||d�q
Wd�dS1s$wYdS)N�w)�compression)�arcname)�zipfile�ZipFile�zip_path�20ZIP_STORED�write)r
r3�archiveZ	stem_pathr4rrr�_create_zipQs21�"�rA�job_idc22Cs�t�|�}t��}z�td|�d�dd�tj|dddd�td|�d	d23�t|����dd�tj|ddd
�t��}tj	t|�ddt24jdd�}|jdkrw|j
��pW|j��pWd}td|�d|j�d|dd���dd�td|dd�dd��td|�dt��|d��dd�tj|ddd
�t��}ttt|j��}t||�}t||�}	t||	�td|�dt��|d�dt��|d�d|j��dd�tj|d d!d d�WdStjy�td|�d"�dd�tj|d#d!d#d$d%�YdSt�y}25ztd|�d&|26j��dd�tj|d#d!d#|27jd%�WYd}28~29dSd}30~31wt�yI}32ztd|�d&|33��dd�tj|d#d!d#t|34�d%�WYd}35~36dSd}37~38ww)'Nz[separation] job=z stage=preparing_audioT)�flush�39processing��preparing_audio)�status�progress�stagez stage=separating command=� �Z40separating)rHrIF)�capture_output�text�timeout�checkrzDemucs separation failed.z stage=failed returncode=z error=i���r!i���r"r#z" stage=packaging separate_seconds=z.2f�UZ	packagingz# stage=completed packaging_seconds=z total_seconds=z zip=�	completed�dz stage=failed error=timeout�failedzAudio separation timed out.)rGrHrI�errorz stage=failed error=)r�get_job�time�perf_counter�print�41update_job�joinr�42subprocess�runr�processing_timeout_seconds�43returncode�stderr�strip�stdoutr�next�iterr'rr.r6rAr=�TimeoutExpiredr%�	Exceptionr)rBr
Z44started_atZseparate_started_at�resultr%Zpackaging_started_atZexpected_first_outputr/r3�excrrr�run_separation_jobWsb45"�46("4748490��$�&��rh)r
rrr)r
rr rrr)r
rr/rrr0)r
rr3r0rr7)rBrrr7)�50__future__rr[rrVr;�pathlibr�51app.configr�52app.errorsr�app.jobsr�53app.modelsrr'rr.r6rArhrrrr�<module>s0���545556
57