Spaces:
Build error
Build error
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230# 1. Install all necessary libraries for the full application!pip install gradio "demucs>=4.0.0" librosa soundfile matplotlib# 2. Import librariesimport gradio as grimport osimport shutilimport asyncioimport librosaimport librosa.displayimport soundfile as sfimport numpy as npimport timeimport zipfileimport tempfileimport matplotlib.pyplot as pltimport matplotlibmatplotlib.use('Agg') # Use a non-interactive backend for plotting# --- Helper/Processing Functions ---def update_output_visibility(choice):if "2 Stems" in choice:return {vocals_output: gr.update(visible=True),drums_output: gr.update(visible=False),bass_output: gr.update(visible=False),other_output: gr.update(visible=True, label="Instrumental (No Vocals)")}elif "4 Stems" in choice:return {vocals_output: gr.update(visible=True),drums_output: gr.update(visible=True),bass_output: gr.update(visible=True),other_output: gr.update(visible=True, label="Other")}async def separate_stems(audio_file_path, stem_choice, progress=gr.Progress(track_tqdm=True)):if audio_file_path is None: raise gr.Error("No audio file uploaded!")progress(0, desc="Starting...")try:progress(0.05, desc="Preparing audio file...")original_filename_base = os.path.basename(audio_file_path).rsplit('.', 1)[0]stable_input_path = f"stable_input_{original_filename_base}.wav"shutil.copy(audio_file_path, stable_input_path)model_arg = "--two-stems=vocals" if "2 Stems" in stem_choice else ""output_dir = "separated"if os.path.exists(output_dir): shutil.rmtree(output_dir)command = f"python3 -m demucs {model_arg} -o \"{output_dir}\" \"{stable_input_path}\""progress(0.2, desc="Running Demucs (this may take a minute)...")process = await asyncio.create_subprocess_shell(command,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)stdout, stderr = await process.communicate()if process.returncode != 0:raise gr.Error(f"Demucs failed to run. Error: {stderr.decode()[:500]}")progress(0.8, desc="Locating separated stem files...")stable_filename_base = os.path.basename(stable_input_path).rsplit('.', 1)[0]model_folder_name = next(os.walk(output_dir))[1][0]stems_path = os.path.join(output_dir, model_folder_name, stable_filename_base)if not os.path.exists(stems_path):raise gr.Error(f"Demucs finished, but the output directory was not found!")vocals_path = os.path.join(stems_path, "vocals.wav") if os.path.exists(os.path.join(stems_path, "vocals.wav")) else Nonedrums_path = os.path.join(stems_path, "drums.wav") if os.path.exists(os.path.join(stems_path, "drums.wav")) else Nonebass_path = os.path.join(stems_path, "bass.wav") if os.path.exists(os.path.join(stems_path, "bass.wav")) else Noneother_filename = "no_vocals.wav" if "2 Stems" in stem_choice else "other.wav"other_path = os.path.join(stems_path, other_filename) if os.path.exists(os.path.join(stems_path, other_filename)) else Noneos.remove(stable_input_path)return vocals_path, drums_path, bass_path, other_pathexcept Exception as e:print(f"An error occurred: {e}")raise gr.Error(str(e))def slice_stem_real(stem_audio_data, loop_choice, sensitivity, progress=gr.Progress(track_tqdm=True)):if stem_audio_data is None:gr.Warning("This stem is empty. Cannot slice.")return None, Nonesample_rate, y_int = stem_audio_datay = librosa.util.buf_to_float(y_int)y_mono = librosa.to_mono(y.T) if y.ndim > 1 else yprogress(0.1, desc="Detecting BPM...")tempo, beats = librosa.beat.beat_track(y=y_mono, sr=sample_rate)bpm = 120 if tempo is None else np.round(tempo)bpm_int = int(bpm.item())if bpm_int == 0:bpm_int = 120gr.Warning("BPM detection failed, defaulting to 120 BPM.")print(f"Detected BPM: {bpm_int}")output_files = []loops_dir = tempfile.mkdtemp()if "One-Shots" in loop_choice:progress(0.3, desc="Finding transients...")onset_frames = librosa.onset.onset_detect(y=y_mono, sr=sample_rate, delta=sensitivity, wait=1, pre_avg=1, post_avg=1, post_max=1)onset_samples = librosa.frames_to_samples(onset_frames)progress(0.5, desc="Slicing one-shots...")if len(onset_samples) > 0:for i, start_sample in enumerate(onset_samples):end_sample = onset_samples[i+1] if i+1 < len(onset_samples) else len(y)slice_data = y[start_sample:end_sample]filename = os.path.join(loops_dir, f"one_shot_{i+1:03d}.wav")sf.write(filename, slice_data, sample_rate, subtype='PCM_16')output_files.append(filename)if len(onset_samples) > 1:progress(0.5 + (i / (len(onset_samples) - 1) * 0.5), desc=f"Exporting slice {i+1}...")else: # Handle bar loopsbars = int(loop_choice.split(" ")[0])seconds_per_beat = 60.0 / bpm_intseconds_per_bar = seconds_per_beat * 4loop_duration_seconds = seconds_per_bar * barsloop_duration_samples = int(loop_duration_seconds * sample_rate)progress(0.4, desc=f"Slicing into {bars}-bar loops...")num_loops = len(y) // loop_duration_samplesif num_loops == 0:gr.Warning(f"Audio is too short to create a {bars}-bar loop at {bpm_int} BPM.")return None, Nonefor i in range(num_loops):start_sample = i * loop_duration_samplesend_sample = start_sample + loop_duration_samplesslice_data = y[start_sample:end_sample]filename = os.path.join(loops_dir, f"{bars}bar_loop_{i+1:03d}_{bpm_int}bpm.wav")sf.write(filename, slice_data, sample_rate, subtype='PCM_16')output_files.append(filename)if num_loops > 1:progress(0.4 + (i / (num_loops - 1) * 0.6), desc=f"Exporting loop {i+1}...")if not output_files:gr.Warning("No loops or one-shots could be generated.")return None, Nonereturn output_files, loops_dirasync def slice_all_and_zip_real(vocals, drums, bass, other, loop_choice, sensitivity, progress=gr.Progress(track_tqdm=True)):progress(0, desc="Starting batch slice...")await asyncio.sleep(0.1)stems_to_process = {"vocals": vocals, "drums": drums, "bass": bass, "other": other}zip_path = "Loop_Architect_Pack.zip"num_stems = sum(1 for data in stems_to_process.values() if data is not None)if num_stems == 0: raise gr.Error("No stems to process! Please separate stems first.")with zipfile.ZipFile(zip_path, 'w') as zf:processed_count = 0for name, data in stems_to_process.items():if data is not None:def update_main_progress(p, desc=""):overall_progress = (processed_count + p) / num_stemsprogress(overall_progress, desc=f"Slicing {name}: {desc}")sliced_files, temp_dir = slice_stem_real((data[0], data[1]), loop_choice, sensitivity, progress=update_main_progress)if sliced_files:for loop_file in sliced_files:arcname = os.path.join(name, os.path.basename(loop_file))zf.write(loop_file, arcname)if temp_dir and os.path.exists(temp_dir):shutil.rmtree(temp_dir)processed_count += 1progress(1, desc="Pack Ready!")return zip_path, gr.update(visible=True)# --- Create the full Gradio Interface ---with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red")) as demo:gr.Markdown("# 🎵 Loop Architect")with gr.Row():with gr.Column(scale=1):gr.Markdown("### 1. Separate Stems")audio_input = gr.Audio(type="filepath", label="Upload a Track")stem_options = gr.Radio(["4 Stems (Vocals, Drums, Bass, Other)", "2 Stems (Vocals + Instrumental)"], label="Separation Type", value="4 Stems (Vocals, Drums, Bass, Other)")submit_button = gr.Button("Separate Stems")with gr.Accordion("Slicing Options", open=True):loop_options_radio = gr.Radio(["One-Shots (All Transients)", "4 Bar Loops", "8 Bar Loops"], label="Loop Length", value="One-Shots (All Transients)")sensitivity_slider = gr.Slider(minimum=0.01, maximum=0.5, value=0.05, step=0.01, label="One-Shot Sensitivity")gr.Markdown("### 3. Create Pack")slice_all_button = gr.Button("Slice All Stems & Create Pack", variant="primary")download_zip_file = gr.File(label="Download Your Loop Pack", visible=False)with gr.Column(scale=2):with gr.Accordion("Separated Stems", open=True):with gr.Row():vocals_output = gr.Audio(label="Vocals", scale=4)slice_vocals_btn = gr.Button("Slice", scale=1)with gr.Row():drums_output = gr.Audio(label="Drums", scale=4)slice_drums_btn = gr.Button("Slice", scale=1)with gr.Row():bass_output = gr.Audio(label="Bass", scale=4)slice_bass_btn = gr.Button("Slice", scale=1)with gr.Row():other_output = gr.Audio(label="Other / Instrumental", scale=4)slice_other_btn = gr.Button("Slice", scale=1)gr.Markdown("### Sliced Loops / Samples")loop_gallery = gr.Gallery(label="Generated Loops", columns=8, object_fit="contain", height="auto")# --- Define Event Listeners ---def slice_and_display(stem_data, loop_choice, sensitivity):files, _ = slice_stem_real(stem_data, loop_choice, sensitivity)return filessubmit_button.click(fn=separate_stems, inputs=[audio_input, stem_options], outputs=[vocals_output, drums_output, bass_output, other_output])stem_options.change(fn=update_output_visibility, inputs=[stem_options], outputs=[vocals_output, drums_output, bass_output, other_output])slice_vocals_btn.click(fn=slice_and_display, inputs=[vocals_output, loop_options_radio, sensitivity_slider], outputs=loop_gallery)slice_drums_btn.click(fn=slice_and_display, inputs=[drums_output, loop_options_radio, sensitivity_slider], outputs=loop_gallery)slice_bass_btn.click(fn=slice_and_display, inputs=[bass_output, loop_options_radio, sensitivity_slider], outputs=loop_gallery)slice_other_btn.click(fn=slice_and_display, inputs=[other_output, loop_options_radio, sensitivity_slider], outputs=loop_gallery)slice_all_button.click(fn=slice_all_and_zip_real, inputs=[vocals_output, drums_output, bass_output, other_output, loop_options_radio, sensitivity_slider], outputs=[download_zip_file, download_zip_file])# --- Launch the UI ---# Use demo.launch() for Hugging Face Spaces# Use demo.launch(debug=True) for local testingdemo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229# 1. Install all necessary libraries for the full application# 2. Import librariesimport gradio as grimport osimport shutilimport asyncioimport librosaimport librosa.displayimport soundfile as sfimport numpy as npimport timeimport zipfileimport tempfileimport matplotlib.pyplot as pltimport matplotlibmatplotlib.use('Agg') # Use a non-interactive backend for plotting# --- Helper/Processing Functions ---def update_output_visibility(choice):if "2 Stems" in choice:return {vocals_output: gr.update(visible=True),drums_output: gr.update(visible=False),bass_output: gr.update(visible=False),other_output: gr.update(visible=True, label="Instrumental (No Vocals)")}elif "4 Stems" in choice:return {vocals_output: gr.update(visible=True),drums_output: gr.update(visible=True),bass_output: gr.update(visible=True),other_output: gr.update(visible=True, label="Other")}async def separate_stems(audio_file_path, stem_choice, progress=gr.Progress(track_tqdm=True)):if audio_file_path is None: raise gr.Error("No audio file uploaded!")progress(0, desc="Starting...")try:progress(0.05, desc="Preparing audio file...")original_filename_base = os.path.basename(audio_file_path).rsplit('.', 1)[0]stable_input_path = f"stable_input_{original_filename_base}.wav"shutil.copy(audio_file_path, stable_input_path)model_arg = "--two-stems=vocals" if "2 Stems" in stem_choice else ""output_dir = "separated"if os.path.exists(output_dir): shutil.rmtree(output_dir)command = f"python3 -m demucs {model_arg} -o \"{output_dir}\" \"{stable_input_path}\""progress(0.2, desc="Running Demucs (this may take a minute)...")process = await asyncio.create_subprocess_shell(command,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)stdout, stderr = await process.communicate()if process.returncode != 0:raise gr.Error(f"Demucs failed to run. Error: {stderr.decode()[:500]}")progress(0.8, desc="Locating separated stem files...")stable_filename_base = os.path.basename(stable_input_path).rsplit('.', 1)[0]model_folder_name = next(os.walk(output_dir))[1][0]stems_path = os.path.join(output_dir, model_folder_name, stable_filename_base)if not os.path.exists(stems_path):raise gr.Error(f"Demucs finished, but the output directory was not found!")vocals_path = os.path.join(stems_path, "vocals.wav") if os.path.exists(os.path.join(stems_path, "vocals.wav")) else Nonedrums_path = os.path.join(stems_path, "drums.wav") if os.path.exists(os.path.join(stems_path, "drums.wav")) else Nonebass_path = os.path.join(stems_path, "bass.wav") if os.path.exists(os.path.join(stems_path, "bass.wav")) else Noneother_filename = "no_vocals.wav" if "2 Stems" in stem_choice else "other.wav"other_path = os.path.join(stems_path, other_filename) if os.path.exists(os.path.join(stems_path, other_filename)) else Noneos.remove(stable_input_path)return vocals_path, drums_path, bass_path, other_pathexcept Exception as e:print(f"An error occurred: {e}")raise gr.Error(str(e))def slice_stem_real(stem_audio_data, loop_choice, sensitivity, progress=gr.Progress(track_tqdm=True)):if stem_audio_data is None:gr.Warning("This stem is empty. Cannot slice.")return None, Nonesample_rate, y_int = stem_audio_datay = librosa.util.buf_to_float(y_int)y_mono = librosa.to_mono(y.T) if y.ndim > 1 else yprogress(0.1, desc="Detecting BPM...")tempo, beats = librosa.beat.beat_track(y=y_mono, sr=sample_rate)bpm = 120 if tempo is None else np.round(tempo)bpm_int = int(bpm.item())if bpm_int == 0:bpm_int = 120gr.Warning("BPM detection failed, defaulting to 120 BPM.")print(f"Detected BPM: {bpm_int}")output_files = []loops_dir = tempfile.mkdtemp()if "One-Shots" in loop_choice:progress(0.3, desc="Finding transients...")onset_frames = librosa.onset.onset_detect(y=y_mono, sr=sample_rate, delta=sensitivity, wait=1, pre_avg=1, post_avg=1, post_max=1)onset_samples = librosa.frames_to_samples(onset_frames)progress(0.5, desc="Slicing one-shots...")if len(onset_samples) > 0:for i, start_sample in enumerate(onset_samples):end_sample = onset_samples[i+1] if i+1 < len(onset_samples) else len(y)slice_data = y[start_sample:end_sample]filename = os.path.join(loops_dir, f"one_shot_{i+1:03d}.wav")sf.write(filename, slice_data, sample_rate, subtype='PCM_16')output_files.append(filename)if len(onset_samples) > 1:progress(0.5 + (i / (len(onset_samples) - 1) * 0.5), desc=f"Exporting slice {i+1}...")else: # Handle bar loopsbars = int(loop_choice.split(" ")[0])seconds_per_beat = 60.0 / bpm_intseconds_per_bar = seconds_per_beat * 4loop_duration_seconds = seconds_per_bar * barsloop_duration_samples = int(loop_duration_seconds * sample_rate)progress(0.4, desc=f"Slicing into {bars}-bar loops...")num_loops = len(y) // loop_duration_samplesif num_loops == 0:gr.Warning(f"Audio is too short to create a {bars}-bar loop at {bpm_int} BPM.")return None, Nonefor i in range(num_loops):start_sample = i * loop_duration_samplesend_sample = start_sample + loop_duration_samplesslice_data = y[start_sample:end_sample]filename = os.path.join(loops_dir, f"{bars}bar_loop_{i+1:03d}_{bpm_int}bpm.wav")sf.write(filename, slice_data, sample_rate, subtype='PCM_16')output_files.append(filename)if num_loops > 1:progress(0.4 + (i / (num_loops - 1) * 0.6), desc=f"Exporting loop {i+1}...")if not output_files:gr.Warning("No loops or one-shots could be generated.")return None, Nonereturn output_files, loops_dirasync def slice_all_and_zip_real(vocals, drums, bass, other, loop_choice, sensitivity, progress=gr.Progress(track_tqdm=True)):progress(0, desc="Starting batch slice...")await asyncio.sleep(0.1)stems_to_process = {"vocals": vocals, "drums": drums, "bass": bass, "other": other}zip_path = "Loop_Architect_Pack.zip"num_stems = sum(1 for data in stems_to_process.values() if data is not None)if num_stems == 0: raise gr.Error("No stems to process! Please separate stems first.")with zipfile.ZipFile(zip_path, 'w') as zf:processed_count = 0for name, data in stems_to_process.items():if data is not None:def update_main_progress(p, desc=""):overall_progress = (processed_count + p) / num_stemsprogress(overall_progress, desc=f"Slicing {name}: {desc}")sliced_files, temp_dir = slice_stem_real((data[0], data[1]), loop_choice, sensitivity, progress=update_main_progress)if sliced_files:for loop_file in sliced_files:arcname = os.path.join(name, os.path.basename(loop_file))zf.write(loop_file, arcname)if temp_dir and os.path.exists(temp_dir):shutil.rmtree(temp_dir)processed_count += 1progress(1, desc="Pack Ready!")return zip_path, gr.update(visible=True)# --- Create the full Gradio Interface ---with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red")) as demo:gr.Markdown("# 🎵 Loop Architect")with gr.Row():with gr.Column(scale=1):gr.Markdown("### 1. Separate Stems")audio_input = gr.Audio(type="filepath", label="Upload a Track")stem_options = gr.Radio(["4 Stems (Vocals, Drums, Bass, Other)", "2 Stems (Vocals + Instrumental)"], label="Separation Type", value="4 Stems (Vocals, Drums, Bass, Other)")submit_button = gr.Button("Separate Stems")with gr.Accordion("Slicing Options", open=True):loop_options_radio = gr.Radio(["One-Shots (All Transients)", "4 Bar Loops", "8 Bar Loops"], label="Loop Length", value="One-Shots (All Transients)")sensitivity_slider = gr.Slider(minimum=0.01, maximum=0.5, value=0.05, step=0.01, label="One-Shot Sensitivity")gr.Markdown("### 3. Create Pack")slice_all_button = gr.Button("Slice All Stems & Create Pack", variant="primary")download_zip_file = gr.File(label="Download Your Loop Pack", visible=False)with gr.Column(scale=2):with gr.Accordion("Separated Stems", open=True):with gr.Row():vocals_output = gr.Audio(label="Vocals", scale=4)slice_vocals_btn = gr.Button("Slice", scale=1)with gr.Row():drums_output = gr.Audio(label="Drums", scale=4)slice_drums_btn = gr.Button("Slice", scale=1)with gr.Row():bass_output = gr.Audio(label="Bass", scale=4)slice_bass_btn = gr.Button("Slice", scale=1)with gr.Row():other_output = gr.Audio(label="Other / Instrumental", scale=4)slice_other_btn = gr.Button("Slice", scale=1)gr.Markdown("### Sliced Loops / Samples")loop_gallery = gr.Gallery(label="Generated Loops", columns=8, object_fit="contain", height="auto")# --- Define Event Listeners ---def slice_and_display(stem_data, loop_choice, sensitivity):files, _ = slice_stem_real(stem_data, loop_choice, sensitivity)return filessubmit_button.click(fn=separate_stems, inputs=[audio_input, stem_options], outputs=[vocals_output, drums_output, bass_output, other_output])stem_options.change(fn=update_output_visibility, inputs=[stem_options], outputs=[vocals_output, drums_output, bass_output, other_output])slice_vocals_btn.click(fn=slice_and_display, inputs=[vocals_output, loop_options_radio, sensitivity_slider], outputs=loop_gallery)slice_drums_btn.click(fn=slice_and_display, inputs=[drums_output, loop_options_radio, sensitivity_slider], outputs=loop_gallery)slice_bass_btn.click(fn=slice_and_display, inputs=[bass_output, loop_options_radio, sensitivity_slider], outputs=loop_gallery)slice_other_btn.click(fn=slice_and_display, inputs=[other_output, loop_options_radio, sensitivity_slider], outputs=loop_gallery)slice_all_button.click(fn=slice_all_and_zip_real, inputs=[vocals_output, drums_output, bass_output, other_output, loop_options_radio, sensitivity_slider], outputs=[download_zip_file, download_zip_file])# --- Launch the UI ---# Use demo.launch() for Hugging Face Spaces# Use demo.launch(debug=True) for local testingdemo.launch()
app.py+228-229