justyoung commited on
Commit
265c6e4
·
verified ·
1 Parent(s): 7b13afd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -20
app.py CHANGED
@@ -1,21 +1,94 @@
 
 
 
 
 
1
  import gradio as gr
2
- from audio_separator.separator import Separator
3
-
4
- def separate_audio(audio_file):
5
- separator = Separator()
6
- stems = separator.separate(audio_file, stem_names=["instrumental", "vocals", "backing_vocals"])
7
- return stems["instrumental"], stems["vocals"], stems["backing_vocals"]
8
-
9
- iface = gr.Interface(
10
- fn=separate_audio,
11
- inputs=gr.Audio(type="filepath", label="Upload Audio"),
12
- outputs=[
13
- gr.Audio(label="Instrumental"),
14
- gr.Audio(label="Vocals"),
15
- gr.Audio(label="Backing Vocals")
16
- ],
17
- title="Audio Separator",
18
- description="Separate audio into instrumental, vocals, and backing vocals using audio-separator."
19
- )
20
-
21
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import random
4
+ from scipy.io.wavfile import write, read
5
+ import numpy as np
6
  import gradio as gr
7
+ import yt_dlp
8
+
9
+ # Model dictionaries and lists
10
+ roformer_models = {
11
+ 'BS-Roformer-Viperx-1297.ckpt': 'model_bs_roformer_ep_317_sdr_12.9755.ckpt',
12
+ 'BS-Roformer-Viperx-1296.ckpt': 'model_bs_roformer_ep_368_sdr_12.9628.ckpt',
13
+ 'BS-Roformer-Viperx-1053.ckpt': 'model_bs_roformer_ep_937_sdr_10.5309.ckpt',
14
+ 'Mel-Roformer-Viperx-1143.ckpt': 'model_mel_band_roformer_ep_3005_sdr_11.4360.ckpt'
15
+ }
16
+
17
+ mdx23c_models = [
18
+ 'MDX23C_D1581.ckpt',
19
+ 'MDX23C-8KFFT-InstVoc_HQ.ckpt',
20
+ 'MDX23C-8KFFT-InstVoc_HQ_2.ckpt',
21
+ ]
22
+
23
+ # More model lists...
24
+
25
+ output_format = ['wav', 'flac', 'mp3']
26
+ mdxnet_overlap_values = ['0.25', '0.5', '0.75', '0.99']
27
+ vrarch_window_size_values = ['320', '512', '1024']
28
+ demucs_overlap_values = ['0.25', '0.50', '0.75', '0.99']
29
+
30
+ # Function to download audio
31
+ def download_audio(url):
32
+ ydl_opts = {
33
+ 'format': 'bestaudio/best',
34
+ 'outtmpl': 'ytdl/%(title)s.%(ext)s',
35
+ 'postprocessors': [{
36
+ 'key': 'FFmpegExtractAudio',
37
+ 'preferredcodec': 'wav',
38
+ 'preferredquality': '192',
39
+ }],
40
+ }
41
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
42
+ info_dict = ydl.extract_info(url, download=True)
43
+ file_path = ydl.prepare_filename(info_dict).rsplit('.', 1)[0] + '.wav'
44
+ sample_rate, audio_data = read(file_path)
45
+ audio_array = np.asarray(audio_data, dtype=np.int16)
46
+ return sample_rate, audio_array
47
+
48
+ # Function to separate audio using Roformer
49
+ def roformer_separator(audio, model, output_format, overlap, segment_size, denoise):
50
+ directory = "./outputs"
51
+ random_id = str(random.randint(10000, 99999))
52
+ os.makedirs("outputs", exist_ok=True)
53
+ write(f'{random_id}.wav', audio[0], audio[1])
54
+ full_roformer_model = roformer_models[model]
55
+
56
+ prompt = f"audio-separator {random_id}.wav --model_filename {full_roformer_model} --output_dir=./outputs --output_format={output_format} --normalization=0.9 --mdxc_overlap={overlap} --mdxc_segment_size={segment_size}"
57
+ if denoise:
58
+ prompt += " --mdx_enable_denoise"
59
+
60
+ os.system(prompt)
61
+
62
+ files_list = [os.path.join(directory, file) for file in os.listdir(directory) if re.search(random_id, file)]
63
+ stem1_file, stem2_file, stem3_file = files_list[:3] # Assuming the files are in the correct order
64
+ return stem1_file, stem2_file, stem3_file
65
+
66
+ # Gradio interface
67
+ def process_audio(url, model, output_format, overlap, segment_size, denoise):
68
+ sample_rate, audio_array = download_audio(url)
69
+ stems = roformer_separator((sample_rate, audio_array), model, output_format, overlap, segment_size, denoise)
70
+ return stems
71
+
72
+ # Gradio UI
73
+ with gr.Blocks() as demo:
74
+ gr.Markdown("# Hex Audio Separator")
75
+ with gr.Row():
76
+ url_input = gr.Textbox(label="YouTube URL")
77
+ model_input = gr.Dropdown(choices=list(roformer_models.keys()), label="Roformer Model")
78
+ format_input = gr.Dropdown(choices=output_format, label="Output Format")
79
+ overlap_input = gr.Dropdown(choices=mdxnet_overlap_values, label="Overlap")
80
+ segment_input = gr.Slider(0, 100, label="Segment Size")
81
+ denoise_input = gr.Checkbox(label="Enable Denoise")
82
+
83
+ output1 = gr.Audio(label="Vocals")
84
+ output2 = gr.Audio(label="Instrumental")
85
+ output3 = gr.Audio(label="Backing Vocals")
86
+
87
+ submit_button = gr.Button("Process")
88
+ submit_button.click(
89
+ process_audio,
90
+ inputs=[url_input, model_input, format_input, overlap_input, segment_input, denoise_input],
91
+ outputs=[output1, output2, output3]
92
+ )
93
+
94
+ demo.launch()